summaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
authoramistry <amistry@chromium.org>2016-03-11 20:51:43 -0800
committerCommit bot <commit-bot@chromium.org>2016-03-12 04:53:54 +0000
commite04cef7c43dc8c9562f41e3fc51fef73f48da05a (patch)
tree679a8faa9ee91fd07fd2456e8d9f624682c11b68 /ipc
parent5dbc89896ef3ed45ec4f2d7d761d53e6ba2dc60e (diff)
downloadchromium_src-e04cef7c43dc8c9562f41e3fc51fef73f48da05a.zip
chromium_src-e04cef7c43dc8c9562f41e3fc51fef73f48da05a.tar.gz
chromium_src-e04cef7c43dc8c9562f41e3fc51fef73f48da05a.tar.bz2
Move mach code shared by Chrome IPC and Mojo to //base/mac.
BUG=582468 Review URL: https://codereview.chromium.org/1778203002 Cr-Commit-Position: refs/heads/master@{#380855}
Diffstat (limited to 'ipc')
-rw-r--r--ipc/attachment_broker_privileged_mac.cc123
-rw-r--r--ipc/attachment_broker_privileged_mac.h11
-rw-r--r--ipc/attachment_broker_privileged_mac_unittest.cc18
-rw-r--r--ipc/attachment_broker_unprivileged_mac.cc33
4 files changed, 36 insertions, 149 deletions
diff --git a/ipc/attachment_broker_privileged_mac.cc b/ipc/attachment_broker_privileged_mac.cc
index 725085b..6b53ce0 100644
--- a/ipc/attachment_broker_privileged_mac.cc
+++ b/ipc/attachment_broker_privileged_mac.cc
@@ -8,6 +8,7 @@
#include <tuple>
+#include "base/mac/mach_port_util.h"
#include "base/mac/scoped_mach_port.h"
#include "base/memory/shared_memory.h"
#include "base/process/port_provider_mac.h"
@@ -18,49 +19,6 @@
#include "ipc/ipc_channel.h"
#include "ipc/mach_port_attachment_mac.h"
-namespace {
-
-// Struct for sending a complex Mach message.
-struct MachSendComplexMessage {
- mach_msg_header_t header;
- mach_msg_body_t body;
- mach_msg_port_descriptor_t data;
-};
-
-// Sends a Mach port to |endpoint|. Assumes that |endpoint| is a send once
-// right. Takes ownership of |endpoint|.
-kern_return_t SendMachPort(mach_port_t endpoint,
- mach_port_t port_to_send,
- int disposition) {
- MachSendComplexMessage send_msg;
- send_msg.header.msgh_bits =
- MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0) | MACH_MSGH_BITS_COMPLEX;
- send_msg.header.msgh_size = sizeof(send_msg);
- send_msg.header.msgh_remote_port = endpoint;
- send_msg.header.msgh_local_port = MACH_PORT_NULL;
- send_msg.header.msgh_reserved = 0;
- send_msg.header.msgh_id = 0;
- send_msg.body.msgh_descriptor_count = 1;
- send_msg.data.name = port_to_send;
- send_msg.data.disposition = disposition;
- send_msg.data.type = MACH_MSG_PORT_DESCRIPTOR;
-
- kern_return_t kr =
- mach_msg(&send_msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
- send_msg.header.msgh_size,
- 0, // receive limit
- MACH_PORT_NULL, // receive name
- 0, // timeout
- MACH_PORT_NULL); // notification port
-
- if (kr != KERN_SUCCESS)
- mach_port_deallocate(mach_task_self(), endpoint);
-
- return kr;
-}
-
-} // namespace
-
namespace IPC {
AttachmentBrokerPrivilegedMac::AttachmentBrokerPrivilegedMac(
@@ -232,61 +190,6 @@ bool AttachmentBrokerPrivilegedMac::RouteWireFormatToAnother(
return true;
}
-mach_port_name_t AttachmentBrokerPrivilegedMac::CreateIntermediateMachPort(
- mach_port_t task_port,
- base::mac::ScopedMachSendRight port_to_insert) {
- DCHECK_NE(mach_task_self(), task_port);
- DCHECK_NE(static_cast<mach_port_name_t>(MACH_PORT_NULL), task_port);
-
- // Make a port with receive rights in the destination task.
- mach_port_name_t endpoint;
- kern_return_t kr =
- mach_port_allocate(task_port, MACH_PORT_RIGHT_RECEIVE, &endpoint);
- if (kr != KERN_SUCCESS) {
- LogError(ERROR_MAKE_RECEIVE_PORT);
- return MACH_PORT_NULL;
- }
-
- // Change its message queue limit so that it accepts one message.
- mach_port_limits limits = {};
- limits.mpl_qlimit = 1;
- kr = mach_port_set_attributes(task_port, endpoint, MACH_PORT_LIMITS_INFO,
- reinterpret_cast<mach_port_info_t>(&limits),
- MACH_PORT_LIMITS_INFO_COUNT);
- if (kr != KERN_SUCCESS) {
- LogError(ERROR_SET_ATTRIBUTES);
- mach_port_deallocate(task_port, endpoint);
- return MACH_PORT_NULL;
- }
-
- // Get a send right.
- mach_port_t send_once_right;
- mach_msg_type_name_t send_right_type;
- kr =
- mach_port_extract_right(task_port, endpoint, MACH_MSG_TYPE_MAKE_SEND_ONCE,
- &send_once_right, &send_right_type);
- if (kr != KERN_SUCCESS) {
- LogError(ERROR_EXTRACT_DEST_RIGHT);
- mach_port_deallocate(task_port, endpoint);
- return MACH_PORT_NULL;
- }
- DCHECK_EQ(static_cast<mach_msg_type_name_t>(MACH_MSG_TYPE_PORT_SEND_ONCE),
- send_right_type);
-
- // This call takes ownership of |send_once_right|.
- kr = SendMachPort(
- send_once_right, port_to_insert.get(), MACH_MSG_TYPE_COPY_SEND);
- if (kr != KERN_SUCCESS) {
- LogError(ERROR_SEND_MACH_PORT);
- mach_port_deallocate(task_port, endpoint);
- return MACH_PORT_NULL;
- }
-
- // Endpoint is intentionally leaked into the destination task. An IPC must be
- // sent to the destination task so that it can clean up this port.
- return endpoint;
-}
-
base::mac::ScopedMachSendRight AttachmentBrokerPrivilegedMac::ExtractNamedRight(
mach_port_t task_port,
mach_port_name_t named_right) {
@@ -367,8 +270,28 @@ bool AttachmentBrokerPrivilegedMac::SendPrecursor(
base::mac::ScopedMachSendRight port_to_insert = precursor->TakePort();
mach_port_name_t intermediate_port = MACH_PORT_NULL;
if (port_to_insert.get() != MACH_PORT_NULL) {
- intermediate_port = CreateIntermediateMachPort(
- task_port, base::mac::ScopedMachSendRight(port_to_insert.release()));
+ base::MachCreateError error_code;
+ intermediate_port = base::CreateIntermediateMachPort(
+ task_port, base::mac::ScopedMachSendRight(port_to_insert.release()),
+ &error_code);
+ if (intermediate_port == MACH_PORT_NULL) {
+ UMAError uma_error;
+ switch (error_code) {
+ case base::MachCreateError::ERROR_MAKE_RECEIVE_PORT:
+ uma_error = ERROR_MAKE_RECEIVE_PORT;
+ break;
+ case base::MachCreateError::ERROR_SET_ATTRIBUTES:
+ uma_error = ERROR_SET_ATTRIBUTES;
+ break;
+ case base::MachCreateError::ERROR_EXTRACT_DEST_RIGHT:
+ uma_error = ERROR_EXTRACT_DEST_RIGHT;
+ break;
+ case base::MachCreateError::ERROR_SEND_MACH_PORT:
+ uma_error = ERROR_SEND_MACH_PORT;
+ break;
+ }
+ LogError(uma_error);
+ }
}
return RouteWireFormatToAnother(
CopyWireFormat(wire_format, intermediate_port));
diff --git a/ipc/attachment_broker_privileged_mac.h b/ipc/attachment_broker_privileged_mac.h
index c13cba0..0393fe1 100644
--- a/ipc/attachment_broker_privileged_mac.h
+++ b/ipc/attachment_broker_privileged_mac.h
@@ -141,17 +141,6 @@ class IPC_EXPORT AttachmentBrokerPrivilegedMac
MachPortWireFormat DuplicateMachPort(const MachPortWireFormat& wire_format,
base::ProcessId source_process);
- // |task_port| is the task port of another process.
- // |port_to_insert| must be a send right in the current task's name space.
- // Creates an intermediate Mach port in |pid| and sends |port_to_insert| as a
- // mach_msg to the intermediate Mach port.
- // Returns the intermediate port on success, and MACH_PORT_NULL on failure.
- // This method takes ownership of |port_to_insert|. On success, ownership is
- // passed to the intermediate Mach port.
- mach_port_name_t CreateIntermediateMachPort(
- mach_port_t task_port,
- base::mac::ScopedMachSendRight port_to_insert);
-
// Extracts a copy of the send right to |named_right| from |task_port|.
// Returns MACH_PORT_NULL on error.
base::mac::ScopedMachSendRight ExtractNamedRight(
diff --git a/ipc/attachment_broker_privileged_mac_unittest.cc b/ipc/attachment_broker_privileged_mac_unittest.cc
index 15aecb6..dbecc73 100644
--- a/ipc/attachment_broker_privileged_mac_unittest.cc
+++ b/ipc/attachment_broker_privileged_mac_unittest.cc
@@ -14,6 +14,7 @@
#include "base/command_line.h"
#include "base/mac/mac_util.h"
#include "base/mac/mach_logging.h"
+#include "base/mac/mach_port_util.h"
#include "base/mac/scoped_mach_port.h"
#include "base/macros.h"
#include "base/memory/shared_memory.h"
@@ -198,9 +199,10 @@ TEST_F(AttachmentBrokerPrivilegedMacMultiProcessTest, InsertRight) {
// port.
IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
MACH_PORT_RIGHT_SEND);
- mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
- client_task_port_.get(), base::mac::ScopedMachSendRight(
- shared_memory->handle().GetMemoryObject()));
+ mach_port_name_t inserted_memory_object = base::CreateIntermediateMachPort(
+ client_task_port_.get(),
+ base::mac::ScopedMachSendRight(shared_memory->handle().GetMemoryObject()),
+ nullptr);
EXPECT_NE(inserted_memory_object,
static_cast<mach_port_name_t>(MACH_PORT_NULL));
SendUInt32(client_port_.get(), inserted_memory_object);
@@ -266,10 +268,11 @@ TEST_F(AttachmentBrokerPrivilegedMacMultiProcessTest, InsertSameRightTwice) {
for (int i = 0; i < 2; ++i) {
IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
MACH_PORT_RIGHT_SEND);
- mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
+ mach_port_name_t inserted_memory_object = base::CreateIntermediateMachPort(
client_task_port_.get(),
base::mac::ScopedMachSendRight(
- shared_memory->handle().GetMemoryObject()));
+ shared_memory->handle().GetMemoryObject()),
+ nullptr);
EXPECT_NE(inserted_memory_object,
static_cast<mach_port_name_t>(MACH_PORT_NULL));
SendUInt32(client_port_.get(), inserted_memory_object);
@@ -362,10 +365,11 @@ TEST_F(AttachmentBrokerPrivilegedMacMultiProcessTest, InsertTwoRights) {
// port.
IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
MACH_PORT_RIGHT_SEND);
- mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
+ mach_port_name_t inserted_memory_object = base::CreateIntermediateMachPort(
client_task_port_.get(),
base::mac::ScopedMachSendRight(
- shared_memory->handle().GetMemoryObject()));
+ shared_memory->handle().GetMemoryObject()),
+ nullptr);
EXPECT_NE(inserted_memory_object,
static_cast<mach_port_name_t>(MACH_PORT_NULL));
SendUInt32(client_port_.get(), inserted_memory_object);
diff --git a/ipc/attachment_broker_unprivileged_mac.cc b/ipc/attachment_broker_unprivileged_mac.cc
index 4e76688..4f37698 100644
--- a/ipc/attachment_broker_unprivileged_mac.cc
+++ b/ipc/attachment_broker_unprivileged_mac.cc
@@ -6,6 +6,7 @@
#include <mach/mach.h>
+#include "base/mac/mach_port_util.h"
#include "base/mac/scoped_mach_port.h"
#include "base/process/process.h"
#include "ipc/attachment_broker_messages.h"
@@ -13,36 +14,6 @@
#include "ipc/ipc_sender.h"
#include "ipc/mach_port_attachment_mac.h"
-namespace {
-
-// Struct for receiving a complex message.
-struct MachReceiveComplexMessage {
- mach_msg_header_t header;
- mach_msg_body_t body;
- mach_msg_port_descriptor_t data;
- mach_msg_trailer_t trailer;
-};
-
-// Receives a Mach port from |port_to_listen_on|, which should have exactly one
-// queued message. Returns |MACH_PORT_NULL| on any error.
-base::mac::ScopedMachSendRight ReceiveMachPort(mach_port_t port_to_listen_on) {
- MachReceiveComplexMessage recv_msg;
- mach_msg_header_t* recv_hdr = &recv_msg.header;
- recv_hdr->msgh_local_port = port_to_listen_on;
- recv_hdr->msgh_size = sizeof(recv_msg);
-
- kern_return_t kr =
- mach_msg(recv_hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
- recv_hdr->msgh_size, port_to_listen_on, 0, MACH_PORT_NULL);
- if (kr != KERN_SUCCESS)
- return base::mac::ScopedMachSendRight(MACH_PORT_NULL);
- if (recv_msg.header.msgh_id != 0)
- return base::mac::ScopedMachSendRight(MACH_PORT_NULL);
- return base::mac::ScopedMachSendRight(recv_msg.data.name);
-}
-
-} // namespace
-
namespace IPC {
AttachmentBrokerUnprivilegedMac::AttachmentBrokerUnprivilegedMac() {}
@@ -93,7 +64,7 @@ void AttachmentBrokerUnprivilegedMac::OnMachPortHasBeenDuplicated(
base::mac::ScopedMachReceiveRight message_port(wire_format.mach_port);
base::mac::ScopedMachSendRight memory_object(
- ReceiveMachPort(message_port.get()));
+ base::ReceiveMachPort(message_port.get()));
LogError(memory_object.get() == MACH_PORT_NULL ? ERR_RECEIVE_MACH_MESSAGE
: SUCCESS);