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
3 // found in the LICENSE file.
5 #include "base/posix/unix_domain_socket_linux.h"
8 #include <sys/socket.h>
14 #include "base/files/scoped_file.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/pickle.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/stl_util.h"
21 const size_t UnixDomainSocket::kMaxFileDescriptors = 16;
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) {
28 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1)
30 one->reset(raw_socks[0]);
31 two->reset(raw_socks[1]);
36 bool UnixDomainSocket::EnableReceiveProcessId(int fd) {
38 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0;
42 bool UnixDomainSocket::SendMsg(int fd,
45 const std::vector<int>& fds) {
46 struct msghdr msg = {};
47 struct iovec iov = { const_cast<void*>(buf), length };
51 char* control_buffer = NULL;
53 const unsigned control_len = CMSG_SPACE(sizeof(int) * fds.size());
54 control_buffer = new char[control_len];
57 msg.msg_control = control_buffer;
58 msg.msg_controllen = control_len;
59 cmsg = CMSG_FIRSTHDR(&msg);
60 cmsg->cmsg_level = SOL_SOCKET;
61 cmsg->cmsg_type = SCM_RIGHTS;
62 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds.size());
63 memcpy(CMSG_DATA(cmsg), &fds[0], sizeof(int) * fds.size());
64 msg.msg_controllen = cmsg->cmsg_len;
67 // Avoid a SIGPIPE if the other end breaks the connection.
68 // Due to a bug in the Linux kernel (net/unix/af_unix.c) MSG_NOSIGNAL isn't
69 // regarded for SOCK_SEQPACKET in the AF_UNIX domain, but it is mandated by
71 const int flags = MSG_NOSIGNAL;
72 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags));
73 const bool ret = static_cast<ssize_t>(length) == r;
74 delete[] control_buffer;
79 ssize_t UnixDomainSocket::RecvMsg(int fd,
82 ScopedVector<base::ScopedFD>* fds) {
83 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL);
87 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd,
90 ScopedVector<base::ScopedFD>* fds,
91 base::ProcessId* pid) {
92 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid);
96 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd,
100 ScopedVector<base::ScopedFD>* fds,
101 base::ProcessId* out_pid) {
104 struct msghdr msg = {};
105 struct iovec iov = { buf, length };
109 char control_buffer[CMSG_SPACE(sizeof(int) * kMaxFileDescriptors) +
110 CMSG_SPACE(sizeof(struct ucred))];
111 msg.msg_control = control_buffer;
112 msg.msg_controllen = sizeof(control_buffer);
114 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags));
118 int* wire_fds = NULL;
119 unsigned wire_fds_len = 0;
120 base::ProcessId pid = -1;
122 if (msg.msg_controllen > 0) {
123 struct cmsghdr* cmsg;
124 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
125 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0);
126 if (cmsg->cmsg_level == SOL_SOCKET &&
127 cmsg->cmsg_type == SCM_RIGHTS) {
128 DCHECK(payload_len % sizeof(int) == 0);
129 DCHECK(wire_fds == NULL);
130 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
131 wire_fds_len = payload_len / sizeof(int);
133 if (cmsg->cmsg_level == SOL_SOCKET &&
134 cmsg->cmsg_type == SCM_CREDENTIALS) {
135 DCHECK(payload_len == sizeof(struct ucred));
137 pid = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsg))->pid;
142 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) {
143 for (unsigned i = 0; i < wire_fds_len; ++i)
150 for (unsigned i = 0; i < wire_fds_len; ++i)
151 fds->push_back(new base::ScopedFD(wire_fds[i]));
155 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we
156 // actually received a message. Unfortunately, Linux allows sending zero
157 // length messages, which are indistinguishable from EOF, so this check
158 // has false negatives.
159 if (r > 0 || msg.msg_controllen > 0)
169 ssize_t UnixDomainSocket::SendRecvMsg(int fd,
171 unsigned max_reply_len,
173 const Pickle& request) {
174 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len,
175 0, /* recvmsg_flags */
180 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd,
182 unsigned max_reply_len,
185 const Pickle& request) {
186 // This socketpair is only used for the IPC and is cleaned up before
188 base::ScopedFD recv_sock, send_sock;
189 if (!CreateSocketPair(&recv_sock, &send_sock))
193 std::vector<int> send_fds;
194 send_fds.push_back(send_sock.get());
195 if (!SendMsg(fd, request.data(), request.size(), send_fds))
199 // Close the sending end of the socket right away so that if our peer closes
200 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will
201 // return EOF instead of hanging.
204 ScopedVector<base::ScopedFD> recv_fds;
205 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the
206 // sender might get a SIGPIPE.
207 const ssize_t reply_len = RecvMsgWithFlags(
208 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL);
213 // If we received more file descriptors than caller expected, then we treat
215 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) {
221 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release();