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