OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sandbox/mac/os_compatibility.h" | |
6 | |
7 #include <servers/bootstrap.h> | |
8 #include <unistd.h> | |
9 | |
10 #include "base/mac/mac_util.h" | |
11 | |
12 namespace sandbox { | |
13 | |
14 namespace { | |
15 | |
16 // Verified from launchd-329.3.3 (10.6.8). | |
17 struct look_up2_request_10_6 { | |
18 mach_msg_header_t Head; | |
19 NDR_record_t NDR; | |
20 name_t servicename; | |
21 pid_t targetpid; | |
22 uint64_t flags; | |
23 }; | |
24 | |
25 struct look_up2_reply_10_6 { | |
26 mach_msg_header_t Head; | |
27 mach_msg_body_t msgh_body; | |
28 mach_msg_port_descriptor_t service_port; | |
29 }; | |
30 | |
31 // Verified from: | |
32 // launchd-392.39 (10.7.5) | |
33 // launchd-442.26.2 (10.8.5) | |
34 // launchd-842.1.4 (10.9.0) | |
35 struct look_up2_request_10_7 { | |
36 mach_msg_header_t Head; | |
37 NDR_record_t NDR; | |
38 name_t servicename; | |
39 pid_t targetpid; | |
40 uuid_t instanceid; | |
41 uint64_t flags; | |
42 }; | |
43 | |
44 // look_up2_reply_10_7 is the same as the 10_6 version. | |
45 | |
46 // TODO(rsesek): Libc provides strnlen() staring in 10.7. | |
Avi (use Gerrit)
2014/05/09 21:02:06
typo: starting
Robert Sesek
2014/05/09 22:04:03
Done.
| |
47 size_t strnlen(const char* str, size_t maxlen) { | |
48 size_t len = 0; | |
49 for (; len < maxlen; ++len, ++str) { | |
50 if (*str == '\0') | |
51 break; | |
52 } | |
53 return len; | |
54 } | |
55 | |
56 template <typename R> | |
57 std::string LaunchdLookUp2GetRequestName(const mach_msg_header_t* header) { | |
58 DCHECK_EQ(sizeof(R), header->msgh_size); | |
59 const R* request = reinterpret_cast<const R*>(header); | |
60 // Make sure the name is properly NUL-terminated. | |
61 const size_t name_length = | |
62 strnlen(request->servicename, BOOTSTRAP_MAX_NAME_LEN); | |
63 std::string name = std::string(request->servicename, name_length); | |
64 name[name_length] = '\0'; | |
Mark Mentovai
2014/05/09 20:11:19
Not necessary, std::string guarantees NUL-terminat
Robert Sesek
2014/05/09 22:04:03
Done. For posterity, C++ § 21.4.5.
| |
65 return name; | |
66 } | |
67 | |
68 template <typename R> | |
69 void LaunchdLookUp2FillReply(mach_msg_header_t* header, mach_port_t port) { | |
70 R* reply = reinterpret_cast<R*>(header); | |
71 reply->Head.msgh_size = sizeof(R); | |
72 reply->Head.msgh_bits = | |
73 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MOVE_SEND_ONCE) | | |
74 MACH_MSGH_BITS_COMPLEX; | |
75 reply->msgh_body.msgh_descriptor_count = 1; | |
76 reply->service_port.name = port; | |
77 reply->service_port.disposition = MACH_MSG_TYPE_PORT_SEND; | |
78 reply->service_port.type = MACH_MSG_PORT_DESCRIPTOR; | |
79 } | |
80 | |
81 } // namespace | |
82 | |
83 const LaunchdCompatibilityShim GetLaunchdCompatibilityShim() { | |
84 LaunchdCompatibilityShim shim = { | |
85 .msg_id_look_up2 = 404, | |
86 .msg_id_swap_integer = 416, | |
87 .look_up2_fill_reply = &LaunchdLookUp2FillReply<look_up2_reply_10_6> | |
88 }; | |
89 | |
90 if (base::mac::IsOSSnowLeopard()) { | |
91 shim.look_up2_get_request_name = | |
92 &LaunchdLookUp2GetRequestName<look_up2_request_10_6>; | |
93 } else if (base::mac::IsOSLionOrLater() && | |
94 !base::mac::IsOSLaterThanMavericks_DontCallThis()) { | |
95 shim.look_up2_get_request_name = | |
96 &LaunchdLookUp2GetRequestName<look_up2_request_10_7>; | |
97 } else { | |
98 LOG(ERROR) << "Unknown OS, using launchd compatibility shim from 10.7."; | |
99 shim.look_up2_get_request_name = | |
100 &LaunchdLookUp2GetRequestName<look_up2_request_10_7>; | |
Avi (use Gerrit)
2014/05/09 21:02:06
In the past when we blindly patched CFAllocator in
Robert Sesek
2014/05/09 22:04:03
Launchd hasn't changed these messages since 10.7,
| |
101 } | |
102 | |
103 return shim; | |
104 } | |
105 | |
106 } // namespace sandbox | |
OLD | NEW |