Index: content/browser/mach_broker_mac.mm |
diff --git a/content/browser/mach_broker_mac.mm b/content/browser/mach_broker_mac.mm |
index 7c527e55b6f5faaf1e677d2096947c2d4f66a54d..3efec6b94d6b589074a77581b43d87917b45eb6d 100644 |
--- a/content/browser/mach_broker_mac.mm |
+++ b/content/browser/mach_broker_mac.mm |
@@ -13,11 +13,9 @@ |
#include "base/logging.h" |
#include "base/mac/foundation_util.h" |
#include "base/mac/mach_logging.h" |
-#include "base/mac/scoped_mach_port.h" |
#include "base/strings/string_util.h" |
#include "base/strings/stringprintf.h" |
#include "base/strings/sys_string_conversions.h" |
-#include "base/threading/platform_thread.h" |
#include "content/browser/renderer_host/render_process_host_impl.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/child_process_data.h" |
@@ -44,94 +42,6 @@ struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { |
} // namespace |
-class MachListenerThreadDelegate : public base::PlatformThread::Delegate { |
- public: |
- explicit MachListenerThreadDelegate(MachBroker* broker) |
- : broker_(broker), |
- server_port_(MACH_PORT_NULL) { |
- DCHECK(broker_); |
- } |
- |
- bool Init() { |
- DCHECK(server_port_.get() == MACH_PORT_NULL); |
- |
- mach_port_t port; |
- kern_return_t kr = mach_port_allocate(mach_task_self(), |
- MACH_PORT_RIGHT_RECEIVE, |
- &port); |
- if (kr != KERN_SUCCESS) { |
- MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
- return false; |
- } |
- server_port_.reset(port); |
- |
- // Allocate a send right for the server port. |
- kr = mach_port_insert_right( |
- mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); |
- if (kr != KERN_SUCCESS) { |
- MACH_LOG(ERROR, kr) << "mach_port_insert_right"; |
- return false; |
- } |
- // Deallocate the right after registering with the bootstrap server. |
- base::mac::ScopedMachSendRight send_right(port); |
- |
- // Register the port with the bootstrap server. Because bootstrap_register |
- // is deprecated, this has to be wraped in an ObjC interface. |
- NSPort* ns_port = [NSMachPort portWithMachPort:port |
- options:NSMachPortDeallocateNone]; |
- NSString* name = base::SysUTF8ToNSString(broker_->GetMachPortName()); |
- return [[NSMachBootstrapServer sharedInstance] registerPort:ns_port |
- name:name]; |
- } |
- |
- // Implement |PlatformThread::Delegate|. |
- void ThreadMain() override { |
- MachBroker_ParentRecvMsg msg; |
- bzero(&msg, sizeof(msg)); |
- msg.header.msgh_size = sizeof(msg); |
- msg.header.msgh_local_port = server_port_.get(); |
- |
- const mach_msg_option_t options = MACH_RCV_MSG | |
- MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | |
- MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); |
- |
- kern_return_t kr; |
- while ((kr = mach_msg(&msg.header, |
- options, |
- 0, |
- sizeof(msg), |
- server_port_, |
- MACH_MSG_TIMEOUT_NONE, |
- MACH_PORT_NULL)) == KERN_SUCCESS) { |
- // Use the kernel audit information to make sure this message is from |
- // a task that this process spawned. The kernel audit token contains the |
- // unspoofable pid of the task that sent the message. |
- // |
- // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). |
- pid_t child_pid; |
- audit_token_to_au32(msg.trailer.msgh_audit, |
- NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); |
- |
- mach_port_t child_task_port = msg.child_task_port.name; |
- |
- // Take the lock and update the broker information. |
- base::AutoLock lock(broker_->GetLock()); |
- broker_->FinalizePid(child_pid, child_task_port); |
- } |
- |
- MACH_LOG(ERROR, kr) << "mach_msg"; |
- } |
- |
- private: |
- // The MachBroker to use when new child task rights are received. Can be |
- // NULL. |
- MachBroker* broker_; // weak |
- |
- base::mac::ScopedMachReceiveRight server_port_; |
- |
- DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); |
-}; |
- |
bool MachBroker::ChildSendTaskPortToParent() { |
// Look up the named MachBroker port that's been registered with the |
// bootstrap server. |
@@ -168,7 +78,7 @@ bool MachBroker::ChildSendTaskPortToParent() { |
} |
MachBroker* MachBroker::GetInstance() { |
- return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); |
+ return Singleton<MachBroker, LeakySingletonTraits<MachBroker>>::get(); |
} |
base::Lock& MachBroker::GetLock() { |
@@ -178,20 +88,18 @@ base::Lock& MachBroker::GetLock() { |
void MachBroker::EnsureRunning() { |
lock_.AssertAcquired(); |
- if (!listener_thread_started_) { |
- listener_thread_started_ = true; |
+ if (initialized_) |
+ return; |
- BrowserThread::PostTask( |
- BrowserThread::UI, FROM_HERE, |
- base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); |
+ // Do not attempt to reinitialize in the event of failure. |
+ initialized_ = true; |
- // Intentional leak. This thread is never joined or reaped. |
- MachListenerThreadDelegate* thread = new MachListenerThreadDelegate(this); |
- if (thread->Init()) { |
- base::PlatformThread::CreateNonJoinable(0, thread); |
- } else { |
- LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; |
- } |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); |
+ |
+ if (!Init()) { |
+ LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; |
} |
} |
@@ -238,11 +146,72 @@ void MachBroker::Observe(int type, |
} |
} |
-MachBroker::MachBroker() : listener_thread_started_(false) { |
+MachBroker::MachBroker() : initialized_(false) { |
} |
MachBroker::~MachBroker() {} |
+bool MachBroker::Init() { |
+ DCHECK(server_port_.get() == MACH_PORT_NULL); |
+ |
+ // Check in with launchd and publish the service name. |
+ mach_port_t port; |
+ kern_return_t kr = |
+ bootstrap_check_in(bootstrap_port, GetMachPortName().c_str(), &port); |
+ if (kr != KERN_SUCCESS) { |
+ BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in"; |
+ return false; |
+ } |
+ server_port_.reset(port); |
+ |
+ // Start the dispatch source. |
+ std::string queue_name = |
+ base::StringPrintf("%s.MachBroker", base::mac::BaseBundleID()); |
+ dispatch_source_.reset(new base::DispatchSourceMach( |
+ queue_name.c_str(), server_port_.get(), ^{ HandleRequest(); })); |
+ dispatch_source_->Resume(); |
+ |
+ return true; |
+} |
+ |
+void MachBroker::HandleRequest() { |
+ MachBroker_ParentRecvMsg msg; |
+ bzero(&msg, sizeof(msg)); |
+ msg.header.msgh_size = sizeof(msg); |
+ msg.header.msgh_local_port = server_port_.get(); |
+ |
+ const mach_msg_option_t options = MACH_RCV_MSG | |
+ MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) | |
+ MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); |
+ |
+ kern_return_t kr = mach_msg(&msg.header, |
+ options, |
+ 0, |
+ sizeof(msg), |
+ server_port_, |
+ MACH_MSG_TIMEOUT_NONE, |
+ MACH_PORT_NULL); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_msg"; |
+ return; |
+ } |
+ |
+ // Use the kernel audit information to make sure this message is from |
+ // a task that this process spawned. The kernel audit token contains the |
+ // unspoofable pid of the task that sent the message. |
+ // |
+ // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid(). |
+ pid_t child_pid; |
+ audit_token_to_au32(msg.trailer.msgh_audit, |
+ NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); |
+ |
+ mach_port_t child_task_port = msg.child_task_port.name; |
+ |
+ // Take the lock and update the broker information. |
+ base::AutoLock lock(GetLock()); |
+ FinalizePid(child_pid, child_task_port); |
+} |
+ |
void MachBroker::FinalizePid(base::ProcessHandle pid, |
mach_port_t task_port) { |
lock_.AssertAcquired(); |