Chromium Code Reviews| 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> | 9 #include <sys/uio.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/files/scoped_file.h" | |
| 12 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_vector.h" | |
| 13 #include "base/pickle.h" | 17 #include "base/pickle.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 16 | 20 |
| 17 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; | 21 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; |
| 18 | 22 |
| 23 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes | |
| 24 // ownership of the newly allocated file descriptors to |one| and |two|. | |
| 25 // Returns true on success. | |
| 26 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) { | |
| 27 int raw_socks[2]; | |
| 28 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) | |
| 29 return false; | |
| 30 one->reset(raw_socks[0]); | |
| 31 two->reset(raw_socks[1]); | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 19 // static | 35 // static |
| 20 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { | 36 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { |
| 21 const int enable = 1; | 37 const int enable = 1; |
| 22 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; | 38 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; |
| 23 } | 39 } |
| 24 | 40 |
| 25 // static | 41 // static |
| 26 bool UnixDomainSocket::SendMsg(int fd, | 42 bool UnixDomainSocket::SendMsg(int fd, |
| 27 const void* buf, | 43 const void* buf, |
| 28 size_t length, | 44 size_t length, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 56 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); | 72 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); |
| 57 const bool ret = static_cast<ssize_t>(length) == r; | 73 const bool ret = static_cast<ssize_t>(length) == r; |
| 58 delete[] control_buffer; | 74 delete[] control_buffer; |
| 59 return ret; | 75 return ret; |
| 60 } | 76 } |
| 61 | 77 |
| 62 // static | 78 // static |
| 63 ssize_t UnixDomainSocket::RecvMsg(int fd, | 79 ssize_t UnixDomainSocket::RecvMsg(int fd, |
| 64 void* buf, | 80 void* buf, |
| 65 size_t length, | 81 size_t length, |
| 66 std::vector<int>* fds) { | 82 ScopedVector<base::ScopedFD>* fds) { |
| 67 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); | 83 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); |
| 68 } | 84 } |
| 69 | 85 |
| 70 // static | 86 // static |
| 71 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, | 87 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, |
| 72 void* buf, | 88 void* buf, |
| 73 size_t length, | 89 size_t length, |
| 74 std::vector<int>* fds, | 90 ScopedVector<base::ScopedFD>* fds, |
| 75 base::ProcessId* pid) { | 91 base::ProcessId* pid) { |
| 76 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); | 92 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); |
| 77 } | 93 } |
| 78 | 94 |
| 79 // static | 95 // static |
| 80 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, | 96 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, |
| 81 void* buf, | 97 void* buf, |
| 82 size_t length, | 98 size_t length, |
| 83 int flags, | 99 int flags, |
| 84 std::vector<int>* fds, | 100 ScopedVector<base::ScopedFD>* fds, |
| 85 base::ProcessId* out_pid) { | 101 base::ProcessId* out_pid) { |
| 86 fds->clear(); | 102 fds->clear(); |
| 87 | 103 |
| 88 struct msghdr msg = {}; | 104 struct msghdr msg = {}; |
| 89 struct iovec iov = { buf, length }; | 105 struct iovec iov = { buf, length }; |
| 90 msg.msg_iov = &iov; | 106 msg.msg_iov = &iov; |
| 91 msg.msg_iovlen = 1; | 107 msg.msg_iovlen = 1; |
| 92 | 108 |
| 93 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) + | 109 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) + |
| 94 CMSG_SPACE(sizeof(struct ucred))]; | 110 CMSG_SPACE(sizeof(struct ucred))]; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 124 } | 140 } |
| 125 | 141 |
| 126 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | 142 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { |
| 127 for (unsigned i = 0; i < wire_fds_len; ++i) | 143 for (unsigned i = 0; i < wire_fds_len; ++i) |
| 128 close(wire_fds[i]); | 144 close(wire_fds[i]); |
| 129 errno = EMSGSIZE; | 145 errno = EMSGSIZE; |
| 130 return -1; | 146 return -1; |
| 131 } | 147 } |
| 132 | 148 |
| 133 if (wire_fds) { | 149 if (wire_fds) { |
| 134 fds->resize(wire_fds_len); | 150 DCHECK(fds->empty()); |
|
brettw
2014/04/28 21:57:02
Weighing in on this, I think we should remove both
mdempsky
2014/04/28 22:01:52
Done.
| |
| 135 memcpy(vector_as_array(fds), wire_fds, sizeof(int) * wire_fds_len); | 151 fds->reserve(wire_fds_len); |
| 152 for (unsigned i = 0; i < wire_fds_len; ++i) | |
| 153 fds->push_back(new base::ScopedFD(wire_fds[i])); | |
| 136 } | 154 } |
| 137 | 155 |
| 138 if (out_pid) { | 156 if (out_pid) { |
| 139 DCHECK(pid != -1); | 157 DCHECK(pid != -1); |
| 140 *out_pid = pid; | 158 *out_pid = pid; |
| 141 } | 159 } |
| 142 | 160 |
| 143 return r; | 161 return r; |
| 144 } | 162 } |
| 145 | 163 |
| 146 // static | 164 // static |
| 147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 165 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
| 148 uint8_t* reply, | 166 uint8_t* reply, |
| 149 unsigned max_reply_len, | 167 unsigned max_reply_len, |
| 150 int* result_fd, | 168 int* result_fd, |
| 151 const Pickle& request) { | 169 const Pickle& request) { |
| 152 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 170 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, |
| 153 0, /* recvmsg_flags */ | 171 0, /* recvmsg_flags */ |
| 154 result_fd, request); | 172 result_fd, request); |
| 155 } | 173 } |
| 156 | 174 |
| 157 // static | 175 // static |
| 158 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, | 176 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, |
| 159 uint8_t* reply, | 177 uint8_t* reply, |
| 160 unsigned max_reply_len, | 178 unsigned max_reply_len, |
| 161 int recvmsg_flags, | 179 int recvmsg_flags, |
| 162 int* result_fd, | 180 int* result_fd, |
| 163 const Pickle& request) { | 181 const Pickle& request) { |
| 164 int fds[2]; | |
| 165 | |
| 166 // This socketpair is only used for the IPC and is cleaned up before | 182 // This socketpair is only used for the IPC and is cleaned up before |
| 167 // returning. | 183 // returning. |
| 168 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == -1) | 184 base::ScopedFD recv_sock, send_sock; |
| 185 if (!CreateSocketPair(&recv_sock, &send_sock)) | |
| 169 return -1; | 186 return -1; |
| 170 | 187 |
| 171 std::vector<int> fd_vector; | 188 { |
| 172 fd_vector.push_back(fds[1]); | 189 std::vector<int> send_fds; |
| 173 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | 190 send_fds.push_back(send_sock.get()); |
| 174 close(fds[0]); | 191 if (!SendMsg(fd, request.data(), request.size(), send_fds)) |
| 175 close(fds[1]); | 192 return -1; |
| 176 return -1; | |
| 177 } | 193 } |
| 178 close(fds[1]); | |
| 179 | 194 |
| 180 fd_vector.clear(); | 195 // Close the sending end of the socket right away so that if our peer closes |
| 196 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will | |
| 197 // return EOF instead of hanging. | |
| 198 send_sock.reset(); | |
| 199 | |
| 200 ScopedVector<base::ScopedFD> recv_fds; | |
| 181 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 201 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
| 182 // sender might get a SIGPIPE. | 202 // sender might get a SIGPIPE. |
| 183 const ssize_t reply_len = RecvMsgWithFlags( | 203 const ssize_t reply_len = RecvMsgWithFlags( |
| 184 fds[0], reply, max_reply_len, recvmsg_flags, &fd_vector, NULL); | 204 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); |
| 185 close(fds[0]); | 205 recv_sock.reset(); |
| 186 if (reply_len == -1) | 206 if (reply_len == -1) |
| 187 return -1; | 207 return -1; |
| 188 | 208 |
| 189 if ((!fd_vector.empty() && result_fd == NULL) || fd_vector.size() > 1) { | 209 // If we received more file descriptors than caller expected, then we treat |
| 190 for (std::vector<int>::const_iterator | 210 // that as an error. |
| 191 i = fd_vector.begin(); i != fd_vector.end(); ++i) { | 211 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
| 192 close(*i); | |
| 193 } | |
| 194 | |
| 195 NOTREACHED(); | 212 NOTREACHED(); |
| 196 | |
| 197 return -1; | 213 return -1; |
| 198 } | 214 } |
| 199 | 215 |
| 200 if (result_fd) | 216 if (result_fd) |
| 201 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; | 217 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); |
| 202 | 218 |
| 203 return reply_len; | 219 return reply_len; |
| 204 } | 220 } |
| OLD | NEW |