OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/embedder/platform_channel_utils_posix.h" | 5 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/uio.h> | 9 #include <sys/uio.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 } | 140 } |
141 | 141 |
142 for (size_t i = 0; i < num_handles; i++) | 142 for (size_t i = 0; i < num_handles; i++) |
143 handles[i].CloseIfNecessary(); | 143 handles[i].CloseIfNecessary(); |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 ssize_t PlatformChannelRecvmsg(PlatformHandle h, | 147 ssize_t PlatformChannelRecvmsg(PlatformHandle h, |
148 void* buf, | 148 void* buf, |
149 size_t num_bytes, | 149 size_t num_bytes, |
150 std::deque<PlatformHandle>* platform_handles, | 150 std::deque<PlatformHandle>* platform_handles) { |
151 bool should_block) { | |
152 DCHECK(buf); | 151 DCHECK(buf); |
153 DCHECK_GT(num_bytes, 0u); | 152 DCHECK_GT(num_bytes, 0u); |
154 DCHECK(platform_handles); | 153 DCHECK(platform_handles); |
155 | 154 |
156 struct iovec iov = {buf, num_bytes}; | 155 struct iovec iov = {buf, num_bytes}; |
157 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; | 156 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; |
158 struct msghdr msg = {}; | 157 struct msghdr msg = {}; |
159 msg.msg_iov = &iov; | 158 msg.msg_iov = &iov; |
160 msg.msg_iovlen = 1; | 159 msg.msg_iovlen = 1; |
161 msg.msg_control = cmsg_buf; | 160 msg.msg_control = cmsg_buf; |
162 msg.msg_controllen = sizeof(cmsg_buf); | 161 msg.msg_controllen = sizeof(cmsg_buf); |
163 | 162 |
164 // We use SO_PEEK_OFF to hold a common identifier between sockets to detect if | 163 // We use SO_PEEK_OFF to hold a common identifier between sockets to detect if |
165 // they're connected. recvmsg modifies it, so we cache it and set it again | 164 // they're connected. recvmsg modifies it, so we cache it and set it again |
166 // after the call. | 165 // after the call. |
167 int id = 0; | 166 int id = 0; |
168 socklen_t peek_off_size = sizeof(id); | 167 socklen_t peek_off_size = sizeof(id); |
169 getsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size); | 168 getsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size); |
170 ssize_t result = | 169 ssize_t result = HANDLE_EINTR(recvmsg(h.handle, &msg, MSG_DONTWAIT)); |
171 HANDLE_EINTR(recvmsg(h.handle, &msg, should_block ? 0 : MSG_DONTWAIT)); | |
172 if (result < 0) | 170 if (result < 0) |
173 return result; | 171 return result; |
174 | 172 |
175 setsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id)); | 173 setsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id)); |
176 | 174 |
177 // Success; no control messages. | 175 // Success; no control messages. |
178 if (msg.msg_controllen == 0) | 176 if (msg.msg_controllen == 0) |
179 return result; | 177 return result; |
180 | 178 |
181 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); | 179 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); |
(...skipping 10 matching lines...) Expand all Loading... |
192 DCHECK(platform_handles->back().is_valid()); | 190 DCHECK(platform_handles->back().is_valid()); |
193 } | 191 } |
194 } | 192 } |
195 } | 193 } |
196 | 194 |
197 return result; | 195 return result; |
198 } | 196 } |
199 | 197 |
200 } // namespace edk | 198 } // namespace edk |
201 } // namespace mojo | 199 } // namespace mojo |
OLD | NEW |