| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/unix_domain_socket_posix.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <unistd.h> | |
| 9 #include <sys/uio.h> | |
| 10 #include <sys/socket.h> | |
| 11 | |
| 12 #include "base/eintr_wrapper.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/pickle.h" | |
| 15 | |
| 16 // static | |
| 17 bool UnixDomainSocket::SendMsg(int fd, | |
| 18 const void* buf, | |
| 19 size_t length, | |
| 20 const std::vector<int>& fds) { | |
| 21 struct msghdr msg; | |
| 22 memset(&msg, 0, sizeof(msg)); | |
| 23 struct iovec iov = {const_cast<void*>(buf), length}; | |
| 24 msg.msg_iov = &iov; | |
| 25 msg.msg_iovlen = 1; | |
| 26 | |
| 27 char* control_buffer = NULL; | |
| 28 if (fds.size()) { | |
| 29 const unsigned control_len = CMSG_SPACE(sizeof(int) * fds.size()); | |
| 30 control_buffer = new char[control_len]; | |
| 31 | |
| 32 struct cmsghdr *cmsg; | |
| 33 msg.msg_control = control_buffer; | |
| 34 msg.msg_controllen = control_len; | |
| 35 cmsg = CMSG_FIRSTHDR(&msg); | |
| 36 cmsg->cmsg_level = SOL_SOCKET; | |
| 37 cmsg->cmsg_type = SCM_RIGHTS; | |
| 38 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds.size()); | |
| 39 memcpy(CMSG_DATA(cmsg), &fds[0], sizeof(int) * fds.size()); | |
| 40 msg.msg_controllen = cmsg->cmsg_len; | |
| 41 } | |
| 42 | |
| 43 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, 0)); | |
| 44 const bool ret = static_cast<ssize_t>(length) == r; | |
| 45 delete[] control_buffer; | |
| 46 return ret; | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 ssize_t UnixDomainSocket::RecvMsg(int fd, | |
| 51 void* buf, | |
| 52 size_t length, | |
| 53 std::vector<int>* fds) { | |
| 54 static const unsigned kMaxDescriptors = 16; | |
| 55 | |
| 56 fds->clear(); | |
| 57 | |
| 58 struct msghdr msg; | |
| 59 memset(&msg, 0, sizeof(msg)); | |
| 60 struct iovec iov = {buf, length}; | |
| 61 msg.msg_iov = &iov; | |
| 62 msg.msg_iovlen = 1; | |
| 63 | |
| 64 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxDescriptors)]; | |
| 65 msg.msg_control = control_buffer; | |
| 66 msg.msg_controllen = sizeof(control_buffer); | |
| 67 | |
| 68 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, 0)); | |
| 69 if (r == -1) | |
| 70 return -1; | |
| 71 | |
| 72 int* wire_fds = NULL; | |
| 73 unsigned wire_fds_len = 0; | |
| 74 | |
| 75 if (msg.msg_controllen > 0) { | |
| 76 struct cmsghdr* cmsg; | |
| 77 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { | |
| 78 if (cmsg->cmsg_level == SOL_SOCKET && | |
| 79 cmsg->cmsg_type == SCM_RIGHTS) { | |
| 80 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | |
| 81 DCHECK(payload_len % sizeof(int) == 0); | |
| 82 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | |
| 83 wire_fds_len = payload_len / sizeof(int); | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { | |
| 90 for (unsigned i = 0; i < wire_fds_len; ++i) | |
| 91 close(wire_fds[i]); | |
| 92 errno = EMSGSIZE; | |
| 93 return -1; | |
| 94 } | |
| 95 | |
| 96 fds->resize(wire_fds_len); | |
| 97 memcpy(&(*fds)[0], wire_fds, sizeof(int) * wire_fds_len); | |
| 98 | |
| 99 return r; | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 ssize_t UnixDomainSocket::SendRecvMsg(int fd, | |
| 104 uint8_t* reply, | |
| 105 unsigned max_reply_len, | |
| 106 int* result_fd, | |
| 107 const Pickle& request) { | |
| 108 int fds[2]; | |
| 109 | |
| 110 // This socketpair is only used for the IPC and is cleaned up before | |
| 111 // returning. | |
| 112 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) == -1) | |
| 113 return false; | |
| 114 | |
| 115 std::vector<int> fd_vector; | |
| 116 fd_vector.push_back(fds[1]); | |
| 117 if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { | |
| 118 close(fds[0]); | |
| 119 close(fds[1]); | |
| 120 return -1; | |
| 121 } | |
| 122 close(fds[1]); | |
| 123 | |
| 124 fd_vector.clear(); | |
| 125 const ssize_t reply_len = RecvMsg(fds[0], reply, max_reply_len, &fd_vector); | |
| 126 close(fds[0]); | |
| 127 if (reply_len == -1) | |
| 128 return -1; | |
| 129 | |
| 130 if ((!fd_vector.empty() && result_fd == NULL) || fd_vector.size() > 1) { | |
| 131 for (std::vector<int>::const_iterator | |
| 132 i = fd_vector.begin(); i != fd_vector.end(); ++i) { | |
| 133 close(*i); | |
| 134 } | |
| 135 | |
| 136 NOTREACHED(); | |
| 137 | |
| 138 return -1; | |
| 139 } | |
| 140 | |
| 141 if (result_fd) | |
| 142 *result_fd = fd_vector.empty() ? -1 : fd_vector[0]; | |
| 143 | |
| 144 return reply_len; | |
| 145 } | |
| 146 | |
| OLD | NEW |