initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / Launcher / qt / ProcessLauncherQt.cpp
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
58 using namespace WebCore;
59
60 namespace WebKit {
61
62 class QtWebProcess : public QProcess
63 {
64     Q_OBJECT
65 public:
66     QtWebProcess(QObject* parent = 0)
67         : QProcess(parent)
68     {
69     }
70
71 protected:
72     virtual void setupChildProcess();
73 };
74
75 void 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 void ProcessLauncher::launchProcess()
87 {
88     QString applicationPath = QLatin1String("%1 %2");
89
90     if (QFile::exists(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"))) {
91         applicationPath = applicationPath.arg(QCoreApplication::applicationDirPath() + QLatin1String("/QtWebProcess"));
92     } else {
93         applicationPath = applicationPath.arg(QLatin1String("QtWebProcess"));
94     }
95
96     int sockets[2];
97     if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) == -1) {
98         qDebug() << "Creation of socket failed with errno:" << errno;
99         ASSERT_NOT_REACHED();
100         return;
101     }
102
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) { }
109             return;
110         }
111     }
112
113     QString program(applicationPath.arg(sockets[0]));
114
115     QProcess* webProcess = new QtWebProcess();
116     webProcess->setProcessChannelMode(QProcess::ForwardedChannels);
117     webProcess->start(program);
118
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();
123             delete webProcess;
124             return;
125         }
126     }
127
128     if (!webProcess->waitForStarted()) {
129         qDebug() << "Failed to start" << program;
130         ASSERT_NOT_REACHED();
131         delete webProcess;
132         return;
133     }
134
135     setpriority(PRIO_PROCESS, webProcess->pid(), 10);
136
137     RunLoop::main()->scheduleWork(WorkItem::create(this, &WebKit::ProcessLauncher::didFinishLaunchingProcess, webProcess, sockets[1]));
138 }
139
140 void ProcessLauncher::terminateProcess()
141 {
142     if (!m_processIdentifier)
143         return;
144
145     QObject::connect(m_processIdentifier, SIGNAL(finished(int)), m_processIdentifier, SLOT(deleteLater()), Qt::QueuedConnection);
146     m_processIdentifier->terminate();
147 }
148
149 void ProcessLauncher::platformInvalidate()
150 {
151
152 }
153
154 } // namespace WebKit
155
156 #include "ProcessLauncherQt.moc"