2 * Copyright (C) 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #import "ProcessLauncher.h"
29 #import "DynamicLinkerEnvironmentExtractor.h"
30 #import "EnvironmentVariables.h"
31 #import "WebKitSystemInterface.h"
32 #import <WebCore/RunLoop.h>
33 #import <crt_externs.h>
34 #import <mach-o/dyld.h>
35 #import <mach/machine.h>
36 #import <runtime/InitializeThreading.h>
37 #import <servers/bootstrap.h>
41 #import <wtf/PassRefPtr.h>
42 #import <wtf/RetainPtr.h>
43 #import <wtf/Threading.h>
44 #import <wtf/text/CString.h>
45 #import <wtf/text/WTFString.h>
51 using namespace WebCore;
53 // FIXME: We should be doing this another way.
54 extern "C" kern_return_t bootstrap_register2(mach_port_t, name_t, mach_port_t, uint64_t);
57 extern "C" void xpc_connection_set_instance(xpc_connection_t, uuid_t);
58 extern "C" void xpc_dictionary_set_mach_send(xpc_object_t, const char*, mach_port_t);
65 struct UUIDHolder : public RefCounted<UUIDHolder> {
66 static PassRefPtr<UUIDHolder> create()
68 return adoptRef(new UUIDHolder);
81 static void setUpTerminationNotificationHandler(pid_t pid)
84 dispatch_source_t processDiedSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
85 dispatch_source_set_event_handler(processDiedSource, ^{
87 waitpid(dispatch_source_get_handle(processDiedSource), &status, 0);
88 dispatch_source_cancel(processDiedSource);
90 dispatch_source_set_cancel_handler(processDiedSource, ^{
91 dispatch_release(processDiedSource);
93 dispatch_resume(processDiedSource);
97 static void addDYLDEnvironmentAdditions(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, EnvironmentVariables& environmentVariables)
99 DynamicLinkerEnvironmentExtractor environmentExtractor([[NSBundle mainBundle] executablePath], _NSGetMachExecuteHeader()->cputype);
100 environmentExtractor.getExtractedEnvironmentVariables(environmentVariables);
102 NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"];
103 NSString *frameworksPath = [[webKit2Bundle bundlePath] stringByDeletingLastPathComponent];
105 // To make engineering builds work, if the path is outside of /System set up
106 // DYLD_FRAMEWORK_PATH to pick up other frameworks, but don't do it for the
107 // production configuration because it involves extra file system access.
108 if (isWebKitDevelopmentBuild)
109 environmentVariables.appendValue("DYLD_FRAMEWORK_PATH", [frameworksPath fileSystemRepresentation], ':');
111 NSString *processShimPathNSString = nil;
112 #if ENABLE(PLUGIN_PROCESS)
113 if (launchOptions.processType == ProcessLauncher::PluginProcess) {
114 NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"PluginProcess.app"];
115 NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
117 processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PluginProcessShim.dylib"];
119 #endif // ENABLE(PLUGIN_PROCESS)
120 if (launchOptions.processType == ProcessLauncher::WebProcess) {
121 NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"WebProcess.app"];
122 NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
124 processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"SecItemShim.dylib"];
125 } else if (launchOptions.processType == ProcessLauncher::NetworkProcess) {
126 NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"NetworkProcess.app"];
127 NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
129 processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"SecItemShim.dylib"];
132 // Make sure that the shim library file exists and insert it.
133 if (processShimPathNSString) {
134 const char* processShimPath = [processShimPathNSString fileSystemRepresentation];
136 if (stat(processShimPath, &statBuf) == 0 && (statBuf.st_mode & S_IFMT) == S_IFREG)
137 environmentVariables.appendValue("DYLD_INSERT_LIBRARIES", processShimPath, ':');
142 typedef void (ProcessLauncher::*DidFinishLaunchingProcessFunction)(PlatformProcessIdentifier, CoreIPC::Connection::Identifier);
146 static const char* serviceName(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment)
148 switch (launchOptions.processType) {
149 case ProcessLauncher::WebProcess:
151 return "com.apple.WebKit.WebContent.Development";
152 return "com.apple.WebKit.WebContent";
153 #if ENABLE(NETWORK_PROCESS)
154 case ProcessLauncher::NetworkProcess:
156 return "com.apple.WebKit.Networking.Development";
157 return "com.apple.WebKit.Networking";
159 #if ENABLE(PLUGIN_PROCESS)
160 case ProcessLauncher::PluginProcess:
162 return "com.apple.WebKit.Plugin.Development";
164 // FIXME: Support plugins that require an executable heap.
165 if (launchOptions.architecture == CPU_TYPE_X86)
166 return "com.apple.WebKit.Plugin.32";
167 if (launchOptions.architecture == CPU_TYPE_X86_64)
168 return "com.apple.WebKit.Plugin.64";
170 ASSERT_NOT_REACHED();
173 #if ENABLE(SHARED_WORKER_PROCESS)
174 case ProcessLauncher::SharedWorkerProcess:
175 ASSERT_NOT_REACHED();
181 static void connectToService(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction, UUIDHolder* instanceUUID)
183 // Create a connection to the WebKit2 XPC service.
184 xpc_connection_t connection = xpc_connection_create(serviceName(launchOptions, forDevelopment), 0);
185 xpc_connection_set_instance(connection, instanceUUID->uuid);
187 // XPC requires having an event handler, even if it is not used.
188 xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { });
189 xpc_connection_resume(connection);
191 #if ENABLE(NETWORK_PROCESS)
192 if (launchOptions.processType == ProcessLauncher::NetworkProcess) {
193 xpc_object_t preBootstrapMessage = xpc_dictionary_create(0, 0, 0);
194 xpc_dictionary_set_string(preBootstrapMessage, "message-name", "pre-bootstrap");
195 xpc_connection_send_message(connection, preBootstrapMessage);
196 xpc_release(preBootstrapMessage);
200 // Create the listening port.
201 mach_port_t listeningPort;
202 mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
204 // Insert a send right so we can send to it.
205 mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
207 NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
208 CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname();
210 xpc_object_t bootstrapMessage = xpc_dictionary_create(0, 0, 0);
211 xpc_dictionary_set_string(bootstrapMessage, "message-name", "bootstrap");
212 xpc_dictionary_set_string(bootstrapMessage, "framework-executable-path", [[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] executablePath] fileSystemRepresentation]);
213 xpc_dictionary_set_mach_send(bootstrapMessage, "server-port", listeningPort);
214 xpc_dictionary_set_string(bootstrapMessage, "client-identifier", clientIdentifier.data());
215 xpc_dictionary_set_string(bootstrapMessage, "ui-process-name", [[[NSProcessInfo processInfo] processName] UTF8String]);
217 if (forDevelopment) {
218 xpc_dictionary_set_fd(bootstrapMessage, "stdout", STDOUT_FILENO);
219 xpc_dictionary_set_fd(bootstrapMessage, "stderr", STDERR_FILENO);
222 xpc_object_t extraInitializationData = xpc_dictionary_create(0, 0, 0);
223 HashMap<String, String>::const_iterator it = launchOptions.extraInitializationData.begin();
224 HashMap<String, String>::const_iterator end = launchOptions.extraInitializationData.end();
225 for (; it != end; ++it)
226 xpc_dictionary_set_string(extraInitializationData, it->key.utf8().data(), it->value.utf8().data());
227 xpc_dictionary_set_value(bootstrapMessage, "extra-initialization-data", extraInitializationData);
228 xpc_release(extraInitializationData);
232 xpc_connection_send_message_with_reply(connection, bootstrapMessage, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(xpc_object_t reply) {
233 xpc_type_t type = xpc_get_type(reply);
234 if (type == XPC_TYPE_ERROR) {
235 // We failed to launch. Release the send right.
236 mach_port_deallocate(mach_task_self(), listeningPort);
238 // And the receive right.
239 mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1);
241 RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, 0, CoreIPC::Connection::Identifier()));
243 ASSERT(type == XPC_TYPE_DICTIONARY);
244 ASSERT(!strcmp(xpc_dictionary_get_string(reply, "message-name"), "process-finished-launching"));
246 // The process has finished launching, grab the pid from the connection.
247 pid_t processIdentifier = xpc_connection_get_pid(connection);
249 // We've finished launching the process, message back to the main run loop.
250 RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, CoreIPC::Connection::Identifier(listeningPort, connection)));
255 xpc_release(bootstrapMessage);
258 static void connectToReExecService(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
260 EnvironmentVariables environmentVariables;
261 addDYLDEnvironmentAdditions(launchOptions, true, environmentVariables);
263 // Generate the uuid for the service instance we are about to create.
264 // FIXME: This UUID should be stored on the ChildProcessProxy.
265 RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create();
267 xpc_connection_t reExecConnection = xpc_connection_create(serviceName(launchOptions, true), 0);
268 xpc_connection_set_instance(reExecConnection, instanceUUID->uuid);
270 // Keep the ProcessLauncher alive while we do the re-execing (balanced in event handler).
273 // We wait for the connection to tear itself down (indicated via an error event)
274 // to indicate that the service instance re-execed itself, and is now ready to be
276 xpc_connection_set_event_handler(reExecConnection, ^(xpc_object_t event) {
277 ASSERT(xpc_get_type(event) == XPC_TYPE_ERROR);
279 connectToService(launchOptions, true, that, didFinishLaunchingProcessFunction, instanceUUID.get());
281 // Release the connection.
282 xpc_release(reExecConnection);
284 // Other end of ref called before we setup the event handler.
287 xpc_connection_resume(reExecConnection);
289 xpc_object_t reExecMessage = xpc_dictionary_create(0, 0, 0);
290 xpc_dictionary_set_string(reExecMessage, "message-name", "re-exec");
292 cpu_type_t architecture = launchOptions.architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture ? _NSGetMachExecuteHeader()->cputype : launchOptions.architecture;
293 xpc_dictionary_set_uint64(reExecMessage, "architecture", (uint64_t)architecture);
295 xpc_object_t environment = xpc_array_create(0, 0);
296 char** environmentPointer = environmentVariables.environmentPointer();
297 Vector<CString> temps;
298 for (size_t i = 0; environmentPointer[i]; ++i) {
299 CString temp(environmentPointer[i], strlen(environmentPointer[i]));
302 xpc_array_set_string(environment, XPC_ARRAY_APPEND, temp.data());
304 xpc_dictionary_set_value(reExecMessage, "environment", environment);
305 xpc_release(environment);
307 xpc_dictionary_set_bool(reExecMessage, "executable-heap", launchOptions.executableHeap);
309 xpc_connection_send_message(reExecConnection, reExecMessage);
310 xpc_release(reExecMessage);
313 static void createService(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
315 if (forDevelopment) {
316 connectToReExecService(launchOptions, that, didFinishLaunchingProcessFunction);
320 // Generate the uuid for the service instance we are about to create.
321 // FIXME: This UUID should be stored on the ChildProcessProxy.
322 RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create();
323 connectToService(launchOptions, false, that, didFinishLaunchingProcessFunction, instanceUUID.get());
328 static bool tryPreexistingProcess(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
330 EnvironmentVariables environmentVariables;
331 static const char* preexistingProcessServiceName = environmentVariables.get(EnvironmentVariables::preexistingProcessServiceNameKey());
333 ProcessLauncher::ProcessType preexistingProcessType;
334 if (preexistingProcessServiceName)
335 ProcessLauncher::getProcessTypeFromString(environmentVariables.get(EnvironmentVariables::preexistingProcessTypeKey()), preexistingProcessType);
337 bool usePreexistingProcess = preexistingProcessServiceName && preexistingProcessType == launchOptions.processType;
338 if (!usePreexistingProcess)
341 // Create the listening port.
342 mach_port_t listeningPort;
343 mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
345 // Insert a send right so we can send to it.
346 mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
348 pid_t processIdentifier = 0;
350 mach_port_t lookupPort;
351 bootstrap_look_up(bootstrap_port, preexistingProcessServiceName, &lookupPort);
353 mach_msg_header_t header;
354 header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND);
356 header.msgh_local_port = listeningPort;
357 header.msgh_remote_port = lookupPort;
358 header.msgh_size = sizeof(header);
359 kern_return_t kr = mach_msg(&header, MACH_SEND_MSG, sizeof(header), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
361 mach_port_deallocate(mach_task_self(), lookupPort);
362 preexistingProcessServiceName = 0;
365 LOG_ERROR("Failed to pick up preexisting process at %s (%x). Launching a new process of type %s instead.", preexistingProcessServiceName, kr, ProcessLauncher::processTypeAsString(launchOptions.processType));
369 // We've finished launching the process, message back to the main run loop.
370 RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, CoreIPC::Connection::Identifier(listeningPort)));
374 static void createProcess(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
376 EnvironmentVariables environmentVariables;
377 addDYLDEnvironmentAdditions(launchOptions, isWebKitDevelopmentBuild, environmentVariables);
379 // Create the listening port.
380 mach_port_t listeningPort;
381 mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
383 // Insert a send right so we can send to it.
384 mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
386 RetainPtr<CFStringRef> cfLocalization(AdoptCF, WKCopyCFLocalizationPreferredName(NULL));
387 CString localization = String(cfLocalization.get()).utf8();
389 NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"];
391 NSString *processPath = nil;
392 switch (launchOptions.processType) {
393 case ProcessLauncher::WebProcess:
394 processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"WebProcess.app"];
396 #if ENABLE(PLUGIN_PROCESS)
397 case ProcessLauncher::PluginProcess:
398 processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"PluginProcess.app"];
401 #if ENABLE(NETWORK_PROCESS)
402 case ProcessLauncher::NetworkProcess:
403 processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"NetworkProcess.app"];
406 #if ENABLE(SHARED_WORKER_PROCESS)
407 case ProcessLauncher::SharedWorkerProcess:
408 processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"SharedWorkerProcess.app"];
413 NSString *frameworkExecutablePath = [webKit2Bundle executablePath];
414 NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
416 NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
417 CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname();
419 // Make a unique, per pid, per process launcher web process service name.
420 CString serviceName = String::format("com.apple.WebKit.WebProcess-%d-%p", getpid(), that).utf8();
422 Vector<const char*> args;
423 args.append([processAppExecutablePath fileSystemRepresentation]);
424 args.append([frameworkExecutablePath fileSystemRepresentation]);
425 args.append("-type");
426 args.append(ProcessLauncher::processTypeAsString(launchOptions.processType));
427 args.append("-servicename");
428 args.append(serviceName.data());
429 args.append("-localization");
430 args.append(localization.data());
431 args.append("-client-identifier");
432 args.append(clientIdentifier.data());
433 args.append("-ui-process-name");
434 args.append([[[NSProcessInfo processInfo] processName] UTF8String]);
436 HashMap<String, String>::const_iterator it = launchOptions.extraInitializationData.begin();
437 HashMap<String, String>::const_iterator end = launchOptions.extraInitializationData.end();
438 Vector<CString> temps;
439 for (; it != end; ++it) {
440 String keyPlusDash = "-" + it->key;
441 CString key(keyPlusDash.utf8().data());
443 args.append(key.data());
445 CString value(it->value.utf8().data());
447 args.append(value.data());
450 args.append(nullptr);
452 // Register ourselves.
453 kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.data()), listeningPort, 0);
454 ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
456 posix_spawnattr_t attr;
457 posix_spawnattr_init(&attr);
461 // We want our process to receive all signals.
462 sigset_t signalMaskSet;
463 sigemptyset(&signalMaskSet);
465 posix_spawnattr_setsigmask(&attr, &signalMaskSet);
466 flags |= POSIX_SPAWN_SETSIGMASK;
468 // Determine the architecture to use.
469 cpu_type_t architecture = launchOptions.architecture;
470 if (architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture)
471 architecture = _NSGetMachExecuteHeader()->cputype;
473 cpu_type_t cpuTypes[] = { architecture };
475 posix_spawnattr_setbinpref_np(&attr, 1, cpuTypes, &outCount);
477 // Start suspended so we can set up the termination notification handler.
478 flags |= POSIX_SPAWN_START_SUSPENDED;
480 static const int allowExecutableHeapFlag = 0x2000;
481 if (launchOptions.executableHeap)
482 flags |= allowExecutableHeapFlag;
484 posix_spawnattr_setflags(&attr, flags);
486 pid_t processIdentifier = 0;
487 int result = posix_spawn(&processIdentifier, args[0], 0, &attr, const_cast<char**>(args.data()), environmentVariables.environmentPointer());
489 posix_spawnattr_destroy(&attr);
492 // Set up the termination notification handler and then ask the child process to continue.
493 setUpTerminationNotificationHandler(processIdentifier);
494 kill(processIdentifier, SIGCONT);
496 // We failed to launch. Release the send right.
497 mach_port_deallocate(mach_task_self(), listeningPort);
499 // And the receive right.
500 mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1);
502 listeningPort = MACH_PORT_NULL;
503 processIdentifier = 0;
506 // We've finished launching the process, message back to the main run loop.
507 RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, CoreIPC::Connection::Identifier(listeningPort)));
510 void ProcessLauncher::launchProcess()
512 if (tryPreexistingProcess(m_launchOptions, this, &ProcessLauncher::didFinishLaunchingProcess))
515 bool isWebKitDevelopmentBuild = ![[[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] bundlePath] stringByDeletingLastPathComponent] hasPrefix:@"/System/"];
518 if (m_launchOptions.useXPC) {
519 createService(m_launchOptions, isWebKitDevelopmentBuild, this, &ProcessLauncher::didFinishLaunchingProcess);
524 createProcess(m_launchOptions, isWebKitDevelopmentBuild, this, &ProcessLauncher::didFinishLaunchingProcess);
527 void ProcessLauncher::terminateProcess()
529 if (!m_processIdentifier)
532 kill(m_processIdentifier, SIGKILL);
535 void ProcessLauncher::platformInvalidate()
539 } // namespace WebKit