1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY MOTOROLA INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "ProcessLauncher.h"
29
30#include "Connection.h"
31#include "RunLoop.h"
32#include <WebCore/FileSystem.h>
33#include <WebCore/ResourceHandle.h>
34#include <errno.h>
35#if OS(LINUX)
36#include <sys/prctl.h>
37#endif
38#include <wtf/text/CString.h>
39#include <wtf/text/WTFString.h>
40#include <wtf/gobject/GOwnPtr.h>
41
42using namespace WebCore;
43
44namespace WebKit {
45
46const char* gWebKitWebProcessName = "WebKitWebProcess";
47
48static void childSetupFunction(gpointer userData)
49{
50    int socket = GPOINTER_TO_INT(userData);
51    close(socket);
52
53#if OS(LINUX)
54    // Kill child process when parent dies.
55    prctl(PR_SET_PDEATHSIG, SIGKILL);
56#endif
57}
58
59void ProcessLauncher::launchProcess()
60{
61    GPid pid = 0;
62
63    int sockets[2];
64    if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) < 0) {
65        g_printerr("Creation of socket failed: %s.\n", g_strerror(errno));
66        ASSERT_NOT_REACHED();
67        return;
68    }
69
70    GOwnPtr<gchar> binaryPath(g_build_filename(applicationDirectoryPath().data(), gWebKitWebProcessName, NULL));
71    GOwnPtr<gchar> socket(g_strdup_printf("%d", sockets[0]));
72    char* argv[3];
73    argv[0] = binaryPath.get();
74    argv[1] = socket.get();
75    argv[2] = 0;
76
77    GOwnPtr<GError> error;
78    int spawnFlags = G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD;
79    if (!g_spawn_async(0, argv, 0, static_cast<GSpawnFlags>(spawnFlags), childSetupFunction, GINT_TO_POINTER(sockets[1]), &pid, &error.outPtr())) {
80        g_printerr("Unable to fork a new WebProcess: %s.\n", error->message);
81        ASSERT_NOT_REACHED();
82    }
83
84    close(sockets[0]);
85    m_processIdentifier = pid;
86    // We've finished launching the process, message back to the main run loop.
87    RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, m_processIdentifier, sockets[1]));
88}
89
90void ProcessLauncher::terminateProcess()
91{
92    if (!m_processIdentifier)
93        return;
94
95    kill(m_processIdentifier, SIGKILL);
96}
97
98void ProcessLauncher::platformInvalidate()
99{
100}
101
102} // namespace WebKit
103