1 // Copyright 2014 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 "mojo/embedder/platform_channel_utils_posix.h"
6
7 #include <sys/socket.h>
8 #include <sys/uio.h>
9 #include <unistd.h>
10
11 #include "base/logging.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "build/build_config.h"
14
15 namespace mojo {
16 namespace embedder {
17
18 // On Linux, |SIGPIPE| is suppressed by passing |MSG_NOSIGNAL| to
19 // |send()|/|sendmsg()|. (There is no way of suppressing |SIGPIPE| on
20 // |write()|/|writev().) On Mac, |SIGPIPE| is suppressed by setting the
21 // |SO_NOSIGPIPE| option on the socket.
22 //
23 // Performance notes:
24 // - On Linux, we have to use |send()|/|sendmsg()| rather than
25 // |write()|/|writev()| in order to suppress |SIGPIPE|. This is okay, since
26 // |send()| is (slightly) faster than |write()| (!), while |sendmsg()| is
27 // quite comparable to |writev()|.
28 // - On Mac, we may use |write()|/|writev()|. Here, |write()| is considerably
29 // faster than |send()|, whereas |sendmsg()| is quite comparable to
30 // |writev()|.
31 // - On both platforms, an appropriate |sendmsg()|/|writev()| is considerably
32 // faster than two |send()|s/|write()|s.
33 // - Relative numbers (minimum real times from 10 runs) for one |write()| of
34 // 1032 bytes, one |send()| of 1032 bytes, one |writev()| of 32+1000 bytes,
35 // one |sendmsg()| of 32+1000 bytes, two |write()|s of 32 and 1000 bytes, two
36 // |send()|s of 32 and 1000 bytes:
37 // - Linux: 0.81 s, 0.77 s, 0.87 s, 0.89 s, 1.31 s, 1.22 s
38 // - Mac: 2.21 s, 2.91 s, 2.98 s, 3.08 s, 3.59 s, 4.74 s
39
40 // Flags to use with calling |send()| or |sendmsg()| (see above).
41 #if defined(OS_MACOSX)
42 const int kSendFlags = 0;
43 #else
44 const int kSendFlags = MSG_NOSIGNAL;
45 #endif
46
PlatformChannelWrite(PlatformHandle h,const void * bytes,size_t num_bytes)47 ssize_t PlatformChannelWrite(PlatformHandle h,
48 const void* bytes,
49 size_t num_bytes) {
50 DCHECK(h.is_valid());
51 DCHECK(bytes);
52 DCHECK_GT(num_bytes, 0u);
53
54 #if defined(OS_MACOSX)
55 return HANDLE_EINTR(write(h.fd, bytes, num_bytes));
56 #else
57 return send(h.fd, bytes, num_bytes, kSendFlags);
58 #endif
59 }
60
PlatformChannelWritev(PlatformHandle h,struct iovec * iov,size_t num_iov)61 ssize_t PlatformChannelWritev(PlatformHandle h,
62 struct iovec* iov,
63 size_t num_iov) {
64 DCHECK(h.is_valid());
65 DCHECK(iov);
66 DCHECK_GT(num_iov, 0u);
67
68 #if defined(OS_MACOSX)
69 return HANDLE_EINTR(writev(h.fd, iov, static_cast<int>(num_iov)));
70 #else
71 struct msghdr msg = {};
72 msg.msg_iov = iov;
73 msg.msg_iovlen = num_iov;
74 return HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags));
75 #endif
76 }
77
PlatformChannelSendmsgWithHandles(PlatformHandle h,struct iovec * iov,size_t num_iov,PlatformHandle * platform_handles,size_t num_platform_handles)78 ssize_t PlatformChannelSendmsgWithHandles(PlatformHandle h,
79 struct iovec* iov,
80 size_t num_iov,
81 PlatformHandle* platform_handles,
82 size_t num_platform_handles) {
83 DCHECK(iov);
84 DCHECK_GT(num_iov, 0u);
85 DCHECK(platform_handles);
86 DCHECK_GT(num_platform_handles, 0u);
87 DCHECK_LE(num_platform_handles, kPlatformChannelMaxNumHandles);
88
89 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))];
90 struct msghdr msg = {};
91 msg.msg_iov = iov;
92 msg.msg_iovlen = num_iov;
93 msg.msg_control = cmsg_buf;
94 msg.msg_controllen = CMSG_LEN(num_platform_handles * sizeof(int));
95 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
96 cmsg->cmsg_level = SOL_SOCKET;
97 cmsg->cmsg_type = SCM_RIGHTS;
98 cmsg->cmsg_len = CMSG_LEN(num_platform_handles * sizeof(int));
99 for (size_t i = 0; i < num_platform_handles; i++) {
100 DCHECK(platform_handles[i].is_valid());
101 reinterpret_cast<int*>(CMSG_DATA(cmsg))[i] = platform_handles[i].fd;
102 }
103
104 return HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags));
105 }
106
PlatformChannelSendHandles(PlatformHandle h,PlatformHandle * handles,size_t num_handles)107 bool PlatformChannelSendHandles(PlatformHandle h,
108 PlatformHandle* handles,
109 size_t num_handles) {
110 DCHECK(handles);
111 DCHECK_GT(num_handles, 0u);
112 DCHECK_LE(num_handles, kPlatformChannelMaxNumHandles);
113
114 // Note: |sendmsg()| fails on Mac if we don't write at least one character.
115 struct iovec iov = {const_cast<char*>(""), 1};
116 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))];
117 struct msghdr msg = {};
118 msg.msg_iov = &iov;
119 msg.msg_iovlen = 1;
120 msg.msg_control = cmsg_buf;
121 msg.msg_controllen = CMSG_LEN(num_handles * sizeof(int));
122 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
123 cmsg->cmsg_level = SOL_SOCKET;
124 cmsg->cmsg_type = SCM_RIGHTS;
125 cmsg->cmsg_len = CMSG_LEN(num_handles * sizeof(int));
126 for (size_t i = 0; i < num_handles; i++) {
127 DCHECK(handles[i].is_valid());
128 reinterpret_cast<int*>(CMSG_DATA(cmsg))[i] = handles[i].fd;
129 }
130
131 ssize_t result = HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags));
132 if (result < 1) {
133 DCHECK_EQ(result, -1);
134 return false;
135 }
136
137 for (size_t i = 0; i < num_handles; i++)
138 handles[i].CloseIfNecessary();
139 return true;
140 }
141
PlatformChannelRecvmsg(PlatformHandle h,void * buf,size_t num_bytes,std::deque<PlatformHandle> * platform_handles)142 ssize_t PlatformChannelRecvmsg(PlatformHandle h,
143 void* buf,
144 size_t num_bytes,
145 std::deque<PlatformHandle>* platform_handles) {
146 DCHECK(buf);
147 DCHECK_GT(num_bytes, 0u);
148 DCHECK(platform_handles);
149
150 struct iovec iov = {buf, num_bytes};
151 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))];
152 struct msghdr msg = {};
153 msg.msg_iov = &iov;
154 msg.msg_iovlen = 1;
155 msg.msg_control = cmsg_buf;
156 msg.msg_controllen = sizeof(cmsg_buf);
157
158 ssize_t result = HANDLE_EINTR(recvmsg(h.fd, &msg, MSG_DONTWAIT));
159 if (result < 0)
160 return result;
161
162 // Success; no control messages.
163 if (msg.msg_controllen == 0)
164 return result;
165
166 DCHECK(!(msg.msg_flags & MSG_CTRUNC));
167
168 for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg;
169 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
170 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
171 size_t payload_length = cmsg->cmsg_len - CMSG_LEN(0);
172 DCHECK_EQ(payload_length % sizeof(int), 0u);
173 size_t num_fds = payload_length / sizeof(int);
174 const int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
175 for (size_t i = 0; i < num_fds; i++) {
176 platform_handles->push_back(PlatformHandle(fds[i]));
177 DCHECK(platform_handles->back().is_valid());
178 }
179 }
180 }
181
182 return result;
183 }
184
185 } // namespace embedder
186 } // namespace mojo
187