ProcessLauncherGtk.cpp [plain text]
#include "config.h"
#include "ProcessLauncher.h"
#include "Connection.h"
#include "ProcessExecutablePath.h"
#include <WebCore/FileSystem.h>
#include <WebCore/ResourceHandle.h>
#include <WebCore/RunLoop.h>
#include <errno.h>
#include <locale.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
#include <wtf/gobject/GOwnPtr.h>
#include <wtf/gobject/GlibUtilities.h>
#if OS(LINUX)
#include <sys/prctl.h>
#endif
#ifdef SOCK_SEQPACKET
#define SOCKET_TYPE SOCK_SEQPACKET
#else
#define SOCKET_TYPE SOCK_STREAM
#endif
using namespace WebCore;
namespace WebKit {
static void childSetupFunction(gpointer userData)
{
int socket = GPOINTER_TO_INT(userData);
close(socket);
g_setenv("LC_ALL", setlocale(LC_ALL, 0), TRUE);
#if OS(LINUX)
prctl(PR_SET_PDEATHSIG, SIGKILL);
#endif
}
static void childFinishedFunction(GPid, gint status, gpointer userData)
{
if (WIFEXITED(status) && !WEXITSTATUS(status))
return;
close(GPOINTER_TO_INT(userData));
}
void ProcessLauncher::launchProcess()
{
GPid pid = 0;
int sockets[2];
if (socketpair(AF_UNIX, SOCKET_TYPE, 0, sockets) < 0) {
g_printerr("Creation of socket failed: %s.\n", g_strerror(errno));
ASSERT_NOT_REACHED();
return;
}
String executablePath = m_launchOptions.processType == WebProcess ?
executablePathOfWebProcess() : executablePathOfPluginProcess();
CString binaryPath = fileSystemRepresentation(executablePath);
GOwnPtr<gchar> socket(g_strdup_printf("%d", sockets[0]));
char* argv[3];
argv[0] = const_cast<char*>(binaryPath.data());
argv[1] = socket.get();
argv[2] = 0;
GOwnPtr<GError> error;
int spawnFlags = G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD;
if (!g_spawn_async(0, argv, 0, static_cast<GSpawnFlags>(spawnFlags), childSetupFunction, GINT_TO_POINTER(sockets[1]), &pid, &error.outPtr())) {
g_printerr("Unable to fork a new WebProcess: %s.\n", error->message);
ASSERT_NOT_REACHED();
}
close(sockets[0]);
m_processIdentifier = pid;
g_child_watch_add(m_processIdentifier, childFinishedFunction, GINT_TO_POINTER(sockets[1]));
RunLoop::main()->dispatch(bind(&ProcessLauncher::didFinishLaunchingProcess, this, m_processIdentifier, sockets[1]));
}
void ProcessLauncher::terminateProcess()
{
if (!m_processIdentifier)
return;
kill(m_processIdentifier, SIGKILL);
}
void ProcessLauncher::platformInvalidate()
{
}
}