1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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 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.
25 */
26
27#include "config.h"
28#include "ProcessLauncher.h"
29
30#include "Connection.h"
31#include "RunLoop.h"
32#include "WebProcess.h"
33#include <QApplication>
34#include <QDebug>
35#include <QFile>
36#include <QLocalServer>
37#include <QMetaType>
38#include <QProcess>
39#include <QString>
40#include <QtCore/qglobal.h>
41#include <WebCore/NotImplemented.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <runtime/InitializeThreading.h>
45#include <string>
46#include <sys/resource.h>
47#include <sys/socket.h>
48#include <unistd.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>
55#include <signal.h>
56#endif
57
58using namespace WebCore;
59
60namespace WebKit {
61
62class QtWebProcess : public QProcess
63{
64    Q_OBJECT
65public:
66    QtWebProcess(QObject* parent = 0)
67        : QProcess(parent)
68    {
69    }
70
71protected:
72    virtual void setupChildProcess();
73};
74
75void QtWebProcess::setupChildProcess()
76{
77#if defined Q_OS_LINUX
78#ifndef NDEBUG
79    if (getenv("QT_WEBKIT_KEEP_ALIVE_WEB_PROCESS"))
80        return;
81#endif
82    prctl(PR_SET_PDEATHSIG, SIGKILL);
83#endif
84}
85
86#if OS(SYMBIAN)
87// FIXME: Symbian's POSIX layer doesn't have a socketpair(), so
88// the following is just to fix the build until a pure Symbian
89// IPC implementation lands on trunk
90static int socketpair(int, int, int , int[2])
91{
92    return -1;
93}
94#endif
95
96void ProcessLauncher::launchProcess()
97{
98    QString applicationPath = QLatin1String("%1 %2");
99
100    if (QFile::exists(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"))) {
101        applicationPath = applicationPath.arg(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"));
102    } else {
103        applicationPath = applicationPath.arg(QLatin1String("QtWebProcess"));
104    }
105
106    int sockets[2];
107    if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) == -1) {
108        qDebug() << "Creation of socket failed with errno:" << errno;
109        ASSERT_NOT_REACHED();
110        return;
111    }
112
113    // Don't expose the ui socket to the web process
114    while (fcntl(sockets[1], F_SETFD, FD_CLOEXEC)  == -1) {
115        if (errno != EINTR) {
116            ASSERT_NOT_REACHED();
117            while (close(sockets[0]) == -1 && errno == EINTR) { }
118            while (close(sockets[1]) == -1 && errno == EINTR) { }
119            return;
120        }
121    }
122
123    QString program(applicationPath.arg(sockets[0]));
124
125    QProcess* webProcess = new QtWebProcess();
126    webProcess->setProcessChannelMode(QProcess::ForwardedChannels);
127    webProcess->start(program);
128
129    // Don't expose the web socket to possible future web processes
130    while (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) {
131        if (errno != EINTR) {
132            ASSERT_NOT_REACHED();
133            delete webProcess;
134            return;
135        }
136    }
137
138    if (!webProcess->waitForStarted()) {
139        qDebug() << "Failed to start" << program;
140        ASSERT_NOT_REACHED();
141        delete webProcess;
142        return;
143    }
144
145    setpriority(PRIO_PROCESS, webProcess->pid(), 10);
146
147    RunLoop::main()->scheduleWork(WorkItem::create(this, &WebKit::ProcessLauncher::didFinishLaunchingProcess, webProcess, sockets[1]));
148}
149
150void ProcessLauncher::terminateProcess()
151{
152    if (!m_processIdentifier)
153        return;
154
155    QObject::connect(m_processIdentifier, SIGNAL(finished(int)), m_processIdentifier, SLOT(deleteLater()), Qt::QueuedConnection);
156    m_processIdentifier->terminate();
157}
158
159void ProcessLauncher::platformInvalidate()
160{
161
162}
163
164} // namespace WebKit
165
166#include "ProcessLauncherQt.moc"
167