Use XPC services in the iOS Simulator, but not in Mountain Lion
[WebKit-https.git] / Source / WebKit2 / UIProcess / Launcher / mac / ProcessLauncherMac.mm
1 /*
2  * Copyright (C) 2010, 2011, 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #import "config.h"
27 #import "ProcessLauncher.h"
28
29 #import "DynamicLinkerEnvironmentExtractor.h"
30 #import "EnvironmentVariables.h"
31 #import "WebKitSystemInterface.h"
32 #import <crt_externs.h>
33 #import <mach-o/dyld.h>
34 #import <mach/machine.h>
35 #import <servers/bootstrap.h>
36 #import <spawn.h>
37 #import <sys/param.h>
38 #import <sys/stat.h>
39 #import <wtf/PassRefPtr.h>
40 #import <wtf/RetainPtr.h>
41 #import <wtf/RunLoop.h>
42 #import <wtf/Threading.h>
43 #import <wtf/text/CString.h>
44 #import <wtf/text/WTFString.h>
45 #import <xpc/xpc.h>
46
47 // FIXME: We should be doing this another way.
48 extern "C" kern_return_t bootstrap_register2(mach_port_t, name_t, mach_port_t, uint64_t);
49
50 extern "C" void xpc_connection_set_instance(xpc_connection_t, uuid_t);
51 extern "C" void xpc_dictionary_set_mach_send(xpc_object_t, const char*, mach_port_t);
52
53 namespace WebKit {
54
55 namespace {
56
57 struct UUIDHolder : public RefCounted<UUIDHolder> {
58     static PassRefPtr<UUIDHolder> create()
59     {
60         return adoptRef(new UUIDHolder);
61     }
62
63     UUIDHolder()
64     {
65         uuid_generate(uuid);
66     }
67
68     uuid_t uuid;
69 };
70
71 }
72
73 static void setUpTerminationNotificationHandler(pid_t pid)
74 {
75     dispatch_source_t processDiedSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
76     dispatch_source_set_event_handler(processDiedSource, ^{
77         int status;
78         waitpid(dispatch_source_get_handle(processDiedSource), &status, 0);
79         dispatch_source_cancel(processDiedSource);
80     });
81     dispatch_source_set_cancel_handler(processDiedSource, ^{
82         dispatch_release(processDiedSource);
83     });
84     dispatch_resume(processDiedSource);
85 }
86
87 static void addDYLDEnvironmentAdditions(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, EnvironmentVariables& environmentVariables)
88 {
89     DynamicLinkerEnvironmentExtractor environmentExtractor([[NSBundle mainBundle] executablePath], _NSGetMachExecuteHeader()->cputype);
90     environmentExtractor.getExtractedEnvironmentVariables(environmentVariables);
91
92     NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"];
93     NSString *frameworksPath = [[webKit2Bundle bundlePath] stringByDeletingLastPathComponent];
94
95     // To make engineering builds work, if the path is outside of /System set up
96     // DYLD_FRAMEWORK_PATH to pick up other frameworks, but don't do it for the
97     // production configuration because it involves extra file system access.
98     if (isWebKitDevelopmentBuild)
99         environmentVariables.appendValue("DYLD_FRAMEWORK_PATH", [frameworksPath fileSystemRepresentation], ':');
100
101     NSString *processShimPathNSString = nil;
102 #if ENABLE(NETSCAPE_PLUGIN_API)
103     if (launchOptions.processType == ProcessLauncher::PluginProcess) {
104         NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"PluginProcess.app"];
105         NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
106
107         processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PluginProcessShim.dylib"];
108     } else
109 #endif // ENABLE(NETSCAPE_PLUGIN_API)
110 #if ENABLE(NETWORK_PROCESS)
111     if (launchOptions.processType == ProcessLauncher::NetworkProcess) {
112         NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"NetworkProcess.app"];
113         NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
114
115         processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"SecItemShim.dylib"];
116     } else
117 #endif // ENABLE(NETWORK_PROCESS)
118     if (launchOptions.processType == ProcessLauncher::WebProcess) {
119         NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"WebProcess.app"];
120         NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
121
122         processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"WebProcessShim.dylib"];
123     }
124
125     // Make sure that the shim library file exists and insert it.
126     if (processShimPathNSString) {
127         const char* processShimPath = [processShimPathNSString fileSystemRepresentation];
128         struct stat statBuf;
129         if (stat(processShimPath, &statBuf) == 0 && (statBuf.st_mode & S_IFMT) == S_IFREG)
130             environmentVariables.appendValue("DYLD_INSERT_LIBRARIES", processShimPath, ':');
131     }
132
133 }
134
135 typedef void (ProcessLauncher::*DidFinishLaunchingProcessFunction)(PlatformProcessIdentifier, IPC::Connection::Identifier);
136
137 static const char* serviceName(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment)
138 {
139     switch (launchOptions.processType) {
140     case ProcessLauncher::WebProcess:
141         if (forDevelopment)
142             return "com.apple.WebKit.WebContent.Development";
143         return "com.apple.WebKit.WebContent";
144 #if ENABLE(NETWORK_PROCESS)
145     case ProcessLauncher::NetworkProcess:
146         if (forDevelopment)
147             return "com.apple.WebKit.Networking.Development";
148         return "com.apple.WebKit.Networking";
149 #endif
150 #if ENABLE(DATABASE_PROCESS)
151     case ProcessLauncher::DatabaseProcess:
152         if (forDevelopment)
153             return "com.apple.WebKit.Databases.Development";
154         return "com.apple.WebKit.Databases";
155 #endif
156 #if ENABLE(NETSCAPE_PLUGIN_API)
157     case ProcessLauncher::PluginProcess:
158         if (forDevelopment)
159             return "com.apple.WebKit.Plugin.Development";
160
161         // FIXME: Support plugins that require an executable heap.
162         if (launchOptions.architecture == CPU_TYPE_X86)
163             return "com.apple.WebKit.Plugin.32";
164         if (launchOptions.architecture == CPU_TYPE_X86_64)
165             return "com.apple.WebKit.Plugin.64";
166
167         ASSERT_NOT_REACHED();
168         return 0;
169 #endif
170     }
171 }
172
173 static void connectToService(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction, UUIDHolder* instanceUUID)
174 {
175     // Create a connection to the WebKit2 XPC service.
176     xpc_connection_t connection = xpc_connection_create(serviceName(launchOptions, forDevelopment), 0);
177     xpc_connection_set_instance(connection, instanceUUID->uuid);
178
179     // XPC requires having an event handler, even if it is not used.
180     xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { });
181     xpc_connection_resume(connection);
182
183 #if ENABLE(NETWORK_PROCESS) && !PLATFORM(IOS)
184     // Leak a boost onto the NetworkProcess.
185     if (launchOptions.processType == ProcessLauncher::NetworkProcess) {
186         xpc_object_t preBootstrapMessage = xpc_dictionary_create(0, 0, 0);
187         xpc_dictionary_set_string(preBootstrapMessage, "message-name", "pre-bootstrap");
188         xpc_connection_send_message(connection, preBootstrapMessage);
189         xpc_release(preBootstrapMessage);
190     }
191 #endif
192
193     // Create the listening port.
194     mach_port_t listeningPort;
195     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
196     
197     // Insert a send right so we can send to it.
198     mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
199
200     NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
201     CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname();
202
203     xpc_object_t bootstrapMessage = xpc_dictionary_create(0, 0, 0);
204     xpc_dictionary_set_string(bootstrapMessage, "message-name", "bootstrap");
205     xpc_dictionary_set_string(bootstrapMessage, "framework-executable-path", [[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] executablePath] fileSystemRepresentation]);
206     xpc_dictionary_set_mach_send(bootstrapMessage, "server-port", listeningPort);
207     xpc_dictionary_set_string(bootstrapMessage, "client-identifier", clientIdentifier.data());
208     xpc_dictionary_set_string(bootstrapMessage, "ui-process-name", [[[NSProcessInfo processInfo] processName] UTF8String]);
209
210     if (forDevelopment) {
211         xpc_dictionary_set_fd(bootstrapMessage, "stdout", STDOUT_FILENO);
212         xpc_dictionary_set_fd(bootstrapMessage, "stderr", STDERR_FILENO);
213     }
214
215     xpc_object_t extraInitializationData = xpc_dictionary_create(0, 0, 0);
216     HashMap<String, String>::const_iterator it = launchOptions.extraInitializationData.begin();
217     HashMap<String, String>::const_iterator end = launchOptions.extraInitializationData.end();
218     for (; it != end; ++it)
219         xpc_dictionary_set_string(extraInitializationData, it->key.utf8().data(), it->value.utf8().data());
220     xpc_dictionary_set_value(bootstrapMessage, "extra-initialization-data", extraInitializationData);
221     xpc_release(extraInitializationData);
222
223     that->ref();
224
225     xpc_connection_send_message_with_reply(connection, bootstrapMessage, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(xpc_object_t reply) {
226         xpc_type_t type = xpc_get_type(reply);
227         if (type == XPC_TYPE_ERROR) {
228             // We failed to launch. Release the send right.
229             mach_port_deallocate(mach_task_self(), listeningPort);
230
231             // And the receive right.
232             mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1);
233
234             RunLoop::main().dispatch(bind(didFinishLaunchingProcessFunction, that, 0, IPC::Connection::Identifier()));
235         } else {
236             ASSERT(type == XPC_TYPE_DICTIONARY);
237             ASSERT(!strcmp(xpc_dictionary_get_string(reply, "message-name"), "process-finished-launching"));
238
239             // The process has finished launching, grab the pid from the connection.
240             pid_t processIdentifier = xpc_connection_get_pid(connection);
241
242             // We've finished launching the process, message back to the main run loop.
243             RunLoop::main().dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, IPC::Connection::Identifier(listeningPort, connection)));
244         }
245
246         that->deref();
247     });
248     xpc_release(bootstrapMessage);
249 }
250
251 static void connectToReExecService(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
252 {
253     EnvironmentVariables environmentVariables;
254     addDYLDEnvironmentAdditions(launchOptions, true, environmentVariables);
255
256     // Generate the uuid for the service instance we are about to create.
257     // FIXME: This UUID should be stored on the ChildProcessProxy.
258     RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create();
259
260     xpc_connection_t reExecConnection = xpc_connection_create(serviceName(launchOptions, true), 0);
261     xpc_connection_set_instance(reExecConnection, instanceUUID->uuid);
262
263     // Keep the ProcessLauncher alive while we do the re-execing (balanced in event handler).
264     that->ref();
265
266     // We wait for the connection to tear itself down (indicated via an error event)
267     // to indicate that the service instance re-execed itself, and is now ready to be
268     // connected to.
269     xpc_connection_set_event_handler(reExecConnection, ^(xpc_object_t event) {
270         ASSERT(xpc_get_type(event) == XPC_TYPE_ERROR);
271
272         connectToService(launchOptions, true, that, didFinishLaunchingProcessFunction, instanceUUID.get());
273
274         // Release the connection.
275         xpc_release(reExecConnection);
276
277         // Other end of ref called before we setup the event handler.
278         that->deref();
279     });
280     xpc_connection_resume(reExecConnection);
281
282     xpc_object_t reExecMessage = xpc_dictionary_create(0, 0, 0);
283     xpc_dictionary_set_string(reExecMessage, "message-name", "re-exec");
284
285     cpu_type_t architecture = launchOptions.architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture ? _NSGetMachExecuteHeader()->cputype : launchOptions.architecture;
286     xpc_dictionary_set_uint64(reExecMessage, "architecture", (uint64_t)architecture);
287     
288     xpc_object_t environment = xpc_array_create(0, 0);
289     char** environmentPointer = environmentVariables.environmentPointer();
290     Vector<CString> temps;
291     for (size_t i = 0; environmentPointer[i]; ++i) {
292         CString temp(environmentPointer[i], strlen(environmentPointer[i]));
293         temps.append(temp);
294
295         xpc_array_set_string(environment, XPC_ARRAY_APPEND, temp.data());
296     }
297     xpc_dictionary_set_value(reExecMessage, "environment", environment);
298     xpc_release(environment);
299
300     xpc_dictionary_set_bool(reExecMessage, "executable-heap", launchOptions.executableHeap);
301
302     xpc_connection_send_message(reExecConnection, reExecMessage);
303     xpc_release(reExecMessage);
304 }
305
306 static void createService(const ProcessLauncher::LaunchOptions& launchOptions, bool forDevelopment, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
307 {
308     if (forDevelopment) {
309         connectToReExecService(launchOptions, that, didFinishLaunchingProcessFunction);
310         return;
311     }
312
313     // Generate the uuid for the service instance we are about to create.
314     // FIXME: This UUID should be stored on the ChildProcessProxy.
315     RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create();
316     connectToService(launchOptions, false, that, didFinishLaunchingProcessFunction, instanceUUID.get());
317 }
318
319 static bool tryPreexistingProcess(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
320 {
321     EnvironmentVariables environmentVariables;
322     static const char* preexistingProcessServiceName = environmentVariables.get(EnvironmentVariables::preexistingProcessServiceNameKey());
323
324     ProcessLauncher::ProcessType preexistingProcessType;
325     if (preexistingProcessServiceName)
326         ProcessLauncher::getProcessTypeFromString(environmentVariables.get(EnvironmentVariables::preexistingProcessTypeKey()), preexistingProcessType);
327
328     bool usePreexistingProcess = preexistingProcessServiceName && preexistingProcessType == launchOptions.processType;
329     if (!usePreexistingProcess)
330         return false;
331
332     // Create the listening port.
333     mach_port_t listeningPort;
334     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
335     
336     // Insert a send right so we can send to it.
337     mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
338
339     pid_t processIdentifier = 0;
340
341     mach_port_t lookupPort;
342     bootstrap_look_up(bootstrap_port, preexistingProcessServiceName, &lookupPort);
343
344     mach_msg_header_t header;
345     header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND);
346     header.msgh_id = 0;
347     header.msgh_local_port = listeningPort;
348     header.msgh_remote_port = lookupPort;
349     header.msgh_size = sizeof(header);
350     kern_return_t kr = mach_msg(&header, MACH_SEND_MSG, sizeof(header), 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
351
352     mach_port_deallocate(mach_task_self(), lookupPort);
353     preexistingProcessServiceName = 0;
354
355     if (kr) {
356         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));
357         return false;
358     }
359     
360     // We've finished launching the process, message back to the main run loop.
361     RunLoop::main().dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, IPC::Connection::Identifier(listeningPort)));
362     return true;
363 }
364
365 static void createProcess(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction)
366 {
367     EnvironmentVariables environmentVariables;
368     addDYLDEnvironmentAdditions(launchOptions, isWebKitDevelopmentBuild, environmentVariables);
369
370     // Create the listening port.
371     mach_port_t listeningPort;
372     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
373
374     // Insert a send right so we can send to it.
375     mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
376
377     NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"];
378
379     NSString *processPath = nil;
380     switch (launchOptions.processType) {
381     case ProcessLauncher::WebProcess:
382         processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"WebProcess.app"];
383         break;
384 #if ENABLE(NETSCAPE_PLUGIN_API)
385     case ProcessLauncher::PluginProcess:
386         processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"PluginProcess.app"];
387         break;
388 #endif
389 #if ENABLE(NETWORK_PROCESS)
390     case ProcessLauncher::NetworkProcess:
391         processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"NetworkProcess.app"];
392         break;
393 #endif
394 #if ENABLE(DATABASE_PROCESS)
395     case ProcessLauncher::DatabaseProcess:
396         processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"DatabaseProcess.app"];
397         break;
398 #endif
399     }
400
401     NSString *frameworkExecutablePath = [webKit2Bundle executablePath];
402     NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath];
403
404     NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
405     CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname();
406
407     // Make a unique, per pid, per process launcher web process service name.
408     CString serviceName = String::format("com.apple.WebKit.WebProcess-%d-%p", getpid(), that).utf8();
409
410     // Inherit UI process localization. It can be different from child process default localization:
411     // 1. When the application and system frameworks simply have different localized resources available, we should match the application.
412     // 1.1. An important case is WebKitTestRunner, where we should use English localizations for all system frameworks.
413     // 2. When AppleLanguages is passed as command line argument for UI process, or set in its preferences, we should respect it in child processes.
414     CString appleLanguagesArgument = String("('" + String(adoptCF(WKCopyCFLocalizationPreferredName(0)).get()) + "')").utf8();
415
416     Vector<const char*> args;
417     args.append([processAppExecutablePath fileSystemRepresentation]);
418     args.append([frameworkExecutablePath fileSystemRepresentation]);
419     args.append("-type");
420     args.append(ProcessLauncher::processTypeAsString(launchOptions.processType));
421     args.append("-servicename");
422     args.append(serviceName.data());
423     args.append("-client-identifier");
424     args.append(clientIdentifier.data());
425     args.append("-ui-process-name");
426     args.append([[[NSProcessInfo processInfo] processName] UTF8String]);
427     args.append("-AppleLanguages"); // This argument will be handled by Core Foundation.
428     args.append(appleLanguagesArgument.data());
429
430     HashMap<String, String>::const_iterator it = launchOptions.extraInitializationData.begin();
431     HashMap<String, String>::const_iterator end = launchOptions.extraInitializationData.end();
432     Vector<CString> temps;
433     for (; it != end; ++it) {
434         String keyPlusDash = "-" + it->key;
435         CString key(keyPlusDash.utf8().data());
436         temps.append(key);
437         args.append(key.data());
438
439         CString value(it->value.utf8().data());
440         temps.append(value);
441         args.append(value.data());
442     }
443
444     args.append(nullptr);
445
446     // Register ourselves.
447     kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.data()), listeningPort, 0);
448     ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
449
450     posix_spawnattr_t attr;
451     posix_spawnattr_init(&attr);
452
453     short flags = 0;
454
455     // We want our process to receive all signals.
456     sigset_t signalMaskSet;
457     sigemptyset(&signalMaskSet);
458
459     posix_spawnattr_setsigmask(&attr, &signalMaskSet);
460     flags |= POSIX_SPAWN_SETSIGMASK;
461
462     // Determine the architecture to use.
463     cpu_type_t architecture = launchOptions.architecture;
464     if (architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture)
465         architecture = _NSGetMachExecuteHeader()->cputype;
466
467     cpu_type_t cpuTypes[] = { architecture };
468     size_t outCount = 0;
469     posix_spawnattr_setbinpref_np(&attr, 1, cpuTypes, &outCount);
470
471     // Start suspended so we can set up the termination notification handler.
472     flags |= POSIX_SPAWN_START_SUSPENDED;
473
474     static const int allowExecutableHeapFlag = 0x2000;
475     if (launchOptions.executableHeap)
476         flags |= allowExecutableHeapFlag;
477
478     posix_spawnattr_setflags(&attr, flags);
479
480     pid_t processIdentifier = 0;
481     int result = posix_spawn(&processIdentifier, args[0], 0, &attr, const_cast<char**>(args.data()), environmentVariables.environmentPointer());
482
483     posix_spawnattr_destroy(&attr);
484
485     if (!result) {
486         // Set up the termination notification handler and then ask the child process to continue.
487         setUpTerminationNotificationHandler(processIdentifier);
488         kill(processIdentifier, SIGCONT);
489     } else {
490         // We failed to launch. Release the send right.
491         mach_port_deallocate(mach_task_self(), listeningPort);
492
493         // And the receive right.
494         mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1);
495     
496         listeningPort = MACH_PORT_NULL;
497         processIdentifier = 0;
498     }
499
500     // We've finished launching the process, message back to the main run loop.
501     RunLoop::main().dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, IPC::Connection::Identifier(listeningPort)));
502 }
503
504 static NSString *systemDirectoryPath()
505 {
506     static NSString *path = [^{
507 #if PLATFORM(IOS_SIMULATOR)
508         char *simulatorRoot = getenv("SIMULATOR_ROOT");
509         return simulatorRoot ? [NSString stringWithFormat:@"%s/System/", simulatorRoot] : @"/System/";
510 #else
511         return @"/System/";
512 #endif
513     }() copy];
514
515     return path;
516 }
517
518 void ProcessLauncher::launchProcess()
519 {
520     if (tryPreexistingProcess(m_launchOptions, this, &ProcessLauncher::didFinishLaunchingProcess))
521         return;
522
523     bool isWebKitDevelopmentBuild = ![[[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] bundlePath] stringByDeletingLastPathComponent] hasPrefix:systemDirectoryPath()];
524
525     if (m_launchOptions.useXPC) {
526         createService(m_launchOptions, isWebKitDevelopmentBuild, this, &ProcessLauncher::didFinishLaunchingProcess);
527         return;
528     }
529
530     createProcess(m_launchOptions, isWebKitDevelopmentBuild, this, &ProcessLauncher::didFinishLaunchingProcess);
531 }
532
533 void ProcessLauncher::terminateProcess()
534 {
535     if (m_isLaunching) {
536         invalidate();
537         return;
538     }
539
540     if (!m_processIdentifier)
541         return;
542     
543     kill(m_processIdentifier, SIGKILL);
544     m_processIdentifier = 0;
545 }
546     
547 void ProcessLauncher::platformInvalidate()
548 {
549 }
550
551 } // namespace WebKit