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 for (unsigned i = 0; i < wire_fds_len; ++i) |
135 memcpy(vector_as_array(fds), wire_fds, sizeof(int) * wire_fds_len); | 151 fds->push_back(new base::ScopedFD(wire_fds[i])); |
136 } | 152 } |
137 | 153 |
138 if (out_pid) { | 154 if (out_pid) { |
139 DCHECK(pid != -1); | 155 DCHECK(pid != -1); |
140 *out_pid = pid; | 156 *out_pid = pid; |
141 } | 157 } |
142 | 158 |
143 return r; | 159 return r; |
144 } | 160 } |
145 | 161 |
146 // static | 162 // static |
147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 163 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
148 uint8_t* reply, | 164 uint8_t* reply, |
149 unsigned max_reply_len, | 165 unsigned max_reply_len, |
150 int* result_fd, | 166 int* result_fd, |
151 const Pickle& request) { | 167 const Pickle& request) { |
152 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 168 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, |
153 0, /* recvmsg_flags */ | 169 0, /* recvmsg_flags */ |
154 result_fd, request); | 170 result_fd, request); |
155 } | 171 } |
156 | 172 |
157 // static | 173 // static |
158 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, | 174 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, |
159 uint8_t* reply, | 175 uint8_t* reply, |
160 unsigned max_reply_len, | 176 unsigned max_reply_len, |
161 int recvmsg_flags, | 177 int recvmsg_flags, |
162 int* result_fd, | 178 int* result_fd, |
163 const Pickle& request) { | 179 const Pickle& request) { |
164 int fds[2]; | |
165 | |
166 // This socketpair is only used for the IPC and is cleaned up before | 180 // This socketpair is only used for the IPC and is cleaned up before |
167 // returning. | 181 // returning. |
168 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == -1) | 182 base::ScopedFD recv_sock, send_sock; |
| 183 if (!CreateSocketPair(&recv_sock, &send_sock)) |
169 return -1; | 184 return -1; |
170 | 185 |
171 std::vector<int> fd_vector; | 186 { |
172 fd_vector.push_back(fds[1]); | 187 std::vector<int> send_fds; |
173 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | 188 send_fds.push_back(send_sock.get()); |
174 close(fds[0]); | 189 if (!SendMsg(fd, request.data(), request.size(), send_fds)) |
175 close(fds[1]); | 190 return -1; |
176 return -1; | |
177 } | 191 } |
178 close(fds[1]); | |
179 | 192 |
180 fd_vector.clear(); | 193 // Close the sending end of the socket right away so that if our peer closes |
| 194 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will |
| 195 // return EOF instead of hanging. |
| 196 send_sock.reset(); |
| 197 |
| 198 ScopedVector<base::ScopedFD> recv_fds; |
181 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 199 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
182 // sender might get a SIGPIPE. | 200 // sender might get a SIGPIPE. |
183 const ssize_t reply_len = RecvMsgWithFlags( | 201 const ssize_t reply_len = RecvMsgWithFlags( |
184 fds[0], reply, max_reply_len, recvmsg_flags, &fd_vector, NULL); | 202 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); |
185 close(fds[0]); | 203 recv_sock.reset(); |
186 if (reply_len == -1) | 204 if (reply_len == -1) |
187 return -1; | 205 return -1; |
188 | 206 |
189 if ((!fd_vector.empty() && result_fd == NULL) || fd_vector.size() > 1) { | 207 // If we received more file descriptors than caller expected, then we treat |
190 for (std::vector<int>::const_iterator | 208 // that as an error. |
191 i = fd_vector.begin(); i != fd_vector.end(); ++i) { | 209 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
192 close(*i); | |
193 } | |
194 | |
195 NOTREACHED(); | 210 NOTREACHED(); |
196 | |
197 return -1; | 211 return -1; |
198 } | 212 } |
199 | 213 |
200 if (result_fd) | 214 if (result_fd) |
201 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; | 215 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); |
202 | 216 |
203 return reply_len; | 217 return reply_len; |
204 } | 218 } |
OLD | NEW |