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" | |
16 #include "base/pickle.h" | 15 #include "base/pickle.h" |
17 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
18 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
19 | 18 |
20 #if !defined(OS_NACL_NONSFI) | 19 #if !defined(OS_NACL_NONSFI) |
21 #include <sys/uio.h> | 20 #include <sys/uio.h> |
22 #endif | 21 #endif |
23 | 22 |
24 namespace base { | 23 namespace base { |
25 | 24 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); | 78 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); |
80 const bool ret = static_cast<ssize_t>(length) == r; | 79 const bool ret = static_cast<ssize_t>(length) == r; |
81 delete[] control_buffer; | 80 delete[] control_buffer; |
82 return ret; | 81 return ret; |
83 } | 82 } |
84 | 83 |
85 // static | 84 // static |
86 ssize_t UnixDomainSocket::RecvMsg(int fd, | 85 ssize_t UnixDomainSocket::RecvMsg(int fd, |
87 void* buf, | 86 void* buf, |
88 size_t length, | 87 size_t length, |
89 ScopedVector<ScopedFD>* fds) { | 88 std::vector<ScopedFD>* fds) { |
90 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); | 89 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); |
91 } | 90 } |
92 | 91 |
93 // static | 92 // static |
94 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, | 93 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, |
95 void* buf, | 94 void* buf, |
96 size_t length, | 95 size_t length, |
97 ScopedVector<ScopedFD>* fds, | 96 std::vector<ScopedFD>* fds, |
98 ProcessId* pid) { | 97 ProcessId* pid) { |
99 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); | 98 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); |
100 } | 99 } |
101 | 100 |
102 // static | 101 // static |
103 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, | 102 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, |
104 void* buf, | 103 void* buf, |
105 size_t length, | 104 size_t length, |
106 int flags, | 105 int flags, |
107 ScopedVector<ScopedFD>* fds, | 106 std::vector<ScopedFD>* fds, |
108 ProcessId* out_pid) { | 107 ProcessId* out_pid) { |
109 fds->clear(); | 108 fds->clear(); |
110 | 109 |
111 struct msghdr msg = {}; | 110 struct msghdr msg = {}; |
112 struct iovec iov = { buf, length }; | 111 struct iovec iov = { buf, length }; |
113 msg.msg_iov = &iov; | 112 msg.msg_iov = &iov; |
114 msg.msg_iovlen = 1; | 113 msg.msg_iovlen = 1; |
115 | 114 |
116 const size_t kControlBufferSize = | 115 const size_t kControlBufferSize = |
117 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) | 116 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 | 157 |
159 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | 158 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { |
160 for (unsigned i = 0; i < wire_fds_len; ++i) | 159 for (unsigned i = 0; i < wire_fds_len; ++i) |
161 close(wire_fds[i]); | 160 close(wire_fds[i]); |
162 errno = EMSGSIZE; | 161 errno = EMSGSIZE; |
163 return -1; | 162 return -1; |
164 } | 163 } |
165 | 164 |
166 if (wire_fds) { | 165 if (wire_fds) { |
167 for (unsigned i = 0; i < wire_fds_len; ++i) | 166 for (unsigned i = 0; i < wire_fds_len; ++i) |
168 fds->push_back(new ScopedFD(wire_fds[i])); | 167 fds->push_back(ScopedFD(wire_fds[i])); // TODO(mdempsky): emplace_back |
169 } | 168 } |
170 | 169 |
171 if (out_pid) { | 170 if (out_pid) { |
172 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we | 171 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we |
173 // actually received a message. Unfortunately, Linux allows sending zero | 172 // actually received a message. Unfortunately, Linux allows sending zero |
174 // length messages, which are indistinguishable from EOF, so this check | 173 // length messages, which are indistinguishable from EOF, so this check |
175 // has false negatives. | 174 // has false negatives. |
176 if (r > 0 || msg.msg_controllen > 0) | 175 if (r > 0 || msg.msg_controllen > 0) |
177 DCHECK_GE(pid, 0); | 176 DCHECK_GE(pid, 0); |
178 | 177 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 send_fds.push_back(send_sock.get()); | 211 send_fds.push_back(send_sock.get()); |
213 if (!SendMsg(fd, request.data(), request.size(), send_fds)) | 212 if (!SendMsg(fd, request.data(), request.size(), send_fds)) |
214 return -1; | 213 return -1; |
215 } | 214 } |
216 | 215 |
217 // Close the sending end of the socket right away so that if our peer closes | 216 // Close the sending end of the socket right away so that if our peer closes |
218 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will | 217 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will |
219 // return EOF instead of hanging. | 218 // return EOF instead of hanging. |
220 send_sock.reset(); | 219 send_sock.reset(); |
221 | 220 |
222 ScopedVector<ScopedFD> recv_fds; | 221 std::vector<ScopedFD> recv_fds; |
223 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 222 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
224 // sender might get a SIGPIPE. | 223 // sender might get a SIGPIPE. |
225 const ssize_t reply_len = RecvMsgWithFlags( | 224 const ssize_t reply_len = RecvMsgWithFlags( |
226 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); | 225 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); |
227 recv_sock.reset(); | 226 recv_sock.reset(); |
228 if (reply_len == -1) | 227 if (reply_len == -1) |
229 return -1; | 228 return -1; |
230 | 229 |
231 // If we received more file descriptors than caller expected, then we treat | 230 // If we received more file descriptors than caller expected, then we treat |
232 // that as an error. | 231 // that as an error. |
233 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { | 232 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
234 NOTREACHED(); | 233 NOTREACHED(); |
235 return -1; | 234 return -1; |
236 } | 235 } |
237 | 236 |
238 if (result_fd) | 237 if (result_fd) |
239 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); | 238 *result_fd = recv_fds.empty() ? -1 : recv_fds.at(0).release(); |
jln (very slow on Chromium)
2015/12/08 23:06:24
Is [] to at() conversion just to be paranoid or am
mdempsky
2015/12/08 23:13:55
Yeah, just paranoia. Reverted.
| |
240 | 239 |
241 return reply_len; | 240 return reply_len; |
242 } | 241 } |
243 #endif // !defined(OS_NACL_NONSFI) | 242 #endif // !defined(OS_NACL_NONSFI) |
244 | 243 |
245 } // namespace base | 244 } // namespace base |
OLD | NEW |