| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/posix/unix_domain_socket_linux.h" | 5 #include "base/posix/unix_domain_socket_linux.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 #include <sys/uio.h> | |
| 10 #include <unistd.h> | 9 #include <unistd.h> |
| 11 | 10 |
| 12 #include <vector> | 11 #include <vector> |
| 13 | 12 |
| 14 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 17 #include "base/pickle.h" | 16 #include "base/pickle.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 20 | 19 |
| 20 #if !defined(__native_client_nonsfi__) |
| 21 #include <sys/uio.h> |
| 22 #endif |
| 23 |
| 21 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; | 24 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; |
| 22 | 25 |
| 26 #if !defined(__native_client_nonsfi__) |
| 23 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes | 27 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes |
| 24 // ownership of the newly allocated file descriptors to |one| and |two|. | 28 // ownership of the newly allocated file descriptors to |one| and |two|. |
| 25 // Returns true on success. | 29 // Returns true on success. |
| 26 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) { | 30 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) { |
| 27 int raw_socks[2]; | 31 int raw_socks[2]; |
| 28 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) | 32 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) |
| 29 return false; | 33 return false; |
| 30 one->reset(raw_socks[0]); | 34 one->reset(raw_socks[0]); |
| 31 two->reset(raw_socks[1]); | 35 two->reset(raw_socks[1]); |
| 32 return true; | 36 return true; |
| 33 } | 37 } |
| 34 | 38 |
| 35 // static | 39 // static |
| 36 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { | 40 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { |
| 37 const int enable = 1; | 41 const int enable = 1; |
| 38 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; | 42 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; |
| 39 } | 43 } |
| 44 #endif // !defined(__native_client_nonsfi__) |
| 40 | 45 |
| 41 // static | 46 // static |
| 42 bool UnixDomainSocket::SendMsg(int fd, | 47 bool UnixDomainSocket::SendMsg(int fd, |
| 43 const void* buf, | 48 const void* buf, |
| 44 size_t length, | 49 size_t length, |
| 45 const std::vector<int>& fds) { | 50 const std::vector<int>& fds) { |
| 46 struct msghdr msg = {}; | 51 struct msghdr msg = {}; |
| 47 struct iovec iov = { const_cast<void*>(buf), length }; | 52 struct iovec iov = { const_cast<void*>(buf), length }; |
| 48 msg.msg_iov = &iov; | 53 msg.msg_iov = &iov; |
| 49 msg.msg_iovlen = 1; | 54 msg.msg_iovlen = 1; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 int flags, | 104 int flags, |
| 100 ScopedVector<base::ScopedFD>* fds, | 105 ScopedVector<base::ScopedFD>* fds, |
| 101 base::ProcessId* out_pid) { | 106 base::ProcessId* out_pid) { |
| 102 fds->clear(); | 107 fds->clear(); |
| 103 | 108 |
| 104 struct msghdr msg = {}; | 109 struct msghdr msg = {}; |
| 105 struct iovec iov = { buf, length }; | 110 struct iovec iov = { buf, length }; |
| 106 msg.msg_iov = &iov; | 111 msg.msg_iov = &iov; |
| 107 msg.msg_iovlen = 1; | 112 msg.msg_iovlen = 1; |
| 108 | 113 |
| 109 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) + | 114 const size_t kControlBufferSize = |
| 110 CMSG_SPACE(sizeof(struct ucred))]; | 115 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) |
| 116 #if !defined(__native_client_nonsfi__) |
| 117 // The PNaCl toolchain for Non-SFI binary build does not support ucred. |
| 118 + CMSG_SPACE(sizeof(struct ucred)) |
| 119 #endif |
| 120 ; |
| 121 char control_buffer[kControlBufferSize]; |
| 111 msg.msg_control = control_buffer; | 122 msg.msg_control = control_buffer; |
| 112 msg.msg_controllen = sizeof(control_buffer); | 123 msg.msg_controllen = sizeof(control_buffer); |
| 113 | 124 |
| 114 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags)); | 125 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags)); |
| 115 if (r == -1) | 126 if (r == -1) |
| 116 return -1; | 127 return -1; |
| 117 | 128 |
| 118 int* wire_fds = NULL; | 129 int* wire_fds = NULL; |
| 119 unsigned wire_fds_len = 0; | 130 unsigned wire_fds_len = 0; |
| 120 base::ProcessId pid = -1; | 131 base::ProcessId pid = -1; |
| 121 | 132 |
| 122 if (msg.msg_controllen > 0) { | 133 if (msg.msg_controllen > 0) { |
| 123 struct cmsghdr* cmsg; | 134 struct cmsghdr* cmsg; |
| 124 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 135 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 125 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | 136 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 126 if (cmsg->cmsg_level == SOL_SOCKET && | 137 if (cmsg->cmsg_level == SOL_SOCKET && |
| 127 cmsg->cmsg_type == SCM_RIGHTS) { | 138 cmsg->cmsg_type == SCM_RIGHTS) { |
| 128 DCHECK(payload_len % sizeof(int) == 0); | 139 DCHECK(payload_len % sizeof(int) == 0); |
| 129 DCHECK(wire_fds == NULL); | 140 DCHECK(wire_fds == NULL); |
| 130 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 141 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 131 wire_fds_len = payload_len / sizeof(int); | 142 wire_fds_len = payload_len / sizeof(int); |
| 132 } | 143 } |
| 144 #if !defined(__native_client_nonsfi__) |
| 145 // The PNaCl toolchain for Non-SFI binary build does not support |
| 146 // SCM_CREDENTIALS. |
| 133 if (cmsg->cmsg_level == SOL_SOCKET && | 147 if (cmsg->cmsg_level == SOL_SOCKET && |
| 134 cmsg->cmsg_type == SCM_CREDENTIALS) { | 148 cmsg->cmsg_type == SCM_CREDENTIALS) { |
| 135 DCHECK(payload_len == sizeof(struct ucred)); | 149 DCHECK(payload_len == sizeof(struct ucred)); |
| 136 DCHECK(pid == -1); | 150 DCHECK(pid == -1); |
| 137 pid = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsg))->pid; | 151 pid = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsg))->pid; |
| 138 } | 152 } |
| 153 #endif |
| 139 } | 154 } |
| 140 } | 155 } |
| 141 | 156 |
| 157 #if !defined(__native_client_nonsfi__) |
| 158 // The PNaCl toolchain for Non-SFI binary build does not support |
| 159 // MSG_TRUNC nor MSG_CTRUNC. |
| 142 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | 160 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { |
| 143 for (unsigned i = 0; i < wire_fds_len; ++i) | 161 for (unsigned i = 0; i < wire_fds_len; ++i) |
| 144 close(wire_fds[i]); | 162 close(wire_fds[i]); |
| 145 errno = EMSGSIZE; | 163 errno = EMSGSIZE; |
| 146 return -1; | 164 return -1; |
| 147 } | 165 } |
| 166 #endif |
| 148 | 167 |
| 149 if (wire_fds) { | 168 if (wire_fds) { |
| 150 for (unsigned i = 0; i < wire_fds_len; ++i) | 169 for (unsigned i = 0; i < wire_fds_len; ++i) |
| 151 fds->push_back(new base::ScopedFD(wire_fds[i])); | 170 fds->push_back(new base::ScopedFD(wire_fds[i])); |
| 152 } | 171 } |
| 153 | 172 |
| 154 if (out_pid) { | 173 if (out_pid) { |
| 155 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we | 174 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we |
| 156 // actually received a message. Unfortunately, Linux allows sending zero | 175 // actually received a message. Unfortunately, Linux allows sending zero |
| 157 // length messages, which are indistinguishable from EOF, so this check | 176 // length messages, which are indistinguishable from EOF, so this check |
| 158 // has false negatives. | 177 // has false negatives. |
| 159 if (r > 0 || msg.msg_controllen > 0) | 178 if (r > 0 || msg.msg_controllen > 0) |
| 160 DCHECK_GE(pid, 0); | 179 DCHECK_GE(pid, 0); |
| 161 | 180 |
| 162 *out_pid = pid; | 181 *out_pid = pid; |
| 163 } | 182 } |
| 164 | 183 |
| 165 return r; | 184 return r; |
| 166 } | 185 } |
| 167 | 186 |
| 187 #if !defined(__native_client_nonsfi__) |
| 168 // static | 188 // static |
| 169 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 189 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
| 170 uint8_t* reply, | 190 uint8_t* reply, |
| 171 unsigned max_reply_len, | 191 unsigned max_reply_len, |
| 172 int* result_fd, | 192 int* result_fd, |
| 173 const Pickle& request) { | 193 const Pickle& request) { |
| 174 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 194 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, |
| 175 0, /* recvmsg_flags */ | 195 0, /* recvmsg_flags */ |
| 176 result_fd, request); | 196 result_fd, request); |
| 177 } | 197 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { | 235 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
| 216 NOTREACHED(); | 236 NOTREACHED(); |
| 217 return -1; | 237 return -1; |
| 218 } | 238 } |
| 219 | 239 |
| 220 if (result_fd) | 240 if (result_fd) |
| 221 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); | 241 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); |
| 222 | 242 |
| 223 return reply_len; | 243 return reply_len; |
| 224 } | 244 } |
| 245 #endif // !defined(__native_client_nonsfi__) |
| OLD | NEW |