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>
13 #include "base/files/scoped_file.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/pickle.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "base/stl_util.h"
20 #if !defined(OS_NACL_NONSFI)
26 const size_t UnixDomainSocket::kMaxFileDescriptors
= 16;
28 #if !defined(OS_NACL_NONSFI)
29 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes
30 // ownership of the newly allocated file descriptors to |one| and |two|.
31 // Returns true on success.
32 static bool CreateSocketPair(ScopedFD
* one
, ScopedFD
* two
) {
34 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, raw_socks
) == -1)
36 one
->reset(raw_socks
[0]);
37 two
->reset(raw_socks
[1]);
42 bool UnixDomainSocket::EnableReceiveProcessId(int fd
) {
44 return setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &enable
, sizeof(enable
)) == 0;
46 #endif // !defined(OS_NACL_NONSFI)
49 bool UnixDomainSocket::SendMsg(int fd
,
52 const std::vector
<int>& fds
) {
53 struct msghdr msg
= {};
54 struct iovec iov
= { const_cast<void*>(buf
), length
};
58 char* control_buffer
= NULL
;
60 const unsigned control_len
= CMSG_SPACE(sizeof(int) * fds
.size());
61 control_buffer
= new char[control_len
];
64 msg
.msg_control
= control_buffer
;
65 msg
.msg_controllen
= control_len
;
66 cmsg
= CMSG_FIRSTHDR(&msg
);
67 cmsg
->cmsg_level
= SOL_SOCKET
;
68 cmsg
->cmsg_type
= SCM_RIGHTS
;
69 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * fds
.size());
70 memcpy(CMSG_DATA(cmsg
), &fds
[0], sizeof(int) * fds
.size());
71 msg
.msg_controllen
= cmsg
->cmsg_len
;
74 // Avoid a SIGPIPE if the other end breaks the connection.
75 // Due to a bug in the Linux kernel (net/unix/af_unix.c) MSG_NOSIGNAL isn't
76 // regarded for SOCK_SEQPACKET in the AF_UNIX domain, but it is mandated by
78 const int flags
= MSG_NOSIGNAL
;
79 const ssize_t r
= HANDLE_EINTR(sendmsg(fd
, &msg
, flags
));
80 const bool ret
= static_cast<ssize_t
>(length
) == r
;
81 delete[] control_buffer
;
86 ssize_t
UnixDomainSocket::RecvMsg(int fd
,
89 ScopedVector
<ScopedFD
>* fds
) {
90 return UnixDomainSocket::RecvMsgWithPid(fd
, buf
, length
, fds
, NULL
);
94 ssize_t
UnixDomainSocket::RecvMsgWithPid(int fd
,
97 ScopedVector
<ScopedFD
>* fds
,
99 return UnixDomainSocket::RecvMsgWithFlags(fd
, buf
, length
, 0, fds
, pid
);
103 ssize_t
UnixDomainSocket::RecvMsgWithFlags(int fd
,
107 ScopedVector
<ScopedFD
>* fds
,
108 ProcessId
* out_pid
) {
111 struct msghdr msg
= {};
112 struct iovec iov
= { buf
, length
};
116 const size_t kControlBufferSize
=
117 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors
)
118 #if !defined(OS_NACL_NONSFI)
119 // The PNaCl toolchain for Non-SFI binary build does not support ucred.
120 + CMSG_SPACE(sizeof(struct ucred
))
123 char control_buffer
[kControlBufferSize
];
124 msg
.msg_control
= control_buffer
;
125 msg
.msg_controllen
= sizeof(control_buffer
);
127 const ssize_t r
= HANDLE_EINTR(recvmsg(fd
, &msg
, flags
));
131 int* wire_fds
= NULL
;
132 unsigned wire_fds_len
= 0;
135 if (msg
.msg_controllen
> 0) {
136 struct cmsghdr
* cmsg
;
137 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
138 const unsigned payload_len
= cmsg
->cmsg_len
- CMSG_LEN(0);
139 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
140 cmsg
->cmsg_type
== SCM_RIGHTS
) {
141 DCHECK_EQ(payload_len
% sizeof(int), 0u);
142 DCHECK_EQ(wire_fds
, static_cast<void*>(nullptr));
143 wire_fds
= reinterpret_cast<int*>(CMSG_DATA(cmsg
));
144 wire_fds_len
= payload_len
/ sizeof(int);
146 #if !defined(OS_NACL_NONSFI)
147 // The PNaCl toolchain for Non-SFI binary build does not support
149 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
150 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
151 DCHECK_EQ(payload_len
, sizeof(struct ucred
));
153 pid
= reinterpret_cast<struct ucred
*>(CMSG_DATA(cmsg
))->pid
;
159 if (msg
.msg_flags
& MSG_TRUNC
|| msg
.msg_flags
& MSG_CTRUNC
) {
160 for (unsigned i
= 0; i
< wire_fds_len
; ++i
)
167 for (unsigned i
= 0; i
< wire_fds_len
; ++i
)
168 fds
->push_back(new ScopedFD(wire_fds
[i
]));
172 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we
173 // actually received a message. Unfortunately, Linux allows sending zero
174 // length messages, which are indistinguishable from EOF, so this check
175 // has false negatives.
176 if (r
> 0 || msg
.msg_controllen
> 0)
185 #if !defined(OS_NACL_NONSFI)
187 ssize_t
UnixDomainSocket::SendRecvMsg(int fd
,
189 unsigned max_reply_len
,
191 const Pickle
& request
) {
192 return UnixDomainSocket::SendRecvMsgWithFlags(fd
, reply
, max_reply_len
,
193 0, /* recvmsg_flags */
198 ssize_t
UnixDomainSocket::SendRecvMsgWithFlags(int fd
,
200 unsigned max_reply_len
,
203 const Pickle
& request
) {
204 // This socketpair is only used for the IPC and is cleaned up before
206 ScopedFD recv_sock
, send_sock
;
207 if (!CreateSocketPair(&recv_sock
, &send_sock
))
211 std::vector
<int> send_fds
;
212 send_fds
.push_back(send_sock
.get());
213 if (!SendMsg(fd
, request
.data(), request
.size(), send_fds
))
217 // Close the sending end of the socket right away so that if our peer closes
218 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will
219 // return EOF instead of hanging.
222 ScopedVector
<ScopedFD
> recv_fds
;
223 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the
224 // sender might get a SIGPIPE.
225 const ssize_t reply_len
= RecvMsgWithFlags(
226 recv_sock
.get(), reply
, max_reply_len
, recvmsg_flags
, &recv_fds
, NULL
);
231 // If we received more file descriptors than caller expected, then we treat
233 if (recv_fds
.size() > (result_fd
!= NULL
? 1 : 0)) {
239 *result_fd
= recv_fds
.empty() ? -1 : recv_fds
[0]->release();
243 #endif // !defined(OS_NACL_NONSFI)