Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Unified Diff: util/mach/mach_extensions.cc

Issue 1383283003: Add and use scoped-right-returning wrappers for Mach bootstrap routines (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Self-review Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/mach/mach_extensions.h ('k') | util/mach/mach_extensions_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/mach/mach_extensions.cc
diff --git a/util/mach/mach_extensions.cc b/util/mach/mach_extensions.cc
index c2343c218085c5abe8b9d099c75c21cf698fb3ec..64552f1cba65ce1d7edad926e40cc58dc289b379 100644
--- a/util/mach/mach_extensions.cc
+++ b/util/mach/mach_extensions.cc
@@ -21,6 +21,59 @@
#include "base/mac/mach_logging.h"
#include "util/mac/mac_util.h"
+namespace {
+
+// This forms the internal implementation for BootstrapCheckIn() and
+// BootstrapLookUp(), which follow the same logic aside from the routine called
+// and the right type returned.
+
+struct BootstrapCheckInTraits {
+ using Type = base::mac::ScopedMachReceiveRight;
+ static kern_return_t Call(mach_port_t bootstrap_port,
+ const char* service_name,
+ mach_port_t* service_port) {
+ return bootstrap_check_in(bootstrap_port, service_name, service_port);
+ }
+ static const char kName[];
+};
+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:
+
+struct BootstrapLookUpTraits {
+ using Type = base::mac::ScopedMachSendRight;
+ static kern_return_t Call(mach_port_t bootstrap_port,
+ const char* service_name,
+ mach_port_t* service_port) {
+ return bootstrap_look_up(bootstrap_port, service_name, service_port);
+ }
+ static const char kName[];
+};
+const char BootstrapLookUpTraits::kName[] = "bootstrap_look_up";
+
+template <typename Traits>
+typename Traits::Type BootstrapCheckInOrLookUp(
+ const std::string& service_name) {
+ // bootstrap_check_in() and bootstrap_look_up() silently truncate service
+ // names longer than BOOTSTRAP_MAX_NAME_LEN. This check ensures that the name
+ // will not be truncated.
+ if (service_name.size() >= BOOTSTRAP_MAX_NAME_LEN) {
+ LOG(ERROR) << Traits::kName << " " << service_name << ": name too long";
+ return typename Traits::Type(MACH_PORT_NULL);
+ }
+
+ mach_port_t service_port;
+ kern_return_t kr = Traits::Call(bootstrap_port,
+ service_name.c_str(),
+ &service_port);
+ if (kr != BOOTSTRAP_SUCCESS) {
+ BOOTSTRAP_LOG(ERROR, kr) << Traits::kName << " " << service_name;
+ service_port = MACH_PORT_NULL;
+ }
+
+ return typename Traits::Type(service_port);
+}
+
+} // namespace
+
namespace crashpad {
thread_t MachThreadSelf() {
@@ -97,19 +150,18 @@ exception_mask_t ExcMaskValid() {
return kExcMaskValid_10_11;
}
-base::mac::ScopedMachSendRight SystemCrashReporterHandler() {
- const char kSystemCrashReporterServiceName[] = "com.apple.ReportCrash";
- exception_handler_t system_crash_reporter_handler;
- kern_return_t kr = bootstrap_look_up(bootstrap_port,
- kSystemCrashReporterServiceName,
- &system_crash_reporter_handler);
- if (kr != BOOTSTRAP_SUCCESS) {
- BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up "
- << kSystemCrashReporterServiceName;
- system_crash_reporter_handler = MACH_PORT_NULL;
- }
+base::mac::ScopedMachReceiveRight BootstrapCheckIn(
+ const std::string& service_name) {
+ return BootstrapCheckInOrLookUp<BootstrapCheckInTraits>(service_name);
+}
+
+base::mac::ScopedMachSendRight BootstrapLookUp(
+ const std::string& service_name) {
+ return BootstrapCheckInOrLookUp<BootstrapLookUpTraits>(service_name);
+}
- return base::mac::ScopedMachSendRight(system_crash_reporter_handler);
+base::mac::ScopedMachSendRight SystemCrashReporterHandler() {
+ return BootstrapLookUp("com.apple.ReportCrash");
}
} // namespace crashpad
« no previous file with comments | « util/mach/mach_extensions.h ('k') | util/mach/mach_extensions_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698