Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/mach/mach_extensions.h" | 15 #include "util/mach/mach_extensions.h" |
| 16 | 16 |
| 17 #include <AvailabilityMacros.h> | 17 #include <AvailabilityMacros.h> |
| 18 #include <pthread.h> | 18 #include <pthread.h> |
| 19 #include <servers/bootstrap.h> | 19 #include <servers/bootstrap.h> |
| 20 | 20 |
| 21 #include "base/mac/mach_logging.h" | 21 #include "base/mac/mach_logging.h" |
| 22 #include "util/mac/mac_util.h" | 22 #include "util/mac/mac_util.h" |
| 23 | 23 |
| 24 namespace { | |
| 25 | |
| 26 // This forms the internal implementation for BootstrapCheckIn() and | |
| 27 // BootstrapLookUp(), which follow the same logic aside from the routine called | |
| 28 // and the right type returned. | |
| 29 | |
| 30 struct BootstrapCheckInTraits { | |
| 31 using Type = base::mac::ScopedMachReceiveRight; | |
| 32 static kern_return_t Call(mach_port_t bootstrap_port, | |
| 33 const char* service_name, | |
| 34 mach_port_t* service_port) { | |
| 35 return bootstrap_check_in(bootstrap_port, service_name, service_port); | |
| 36 } | |
| 37 static const char kName[]; | |
| 38 }; | |
| 39 const char BootstrapCheckInTraits::kName[] = "bootstrap_check_in"; | |
|
Robert Sesek
2015/10/05 19:00:20
In C++11 this can't be initialized in the struct i
Mark Mentovai
2015/10/05 19:33:03
Robert Sesek wrote:
| |
| 40 | |
| 41 struct BootstrapLookUpTraits { | |
| 42 using Type = base::mac::ScopedMachSendRight; | |
| 43 static kern_return_t Call(mach_port_t bootstrap_port, | |
| 44 const char* service_name, | |
| 45 mach_port_t* service_port) { | |
| 46 return bootstrap_look_up(bootstrap_port, service_name, service_port); | |
| 47 } | |
| 48 static const char kName[]; | |
| 49 }; | |
| 50 const char BootstrapLookUpTraits::kName[] = "bootstrap_look_up"; | |
| 51 | |
| 52 template <typename Traits> | |
| 53 typename Traits::Type BootstrapCheckInOrLookUp( | |
| 54 const std::string& service_name) { | |
| 55 // bootstrap_check_in() and bootstrap_look_up() silently truncate service | |
| 56 // names longer than BOOTSTRAP_MAX_NAME_LEN. This check ensures that the name | |
| 57 // will not be truncated. | |
| 58 if (service_name.size() >= BOOTSTRAP_MAX_NAME_LEN) { | |
| 59 LOG(ERROR) << Traits::kName << " " << service_name << ": name too long"; | |
| 60 return typename Traits::Type(MACH_PORT_NULL); | |
| 61 } | |
| 62 | |
| 63 mach_port_t service_port; | |
| 64 kern_return_t kr = Traits::Call(bootstrap_port, | |
| 65 service_name.c_str(), | |
| 66 &service_port); | |
| 67 if (kr != BOOTSTRAP_SUCCESS) { | |
| 68 BOOTSTRAP_LOG(ERROR, kr) << Traits::kName << " " << service_name; | |
| 69 service_port = MACH_PORT_NULL; | |
| 70 } | |
| 71 | |
| 72 return typename Traits::Type(service_port); | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 24 namespace crashpad { | 77 namespace crashpad { |
| 25 | 78 |
| 26 thread_t MachThreadSelf() { | 79 thread_t MachThreadSelf() { |
| 27 // The pthreads library keeps its own copy of the thread port. Using it does | 80 // The pthreads library keeps its own copy of the thread port. Using it does |
| 28 // not increment its reference count. | 81 // not increment its reference count. |
| 29 return pthread_mach_thread_np(pthread_self()); | 82 return pthread_mach_thread_np(pthread_self()); |
| 30 } | 83 } |
| 31 | 84 |
| 32 mach_port_t NewMachPort(mach_port_right_t right) { | 85 mach_port_t NewMachPort(mach_port_right_t right) { |
| 33 mach_port_t port = MACH_PORT_NULL; | 86 mach_port_t port = MACH_PORT_NULL; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 return kExcMaskValid_10_6; | 143 return kExcMaskValid_10_6; |
| 91 } | 144 } |
| 92 #endif | 145 #endif |
| 93 | 146 |
| 94 // 10.11 added EXC_MASK_CORPSE_NOTIFY. See 10.11 <mach/exception_types.h>. | 147 // 10.11 added EXC_MASK_CORPSE_NOTIFY. See 10.11 <mach/exception_types.h>. |
| 95 const exception_mask_t kExcMaskValid_10_11 = | 148 const exception_mask_t kExcMaskValid_10_11 = |
| 96 kExcMaskValid_10_6 | EXC_MASK_CORPSE_NOTIFY; | 149 kExcMaskValid_10_6 | EXC_MASK_CORPSE_NOTIFY; |
| 97 return kExcMaskValid_10_11; | 150 return kExcMaskValid_10_11; |
| 98 } | 151 } |
| 99 | 152 |
| 153 base::mac::ScopedMachReceiveRight BootstrapCheckIn( | |
| 154 const std::string& service_name) { | |
| 155 return BootstrapCheckInOrLookUp<BootstrapCheckInTraits>(service_name); | |
| 156 } | |
| 157 | |
| 158 base::mac::ScopedMachSendRight BootstrapLookUp( | |
| 159 const std::string& service_name) { | |
| 160 return BootstrapCheckInOrLookUp<BootstrapLookUpTraits>(service_name); | |
| 161 } | |
| 162 | |
| 100 base::mac::ScopedMachSendRight SystemCrashReporterHandler() { | 163 base::mac::ScopedMachSendRight SystemCrashReporterHandler() { |
| 101 const char kSystemCrashReporterServiceName[] = "com.apple.ReportCrash"; | 164 return BootstrapLookUp("com.apple.ReportCrash"); |
| 102 exception_handler_t system_crash_reporter_handler; | |
| 103 kern_return_t kr = bootstrap_look_up(bootstrap_port, | |
| 104 kSystemCrashReporterServiceName, | |
| 105 &system_crash_reporter_handler); | |
| 106 if (kr != BOOTSTRAP_SUCCESS) { | |
| 107 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up " | |
| 108 << kSystemCrashReporterServiceName; | |
| 109 system_crash_reporter_handler = MACH_PORT_NULL; | |
| 110 } | |
| 111 | |
| 112 return base::mac::ScopedMachSendRight(system_crash_reporter_handler); | |
| 113 } | 165 } |
| 114 | 166 |
| 115 } // namespace crashpad | 167 } // namespace crashpad |
| OLD | NEW |