diff options
| author | Simon Hausmann <simon.hausmann@digia.com> | 2012-09-24 13:09:44 +0200 |
|---|---|---|
| committer | Simon Hausmann <simon.hausmann@digia.com> | 2012-09-24 13:09:44 +0200 |
| commit | dc6262b587c71c14e30d93e57ed812e36a79a33e (patch) | |
| tree | 03ff986e7aa38bba0c0ef374f44fda52aff93f01 /Source/WebKit2/UIProcess/Launcher | |
| parent | 02e1fbbefd49229b102ef107bd70ce974a2d85fb (diff) | |
| download | qtwebkit-dc6262b587c71c14e30d93e57ed812e36a79a33e.tar.gz | |
Imported WebKit commit 6339232fec7f5d9984a33388aecfd2cbc7832053 (http://svn.webkit.org/repository/webkit/trunk@129343)
New snapshot with build fixes for latest qtbase
Diffstat (limited to 'Source/WebKit2/UIProcess/Launcher')
| -rw-r--r-- | Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm | 364 |
1 files changed, 254 insertions, 110 deletions
diff --git a/Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm b/Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm index ec7d9e1d4..560ceb773 100644 --- a/Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm +++ b/Source/WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm @@ -61,6 +61,24 @@ extern "C" void xpc_dictionary_set_mach_send(xpc_object_t, const char*, mach_por namespace WebKit { +namespace { + +struct UUIDHolder : public RefCounted<UUIDHolder> { + static PassRefPtr<UUIDHolder> create() + { + return adoptRef(new UUIDHolder); + } + + UUIDHolder() + { + uuid_generate(uuid); + } + + uuid_t uuid; +}; + +} + static void setUpTerminationNotificationHandler(pid_t pid) { #if HAVE(DISPATCH_H) @@ -77,25 +95,167 @@ static void setUpTerminationNotificationHandler(pid_t pid) #endif } +static void addDYLDEnvironmentAdditions(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, EnvironmentVariables& environmentVariables) +{ +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 + DynamicLinkerEnvironmentExtractor environmentExtractor([[NSBundle mainBundle] executablePath], _NSGetMachExecuteHeader()->cputype); + environmentExtractor.getExtractedEnvironmentVariables(environmentVariables); +#endif + + NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"]; + NSString *frameworksPath = [[webKit2Bundle bundlePath] stringByDeletingLastPathComponent]; + + // To make engineering builds work, if the path is outside of /System set up + // DYLD_FRAMEWORK_PATH to pick up other frameworks, but don't do it for the + // production configuration because it involves extra file system access. + if (isWebKitDevelopmentBuild) + environmentVariables.appendValue("DYLD_FRAMEWORK_PATH", [frameworksPath fileSystemRepresentation], ':'); + + NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:(launchOptions.processType == ProcessLauncher::PluginProcess ? @"PluginProcess.app" : @"WebProcess.app")]; + NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath]; + + NSString *processShimPathNSString = nil; + if (launchOptions.processType == ProcessLauncher::PluginProcess) + processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PluginProcessShim.dylib"]; + else if (launchOptions.processType == ProcessLauncher::WebProcess) + processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"WebProcessShim.dylib"]; + + // Make sure that the shim library file exists and insert it. + if (processShimPathNSString) { + const char* processShimPath = [processShimPathNSString fileSystemRepresentation]; + struct stat statBuf; + if (stat(processShimPath, &statBuf) == 0 && (statBuf.st_mode & S_IFMT) == S_IFREG) + environmentVariables.appendValue("DYLD_INSERT_LIBRARIES", processShimPath, ':'); + } + +} + typedef void (ProcessLauncher::*DidFinishLaunchingProcessFunction)(PlatformProcessIdentifier, CoreIPC::Connection::Identifier); #if HAVE(XPC) -static void launchXPCService(const ProcessLauncher::LaunchOptions&, const EnvironmentVariables&, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) +static void connectToWebProcessServiceForWebKitDevelopment(const ProcessLauncher::LaunchOptions&, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction, UUIDHolder* instanceUUID) { // Create a connection to the WebKit2 XPC service. - xpc_connection_t connection = xpc_connection_create("com.apple.WebKit2Service", 0); + xpc_connection_t connection = xpc_connection_create("com.apple.WebKit2.WebProcessServiceForWebKitDevelopment", 0); + xpc_connection_set_instance(connection, instanceUUID->uuid); - uuid_t uuid; - uuid_generate(uuid); - xpc_connection_set_instance(connection, uuid); + // XPC requires having an event handler, even if it is not used. + xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { }); + xpc_connection_resume(connection); + + // Create the listening port. + mach_port_t listeningPort; + mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); + + // Insert a send right so we can send to it. + mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND); + + NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; + CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname(); + + xpc_object_t bootStrapMessage = xpc_dictionary_create(0, 0, 0); + xpc_dictionary_set_string(bootStrapMessage, "message-name", "bootstrap"); + xpc_dictionary_set_string(bootStrapMessage, "framework-executable-path", [[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] executablePath] fileSystemRepresentation]); + xpc_dictionary_set_mach_send(bootStrapMessage, "server-port", listeningPort); + xpc_dictionary_set_string(bootStrapMessage, "client-identifier", clientIdentifier.data()); - xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { - xpc_type_t type = xpc_get_type(event); + that->ref(); + + xpc_connection_send_message_with_reply(connection, bootStrapMessage, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(xpc_object_t reply) { + xpc_type_t type = xpc_get_type(reply); if (type == XPC_TYPE_ERROR) { - if (event == XPC_ERROR_CONNECTION_INVALID || event == XPC_ERROR_CONNECTION_INTERRUPTED) - NSLog(@"WebKit2 Web Content service exited."); + // We failed to launch. Release the send right. + mach_port_deallocate(mach_task_self(), listeningPort); + + // And the receive right. + mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1); + + RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, 0, CoreIPC::Connection::Identifier())); + } else { + ASSERT(type == XPC_TYPE_DICTIONARY); + ASSERT(!strcmp(xpc_dictionary_get_string(reply, "message-name"), "process-finished-launching")); + + // The process has finished launching, grab the pid from the connection. + pid_t processIdentifier = xpc_connection_get_pid(connection); + + // We've finished launching the process, message back to the main run loop. + RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, CoreIPC::Connection::Identifier(listeningPort, connection))); } + + that->deref(); }); + xpc_release(bootStrapMessage); +} + +static void createWebProcessServiceForWebKitDevelopment(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) +{ + EnvironmentVariables environmentVariables; + addDYLDEnvironmentAdditions(launchOptions, true, environmentVariables); + + // Generate the uuid for the service instance we are about to create. + // FIXME: This UUID should be stored on the WebProcessProxy. + RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create(); + + xpc_connection_t reExecConnection = xpc_connection_create("com.apple.WebKit2.WebProcessServiceForWebKitDevelopment", 0); + xpc_connection_set_instance(reExecConnection, instanceUUID->uuid); + + // Keep the ProcessLauncher alive while we do the re-execing (balanced in event handler). + that->ref(); + + // We wait for the connection to tear itself down (indicated via an error event) + // to indicate that the service instance re-execed itself, and is now ready to be + // connected to. + xpc_connection_set_event_handler(reExecConnection, ^(xpc_object_t event) { + ASSERT(xpc_get_type(event) == XPC_TYPE_ERROR); + + connectToWebProcessServiceForWebKitDevelopment(launchOptions, that, didFinishLaunchingProcessFunction, instanceUUID.get()); + + // Release the connection. + xpc_release(reExecConnection); + + // Other end of ref called before we setup the event handler. + that->deref(); + }); + xpc_connection_resume(reExecConnection); + + xpc_object_t reExecMessage = xpc_dictionary_create(0, 0, 0); + xpc_dictionary_set_string(reExecMessage, "message-name", "re-exec"); + + cpu_type_t architecture = launchOptions.architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture ? _NSGetMachExecuteHeader()->cputype : launchOptions.architecture; + xpc_dictionary_set_uint64(reExecMessage, "architecture", (uint64_t)architecture); + + xpc_object_t environment = xpc_array_create(0, 0); + char** environmentPointer = environmentVariables.environmentPointer(); + Vector<CString> temps; + for (size_t i = 0; environmentPointer[i]; ++i) { + CString temp(environmentPointer[i], strlen(environmentPointer[i])); + temps.append(temp); + + xpc_array_set_string(environment, XPC_ARRAY_APPEND, temp.data()); + } + xpc_dictionary_set_value(reExecMessage, "environment", environment); + xpc_release(environment); + +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 + xpc_dictionary_set_bool(reExecMessage, "executable-heap", launchOptions.executableHeap); +#endif + + xpc_connection_send_message(reExecConnection, reExecMessage); + xpc_release(reExecMessage); +} + +static void createWebProcessService(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) +{ + // Generate the uuid for the service instance we are about to create. + // FIXME: This UUID should be stored on the WebProcessProxy. + RefPtr<UUIDHolder> instanceUUID = UUIDHolder::create(); + + // Create a connection to the WebKit2 XPC service. + xpc_connection_t connection = xpc_connection_create("com.apple.WebKit2.WebProcessService", 0); + xpc_connection_set_instance(connection, instanceUUID->uuid); + + // XPC requires having an event handler, even if it is not used. + xpc_connection_set_event_handler(connection, ^(xpc_object_t event) { }); xpc_connection_resume(connection); // Create the listening port. @@ -143,8 +303,9 @@ static void launchXPCService(const ProcessLauncher::LaunchOptions&, const Enviro #endif #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 -static bool tryPreexistingProcess(const ProcessLauncher::LaunchOptions& launchOptions, const EnvironmentVariables& environmentVariables, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) +static bool tryPreexistingProcess(const ProcessLauncher::LaunchOptions& launchOptions, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) { + EnvironmentVariables environmentVariables; static const char* preexistingProcessServiceName = environmentVariables.get(EnvironmentVariables::preexistingProcessServiceNameKey()); ProcessLauncher::ProcessType preexistingProcessType; @@ -189,134 +350,117 @@ static bool tryPreexistingProcess(const ProcessLauncher::LaunchOptions& launchOp } #endif -void ProcessLauncher::launchProcess() +static void createProcess(const ProcessLauncher::LaunchOptions& launchOptions, bool isWebKitDevelopmentBuild, ProcessLauncher* that, DidFinishLaunchingProcessFunction didFinishLaunchingProcessFunction) { EnvironmentVariables environmentVariables; -#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 - if (tryPreexistingProcess(m_launchOptions, environmentVariables, this, &ProcessLauncher::didFinishLaunchingProcess)) - return; -#endif - -#if HAVE(XPC) - if (m_launchOptions.useXPC) { - launchXPCService(m_launchOptions, environmentVariables, this, &ProcessLauncher::didFinishLaunchingProcess); - return; - } -#endif + addDYLDEnvironmentAdditions(launchOptions, isWebKitDevelopmentBuild, environmentVariables); - // Create the listening port. - mach_port_t listeningPort; - mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); - - // Insert a send right so we can send to it. - mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND); - - NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"]; - NSString *frameworksPath = [[webKit2Bundle bundlePath] stringByDeletingLastPathComponent]; - const char* frameworkExecutablePath = [[webKit2Bundle executablePath] fileSystemRepresentation]; + // Create the listening port. + mach_port_t listeningPort; + mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); - NSString *processPath; - if (m_launchOptions.processType == ProcessLauncher::PluginProcess) - processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"PluginProcess.app"]; - else - processPath = [webKit2Bundle pathForAuxiliaryExecutable:@"WebProcess.app"]; + // Insert a send right so we can send to it. + mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND); - NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath]; + RetainPtr<CFStringRef> cfLocalization(AdoptCF, WKCopyCFLocalizationPreferredName(NULL)); + CString localization = String(cfLocalization.get()).utf8(); - RetainPtr<CFStringRef> cfLocalization(AdoptCF, WKCopyCFLocalizationPreferredName(NULL)); - CString localization = String(cfLocalization.get()).utf8(); + NSBundle *webKit2Bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebKit2"]; + NSString *processPath = [webKit2Bundle pathForAuxiliaryExecutable:(launchOptions.processType == ProcessLauncher::PluginProcess ? @"PluginProcess.app" : @"WebProcess.app")]; + NSString *frameworkExecutablePath = [webKit2Bundle executablePath]; + NSString *processAppExecutablePath = [[NSBundle bundleWithPath:processPath] executablePath]; - NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; - CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname(); + NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; + CString clientIdentifier = bundleIdentifier ? String([[NSBundle mainBundle] bundleIdentifier]).utf8() : *_NSGetProgname(); - // Make a unique, per pid, per process launcher web process service name. - CString serviceName = String::format("com.apple.WebKit.WebProcess-%d-%p", getpid(), this).utf8(); + // Make a unique, per pid, per process launcher web process service name. + CString serviceName = String::format("com.apple.WebKit.WebProcess-%d-%p", getpid(), that).utf8(); - const char* args[] = { [processAppExecutablePath fileSystemRepresentation], frameworkExecutablePath, "-type", processTypeAsString(m_launchOptions.processType), "-servicename", serviceName.data(), "-localization", localization.data(), "-client-identifier", clientIdentifier.data(), 0 }; + const char* args[] = { [processAppExecutablePath fileSystemRepresentation], [frameworkExecutablePath fileSystemRepresentation], "-type", ProcessLauncher::processTypeAsString(launchOptions.processType), "-servicename", serviceName.data(), "-localization", localization.data(), "-client-identifier", clientIdentifier.data(), 0 }; - // Register ourselves. - kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.data()), listeningPort, 0); - ASSERT_UNUSED(kr, kr == KERN_SUCCESS); + // Register ourselves. + kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.data()), listeningPort, 0); + ASSERT_UNUSED(kr, kr == KERN_SUCCESS); - posix_spawnattr_t attr; - posix_spawnattr_init(&attr); + posix_spawnattr_t attr; + posix_spawnattr_init(&attr); - short flags = 0; + short flags = 0; - // We want our process to receive all signals. - sigset_t signalMaskSet; - sigemptyset(&signalMaskSet); + // We want our process to receive all signals. + sigset_t signalMaskSet; + sigemptyset(&signalMaskSet); - posix_spawnattr_setsigmask(&attr, &signalMaskSet); - flags |= POSIX_SPAWN_SETSIGMASK; + posix_spawnattr_setsigmask(&attr, &signalMaskSet); + flags |= POSIX_SPAWN_SETSIGMASK; - // Determine the architecture to use. - cpu_type_t architecture = m_launchOptions.architecture; - if (architecture == LaunchOptions::MatchCurrentArchitecture) - architecture = _NSGetMachExecuteHeader()->cputype; + // Determine the architecture to use. + cpu_type_t architecture = launchOptions.architecture; + if (architecture == ProcessLauncher::LaunchOptions::MatchCurrentArchitecture) + architecture = _NSGetMachExecuteHeader()->cputype; - cpu_type_t cpuTypes[] = { architecture }; - size_t outCount = 0; - posix_spawnattr_setbinpref_np(&attr, 1, cpuTypes, &outCount); + cpu_type_t cpuTypes[] = { architecture }; + size_t outCount = 0; + posix_spawnattr_setbinpref_np(&attr, 1, cpuTypes, &outCount); - // Start suspended so we can set up the termination notification handler. - flags |= POSIX_SPAWN_START_SUSPENDED; + // Start suspended so we can set up the termination notification handler. + flags |= POSIX_SPAWN_START_SUSPENDED; #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 - static const int allowExecutableHeapFlag = 0x2000; - if (m_launchOptions.executableHeap) - flags |= allowExecutableHeapFlag; + static const int allowExecutableHeapFlag = 0x2000; + if (launchOptions.executableHeap) + flags |= allowExecutableHeapFlag; #endif - posix_spawnattr_setflags(&attr, flags); + posix_spawnattr_setflags(&attr, flags); -#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 - DynamicLinkerEnvironmentExtractor environmentExtractor([[NSBundle mainBundle] executablePath], _NSGetMachExecuteHeader()->cputype); - environmentExtractor.getExtractedEnvironmentVariables(environmentVariables); -#endif + pid_t processIdentifier = 0; + int result = posix_spawn(&processIdentifier, args[0], 0, &attr, const_cast<char**>(args), environmentVariables.environmentPointer()); - // To make engineering builds work, if the path is outside of /System set up - // DYLD_FRAMEWORK_PATH to pick up other frameworks, but don't do it for the - // production configuration because it involves extra file system access. - if (![frameworksPath hasPrefix:@"/System/"]) - environmentVariables.appendValue("DYLD_FRAMEWORK_PATH", [frameworksPath fileSystemRepresentation], ':'); - - NSString *processShimPathNSString = nil; - if (m_launchOptions.processType == ProcessLauncher::PluginProcess) - processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PluginProcessShim.dylib"]; - else if (m_launchOptions.processType == ProcessLauncher::WebProcess) - processShimPathNSString = [[processAppExecutablePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"WebProcessShim.dylib"]; - - // Make sure that the shim library file exists and insert it. - if (processShimPathNSString) { - const char* processShimPath = [processShimPathNSString fileSystemRepresentation]; - struct stat statBuf; - if (stat(processShimPath, &statBuf) == 0 && (statBuf.st_mode & S_IFMT) == S_IFREG) - environmentVariables.appendValue("DYLD_INSERT_LIBRARIES", processShimPath, ':'); - } + posix_spawnattr_destroy(&attr); - pid_t processIdentifier = 0; - int result = posix_spawn(&processIdentifier, args[0], 0, &attr, const_cast<char**>(args), environmentVariables.environmentPointer()); + if (!result) { + // Set up the termination notification handler and then ask the child process to continue. + setUpTerminationNotificationHandler(processIdentifier); + kill(processIdentifier, SIGCONT); + } else { + // We failed to launch. Release the send right. + mach_port_deallocate(mach_task_self(), listeningPort); - posix_spawnattr_destroy(&attr); + // And the receive right. + mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1); + + listeningPort = MACH_PORT_NULL; + processIdentifier = 0; + } - if (!result) { - // Set up the termination notification handler and then ask the child process to continue. - setUpTerminationNotificationHandler(processIdentifier); - kill(processIdentifier, SIGCONT); - } else { - // We failed to launch. Release the send right. - mach_port_deallocate(mach_task_self(), listeningPort); + // We've finished launching the process, message back to the main run loop. + RunLoop::main()->dispatch(bind(didFinishLaunchingProcessFunction, that, processIdentifier, CoreIPC::Connection::Identifier(listeningPort))); +} - // And the receive right. - mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1); - - listeningPort = MACH_PORT_NULL; - processIdentifier = 0; - } +void ProcessLauncher::launchProcess() +{ +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 + if (tryPreexistingProcess(m_launchOptions, this, &ProcessLauncher::didFinishLaunchingProcess)) + return; +#endif - // We've finished launching the process, message back to the main run loop. - RunLoop::main()->dispatch(bind(&ProcessLauncher::didFinishLaunchingProcess, this, processIdentifier, CoreIPC::Connection::Identifier(listeningPort))); + bool isWebKitDevelopmentBuild = ![[[[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] bundlePath] stringByDeletingLastPathComponent] hasPrefix:@"/System/"]; + +#if HAVE(XPC) + if (m_launchOptions.useXPC) { + if (m_launchOptions.processType == ProcessLauncher::WebProcess) { + if (isWebKitDevelopmentBuild) + createWebProcessServiceForWebKitDevelopment(m_launchOptions, this, &ProcessLauncher::didFinishLaunchingProcess); + else + createWebProcessService(m_launchOptions, this, &ProcessLauncher::didFinishLaunchingProcess); + } else + ASSERT_NOT_REACHED(); + return; + } +#endif + + createProcess(m_launchOptions, isWebKitDevelopmentBuild, this, &ProcessLauncher::didFinishLaunchingProcess); } void ProcessLauncher::terminateProcess() |
