Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2007, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | #import "OnDemandServer.h" |
michael@0 | 31 | |
michael@0 | 32 | #import "Breakpad.h" |
michael@0 | 33 | #include "common/mac/bootstrap_compat.h" |
michael@0 | 34 | |
michael@0 | 35 | #if DEBUG |
michael@0 | 36 | #define PRINT_MACH_RESULT(result_, message_) \ |
michael@0 | 37 | printf(message_"%s (%d)\n", mach_error_string(result_), result_ ); |
michael@0 | 38 | #if defined(MAC_OS_X_VERSION_10_5) && \ |
michael@0 | 39 | MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 |
michael@0 | 40 | #define PRINT_BOOTSTRAP_RESULT(result_, message_) \ |
michael@0 | 41 | printf(message_"%s (%d)\n", bootstrap_strerror(result_), result_ ); |
michael@0 | 42 | #else |
michael@0 | 43 | #define PRINT_BOOTSTRAP_RESULT(result_, message_) \ |
michael@0 | 44 | PRINT_MACH_RESULT(result_, message_) |
michael@0 | 45 | #endif |
michael@0 | 46 | #else |
michael@0 | 47 | #define PRINT_MACH_RESULT(result_, message_) |
michael@0 | 48 | #define PRINT_BOOTSTRAP_RESULT(result_, message_) |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | //============================================================================== |
michael@0 | 52 | OnDemandServer *OnDemandServer::Create(const char *server_command, |
michael@0 | 53 | const char *service_name, |
michael@0 | 54 | bool unregister_on_cleanup, |
michael@0 | 55 | kern_return_t *out_result) { |
michael@0 | 56 | OnDemandServer *server = new OnDemandServer(); |
michael@0 | 57 | |
michael@0 | 58 | if (!server) return NULL; |
michael@0 | 59 | |
michael@0 | 60 | kern_return_t result = server->Initialize(server_command, |
michael@0 | 61 | service_name, |
michael@0 | 62 | unregister_on_cleanup); |
michael@0 | 63 | |
michael@0 | 64 | if (out_result) { |
michael@0 | 65 | *out_result = result; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | if (result == KERN_SUCCESS) { |
michael@0 | 69 | return server; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | delete server; |
michael@0 | 73 | return NULL; |
michael@0 | 74 | }; |
michael@0 | 75 | |
michael@0 | 76 | //============================================================================== |
michael@0 | 77 | kern_return_t OnDemandServer::Initialize(const char *server_command, |
michael@0 | 78 | const char *service_name, |
michael@0 | 79 | bool unregister_on_cleanup) { |
michael@0 | 80 | unregister_on_cleanup_ = unregister_on_cleanup; |
michael@0 | 81 | |
michael@0 | 82 | mach_port_t self_task = mach_task_self(); |
michael@0 | 83 | |
michael@0 | 84 | mach_port_t bootstrap_port; |
michael@0 | 85 | kern_return_t kr = task_get_bootstrap_port(self_task, &bootstrap_port); |
michael@0 | 86 | if (kr != KERN_SUCCESS) { |
michael@0 | 87 | PRINT_MACH_RESULT(kr, "task_get_bootstrap_port(): "); |
michael@0 | 88 | return kr; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | mach_port_t bootstrap_subset_port; |
michael@0 | 92 | kr = bootstrap_subset(bootstrap_port, self_task, &bootstrap_subset_port); |
michael@0 | 93 | if (kr != BOOTSTRAP_SUCCESS) { |
michael@0 | 94 | PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_subset(): "); |
michael@0 | 95 | return kr; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // The inspector will be invoked with its bootstrap port set to the subset, |
michael@0 | 99 | // but the sender will need access to the original bootstrap port. Although |
michael@0 | 100 | // the original port is the subset's parent, bootstrap_parent can't be used |
michael@0 | 101 | // because it requires extra privileges. Stash the original bootstrap port |
michael@0 | 102 | // in the subset by registering it under a known name. The inspector will |
michael@0 | 103 | // recover this port and set it as its own bootstrap port in Inspector.mm |
michael@0 | 104 | // Inspector::ResetBootstrapPort. |
michael@0 | 105 | kr = breakpad::BootstrapRegister( |
michael@0 | 106 | bootstrap_subset_port, |
michael@0 | 107 | const_cast<char*>(BREAKPAD_BOOTSTRAP_PARENT_PORT), |
michael@0 | 108 | bootstrap_port); |
michael@0 | 109 | if (kr != BOOTSTRAP_SUCCESS) { |
michael@0 | 110 | PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_register(): "); |
michael@0 | 111 | return kr; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | kr = bootstrap_create_server(bootstrap_subset_port, |
michael@0 | 115 | const_cast<char*>(server_command), |
michael@0 | 116 | geteuid(), // server uid |
michael@0 | 117 | true, |
michael@0 | 118 | &server_port_); |
michael@0 | 119 | if (kr != BOOTSTRAP_SUCCESS) { |
michael@0 | 120 | PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_create_server(): "); |
michael@0 | 121 | return kr; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | strlcpy(service_name_, service_name, sizeof(service_name_)); |
michael@0 | 125 | |
michael@0 | 126 | #pragma clang diagnostic push |
michael@0 | 127 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
michael@0 | 128 | // Create a service called service_name, and return send rights to |
michael@0 | 129 | // that port in service_port_. |
michael@0 | 130 | kr = bootstrap_create_service(server_port_, |
michael@0 | 131 | const_cast<char*>(service_name), |
michael@0 | 132 | &service_port_); |
michael@0 | 133 | #pragma clang diagnostic pop |
michael@0 | 134 | if (kr != BOOTSTRAP_SUCCESS) { |
michael@0 | 135 | PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_create_service(): "); |
michael@0 | 136 | |
michael@0 | 137 | // perhaps the service has already been created - try to look it up |
michael@0 | 138 | kr = bootstrap_look_up(bootstrap_port, (char*)service_name, &service_port_); |
michael@0 | 139 | |
michael@0 | 140 | if (kr != BOOTSTRAP_SUCCESS) { |
michael@0 | 141 | PRINT_BOOTSTRAP_RESULT(kr, "bootstrap_look_up(): "); |
michael@0 | 142 | Unregister(); // clean up server port |
michael@0 | 143 | return kr; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | return KERN_SUCCESS; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | //============================================================================== |
michael@0 | 151 | OnDemandServer::~OnDemandServer() { |
michael@0 | 152 | if (unregister_on_cleanup_) { |
michael@0 | 153 | Unregister(); |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | //============================================================================== |
michael@0 | 158 | void OnDemandServer::LaunchOnDemand() { |
michael@0 | 159 | // We need to do this, since the launched server is another process |
michael@0 | 160 | // and holding on to this port delays launching until the current process |
michael@0 | 161 | // exits! |
michael@0 | 162 | mach_port_deallocate(mach_task_self(), server_port_); |
michael@0 | 163 | server_port_ = MACH_PORT_DEAD; |
michael@0 | 164 | |
michael@0 | 165 | // Now, the service is still registered and all we need to do is send |
michael@0 | 166 | // a mach message to the service port in order to launch the server. |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | //============================================================================== |
michael@0 | 170 | void OnDemandServer::Unregister() { |
michael@0 | 171 | if (service_port_ != MACH_PORT_NULL) { |
michael@0 | 172 | mach_port_deallocate(mach_task_self(), service_port_); |
michael@0 | 173 | service_port_ = MACH_PORT_NULL; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | if (server_port_ != MACH_PORT_NULL) { |
michael@0 | 177 | // unregister the service |
michael@0 | 178 | kern_return_t kr = breakpad::BootstrapRegister(server_port_, |
michael@0 | 179 | service_name_, |
michael@0 | 180 | MACH_PORT_NULL); |
michael@0 | 181 | |
michael@0 | 182 | if (kr != KERN_SUCCESS) { |
michael@0 | 183 | PRINT_MACH_RESULT(kr, "Breakpad UNREGISTER : bootstrap_register() : "); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | mach_port_deallocate(mach_task_self(), server_port_); |
michael@0 | 187 | server_port_ = MACH_PORT_NULL; |
michael@0 | 188 | } |
michael@0 | 189 | } |