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 fds->reserve(wire_fds_len); |
awong
2014/04/28 18:48:16
Is resize() or reserve() appropriate here? Is it p
mdempsky
2014/04/28 20:16:58
We call fds->clear() at the very beginning, so the
awong
2014/04/28 20:25:18
Can we CHECK/DCHECK that fds->empty() then? This m
mdempsky
2014/04/28 21:11:54
I added a DCHECK(fds->empty()). The structure of
awong
2014/04/28 21:22:28
I think the resize() was just to guarantee the mem
| |
135 memcpy(vector_as_array(fds), wire_fds, sizeof(int) * wire_fds_len); | 139 for (unsigned i = 0; i < wire_fds_len; ++i) |
140 fds->push_back(new base::ScopedFD(wire_fds[i])); | |
136 } | 141 } |
137 | 142 |
138 if (out_pid) { | 143 if (out_pid) { |
139 DCHECK(pid != -1); | 144 DCHECK(pid != -1); |
140 *out_pid = pid; | 145 *out_pid = pid; |
141 } | 146 } |
142 | 147 |
143 return r; | 148 return r; |
144 } | 149 } |
145 | 150 |
146 // static | 151 // static |
147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 152 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
148 uint8_t* reply, | 153 uint8_t* reply, |
149 unsigned max_reply_len, | 154 unsigned max_reply_len, |
150 int* result_fd, | 155 int* result_fd, |
151 const Pickle& request) { | 156 const Pickle& request) { |
152 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 157 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, |
153 0, /* recvmsg_flags */ | 158 0, /* recvmsg_flags */ |
154 result_fd, request); | 159 result_fd, request); |
155 } | 160 } |
156 | 161 |
157 // static | 162 // static |
158 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, | 163 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, |
159 uint8_t* reply, | 164 uint8_t* reply, |
160 unsigned max_reply_len, | 165 unsigned max_reply_len, |
161 int recvmsg_flags, | 166 int recvmsg_flags, |
162 int* result_fd, | 167 int* result_fd, |
163 const Pickle& request) { | 168 const Pickle& request) { |
164 int fds[2]; | 169 int raw_socks[2]; |
165 | 170 |
166 // This socketpair is only used for the IPC and is cleaned up before | 171 // This socketpair is only used for the IPC and is cleaned up before |
167 // returning. | 172 // returning. |
168 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == -1) | 173 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1) |
169 return -1; | 174 return -1; |
170 | 175 |
171 std::vector<int> fd_vector; | 176 base::ScopedFD recv_sock(raw_socks[0]); |
172 fd_vector.push_back(fds[1]); | 177 base::ScopedFD send_sock(raw_socks[1]); |
awong
2014/04/28 18:48:16
Why isn't send_sock just inside the scope below?
brettw
2014/04/28 19:53:44
Actually, is there a reason for the scope below? A
mdempsky
2014/04/28 20:16:58
It could be, but I feel like it's less error-prone
mdempsky
2014/04/28 20:16:58
Not a particularly good one, but it allows send_fd
awong
2014/04/28 20:25:18
Yeah...seems good enough. If I was being 100% stri
mdempsky
2014/04/28 21:11:54
Acknowledged, but I'd like to tackle this in a fol
| |
173 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | 178 |
174 close(fds[0]); | 179 { |
175 close(fds[1]); | 180 std::vector<int> send_fds; |
176 return -1; | 181 send_fds.push_back(send_sock.get()); |
182 if (!SendMsg(fd, request.data(), request.size(), send_fds)) { | |
brettw
2014/04/28 19:53:44
No {}
mdempsky
2014/04/28 20:16:58
Done.
| |
183 return -1; | |
184 } | |
177 } | 185 } |
178 close(fds[1]); | 186 send_sock.reset(); |
179 | 187 |
180 fd_vector.clear(); | 188 ScopedVector<base::ScopedFD> recv_fds; |
181 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 189 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
182 // sender might get a SIGPIPE. | 190 // sender might get a SIGPIPE. |
183 const ssize_t reply_len = RecvMsgWithFlags( | 191 const ssize_t reply_len = RecvMsgWithFlags( |
184 fds[0], reply, max_reply_len, recvmsg_flags, &fd_vector, NULL); | 192 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL); |
185 close(fds[0]); | 193 recv_sock.reset(); |
186 if (reply_len == -1) | 194 if (reply_len == -1) |
187 return -1; | 195 return -1; |
188 | 196 |
189 if ((!fd_vector.empty() && result_fd == NULL) || fd_vector.size() > 1) { | 197 // If we received more file descriptors than caller expected, then we treat |
190 for (std::vector<int>::const_iterator | 198 // that as an error. |
191 i = fd_vector.begin(); i != fd_vector.end(); ++i) { | 199 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) { |
192 close(*i); | |
193 } | |
194 | |
195 NOTREACHED(); | 200 NOTREACHED(); |
196 | |
197 return -1; | 201 return -1; |
198 } | 202 } |
199 | 203 |
200 if (result_fd) | 204 if (result_fd) { |
brettw
2014/04/28 19:53:44
No {}
mdempsky
2014/04/28 20:16:58
Done.
| |
201 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; | 205 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release(); |
206 } | |
202 | 207 |
203 return reply_len; | 208 return reply_len; |
204 } | 209 } |
OLD | NEW |