| 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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 DCHECK(platform_handles); | 153 DCHECK(platform_handles); |
| 154 | 154 |
| 155 struct iovec iov = {buf, num_bytes}; | 155 struct iovec iov = {buf, num_bytes}; |
| 156 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; | 156 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; |
| 157 struct msghdr msg = {}; | 157 struct msghdr msg = {}; |
| 158 msg.msg_iov = &iov; | 158 msg.msg_iov = &iov; |
| 159 msg.msg_iovlen = 1; | 159 msg.msg_iovlen = 1; |
| 160 msg.msg_control = cmsg_buf; | 160 msg.msg_control = cmsg_buf; |
| 161 msg.msg_controllen = sizeof(cmsg_buf); | 161 msg.msg_controllen = sizeof(cmsg_buf); |
| 162 | 162 |
| 163 // 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 // after the call. | |
| 166 int id = 0; | |
| 167 socklen_t peek_off_size = sizeof(id); | |
| 168 getsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, &peek_off_size); | |
| 169 ssize_t result = HANDLE_EINTR(recvmsg(h.handle, &msg, MSG_DONTWAIT)); | 163 ssize_t result = HANDLE_EINTR(recvmsg(h.handle, &msg, MSG_DONTWAIT)); |
| 170 if (result < 0) | 164 if (result < 0) |
| 171 return result; | 165 return result; |
| 172 | 166 |
| 173 setsockopt(h.handle, SOL_SOCKET, SO_PEEK_OFF, &id, sizeof(id)); | |
| 174 | |
| 175 // Success; no control messages. | 167 // Success; no control messages. |
| 176 if (msg.msg_controllen == 0) | 168 if (msg.msg_controllen == 0) |
| 177 return result; | 169 return result; |
| 178 | 170 |
| 179 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); | 171 DCHECK(!(msg.msg_flags & MSG_CTRUNC)); |
| 180 | 172 |
| 181 for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; | 173 for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| 182 cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 174 cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 183 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { | 175 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 184 size_t payload_length = cmsg->cmsg_len - CMSG_LEN(0); | 176 size_t payload_length = cmsg->cmsg_len - CMSG_LEN(0); |
| 185 DCHECK_EQ(payload_length % sizeof(int), 0u); | 177 DCHECK_EQ(payload_length % sizeof(int), 0u); |
| 186 size_t num_fds = payload_length / sizeof(int); | 178 size_t num_fds = payload_length / sizeof(int); |
| 187 const int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 179 const int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 188 for (size_t i = 0; i < num_fds; i++) { | 180 for (size_t i = 0; i < num_fds; i++) { |
| 189 platform_handles->push_back(PlatformHandle(fds[i])); | 181 platform_handles->push_back(PlatformHandle(fds[i])); |
| 190 DCHECK(platform_handles->back().is_valid()); | 182 DCHECK(platform_handles->back().is_valid()); |
| 191 } | 183 } |
| 192 } | 184 } |
| 193 } | 185 } |
| 194 | 186 |
| 195 return result; | 187 return result; |
| 196 } | 188 } |
| 197 | 189 |
| 198 } // namespace edk | 190 } // namespace edk |
| 199 } // namespace mojo | 191 } // namespace mojo |
| OLD | NEW |