michael@0: // Copyright (c) 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #import "OnDemandServer.h" michael@0: michael@0: #import "Breakpad.h" michael@0: #include "common/mac/bootstrap_compat.h" michael@0: michael@0: #if DEBUG michael@0: #define PRINT_MACH_RESULT(result_, message_) \ michael@0: printf(message_"%s (%d)\n", mach_error_string(result_), result_ ); michael@0: #if defined(MAC_OS_X_VERSION_10_5) && \ michael@0: MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 michael@0: #define PRINT_BOOTSTRAP_RESULT(result_, message_) \ michael@0: printf(message_"%s (%d)\n", bootstrap_strerror(result_), result_ ); michael@0: #else michael@0: #define PRINT_BOOTSTRAP_RESULT(result_, message_) \ michael@0: PRINT_MACH_RESULT(result_, message_) michael@0: #endif michael@0: #else michael@0: #define PRINT_MACH_RESULT(result_, message_) michael@0: #define PRINT_BOOTSTRAP_RESULT(result_, message_) michael@0: #endif michael@0: michael@0: //============================================================================== michael@0: OnDemandServer *OnDemandServer::Create(const char *server_command, michael@0: const char *service_name, michael@0: bool unregister_on_cleanup, michael@0: kern_return_t *out_result) { michael@0: OnDemandServer *server = new OnDemandServer(); michael@0: michael@0: if (!server) return NULL; michael@0: michael@0: kern_return_t result = server->Initialize(server_command, michael@0: service_name, michael@0: unregister_on_cleanup); michael@0: michael@0: if (out_result) { michael@0: *out_result = result; michael@0: } michael@0: michael@0: if (result == KERN_SUCCESS) { michael@0: return server; michael@0: } michael@0: michael@0: delete server; michael@0: return NULL; michael@0: }; michael@0: michael@0: //============================================================================== michael@0: kern_return_t OnDemandServer::Initialize(const char *server_command, michael@0: const char *service_name, michael@0: bool unregister_on_cleanup) { michael@0: unregister_on_cleanup_ = unregister_on_cleanup; michael@0: michael@0: mach_port_t self_task = mach_task_self(); michael@0: michael@0: mach_port_t bootstrap_port; michael@0: kern_return_t kr = task_get_bootstrap_port(self_task, &bootstrap_port); michael@0: if (kr != KERN_SUCCESS) { michael@0: PRINT_MACH_RESULT(kr, "task_get_bootstrap_port(): "); michael@0: return kr; michael@0: } michael@0: michael@0: mach_port_t bootstrap_subset_port; michael@0: kr = bootstrap_subset(bootstrap_port, self_task, &bootstrap_subset_port); michael@0: if (kr != BOOTSTRAP_SUCCESS) { michael@0: PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_subset(): "); michael@0: return kr; michael@0: } michael@0: michael@0: // The inspector will be invoked with its bootstrap port set to the subset, michael@0: // but the sender will need access to the original bootstrap port. Although michael@0: // the original port is the subset's parent, bootstrap_parent can't be used michael@0: // because it requires extra privileges. Stash the original bootstrap port michael@0: // in the subset by registering it under a known name. The inspector will michael@0: // recover this port and set it as its own bootstrap port in Inspector.mm michael@0: // Inspector::ResetBootstrapPort. michael@0: kr = breakpad::BootstrapRegister( michael@0: bootstrap_subset_port, michael@0: const_cast(BREAKPAD_BOOTSTRAP_PARENT_PORT), michael@0: bootstrap_port); michael@0: if (kr != BOOTSTRAP_SUCCESS) { michael@0: PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_register(): "); michael@0: return kr; michael@0: } michael@0: michael@0: kr = bootstrap_create_server(bootstrap_subset_port, michael@0: const_cast(server_command), michael@0: geteuid(), // server uid michael@0: true, michael@0: &server_port_); michael@0: if (kr != BOOTSTRAP_SUCCESS) { michael@0: PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_create_server(): "); michael@0: return kr; michael@0: } michael@0: michael@0: strlcpy(service_name_, service_name, sizeof(service_name_)); michael@0: michael@0: #pragma clang diagnostic push michael@0: #pragma clang diagnostic ignored "-Wdeprecated-declarations" michael@0: // Create a service called service_name, and return send rights to michael@0: // that port in service_port_. michael@0: kr = bootstrap_create_service(server_port_, michael@0: const_cast(service_name), michael@0: &service_port_); michael@0: #pragma clang diagnostic pop michael@0: if (kr != BOOTSTRAP_SUCCESS) { michael@0: PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_create_service(): "); michael@0: michael@0: // perhaps the service has already been created - try to look it up michael@0: kr = bootstrap_look_up(bootstrap_port, (char*)service_name, &service_port_); michael@0: michael@0: if (kr != BOOTSTRAP_SUCCESS) { michael@0: PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_look_up(): "); michael@0: Unregister(); // clean up server port michael@0: return kr; michael@0: } michael@0: } michael@0: michael@0: return KERN_SUCCESS; michael@0: } michael@0: michael@0: //============================================================================== michael@0: OnDemandServer::~OnDemandServer() { michael@0: if (unregister_on_cleanup_) { michael@0: Unregister(); michael@0: } michael@0: } michael@0: michael@0: //============================================================================== michael@0: void OnDemandServer::LaunchOnDemand() { michael@0: // We need to do this, since the launched server is another process michael@0: // and holding on to this port delays launching until the current process michael@0: // exits! michael@0: mach_port_deallocate(mach_task_self(), server_port_); michael@0: server_port_ = MACH_PORT_DEAD; michael@0: michael@0: // Now, the service is still registered and all we need to do is send michael@0: // a mach message to the service port in order to launch the server. michael@0: } michael@0: michael@0: //============================================================================== michael@0: void OnDemandServer::Unregister() { michael@0: if (service_port_ != MACH_PORT_NULL) { michael@0: mach_port_deallocate(mach_task_self(), service_port_); michael@0: service_port_ = MACH_PORT_NULL; michael@0: } michael@0: michael@0: if (server_port_ != MACH_PORT_NULL) { michael@0: // unregister the service michael@0: kern_return_t kr = breakpad::BootstrapRegister(server_port_, michael@0: service_name_, michael@0: MACH_PORT_NULL); michael@0: michael@0: if (kr != KERN_SUCCESS) { michael@0: PRINT_MACH_RESULT(kr, "Breakpad UNREGISTER : bootstrap_register() : "); michael@0: } michael@0: michael@0: mach_port_deallocate(mach_task_self(), server_port_); michael@0: server_port_ = MACH_PORT_NULL; michael@0: } michael@0: }