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> |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 142 |
143 return r; | 143 return r; |
144 } | 144 } |
145 | 145 |
146 // static | 146 // static |
147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | 147 ssize_t UnixDomainSocket::SendRecvMsg(int fd, |
148 uint8_t* reply, | 148 uint8_t* reply, |
149 unsigned max_reply_len, | 149 unsigned max_reply_len, |
150 int* result_fd, | 150 int* result_fd, |
151 const Pickle& request) { | 151 const Pickle& request) { |
152 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len, | 152 const int recvmsg_flags = 0; |
153 0, /* recvmsg_flags */ | 153 const int request_fd = -1; |
154 result_fd, request); | 154 return SendRecvMsgImpl( |
| 155 fd, reply, max_reply_len, recvmsg_flags, result_fd, request, request_fd); |
155 } | 156 } |
156 | 157 |
157 // static | 158 // static |
| 159 ssize_t UnixDomainSocket::SendRecvMsgWithFD(int fd, |
| 160 uint8_t* reply, |
| 161 unsigned max_reply_len, |
| 162 int* result_fd, |
| 163 const Pickle& request, |
| 164 int request_fd) { |
| 165 const int recvmsg_flags = 0; |
| 166 return SendRecvMsgImpl( |
| 167 fd, reply, max_reply_len, recvmsg_flags, result_fd, request, request_fd); |
| 168 } |
| 169 |
| 170 // static |
158 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, | 171 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd, |
159 uint8_t* reply, | 172 uint8_t* reply, |
160 unsigned max_reply_len, | 173 unsigned max_reply_len, |
161 int recvmsg_flags, | 174 int recvmsg_flags, |
162 int* result_fd, | 175 int* result_fd, |
163 const Pickle& request) { | 176 const Pickle& request) { |
| 177 const int request_fd = -1; |
| 178 return SendRecvMsgImpl( |
| 179 fd, reply, max_reply_len, recvmsg_flags, result_fd, request, request_fd); |
| 180 } |
| 181 |
| 182 // static |
| 183 ssize_t UnixDomainSocket::SendRecvMsgImpl(int fd, |
| 184 uint8_t* reply, |
| 185 unsigned max_reply_len, |
| 186 int recvmsg_flags, |
| 187 int* result_fd, |
| 188 const Pickle& request, |
| 189 int request_fd) { |
164 int fds[2]; | 190 int fds[2]; |
165 | 191 |
166 // This socketpair is only used for the IPC and is cleaned up before | 192 // This socketpair is only used for the IPC and is cleaned up before |
167 // returning. | 193 // returning. |
168 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == -1) | 194 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == -1) |
169 return -1; | 195 return -1; |
170 | 196 |
171 std::vector<int> fd_vector; | 197 std::vector<int> fd_vector; |
172 fd_vector.push_back(fds[1]); | 198 fd_vector.push_back(fds[1]); |
| 199 if (request_fd != -1) |
| 200 fd_vector.push_back(request_fd); |
173 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | 201 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { |
174 close(fds[0]); | 202 close(fds[0]); |
175 close(fds[1]); | 203 close(fds[1]); |
176 return -1; | 204 return -1; |
177 } | 205 } |
178 close(fds[1]); | 206 close(fds[1]); |
179 | 207 |
180 fd_vector.clear(); | 208 fd_vector.clear(); |
181 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the | 209 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the |
182 // sender might get a SIGPIPE. | 210 // sender might get a SIGPIPE. |
(...skipping 12 matching lines...) Expand all Loading... |
195 NOTREACHED(); | 223 NOTREACHED(); |
196 | 224 |
197 return -1; | 225 return -1; |
198 } | 226 } |
199 | 227 |
200 if (result_fd) | 228 if (result_fd) |
201 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; | 229 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; |
202 | 230 |
203 return reply_len; | 231 return reply_len; |
204 } | 232 } |
OLD | NEW |