Commit ca5b918a authored by rsesek's avatar rsesek Committed by Commit bot

[Mac] Remove the dedicated MachBroker thread and use a dispatch queue instead.

BUG=487674

Review URL: https://codereview.chromium.org/1138513003

Cr-Commit-Position: refs/heads/master@{#329744}
parent ba254c4e
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
#include <map> #include <map>
#include <string> #include <string>
#include "base/mac/dispatch_source_mach.h"
#include "base/mac/scoped_mach_port.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
#include "base/process/process_metrics.h" #include "base/process/process_metrics.h"
...@@ -79,12 +82,18 @@ class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, ...@@ -79,12 +82,18 @@ class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider,
private: private:
friend class MachBrokerTest; friend class MachBrokerTest;
friend class MachListenerThreadDelegate;
friend struct DefaultSingletonTraits<MachBroker>; friend struct DefaultSingletonTraits<MachBroker>;
MachBroker(); MachBroker();
~MachBroker() override; ~MachBroker() override;
// Performs any initialization work.
bool Init();
// Message handler that is invoked on |dispatch_source_| when an
// incoming message needs to be received.
void HandleRequest();
// Updates the mapping for |pid| to include the given |mach_info|. Does // Updates the mapping for |pid| to include the given |mach_info|. Does
// nothing if PlaceholderForPid() has not already been called for the given // nothing if PlaceholderForPid() has not already been called for the given
// |pid|. Callers MUST acquire the lock given by GetLock() before calling // |pid|. Callers MUST acquire the lock given by GetLock() before calling
...@@ -97,16 +106,23 @@ class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider, ...@@ -97,16 +106,23 @@ class CONTENT_EXPORT MachBroker : public base::ProcessMetrics::PortProvider,
// Returns the Mach port name to use when sending or receiving messages. // Returns the Mach port name to use when sending or receiving messages.
// Does the Right Thing in the browser and in child processes. // Does the Right Thing in the browser and in child processes.
static std::string GetMachPortName(); static std::string GetMachPortName();
// Callback used to register notifications on the UI thread. // Callback used to register notifications on the UI thread.
void RegisterNotifications(); void RegisterNotifications();
// True if the listener thread has been started. // Whether or not the class has been initialized.
bool listener_thread_started_; bool initialized_;
// Used to register for notifications received by NotificationObserver. // Used to register for notifications received by NotificationObserver.
// Accessed only on the UI thread. // Accessed only on the UI thread.
NotificationRegistrar registrar_; NotificationRegistrar registrar_;
// The Mach port on which the server listens.
base::mac::ScopedMachReceiveRight server_port_;
// The dispatch source and queue on which Mach messages will be received.
scoped_ptr<base::DispatchSourceMach> dispatch_source_;
// Stores mach info for every process in the broker. // Stores mach info for every process in the broker.
typedef std::map<base::ProcessHandle, mach_port_t> MachMap; typedef std::map<base::ProcessHandle, mach_port_t> MachMap;
MachMap mach_map_; MachMap mach_map_;
......
...@@ -13,11 +13,9 @@ ...@@ -13,11 +13,9 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/mach_logging.h" #include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.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/browser/renderer_host/render_process_host_impl.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h" #include "content/public/browser/child_process_data.h"
...@@ -44,94 +42,6 @@ struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg { ...@@ -44,94 +42,6 @@ struct MachBroker_ParentRecvMsg : public MachBroker_ChildSendMsg {
} // namespace } // 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() { bool MachBroker::ChildSendTaskPortToParent() {
// Look up the named MachBroker port that's been registered with the // Look up the named MachBroker port that's been registered with the
// bootstrap server. // bootstrap server.
...@@ -168,7 +78,7 @@ bool MachBroker::ChildSendTaskPortToParent() { ...@@ -168,7 +78,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
} }
MachBroker* MachBroker::GetInstance() { MachBroker* MachBroker::GetInstance() {
return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); return Singleton<MachBroker, LeakySingletonTraits<MachBroker>>::get();
} }
base::Lock& MachBroker::GetLock() { base::Lock& MachBroker::GetLock() {
...@@ -178,21 +88,19 @@ base::Lock& MachBroker::GetLock() { ...@@ -178,21 +88,19 @@ base::Lock& MachBroker::GetLock() {
void MachBroker::EnsureRunning() { void MachBroker::EnsureRunning() {
lock_.AssertAcquired(); lock_.AssertAcquired();
if (!listener_thread_started_) { if (initialized_)
listener_thread_started_ = true; return;
// Do not attempt to reinitialize in the event of failure.
initialized_ = true;
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this)));
// Intentional leak. This thread is never joined or reaped. if (!Init()) {
MachListenerThreadDelegate* thread = new MachListenerThreadDelegate(this);
if (thread->Init()) {
base::PlatformThread::CreateNonJoinable(0, thread);
} else {
LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate"; LOG(ERROR) << "Failed to initialize the MachListenerThreadDelegate";
} }
}
} }
void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid, void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid,
...@@ -238,11 +146,72 @@ void MachBroker::Observe(int type, ...@@ -238,11 +146,72 @@ void MachBroker::Observe(int type,
} }
} }
MachBroker::MachBroker() : listener_thread_started_(false) { MachBroker::MachBroker() : initialized_(false) {
} }
MachBroker::~MachBroker() {} 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, void MachBroker::FinalizePid(base::ProcessHandle pid,
mach_port_t task_port) { mach_port_t task_port) {
lock_.AssertAcquired(); lock_.AssertAcquired();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment