summaryrefslogtreecommitdiffstats
path: root/mojo/edk/embedder
diff options
context:
space:
mode:
authorjam <jam@chromium.org>2015-10-26 12:15:33 -0700
committerCommit bot <commit-bot@chromium.org>2015-10-26 19:16:15 +0000
commitbe81738d16b0e1dcb51f2f92fc10ac6d216d42a1 (patch)
treee0e885ff056158dbbf75408e7ebff48d66f0c0eb /mojo/edk/embedder
parent615479bb713dd274decd8facfb2694fffe83a372 (diff)
downloadchromium_src-be81738d16b0e1dcb51f2f92fc10ac6d216d42a1.zip
chromium_src-be81738d16b0e1dcb51f2f92fc10ac6d216d42a1.tar.gz
chromium_src-be81738d16b0e1dcb51f2f92fc10ac6d216d42a1.tar.bz2
Get the new Mojo EDK working on POSIX.
Some notes: -in RawChannel, I'm passing FDs as ints instead of PlatformHandle to make it very clear that this is only used on POSIX BUG=478251 Review URL: https://codereview.chromium.org/1417773004 Cr-Commit-Position: refs/heads/master@{#356092}
Diffstat (limited to 'mojo/edk/embedder')
-rw-r--r--mojo/edk/embedder/embedder.cc3
-rw-r--r--mojo/edk/embedder/embedder_unittest.cc2
-rw-r--r--mojo/edk/embedder/platform_channel_pair_posix.cc14
-rw-r--r--mojo/edk/embedder/platform_channel_utils_posix.cc12
-rw-r--r--mojo/edk/embedder/platform_channel_utils_posix.h5
5 files changed, 31 insertions, 5 deletions
diff --git a/mojo/edk/embedder/embedder.cc b/mojo/edk/embedder/embedder.cc
index 91f03d0..fe5202c 100644
--- a/mojo/edk/embedder/embedder.cc
+++ b/mojo/edk/embedder/embedder.cc
@@ -166,7 +166,8 @@ ScopedMessagePipeHandle CreateMessagePipe(
ScopedMessagePipeHandle rv(
MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher)));
CHECK(rv.is_valid());
- dispatcher->Init(platform_handle.Pass(), nullptr, 0, nullptr, 0);
+ dispatcher->Init(platform_handle.Pass(), nullptr, 0, nullptr, 0, nullptr,
+ nullptr);
// TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it
// once that's fixed.
return rv.Pass();
diff --git a/mojo/edk/embedder/embedder_unittest.cc b/mojo/edk/embedder/embedder_unittest.cc
index bbc3f52..f53e440 100644
--- a/mojo/edk/embedder/embedder_unittest.cc
+++ b/mojo/edk/embedder/embedder_unittest.cc
@@ -126,7 +126,7 @@ TEST_F(EmbedderTest, SendReadableMessagePipe) {
char read_buffer[20000] = {};
uint32_t num_bytes = static_cast<uint32_t>(sizeof(read_buffer));
MojoHandle ports[10];
- uint32_t num_ports;
+ uint32_t num_ports = arraysize(ports);
ASSERT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, read_buffer, &num_bytes, &ports[0],
&num_ports, MOJO_READ_MESSAGE_FLAG_NONE));
diff --git a/mojo/edk/embedder/platform_channel_pair_posix.cc b/mojo/edk/embedder/platform_channel_pair_posix.cc
index 8cdac36..eaeac26 100644
--- a/mojo/edk/embedder/platform_channel_pair_posix.cc
+++ b/mojo/edk/embedder/platform_channel_pair_posix.cc
@@ -12,10 +12,15 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/posix/global_descriptors.h"
+#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/platform_handle.h"
+#if !defined(SO_PEEK_OFF)
+#define SO_PEEK_OFF 42
+#endif
+
namespace mojo {
namespace edk {
@@ -37,7 +42,16 @@ PlatformChannelPair::PlatformChannelPair() {
// Create the Unix domain socket and set the ends to nonblocking.
int fds[2];
// TODO(vtl): Maybe fail gracefully if |socketpair()| fails.
+
PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
+
+ // Store a common id in the SO_PEEK_OFF option (which we don't use since we
+ // don't peak) as a way of determining later if two sockets are connected to
+ // each other.
+ int identifier = base::RandInt(kint32min, kint32max);
+ setsockopt(fds[0], SOL_SOCKET, SO_PEEK_OFF, &identifier, sizeof(identifier));
+ setsockopt(fds[1], SOL_SOCKET, SO_PEEK_OFF, &identifier, sizeof(identifier));
+
PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0);
PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0);
diff --git a/mojo/edk/embedder/platform_channel_utils_posix.cc b/mojo/edk/embedder/platform_channel_utils_posix.cc
index 4af45e82..c064139 100644
--- a/mojo/edk/embedder/platform_channel_utils_posix.cc
+++ b/mojo/edk/embedder/platform_channel_utils_posix.cc
@@ -12,6 +12,10 @@
#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
+#if !defined(SO_PEEK_OFF)
+#define SO_PEEK_OFF 42
+#endif
+
namespace mojo {
namespace edk {
@@ -155,10 +159,18 @@ ssize_t PlatformChannelRecvmsg(PlatformHandle h,
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
+ // We use SO_PEEK_OFF to hold a common identifier between sockets to detect if
+ // they're connected. recvmsg modifies it, so we cache it and set it again
+ // after the call.
+ int id = 0;
+ socklen_t peek_off_size = sizeof(id);
+ getsockopt(h.fd, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size);
ssize_t result = HANDLE_EINTR(recvmsg(h.fd, &msg, MSG_DONTWAIT));
if (result < 0)
return result;
+ setsockopt(h.fd, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id));
+
// Success; no control messages.
if (msg.msg_controllen == 0)
return result;
diff --git a/mojo/edk/embedder/platform_channel_utils_posix.h b/mojo/edk/embedder/platform_channel_utils_posix.h
index 00e6a16..ad58753 100644
--- a/mojo/edk/embedder/platform_channel_utils_posix.h
+++ b/mojo/edk/embedder/platform_channel_utils_posix.h
@@ -20,9 +20,8 @@ namespace mojo {
namespace edk {
// The maximum number of handles that can be sent "at once" using
-// |PlatformChannelSendmsgWithHandles()|.
-// TODO(vtl): This number is taken from ipc/ipc_message_attachment_set.h:
-// |IPC::MessageAttachmentSet::kMaxDescriptorsPerMessage|.
+// |PlatformChannelSendmsgWithHandles()|. This must be less than the Linux
+// kernel's SCM_MAX_FD which is 253.
const size_t kPlatformChannelMaxNumHandles = 128;
// Use these to write to a socket created using |PlatformChannelPair| (or