2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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 APPLE 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 APPLE 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 "WebProcess.h"
33 #include <QApplication>
36 #include <QLocalServer>
40 #include <QtCore/qglobal.h>
41 #include <WebCore/NotImplemented.h>
44 #include <runtime/InitializeThreading.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
49 #include <wtf/HashSet.h>
50 #include <wtf/PassRefPtr.h>
51 #include <wtf/Threading.h>
52 #include <wtf/text/WTFString.h>
53 #if defined Q_OS_LINUX
54 #include <sys/prctl.h>
58 using namespace WebCore;
62 class QtWebProcess : public QProcess
66 QtWebProcess(QObject* parent = 0)
72 virtual void setupChildProcess();
75 void QtWebProcess::setupChildProcess()
77 #if defined Q_OS_LINUX
79 if (getenv("QT_WEBKIT_KEEP_ALIVE_WEB_PROCESS"))
82 prctl(PR_SET_PDEATHSIG, SIGKILL);
86 void ProcessLauncher::launchProcess()
88 QString applicationPath = QLatin1String("%1 %2");
90 if (QFile::exists(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"))) {
91 applicationPath = applicationPath.arg(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"));
93 applicationPath = applicationPath.arg(QLatin1String("QtWebProcess"));
97 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) == -1) {
98 qDebug() << "Creation of socket failed with errno:" << errno;
103 // Don't expose the ui socket to the web process
104 while (fcntl(sockets[1], F_SETFD, FD_CLOEXEC) == -1) {
105 if (errno != EINTR) {
106 ASSERT_NOT_REACHED();
107 while (close(sockets[0]) == -1 && errno == EINTR) { }
108 while (close(sockets[1]) == -1 && errno == EINTR) { }
113 QString program(applicationPath.arg(sockets[0]));
115 QProcess* webProcess = new QtWebProcess();
116 webProcess->setProcessChannelMode(QProcess::ForwardedChannels);
117 webProcess->start(program);
119 // Don't expose the web socket to possible future web processes
120 while (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) {
121 if (errno != EINTR) {
122 ASSERT_NOT_REACHED();
128 if (!webProcess->waitForStarted()) {
129 qDebug() << "Failed to start" << program;
130 ASSERT_NOT_REACHED();
135 setpriority(PRIO_PROCESS, webProcess->pid(), 10);
137 RunLoop::main()->scheduleWork(WorkItem::create(this, &WebKit::ProcessLauncher::didFinishLaunchingProcess, webProcess, sockets[1]));
140 void ProcessLauncher::terminateProcess()
142 if (!m_processIdentifier)
145 QObject::connect(m_processIdentifier, SIGNAL(finished(int)), m_processIdentifier, SLOT(deleteLater()), Qt::QueuedConnection);
146 m_processIdentifier->terminate();
149 void ProcessLauncher::platformInvalidate()
154 } // namespace WebKit
156 #include "ProcessLauncherQt.moc"