Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/unix_domain_socket_posix.h" | 5 #include "base/unix_domain_socket_posix.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 #include <sys/uio.h> | 8 #include <sys/uio.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 | 10 |
| 11 #include "base/eintr_wrapper.h" | 11 #include "base/eintr_wrapper.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/pickle.h" | |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 bool SendMsg(int fd, const void* buf, size_t length, std::vector<int>& fds) { | 17 bool SendMsg(int fd, const void* buf, size_t length, std::vector<int>& fds) { |
| 17 struct msghdr msg; | 18 struct msghdr msg; |
| 18 memset(&msg, 0, sizeof(msg)); | 19 memset(&msg, 0, sizeof(msg)); |
| 19 struct iovec iov = {const_cast<void*>(buf), length}; | 20 struct iovec iov = {const_cast<void*>(buf), length}; |
| 20 msg.msg_iov = &iov; | 21 msg.msg_iov = &iov; |
| 21 msg.msg_iovlen = 1; | 22 msg.msg_iovlen = 1; |
| 22 | 23 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 errno = EMSGSIZE; | 88 errno = EMSGSIZE; |
| 88 return -1; | 89 return -1; |
| 89 } | 90 } |
| 90 | 91 |
| 91 fds->resize(wire_fds_len); | 92 fds->resize(wire_fds_len); |
| 92 memcpy(&(*fds)[0], wire_fds, sizeof(int) * wire_fds_len); | 93 memcpy(&(*fds)[0], wire_fds, sizeof(int) * wire_fds_len); |
| 93 | 94 |
| 94 return r; | 95 return r; |
| 95 } | 96 } |
| 96 | 97 |
| 98 ssize_t SendRecvMsg(int fd, uint8_t* reply, unsigned reply_len, int* result_fd, | |
| 99 const Pickle& request) { | |
| 100 int fds[2]; | |
|
Evan Martin
2009/06/18 00:36:08
2spc tabs
comment in here that this socketpair is
| |
| 101 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) == -1) | |
| 102 return false; | |
| 103 | |
| 104 std::vector<int> fd_vector; | |
| 105 fd_vector.push_back(fds[1]); | |
| 106 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | |
| 107 close(fds[0]); | |
| 108 close(fds[1]); | |
| 109 return -1; | |
| 110 } | |
| 111 close(fds[1]); | |
| 112 | |
| 113 fd_vector.clear(); | |
| 114 const ssize_t r = RecvMsg(fds[0], reply, reply_len, &fd_vector); | |
|
Evan Martin
2009/06/18 00:36:08
would be nice to not use a single letter variable
| |
| 115 close(fds[0]); | |
| 116 if (r == -1) | |
| 117 return -1; | |
| 118 | |
| 119 if ((fd_vector.size() > 0 && result_fd == NULL) || fd_vector.size() > 1) { | |
|
Evan Martin
2009/06/18 00:36:08
It seems these sorts of errors are not like the Se
| |
| 120 for (std::vector<int>::const_iterator | |
| 121 i = fd_vector.begin(); i != fd_vector.end(); ++i) { | |
| 122 close(*i); | |
| 123 } | |
| 124 | |
| 125 return -1; | |
| 126 } | |
| 127 | |
| 128 if (result_fd) { | |
| 129 if (fd_vector.size() == 0) { | |
| 130 *result_fd = -1; | |
| 131 } else { | |
| 132 *result_fd = fd_vector[0]; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 return r; | |
| 137 } | |
| 138 | |
| 97 } // namespace base | 139 } // namespace base |
| OLD | NEW |