Chromium Code Reviews| 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/embedder/platform_channel_utils_posix.h" | 5 #include "mojo/embedder/platform_channel_utils_posix.h" |
| 6 | 6 |
| 7 #include <sys/socket.h> | 7 #include <sys/socket.h> |
| 8 #include <sys/uio.h> | 8 #include <sys/uio.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 // Flags to use with calling |send()| or |sendmsg()| (see above). | 40 // Flags to use with calling |send()| or |sendmsg()| (see above). |
| 41 #if defined(OS_MACOSX) | 41 #if defined(OS_MACOSX) |
| 42 const int kSendFlags = 0; | 42 const int kSendFlags = 0; |
| 43 #else | 43 #else |
| 44 const int kSendFlags = MSG_NOSIGNAL; | 44 const int kSendFlags = MSG_NOSIGNAL; |
| 45 #endif | 45 #endif |
| 46 | 46 |
| 47 ssize_t PlatformChannelWrite(PlatformHandle h, | 47 ssize_t PlatformChannelWrite(PlatformHandle h, |
| 48 const void* bytes, | 48 const void* bytes, |
| 49 size_t num_bytes) { | 49 size_t num_bytes) { |
| 50 DCHECK(h.is_valid()); | |
| 51 DCHECK(bytes); | |
| 52 DCHECK_GT(num_bytes, 0u); | |
| 53 | |
| 50 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
| 51 return HANDLE_EINTR(write(h.fd, bytes, num_bytes)); | 55 return HANDLE_EINTR(write(h.fd, bytes, num_bytes)); |
| 52 #else | 56 #else |
| 53 return send(h.fd, bytes, num_bytes, kSendFlags); | 57 return send(h.fd, bytes, num_bytes, kSendFlags); |
| 54 #endif | 58 #endif |
| 55 } | 59 } |
| 56 | 60 |
| 57 ssize_t PlatformChannelWritev(PlatformHandle h, | 61 ssize_t PlatformChannelWritev(PlatformHandle h, |
| 58 struct iovec* iov, | 62 struct iovec* iov, |
| 59 size_t num_iov) { | 63 size_t num_iov) { |
| 64 DCHECK(h.is_valid()); | |
| 65 DCHECK(iov); | |
| 66 DCHECK_GT(num_iov, 0u); | |
| 67 | |
| 60 #if defined(OS_MACOSX) | 68 #if defined(OS_MACOSX) |
| 61 return HANDLE_EINTR(writev(h.fd, iov, static_cast<int>(num_iov))); | 69 return HANDLE_EINTR(writev(h.fd, iov, static_cast<int>(num_iov))); |
| 62 #else | 70 #else |
| 63 struct msghdr msg = {}; | 71 struct msghdr msg = {}; |
| 64 msg.msg_iov = iov; | 72 msg.msg_iov = iov; |
| 65 msg.msg_iovlen = num_iov; | 73 msg.msg_iovlen = num_iov; |
| 66 return HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags)); | 74 return HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags)); |
| 67 #endif | 75 #endif |
| 68 } | 76 } |
| 69 | 77 |
| 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); | |
|
yzshen1
2014/05/21 23:50:02
nit: It might be good to also DCHECK that it is no
viettrungluu
2014/05/22 00:21:44
Done.
| |
| 87 | |
| 88 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; | |
| 89 struct msghdr msg = {}; | |
| 90 msg.msg_iov = iov; | |
| 91 msg.msg_iovlen = num_iov; | |
| 92 msg.msg_control = cmsg_buf; | |
| 93 msg.msg_controllen = CMSG_LEN(num_platform_handles * sizeof(int)); | |
| 94 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); | |
| 95 cmsg->cmsg_level = SOL_SOCKET; | |
| 96 cmsg->cmsg_type = SCM_RIGHTS; | |
| 97 cmsg->cmsg_len = CMSG_LEN(num_platform_handles * sizeof(int)); | |
| 98 for (size_t i = 0; i < num_platform_handles; i++) { | |
| 99 DCHECK(platform_handles[i].is_valid()); | |
| 100 reinterpret_cast<int*>(CMSG_DATA(cmsg))[i] = platform_handles[i].fd; | |
| 101 } | |
| 102 | |
| 103 return HANDLE_EINTR(sendmsg(h.fd, &msg, kSendFlags)); | |
| 104 } | |
| 105 | |
| 70 bool PlatformChannelSendHandles(PlatformHandle h, | 106 bool PlatformChannelSendHandles(PlatformHandle h, |
| 71 PlatformHandle* handles, | 107 PlatformHandle* handles, |
| 72 size_t num_handles) { | 108 size_t num_handles) { |
| 73 DCHECK(handles); | 109 DCHECK(handles); |
| 74 DCHECK_GT(num_handles, 0u); | 110 DCHECK_GT(num_handles, 0u); |
| 75 DCHECK_LE(num_handles, kPlatformChannelMaxNumHandles); | 111 DCHECK_LE(num_handles, kPlatformChannelMaxNumHandles); |
| 76 | 112 |
| 77 // Note: |sendmsg()| fails on Mac if we don't write at least one character. | 113 // Note: |sendmsg()| fails on Mac if we don't write at least one character. |
| 78 struct iovec iov = { const_cast<char*>(""), 1 }; | 114 struct iovec iov = { const_cast<char*>(""), 1 }; |
| 79 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; | 115 char cmsg_buf[CMSG_SPACE(kPlatformChannelMaxNumHandles * sizeof(int))]; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 DCHECK(platform_handles->back().is_valid()); | 177 DCHECK(platform_handles->back().is_valid()); |
| 142 } | 178 } |
| 143 } | 179 } |
| 144 } | 180 } |
| 145 | 181 |
| 146 return result; | 182 return result; |
| 147 } | 183 } |
| 148 | 184 |
| 149 } // namespace embedder | 185 } // namespace embedder |
| 150 } // namespace mojo | 186 } // namespace mojo |
| OLD | NEW |