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(OS_NACL_NONSFI) | 20 #if !defined(OS_NACL_NONSFI) |
21 #include <sys/uio.h> | 21 #include <sys/uio.h> |
22 #endif | 22 #endif |
23 | 23 |
| 24 namespace base { |
| 25 |
24 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; | 26 const size_t UnixDomainSocket::kMaxFileDescriptors = 16; |
25 | 27 |
26 #if !defined(OS_NACL_NONSFI) | 28 #if !defined(OS_NACL_NONSFI) |
27 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes | 29 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes |
28 // ownership of the newly allocated file descriptors to |one| and |two|. | 30 // ownership of the newly allocated file descriptors to |one| and |two|. |
29 // Returns true on success. | 31 // Returns true on success. |
30 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) { | 32 static bool CreateSocketPair(ScopedFD* one, ScopedFD* two) { |
31 int raw_socks[2]; | 33 int raw_socks[2]; |
32 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) | 34 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) |
33 return false; | 35 return false; |
34 one->reset(raw_socks[0]); | 36 one->reset(raw_socks[0]); |
35 two->reset(raw_socks[1]); | 37 two->reset(raw_socks[1]); |
36 return true; | 38 return true; |
37 } | 39 } |
38 | 40 |
39 // static | 41 // static |
40 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { | 42 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); | 79 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); |
78 const bool ret = static_cast<ssize_t>(length) == r; | 80 const bool ret = static_cast<ssize_t>(length) == r; |
79 delete[] control_buffer; | 81 delete[] control_buffer; |
80 return ret; | 82 return ret; |
81 } | 83 } |
82 | 84 |
83 // static | 85 // static |
84 ssize_t UnixDomainSocket::RecvMsg(int fd, | 86 ssize_t UnixDomainSocket::RecvMsg(int fd, |
85 void* buf, | 87 void* buf, |
86 size_t length, | 88 size_t length, |
87 ScopedVector<base::ScopedFD>* fds) { | 89 ScopedVector<ScopedFD>* fds) { |
88 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); | 90 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); |
89 } | 91 } |
90 | 92 |
91 // static | 93 // static |
92 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, | 94 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, |
93 void* buf, | 95 void* buf, |
94 size_t length, | 96 size_t length, |
95 ScopedVector<base::ScopedFD>* fds, | 97 ScopedVector<ScopedFD>* fds, |
96 base::ProcessId* pid) { | 98 ProcessId* pid) { |
97 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); | 99 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); |
98 } | 100 } |
99 | 101 |
100 // static | 102 // static |
101 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, | 103 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, |
102 void* buf, | 104 void* buf, |
103 size_t length, | 105 size_t length, |
104 int flags, | 106 int flags, |
105 ScopedVector<base::ScopedFD>* fds, | 107 ScopedVector<ScopedFD>* fds, |
106 base::ProcessId* out_pid) { | 108 ProcessId* out_pid) { |
107 fds->clear(); | 109 fds->clear(); |
108 | 110 |
109 struct msghdr msg = {}; | 111 struct msghdr msg = {}; |
110 struct iovec iov = { buf, length }; | 112 struct iovec iov = { buf, length }; |
111 msg.msg_iov = &iov; | 113 msg.msg_iov = &iov; |
112 msg.msg_iovlen = 1; | 114 msg.msg_iovlen = 1; |
113 | 115 |
114 const size_t kControlBufferSize = | 116 const size_t kControlBufferSize = |
115 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) | 117 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) |
116 #if !defined(OS_NACL_NONSFI) | 118 #if !defined(OS_NACL_NONSFI) |
117 // The PNaCl toolchain for Non-SFI binary build does not support ucred. | 119 // The PNaCl toolchain for Non-SFI binary build does not support ucred. |
118 + CMSG_SPACE(sizeof(struct ucred)) | 120 + CMSG_SPACE(sizeof(struct ucred)) |
119 #endif | 121 #endif |
120 ; | 122 ; |
121 char control_buffer[kControlBufferSize]; | 123 char control_buffer[kControlBufferSize]; |
122 msg.msg_control = control_buffer; | 124 msg.msg_control = control_buffer; |
123 msg.msg_controllen = sizeof(control_buffer); | 125 msg.msg_controllen = sizeof(control_buffer); |
124 | 126 |
125 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags)); | 127 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags)); |
126 if (r == -1) | 128 if (r == -1) |
127 return -1; | 129 return -1; |
128 | 130 |
129 int* wire_fds = NULL; | 131 int* wire_fds = NULL; |
130 unsigned wire_fds_len = 0; | 132 unsigned wire_fds_len = 0; |
131 base::ProcessId pid = -1; | 133 ProcessId pid = -1; |
132 | 134 |
133 if (msg.msg_controllen > 0) { | 135 if (msg.msg_controllen > 0) { |
134 struct cmsghdr* cmsg; | 136 struct cmsghdr* cmsg; |
135 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 137 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
136 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | 138 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
137 if (cmsg->cmsg_level == SOL_SOCKET && | 139 if (cmsg->cmsg_level == SOL_SOCKET && |
138 cmsg->cmsg_type == SCM_RIGHTS) { | 140 cmsg->cmsg_type == SCM_RIGHTS) { |
139 DCHECK_EQ(payload_len % sizeof(int), 0u); | 141 DCHECK_EQ(payload_len % sizeof(int), 0u); |
140 DCHECK_EQ(wire_fds, static_cast<void*>(nullptr)); | 142 DCHECK_EQ(wire_fds, static_cast<void*>(nullptr)); |
141 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 143 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
(...skipping 14 matching lines...) Expand all Loading... |
156 | 158 |
157 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | 159 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { |
158 for (unsigned i = 0; i < wire_fds_len; ++i) | 160 for (unsigned i = 0; i < wire_fds_len; ++i) |
159 close(wire_fds[i]); | 161 close(wire_fds[i]); |
160 errno = EMSGSIZE; | 162 errno = EMSGSIZE; |
161 return -1; | 163 return -1; |
162 } | 164 } |
163 | 165 |
164 if (wire_fds) { | 166 if (wire_fds) { |
165 for (unsigned i = 0; i < wire_fds_len; ++i) | 167 for (unsigned i = 0; i < wire_fds_len; ++i) |
166 fds->push_back(new base::ScopedFD(wire_fds[i])); | 168 fds->push_back(new ScopedFD(wire_fds[i])); |
167 } | 169 } |
168 | 170 |
169 if (out_pid) { | 171 if (out_pid) { |
170 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we | 172 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we |
171 // actually received a message. Unfortunately, Linux allows sending zero | 173 // actually received a message. Unfortunately, Linux allows sending zero |
172 // length messages, which are indistinguishable from EOF, so this check | 174 // length messages, which are indistinguishable from EOF, so this check |
173 // has false negatives. | 175 // has false negatives. |
174 if (r > 0 || msg.msg_controllen > 0) | 176 if (r > 0 || msg.msg_controllen > 0) |
175 DCHECK_GE(pid, 0); | 177 DCHECK_GE(pid, 0); |
176 | 178 |
(...skipping 17 matching lines...) Expand all Loading... |
194 | 196 |
195 // static | 197 // static |
196 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, | 198 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, |
197 uint8_t* reply, | 199 uint8_t* reply, |
198 unsigned max_reply_len, | 200 unsigned max_reply_len, |
199 int recvmsg_flags, | 201 int recvmsg_flags, |
200 int* result_fd, | 202 int* result_fd, |
201 const Pickle& request) { | 203 const Pickle& request) { |
202 // This socketpair is only used for the IPC and is cleaned up before | 204 // This socketpair is only used for the IPC and is cleaned up before |
203 // returning. | 205 // returning. |
204 base::ScopedFD recv_sock, send_sock; | 206 ScopedFD recv_sock, send_sock; |
205 if (!CreateSocketPair(&recv_sock, &send_sock)) | 207 if (!CreateSocketPair(&recv_sock, &send_sock)) |
206 return -1; | 208 return -1; |
207 | 209 |
208 { | 210 { |
209 std::vector<int> send_fds; | 211 std::vector<int> send_fds; |
210 send_fds.push_back(send_sock.get()); | 212 send_fds.push_back(send_sock.get()); |
211 if (!SendMsg(fd, request.data(), request.size(), send_fds)) | 213 if (!SendMsg(fd, request.data(), request.size(), send_fds)) |
212 return -1; | 214 return -1; |
213 } | 215 } |
214 | 216 |
215 // Close the sending end of the socket right away so that if our peer closes | 217 // Close the sending end of the socket right away so that if our peer closes |
216 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will | 218 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will |
217 // return EOF instead of hanging. | 219 // return EOF instead of hanging. |
218 send_sock.reset(); | 220 send_sock.reset(); |
219 | 221 |
220 ScopedVector<base::ScopedFD> recv_fds; | 222 ScopedVector<ScopedFD> recv_fds; |
221 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 223 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
222 // sender might get a SIGPIPE. | 224 // sender might get a SIGPIPE. |
223 const ssize_t reply_len = RecvMsgWithFlags( | 225 const ssize_t reply_len = RecvMsgWithFlags( |
224 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); | 226 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); |
225 recv_sock.reset(); | 227 recv_sock.reset(); |
226 if (reply_len == -1) | 228 if (reply_len == -1) |
227 return -1; | 229 return -1; |
228 | 230 |
229 // If we received more file descriptors than caller expected, then we treat | 231 // If we received more file descriptors than caller expected, then we treat |
230 // that as an error. | 232 // that as an error. |
231 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { | 233 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
232 NOTREACHED(); | 234 NOTREACHED(); |
233 return -1; | 235 return -1; |
234 } | 236 } |
235 | 237 |
236 if (result_fd) | 238 if (result_fd) |
237 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); | 239 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); |
238 | 240 |
239 return reply_len; | 241 return reply_len; |
240 } | 242 } |
241 #endif // !defined(OS_NACL_NONSFI) | 243 #endif // !defined(OS_NACL_NONSFI) |
| 244 |
| 245 } // namespace base |
OLD | NEW |