initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / Launcher / gtk / ProcessLauncherGtk.cpp
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
42 using namespace WebCore;
43
44 namespace WebKit {
45
46 const char* gWebKitWebProcessName = "WebKitWebProcess";
47 const char* gWebKitPluginProcessName = "WebKitPluginProcess";
48
49 static void childSetupFunction(gpointer userData)
50 {
51     int socket = GPOINTER_TO_INT(userData);
52     close(socket);
53
54 #if OS(LINUX)
55     // Kill child process when parent dies.
56     prctl(PR_SET_PDEATHSIG, SIGKILL);
57 #endif
58 }
59
60 static void childFinishedFunction(GPid, gint status, gpointer userData)
61 {
62     if (WIFEXITED(status) && !WEXITSTATUS(status))
63         return;
64
65     close(GPOINTER_TO_INT(userData));
66 }
67
68 void ProcessLauncher::launchProcess()
69 {
70     GPid pid = 0;
71
72     int sockets[2];
73     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
74         g_printerr("Creation of socket failed: %s.\n", g_strerror(errno));
75         ASSERT_NOT_REACHED();
76         return;
77     }
78
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]));
83     char* argv[3];
84     argv[0] = binaryPath.get();
85     argv[1] = socket.get();
86     argv[2] = 0;
87
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);
92         ASSERT_NOT_REACHED();
93     }
94
95     close(sockets[0]);
96     m_processIdentifier = pid;
97
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]));
101
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]));
104 }
105
106 void ProcessLauncher::terminateProcess()
107 {   
108     if (!m_processIdentifier)
109         return;
110
111     kill(m_processIdentifier, SIGKILL);
112 }
113
114 void ProcessLauncher::platformInvalidate()
115 {
116 }
117
118 } // namespace WebKit