2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ProcessLauncher.h"
30 #include "Connection.h"
32 #include <WebCore/FileSystem.h>
33 #include <WebCore/ResourceHandle.h>
36 #include <sys/prctl.h>
38 #include <wtf/text/CString.h>
39 #include <wtf/text/WTFString.h>
40 #include <wtf/gobject/GOwnPtr.h>
42 using namespace WebCore;
46 const char* gWebKitWebProcessName = "WebKitWebProcess";
47 const char* gWebKitPluginProcessName = "WebKitPluginProcess";
49 static void childSetupFunction(gpointer userData)
51 int socket = GPOINTER_TO_INT(userData);
55 // Kill child process when parent dies.
56 prctl(PR_SET_PDEATHSIG, SIGKILL);
60 static void childFinishedFunction(GPid, gint status, gpointer userData)
62 if (WIFEXITED(status) && !WEXITSTATUS(status))
65 close(GPOINTER_TO_INT(userData));
68 void ProcessLauncher::launchProcess()
73 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
74 g_printerr("Creation of socket failed: %s.\n", g_strerror(errno));
79 const gchar* execDirectory = g_getenv("WEBKIT_EXEC_PATH");
80 GOwnPtr<gchar> binaryPath(g_build_filename(execDirectory ? execDirectory : LIBEXECDIR,
81 m_launchOptions.processType == ProcessLauncher::WebProcess ? gWebKitWebProcessName : gWebKitPluginProcessName, NULL));
82 GOwnPtr<gchar> socket(g_strdup_printf("%d", sockets[0]));
84 argv[0] = binaryPath.get();
85 argv[1] = socket.get();
88 GOwnPtr<GError> error;
89 int spawnFlags = G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD;
90 if (!g_spawn_async(0, argv, 0, static_cast<GSpawnFlags>(spawnFlags), childSetupFunction, GINT_TO_POINTER(sockets[1]), &pid, &error.outPtr())) {
91 g_printerr("Unable to fork a new WebProcess: %s.\n", error->message);
96 m_processIdentifier = pid;
98 // Monitor the child process, it calls waitpid to prevent the child process from becomming a zombie,
99 // and it allows us to close the socket when the child process crashes.
100 g_child_watch_add(m_processIdentifier, childFinishedFunction, GINT_TO_POINTER(sockets[1]));
102 // We've finished launching the process, message back to the main run loop.
103 RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, m_processIdentifier, sockets[1]));
106 void ProcessLauncher::terminateProcess()
108 if (!m_processIdentifier)
111 kill(m_processIdentifier, SIGKILL);
114 void ProcessLauncher::platformInvalidate()
118 } // namespace WebKit