Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / content / browser / mach_broker_mac.mm
1 // Copyright (c) 2011 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 "content/browser/mach_broker_mac.h"
6
7 #include <bsm/libbsm.h>
8 #include <servers/bootstrap.h>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "base/mac/foundation_util.h"
15 #include "base/mac/mach_logging.h"
16 #include "base/mac/scoped_mach_port.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/sys_string_conversions.h"
20 #include "base/threading/platform_thread.h"
21 #include "content/browser/renderer_host/render_process_host_impl.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/child_process_data.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_types.h"
26 #include "content/public/common/content_switches.h"
27
28 namespace content {
29
30 namespace {
31
32 // Mach message structure used in the child as a sending message.
33 struct MachBroker_ChildSendMsg {
34   mach_msg_header_t header;
35   mach_msg_body_t body;
36   mach_msg_port_descriptor_t child_task_port;
37 };
38
39 // Complement to the ChildSendMsg, this is used in the parent for receiving
40 // a message. Contains a message trailer with audit information.
41 struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg {
42   mach_msg_audit_trailer_t trailer;
43 };
44
45 }  // namespace
46
47 class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
48  public:
49   explicit MachListenerThreadDelegate(MachBroker* broker)
50       : broker_(broker),
51         server_port_(MACH_PORT_NULL) {
52     DCHECK(broker_);
53   }
54
55   bool Init() {
56     DCHECK(server_port_.get() == MACH_PORT_NULL);
57
58     mach_port_t port;
59     kern_return_t kr = mach_port_allocate(mach_task_self(),
60                                           MACH_PORT_RIGHT_RECEIVE,
61                                           &port);
62     if (kr != KERN_SUCCESS) {
63       MACH_LOG(ERROR, kr) << "mach_port_allocate";
64       return false;
65     }
66     server_port_.reset(port);
67
68     // Allocate a send right for the server port.
69     kr = mach_port_insert_right(
70         mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
71     if (kr != KERN_SUCCESS) {
72       MACH_LOG(ERROR, kr) << "mach_port_insert_right";
73       return false;
74     }
75     // Deallocate the right after registering with the bootstrap server.
76     base::mac::ScopedMachSendRight send_right(port);
77
78     // Register the port with the bootstrap server. Because bootstrap_register
79     // is deprecated, this has to be wraped in an ObjC interface.
80     NSPort* ns_port = [NSMachPort portWithMachPort:port
81                                            options:NSMachPortDeallocateNone];
82     NSString* name = base::SysUTF8ToNSString(broker_->GetMachPortName());
83     return [[NSMachBootstrapServer sharedInstance] registerPort:ns_port
84                                                            name:name];
85   }
86
87   // Implement |PlatformThread::Delegate|.
88   virtual void ThreadMain() OVERRIDE {
89     MachBroker_ParentRecvMsg msg;
90     bzero(&msg, sizeof(msg));
91     msg.header.msgh_size = sizeof(msg);
92     msg.header.msgh_local_port = server_port_.get();
93
94     const mach_msg_option_t options = MACH_RCV_MSG |
95         MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
96         MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
97
98     kern_return_t kr;
99     while ((kr = mach_msg(&msg.header,
100                           options,
101                           0,
102                           sizeof(msg),
103                           server_port_,
104                           MACH_MSG_TIMEOUT_NONE,
105                           MACH_PORT_NULL)) == KERN_SUCCESS) {
106       // Use the kernel audit information to make sure this message is from
107       // a task that this process spawned. The kernel audit token contains the
108       // unspoofable pid of the task that sent the message.
109       //
110       // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
111       pid_t child_pid;
112       audit_token_to_au32(msg.trailer.msgh_audit,
113           NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
114
115       mach_port_t child_task_port = msg.child_task_port.name;
116
117       // Take the lock and update the broker information.
118       base::AutoLock lock(broker_->GetLock());
119       broker_->FinalizePid(child_pid, child_task_port);
120     }
121
122     MACH_LOG(ERROR, kr) << "mach_msg";
123   }
124
125  private:
126   // The MachBroker to use when new child task rights are received.  Can be
127   // NULL.
128   MachBroker* broker_;  // weak
129
130   base::mac::ScopedMachReceiveRight server_port_;
131
132   DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate);
133 };
134
135 // static
136 bool MachBroker::ChildSendTaskPortToParent() {
137   // Look up the named MachBroker port that's been registered with the
138   // bootstrap server.
139   mach_port_t parent_port;
140   kern_return_t kr = bootstrap_look_up(bootstrap_port,
141       const_cast<char*>(GetMachPortName().c_str()), &parent_port);
142   if (kr != KERN_SUCCESS) {
143     BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
144     return false;
145   }
146   base::mac::ScopedMachSendRight scoped_right(parent_port);
147
148   // Create the check in message. This will copy a send right on this process'
149   // (the child's) task port and send it to the parent.
150   MachBroker_ChildSendMsg msg;
151   bzero(&msg, sizeof(msg));
152   msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND) |
153                          MACH_MSGH_BITS_COMPLEX;
154   msg.header.msgh_remote_port = parent_port;
155   msg.header.msgh_size = sizeof(msg);
156   msg.body.msgh_descriptor_count = 1;
157   msg.child_task_port.name = mach_task_self();
158   msg.child_task_port.disposition = MACH_MSG_TYPE_PORT_SEND;
159   msg.child_task_port.type = MACH_MSG_PORT_DESCRIPTOR;
160
161   kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
162       0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
163   if (kr != KERN_SUCCESS) {
164     MACH_LOG(ERROR, kr) << "mach_msg";
165     return false;
166   }
167
168   return true;
169 }
170
171 // static
172 std::string MachBroker::GetMachPortName() {
173   const CommandLine* command_line = CommandLine::ForCurrentProcess();
174   const bool is_child = command_line->HasSwitch(switches::kProcessType);
175
176   // In non-browser (child) processes, use the parent's pid.
177   const pid_t pid = is_child ? getppid() : getpid();
178   return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid);
179 }
180
181 // static
182 MachBroker* MachBroker::GetInstance() {
183   return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get();
184 }
185
186 base::Lock& MachBroker::GetLock() {
187   return lock_;
188 }
189
190 void MachBroker::EnsureRunning() {
191   lock_.AssertAcquired();
192
193   if (!listener_thread_started_) {
194     listener_thread_started_ = true;
195
196     BrowserThread::PostTask(
197         BrowserThread::UI, FROM_HERE,
198         base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this)));
199
200     // Intentional leak.  This thread is never joined or reaped.
201     MachListenerThreadDelegate* thread = new MachListenerThreadDelegate(this);
202     if (thread->Init()) {
203       base::PlatformThread::CreateNonJoinable(0, thread);
204     } else {
205       LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate";
206     }
207   }
208 }
209
210 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid) {
211   lock_.AssertAcquired();
212
213   DCHECK_EQ(0u, mach_map_.count(pid));
214   mach_map_[pid] = MACH_PORT_NULL;
215 }
216
217 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const {
218   base::AutoLock lock(lock_);
219   MachBroker::MachMap::const_iterator it = mach_map_.find(pid);
220   if (it == mach_map_.end())
221     return MACH_PORT_NULL;
222   return it->second;
223 }
224
225 void MachBroker::BrowserChildProcessHostDisconnected(
226     const ChildProcessData& data) {
227   InvalidatePid(data.handle);
228 }
229
230 void MachBroker::BrowserChildProcessCrashed(const ChildProcessData& data) {
231   InvalidatePid(data.handle);
232 }
233
234 void MachBroker::Observe(int type,
235                          const NotificationSource& source,
236                          const NotificationDetails& details) {
237   // TODO(rohitrao): These notifications do not always carry the proper PIDs,
238   // especially when the renderer is already gone or has crashed.  Find a better
239   // way to listen for child process deaths.  http://crbug.com/55734
240   base::ProcessHandle handle = 0;
241   switch (type) {
242     case NOTIFICATION_RENDERER_PROCESS_CLOSED:
243       handle = Details<RenderProcessHost::RendererClosedDetails>(
244           details)->handle;
245       break;
246     case NOTIFICATION_RENDERER_PROCESS_TERMINATED:
247       handle = Source<RenderProcessHost>(source)->GetHandle();
248       break;
249     default:
250       NOTREACHED() << "Unexpected notification";
251       break;
252   }
253   InvalidatePid(handle);
254 }
255
256 MachBroker::MachBroker() : listener_thread_started_(false) {
257 }
258
259 MachBroker::~MachBroker() {}
260
261 void MachBroker::FinalizePid(base::ProcessHandle pid,
262                              mach_port_t task_port) {
263   lock_.AssertAcquired();
264
265   MachMap::iterator it = mach_map_.find(pid);
266   if (it == mach_map_.end()) {
267     // Do nothing for unknown pids.
268     LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!";
269     return;
270   }
271
272   DCHECK(it->second == MACH_PORT_NULL);
273   if (it->second == MACH_PORT_NULL)
274     it->second = task_port;
275 }
276
277 void MachBroker::InvalidatePid(base::ProcessHandle pid) {
278   base::AutoLock lock(lock_);
279   MachBroker::MachMap::iterator it = mach_map_.find(pid);
280   if (it == mach_map_.end())
281     return;
282
283   kern_return_t kr = mach_port_deallocate(mach_task_self(),
284                                           it->second);
285   MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
286   mach_map_.erase(it);
287 }
288
289 void MachBroker::RegisterNotifications() {
290   registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED,
291                  NotificationService::AllBrowserContextsAndSources());
292   registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
293                  NotificationService::AllBrowserContextsAndSources());
294
295   // No corresponding StopObservingBrowserChildProcesses,
296   // we leak this singleton.
297   BrowserChildProcessObserver::Add(this);
298 }
299
300 }  // namespace content