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 |
19 // static | 23 // static |
20 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { | 24 bool UnixDomainSocket::EnableReceiveProcessId(int fd) { |
21 const int enable = 1; | 25 const int enable = 1; |
22 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; | 26 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); | 60 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags)); |
57 const bool ret = static_cast<ssize_t>(length) == r; | 61 const bool ret = static_cast<ssize_t>(length) == r; |
58 delete[] control_buffer; | 62 delete[] control_buffer; |
59 return ret; | 63 return ret; |
60 } | 64 } |
61 | 65 |
62 // static | 66 // static |
63 ssize_t UnixDomainSocket::RecvMsg(int fd, | 67 ssize_t UnixDomainSocket::RecvMsg(int fd, |
64 void* buf, | 68 void* buf, |
65 size_t length, | 69 size_t length, |
66 std::vector<int>* fds) { | 70 ScopedVector<base::ScopedFD>* fds) { |
67 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); | 71 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL); |
68 } | 72 } |
69 | 73 |
70 // static | 74 // static |
71 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, | 75 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd, |
72 void* buf, | 76 void* buf, |
73 size_t length, | 77 size_t length, |
74 std::vector<int>* fds, | 78 ScopedVector<base::ScopedFD>* fds, |
75 base::ProcessId* pid) { | 79 base::ProcessId* pid) { |
76 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); | 80 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid); |
77 } | 81 } |
78 | 82 |
79 // static | 83 // static |
80 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, | 84 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd, |
81 void* buf, | 85 void* buf, |
82 size_t length, | 86 size_t length, |
83 int flags, | 87 int flags, |
84 std::vector<int>* fds, | 88 ScopedVector<base::ScopedFD>* fds, |
85 base::ProcessId* out_pid) { | 89 base::ProcessId* out_pid) { |
86 fds->clear(); | 90 fds->clear(); |
87 | 91 |
88 struct msghdr msg = {}; | 92 struct msghdr msg = {}; |
89 struct iovec iov = { buf, length }; | 93 struct iovec iov = { buf, length }; |
90 msg.msg_iov = &iov; | 94 msg.msg_iov = &iov; |
91 msg.msg_iovlen = 1; | 95 msg.msg_iovlen = 1; |
92 | 96 |
93 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) + | 97 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) + |
94 CMSG_SPACE(sizeof(struct ucred))]; | 98 CMSG_SPACE(sizeof(struct ucred))]; |
(...skipping 29 matching lines...) Expand all Loading... | |
124 } | 128 } |
125 | 129 |
126 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | 130 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { |
127 for (unsigned i = 0; i < wire_fds_len; ++i) | 131 for (unsigned i = 0; i < wire_fds_len; ++i) |
128 close(wire_fds[i]); | 132 close(wire_fds[i]); |
129 errno = EMSGSIZE; | 133 errno = EMSGSIZE; |
130 return -1; | 134 return -1; |
131 } | 135 } |
132 | 136 |
133 if (wire_fds) { | 137 if (wire_fds) { |
134 fds->resize(wire_fds_len); | 138 DCHECK(fds->empty()); |
135 memcpy(vector_as_array(fds), wire_fds, sizeof(int) * wire_fds_len); | 139 fds->reserve(wire_fds_len); |
140 for (unsigned i = 0; i < wire_fds_len; ++i) | |
141 fds->push_back(new base::ScopedFD(wire_fds[i])); | |
136 } | 142 } |
137 | 143 |
138 if (out_pid) { | 144 if (out_pid) { |
139 DCHECK(pid != -1); | 145 DCHECK(pid != -1); |
140 *out_pid = pid; | 146 *out_pid = pid; |
141 } | 147 } |
142 | 148 |
143 return r; | 149 return r; |
144 } | 150 } |
145 | 151 |
146 // static | 152 // static |
147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 153 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
148 uint8_t* reply, | 154 uint8_t* reply, |
149 unsigned max_reply_len, | 155 unsigned max_reply_len, |
150 int* result_fd, | 156 int* result_fd, |
151 const Pickle& request) { | 157 const Pickle& request) { |
152 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 158 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, |
153 0, /* recvmsg_flags */ | 159 0, /* recvmsg_flags */ |
154 result_fd, request); | 160 result_fd, request); |
155 } | 161 } |
156 | 162 |
163 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes | |
164 // ownership of the newly allocated file descriptors to |one| and |two|. | |
165 // Returns true on success. | |
166 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) { | |
awong
2014/04/28 21:22:28
nit: move to top of the file.
mdempsky
2014/04/28 21:24:43
Done.
| |
167 int raw_socks[2]; | |
168 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) | |
169 return false; | |
170 one->reset(raw_socks[0]); | |
171 two->reset(raw_socks[1]); | |
172 return true; | |
173 } | |
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 |