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

Side by Side Diff: base/mac/mach_port_broker.mm

Issue 1755973002: Move non-content specific parts of content::MachBroker into base::MachPortBroker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove libbsm from content build rules. Created 4 years, 9 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 unified diff | Download patch
« no previous file with comments | « base/mac/mach_port_broker.h ('k') | base/mac/mach_port_broker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/mach_broker_mac.h" 5 #include "base/mac/mach_port_broker.h"
6 6
7 #include <bsm/libbsm.h> 7 #include <bsm/libbsm.h>
8 #include <servers/bootstrap.h> 8 #include <servers/bootstrap.h>
9 9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/logging.h" 10 #include "base/logging.h"
14 #include "base/mac/foundation_util.h" 11 #include "base/mac/foundation_util.h"
15 #include "base/mac/mach_logging.h" 12 #include "base/mac/mach_logging.h"
16 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "content/browser/renderer_host/render_process_host_impl.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/child_process_data.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/common/content_switches.h"
25 15
26 namespace content { 16 namespace base {
27 17
28 namespace { 18 namespace {
29 19
30 // Mach message structure used in the child as a sending message. 20 // Mach message structure used in the child as a sending message.
31 struct MachBroker_ChildSendMsg { 21 struct MachPortBroker_ChildSendMsg {
32 mach_msg_header_t header; 22 mach_msg_header_t header;
33 mach_msg_body_t body; 23 mach_msg_body_t body;
34 mach_msg_port_descriptor_t child_task_port; 24 mach_msg_port_descriptor_t child_task_port;
35 }; 25 };
36 26
37 // Complement to the ChildSendMsg, this is used in the parent for receiving 27 // Complement to the ChildSendMsg, this is used in the parent for receiving
38 // a message. Contains a message trailer with audit information. 28 // a message. Contains a message trailer with audit information.
39 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { 29 struct MachPortBroker_ParentRecvMsg : public MachPortBroker_ChildSendMsg {
40 mach_msg_audit_trailer_t trailer; 30 mach_msg_audit_trailer_t trailer;
41 }; 31 };
42 32
43 } // namespace 33 } // namespace
44 34
45 bool MachBroker::ChildSendTaskPortToParent() { 35 // static
46 // Look up the named MachBroker port that's been registered with the 36 bool MachPortBroker::ChildSendTaskPortToParent(const std::string& name) {
37 // Look up the named MachPortBroker port that's been registered with the
47 // bootstrap server. 38 // bootstrap server.
48 mach_port_t parent_port; 39 mach_port_t parent_port;
49 kern_return_t kr = bootstrap_look_up(bootstrap_port, 40 kern_return_t kr = bootstrap_look_up(bootstrap_port,
50 const_cast<char*>(GetMachPortName().c_str()), &parent_port); 41 const_cast<char*>(GetMachPortName(name, true).c_str()), &parent_port);
51 if (kr != KERN_SUCCESS) { 42 if (kr != KERN_SUCCESS) {
52 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; 43 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
53 return false; 44 return false;
54 } 45 }
55 base::mac::ScopedMachSendRight scoped_right(parent_port); 46 base::mac::ScopedMachSendRight scoped_right(parent_port);
56 47
57 // Create the check in message. This will copy a send right on this process' 48 // Create the check in message. This will copy a send right on this process'
58 // (the child's) task port and send it to the parent. 49 // (the child's) task port and send it to the parent.
59 MachBroker_ChildSendMsg msg; 50 MachPortBroker_ChildSendMsg msg;
60 bzero(&msg, sizeof(msg)); 51 bzero(&msg, sizeof(msg));
61 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) | 52 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) |
62 MACH_MSGH_BITS_COMPLEX; 53 MACH_MSGH_BITS_COMPLEX;
63 msg.header.msgh_remote_port = parent_port; 54 msg.header.msgh_remote_port = parent_port;
64 msg.header.msgh_size = sizeof(msg); 55 msg.header.msgh_size = sizeof(msg);
65 msg.body.msgh_descriptor_count = 1; 56 msg.body.msgh_descriptor_count = 1;
66 msg.child_task_port.name = mach_task_self(); 57 msg.child_task_port.name = mach_task_self();
67 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND; 58 msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND;
68 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR; 59 msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR;
69 60
70 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg), 61 kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
71 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); 62 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
72 if (kr != KERN_SUCCESS) { 63 if (kr != KERN_SUCCESS) {
73 MACH_LOG(ERROR, kr) << "mach_msg"; 64 MACH_LOG(ERROR, kr) << "mach_msg";
74 return false; 65 return false;
75 } 66 }
76 67
77 return true; 68 return true;
78 } 69 }
79 70
80 MachBroker* MachBroker::GetInstance() { 71 // static
81 return base::Singleton<MachBroker, 72 std::string MachPortBroker::GetMachPortName(const std::string& name,
82 base::LeakySingletonTraits<MachBroker>>::get(); 73 bool is_child) {
74 // In child processes, use the parent's pid.
75 const pid_t pid = is_child ? getppid() : getpid();
76 return base::StringPrintf(
77 "%s.%s.%d", base::mac::BaseBundleID(), name.c_str(), pid);
83 } 78 }
84 79
85 base::Lock& MachBroker::GetLock() { 80 mach_port_t MachPortBroker::TaskForPid(base::ProcessHandle pid) const {
86 return lock_;
87 }
88
89 void MachBroker::EnsureRunning() {
90 lock_.AssertAcquired();
91
92 if (initialized_)
93 return;
94
95 // Do not attempt to reinitialize in the event of failure.
96 initialized_ = true;
97
98 BrowserThread::PostTask(
99 BrowserThread::UI, FROM_HERE,
100 base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this)));
101
102 if (!Init()) {
103 LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate";
104 }
105 }
106
107 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid,
108 int child_process_id) {
109 lock_.AssertAcquired();
110
111 DCHECK_EQ(0u, mach_map_.count(pid));
112 mach_map_[pid] = MACH_PORT_NULL;
113 child_process_id_map_[child_process_id] = pid;
114 }
115
116 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const {
117 base::AutoLock lock(lock_); 81 base::AutoLock lock(lock_);
118 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); 82 MachPortBroker::MachMap::const_iterator it = mach_map_.find(pid);
119 if (it == mach_map_.end()) 83 if (it == mach_map_.end())
120 return MACH_PORT_NULL; 84 return MACH_PORT_NULL;
121 return it->second; 85 return it->second;
122 } 86 }
123 87
124 void MachBroker::BrowserChildProcessHostDisconnected( 88 MachPortBroker::MachPortBroker(const std::string& name) : name_(name) {}
125 const ChildProcessData& data) {
126 InvalidateChildProcessId(data.id);
127 }
128 89
129 void MachBroker::BrowserChildProcessCrashed(const ChildProcessData& data, 90 MachPortBroker::~MachPortBroker() {}
130 int exit_code) {
131 InvalidateChildProcessId(data.id);
132 }
133 91
134 void MachBroker::Observe(int type, 92 bool MachPortBroker::Init() {
135 const NotificationSource& source,
136 const NotificationDetails& details) {
137 switch (type) {
138 case NOTIFICATION_RENDERER_PROCESS_TERMINATED:
139 case NOTIFICATION_RENDERER_PROCESS_CLOSED: {
140 RenderProcessHost* host = Source<RenderProcessHost>(source).ptr();
141 InvalidateChildProcessId(host->GetID());
142 break;
143 }
144 default:
145 NOTREACHED() << "Unexpected notification";
146 break;
147 }
148 }
149
150 // static
151 std::string MachBroker::GetMachPortName() {
152 const base::CommandLine* command_line =
153 base::CommandLine::ForCurrentProcess();
154 const bool is_child = command_line->HasSwitch(switches::kProcessType);
155
156 // In non-browser (child) processes, use the parent's pid.
157 const pid_t pid = is_child ? getppid() : getpid();
158 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid);
159 }
160
161 MachBroker::MachBroker() : initialized_(false) {
162 }
163
164 MachBroker::~MachBroker() {}
165
166 bool MachBroker::Init() {
167 DCHECK(server_port_.get() == MACH_PORT_NULL); 93 DCHECK(server_port_.get() == MACH_PORT_NULL);
168 94
169 // Check in with launchd and publish the service name. 95 // Check in with launchd and publish the service name.
170 mach_port_t port; 96 mach_port_t port;
171 kern_return_t kr = 97 kern_return_t kr = bootstrap_check_in(
172 bootstrap_check_in(bootstrap_port, GetMachPortName().c_str(), &port); 98 bootstrap_port, GetMachPortName(name_, false).c_str(), &port);
173 if (kr != KERN_SUCCESS) { 99 if (kr != KERN_SUCCESS) {
174 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in"; 100 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in";
175 return false; 101 return false;
176 } 102 }
177 server_port_.reset(port); 103 server_port_.reset(port);
178 104
179 // Start the dispatch source. 105 // Start the dispatch source.
180 std::string queue_name = 106 std::string queue_name =
181 base::StringPrintf("%s.MachBroker", base::mac::BaseBundleID()); 107 base::StringPrintf("%s.MachPortBroker", base::mac::BaseBundleID());
182 dispatch_source_.reset(new base::DispatchSourceMach( 108 dispatch_source_.reset(new base::DispatchSourceMach(
183 queue_name.c_str(), server_port_.get(), ^{ HandleRequest(); })); 109 queue_name.c_str(), server_port_.get(), ^{ HandleRequest(); }));
184 dispatch_source_->Resume(); 110 dispatch_source_->Resume();
185 111
186 return true; 112 return true;
187 } 113 }
188 114
189 void MachBroker::HandleRequest() { 115 void MachPortBroker::AddPlaceholderForPid(base::ProcessHandle pid) {
190 MachBroker_ParentRecvMsg msg; 116 lock_.AssertAcquired();
117 DCHECK_EQ(0u, mach_map_.count(pid));
118 mach_map_[pid] = MACH_PORT_NULL;
119 }
120
121 void MachPortBroker::InvalidatePid(base::ProcessHandle pid) {
122 lock_.AssertAcquired();
123
124 MachMap::iterator mach_it = mach_map_.find(pid);
125 if (mach_it != mach_map_.end()) {
126 kern_return_t kr = mach_port_deallocate(mach_task_self(), mach_it->second);
127 MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
128 mach_map_.erase(mach_it);
129 }
130 }
131
132 void MachPortBroker::HandleRequest() {
133 MachPortBroker_ParentRecvMsg msg;
191 bzero(&msg, sizeof(msg)); 134 bzero(&msg, sizeof(msg));
192 msg.header.msgh_size = sizeof(msg); 135 msg.header.msgh_size = sizeof(msg);
193 msg.header.msgh_local_port = server_port_.get(); 136 msg.header.msgh_local_port = server_port_.get();
194 137
195 const mach_msg_option_t options = MACH_RCV_MSG | 138 const mach_msg_option_t options = MACH_RCV_MSG |
196 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | 139 MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
197 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); 140 MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
198 141
199 kern_return_t kr = mach_msg(&msg.header, 142 kern_return_t kr = mach_msg(&msg.header,
200 options, 143 options,
(...skipping 12 matching lines...) Expand all
213 // unspoofable pid of the task that sent the message. 156 // unspoofable pid of the task that sent the message.
214 // 157 //
215 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). 158 // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
216 pid_t child_pid; 159 pid_t child_pid;
217 audit_token_to_au32(msg.trailer.msgh_audit, 160 audit_token_to_au32(msg.trailer.msgh_audit,
218 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); 161 NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
219 162
220 mach_port_t child_task_port = msg.child_task_port.name; 163 mach_port_t child_task_port = msg.child_task_port.name;
221 164
222 // Take the lock and update the broker information. 165 // Take the lock and update the broker information.
223 base::AutoLock lock(GetLock()); 166 base::AutoLock lock(lock_);
224 FinalizePid(child_pid, child_task_port); 167 FinalizePid(child_pid, child_task_port);
225 } 168 }
226 169
227 void MachBroker::FinalizePid(base::ProcessHandle pid, 170 void MachPortBroker::FinalizePid(base::ProcessHandle pid,
228 mach_port_t task_port) { 171 mach_port_t task_port) {
229 lock_.AssertAcquired(); 172 lock_.AssertAcquired();
230 173
231 MachMap::iterator it = mach_map_.find(pid); 174 MachMap::iterator it = mach_map_.find(pid);
232 if (it == mach_map_.end()) { 175 if (it == mach_map_.end()) {
233 // Do nothing for unknown pids. 176 // Do nothing for unknown pids.
234 LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!"; 177 LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!";
235 return; 178 return;
236 } 179 }
237 180
238 DCHECK(it->second == MACH_PORT_NULL); 181 DCHECK(it->second == MACH_PORT_NULL);
239 if (it->second == MACH_PORT_NULL) 182 if (it->second == MACH_PORT_NULL)
240 it->second = task_port; 183 it->second = task_port;
241 } 184 }
242 185
243 void MachBroker::InvalidateChildProcessId(int child_process_id) { 186 } // namespace base
244 base::AutoLock lock(lock_);
245 MachBroker::ChildProcessIdMap::iterator it =
246 child_process_id_map_.find(child_process_id);
247 if (it == child_process_id_map_.end())
248 return;
249
250 MachMap::iterator mach_it = mach_map_.find(it->second);
251 if (mach_it != mach_map_.end()) {
252 kern_return_t kr = mach_port_deallocate(mach_task_self(),
253 mach_it->second);
254 MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
255 mach_map_.erase(mach_it);
256 }
257 child_process_id_map_.erase(it);
258 }
259
260 void MachBroker::RegisterNotifications() {
261 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED,
262 NotificationService::AllBrowserContextsAndSources());
263 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
264 NotificationService::AllBrowserContextsAndSources());
265
266 // No corresponding StopObservingBrowserChildProcesses,
267 // we leak this singleton.
268 BrowserChildProcessObserver::Add(this);
269 }
270
271 } // namespace content
OLDNEW
« no previous file with comments | « base/mac/mach_port_broker.h ('k') | base/mac/mach_port_broker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698