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 "ProcessExecutablePath.h"
32#include <WebCore/AuthenticationChallenge.h>
33#include <WebCore/FileSystem.h>
34#include <WebCore/NetworkingContext.h>
35#include <WebCore/ResourceHandle.h>
36#include <WebCore/RunLoop.h>
37#include <errno.h>
38#include <glib.h>
39#include <locale.h>
40#include <wtf/text/CString.h>
41#include <wtf/text/WTFString.h>
42#include <wtf/gobject/GOwnPtr.h>
43#include <wtf/gobject/GlibUtilities.h>
44
45#if OS(LINUX)
46#include <sys/prctl.h>
47#include <sys/socket.h>
48#endif
49
50#ifdef SOCK_SEQPACKET
51#define SOCKET_TYPE SOCK_SEQPACKET
52#else
53#define SOCKET_TYPE SOCK_STREAM
54#endif
55
56using namespace WebCore;
57
58namespace WebKit {
59
60static void childSetupFunction(gpointer userData)
61{
62    int socket = GPOINTER_TO_INT(userData);
63    close(socket);
64}
65
66void ProcessLauncher::launchProcess()
67{
68    GPid pid = 0;
69
70    int sockets[2];
71    if (socketpair(AF_UNIX, SOCKET_TYPE, 0, sockets) < 0) {
72        g_printerr("Creation of socket failed: %s.\n", g_strerror(errno));
73        ASSERT_NOT_REACHED();
74        return;
75    }
76
77    String executablePath, pluginPath;
78    CString realExecutablePath, realPluginPath;
79    if (m_launchOptions.processType == WebProcess)
80        executablePath = executablePathOfWebProcess();
81    else {
82        executablePath = executablePathOfPluginProcess();
83        pluginPath = m_launchOptions.extraInitializationData.get("plugin-path");
84        realPluginPath = fileSystemRepresentation(pluginPath);
85    }
86
87    realExecutablePath = fileSystemRepresentation(executablePath);
88    GOwnPtr<gchar> socket(g_strdup_printf("%d", sockets[0]));
89    char* argv[4];
90    argv[0] = const_cast<char*>(realExecutablePath.data());
91    argv[1] = socket.get();
92    argv[2] = const_cast<char*>(realPluginPath.data());
93    argv[3] = 0;
94
95    GOwnPtr<GError> error;
96    if (!g_spawn_async(0, argv, 0, G_SPAWN_LEAVE_DESCRIPTORS_OPEN, childSetupFunction, GINT_TO_POINTER(sockets[1]), &pid, &error.outPtr())) {
97        g_printerr("Unable to fork a new WebProcess: %s.\n", error->message);
98        ASSERT_NOT_REACHED();
99    }
100
101    close(sockets[0]);
102    m_processIdentifier = pid;
103
104    // We've finished launching the process, message back to the main run loop.
105    RunLoop::main()->dispatch(bind(&ProcessLauncher::didFinishLaunchingProcess, this, m_processIdentifier, sockets[1]));
106}
107
108void ProcessLauncher::terminateProcess()
109{
110    if (m_isLaunching) {
111        invalidate();
112        return;
113    }
114
115    if (!m_processIdentifier)
116        return;
117
118    kill(m_processIdentifier, SIGKILL);
119    m_processIdentifier = 0;
120}
121
122void ProcessLauncher::platformInvalidate()
123{
124}
125
126} // namespace WebKit
127