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) { |
151 DCHECK(buf); | 152 DCHECK(buf); |
152 DCHECK_GT(num_bytes, 0u); | 153 DCHECK_GT(num_bytes, 0u); |
153 DCHECK(platform_handles); | 154 DCHECK(platform_handles); |
154 | 155 |
155 struct iovec iov = {buf, num_bytes}; | 156 struct iovec iov = {buf, num_bytes}; |
156 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; | 157 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; |
157 struct msghdr msg = {}; | 158 struct msghdr msg = {}; |
158 msg.msg_iov = &iov; | 159 msg.msg_iov = &iov; |
159 msg.msg_iovlen = 1; | 160 msg.msg_iovlen = 1; |
160 msg.msg_control = cmsg_buf; | 161 msg.msg_control = cmsg_buf; |
161 msg.msg_controllen = sizeof(cmsg_buf); | 162 msg.msg_controllen = sizeof(cmsg_buf); |
162 | 163 |
163 // We use SO_PEEK_OFF to hold a common identifier between sockets to detect if | 164 // We use SO_PEEK_OFF to hold a common identifier between sockets to detect if |
164 // they're connected. recvmsg modifies it, so we cache it and set it again | 165 // they're connected. recvmsg modifies it, so we cache it and set it again |
165 // after the call. | 166 // after the call. |
166 int id = 0; | 167 int id = 0; |
167 socklen_t peek_off_size = sizeof(id); | 168 socklen_t peek_off_size = sizeof(id); |
168 getsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size); | 169 getsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size); |
169 ssize_t result = HANDLE_EINTR(recvmsg(h.handle, &msg, MSG_DONTWAIT)); | 170 ssize_t result = |
| 171 HANDLE_EINTR(recvmsg(h.handle, &msg, should_block ? 0 : MSG_DONTWAIT)); |
170 if (result < 0) | 172 if (result < 0) |
171 return result; | 173 return result; |
172 | 174 |
173 setsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id)); | 175 setsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id)); |
174 | 176 |
175 // Success; no control messages. | 177 // Success; no control messages. |
176 if (msg.msg_controllen == 0) | 178 if (msg.msg_controllen == 0) |
177 return result; | 179 return result; |
178 | 180 |
179 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); | 181 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); |
(...skipping 10 matching lines...) Expand all Loading... |
190 DCHECK(platform_handles->back().is_valid()); | 192 DCHECK(platform_handles->back().is_valid()); |
191 } | 193 } |
192 } | 194 } |
193 } | 195 } |
194 | 196 |
195 return result; | 197 return result; |
196 } | 198 } |
197 | 199 |
198 } // namespace edk | 200 } // namespace edk |
199 } // namespace mojo | 201 } // namespace mojo |
OLD | NEW |