Re-applying reverted changes plus fix. ConfidenceScore now takes flat
[chromium-blink-merge.git] / base / posix / unix_domain_socket_linux.cc
blob20a5944b4ee7d4ebe412ee3b34aa8b044a5f97b0
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"
7 #include <errno.h>
8 #include <sys/socket.h>
9 #include <unistd.h>
11 #include <vector>
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(__native_client_nonsfi__)
21 #include <sys/uio.h>
22 #endif
24 const size_t UnixDomainSocket::kMaxFileDescriptors = 16;
26 #if !defined(__native_client_nonsfi__)
27 // Creates a connected pair of UNIX-domain SOCK_SEQPACKET sockets, and passes
28 // ownership of the newly allocated file descriptors to |one| and |two|.
29 // Returns true on success.
30 static bool CreateSocketPair(base::ScopedFD* one, base::ScopedFD* two) {
31 int raw_socks[2];
32 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_socks) == -1)
33 return false;
34 one->reset(raw_socks[0]);
35 two->reset(raw_socks[1]);
36 return true;
39 // static
40 bool UnixDomainSocket::EnableReceiveProcessId(int fd) {
41 const int enable = 1;
42 return setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable)) == 0;
44 #endif // !defined(__native_client_nonsfi__)
46 // static
47 bool UnixDomainSocket::SendMsg(int fd,
48 const void* buf,
49 size_t length,
50 const std::vector<int>& fds) {
51 struct msghdr msg = {};
52 struct iovec iov = { const_cast<void*>(buf), length };
53 msg.msg_iov = &iov;
54 msg.msg_iovlen = 1;
56 char* control_buffer = NULL;
57 if (fds.size()) {
58 const unsigned control_len = CMSG_SPACE(sizeof(int) * fds.size());
59 control_buffer = new char[control_len];
61 struct cmsghdr* cmsg;
62 msg.msg_control = control_buffer;
63 msg.msg_controllen = control_len;
64 cmsg = CMSG_FIRSTHDR(&msg);
65 cmsg->cmsg_level = SOL_SOCKET;
66 cmsg->cmsg_type = SCM_RIGHTS;
67 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds.size());
68 memcpy(CMSG_DATA(cmsg), &fds[0], sizeof(int) * fds.size());
69 msg.msg_controllen = cmsg->cmsg_len;
72 // Avoid a SIGPIPE if the other end breaks the connection.
73 // Due to a bug in the Linux kernel (net/unix/af_unix.c) MSG_NOSIGNAL isn't
74 // regarded for SOCK_SEQPACKET in the AF_UNIX domain, but it is mandated by
75 // POSIX.
76 const int flags = MSG_NOSIGNAL;
77 const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, flags));
78 const bool ret = static_cast<ssize_t>(length) == r;
79 delete[] control_buffer;
80 return ret;
83 // static
84 ssize_t UnixDomainSocket::RecvMsg(int fd,
85 void* buf,
86 size_t length,
87 ScopedVector<base::ScopedFD>* fds) {
88 return UnixDomainSocket::RecvMsgWithPid(fd, buf, length, fds, NULL);
91 // static
92 ssize_t UnixDomainSocket::RecvMsgWithPid(int fd,
93 void* buf,
94 size_t length,
95 ScopedVector<base::ScopedFD>* fds,
96 base::ProcessId* pid) {
97 return UnixDomainSocket::RecvMsgWithFlags(fd, buf, length, 0, fds, pid);
100 // static
101 ssize_t UnixDomainSocket::RecvMsgWithFlags(int fd,
102 void* buf,
103 size_t length,
104 int flags,
105 ScopedVector<base::ScopedFD>* fds,
106 base::ProcessId* out_pid) {
107 fds->clear();
109 struct msghdr msg = {};
110 struct iovec iov = { buf, length };
111 msg.msg_iov = &iov;
112 msg.msg_iovlen = 1;
114 const size_t kControlBufferSize =
115 CMSG_SPACE(sizeof(int) * kMaxFileDescriptors)
116 #if !defined(__native_client_nonsfi__)
117 // The PNaCl toolchain for Non-SFI binary build does not support ucred.
118 + CMSG_SPACE(sizeof(struct ucred))
119 #endif
121 char control_buffer[kControlBufferSize];
122 msg.msg_control = control_buffer;
123 msg.msg_controllen = sizeof(control_buffer);
125 const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, flags));
126 if (r == -1)
127 return -1;
129 int* wire_fds = NULL;
130 unsigned wire_fds_len = 0;
131 base::ProcessId pid = -1;
133 if (msg.msg_controllen > 0) {
134 struct cmsghdr* cmsg;
135 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
136 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0);
137 if (cmsg->cmsg_level == SOL_SOCKET &&
138 cmsg->cmsg_type == SCM_RIGHTS) {
139 DCHECK(payload_len % sizeof(int) == 0);
140 DCHECK(wire_fds == NULL);
141 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
142 wire_fds_len = payload_len / sizeof(int);
144 #if !defined(__native_client_nonsfi__)
145 // The PNaCl toolchain for Non-SFI binary build does not support
146 // SCM_CREDENTIALS.
147 if (cmsg->cmsg_level == SOL_SOCKET &&
148 cmsg->cmsg_type == SCM_CREDENTIALS) {
149 DCHECK(payload_len == sizeof(struct ucred));
150 DCHECK(pid == -1);
151 pid = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsg))->pid;
153 #endif
157 #if !defined(__native_client_nonsfi__)
158 // The PNaCl toolchain for Non-SFI binary build does not support
159 // MSG_TRUNC or MSG_CTRUNC.
160 if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) {
161 for (unsigned i = 0; i < wire_fds_len; ++i)
162 close(wire_fds[i]);
163 errno = EMSGSIZE;
164 return -1;
166 #endif
168 if (wire_fds) {
169 for (unsigned i = 0; i < wire_fds_len; ++i)
170 fds->push_back(new base::ScopedFD(wire_fds[i]));
173 if (out_pid) {
174 // |pid| will legitimately be -1 if we read EOF, so only DCHECK if we
175 // actually received a message. Unfortunately, Linux allows sending zero
176 // length messages, which are indistinguishable from EOF, so this check
177 // has false negatives.
178 if (r > 0 || msg.msg_controllen > 0)
179 DCHECK_GE(pid, 0);
181 *out_pid = pid;
184 return r;
187 #if !defined(__native_client_nonsfi__)
188 // static
189 ssize_t UnixDomainSocket::SendRecvMsg(int fd,
190 uint8_t* reply,
191 unsigned max_reply_len,
192 int* result_fd,
193 const Pickle& request) {
194 return UnixDomainSocket::SendRecvMsgWithFlags(fd, reply, max_reply_len,
195 0, /* recvmsg_flags */
196 result_fd, request);
199 // static
200 ssize_t UnixDomainSocket::SendRecvMsgWithFlags(int fd,
201 uint8_t* reply,
202 unsigned max_reply_len,
203 int recvmsg_flags,
204 int* result_fd,
205 const Pickle& request) {
206 // This socketpair is only used for the IPC and is cleaned up before
207 // returning.
208 base::ScopedFD recv_sock, send_sock;
209 if (!CreateSocketPair(&recv_sock, &send_sock))
210 return -1;
213 std::vector<int> send_fds;
214 send_fds.push_back(send_sock.get());
215 if (!SendMsg(fd, request.data(), request.size(), send_fds))
216 return -1;
219 // Close the sending end of the socket right away so that if our peer closes
220 // it before sending a response (e.g., from exiting), RecvMsgWithFlags() will
221 // return EOF instead of hanging.
222 send_sock.reset();
224 ScopedVector<base::ScopedFD> recv_fds;
225 // When porting to OSX keep in mind it doesn't support MSG_NOSIGNAL, so the
226 // sender might get a SIGPIPE.
227 const ssize_t reply_len = RecvMsgWithFlags(
228 recv_sock.get(), reply, max_reply_len, recvmsg_flags, &recv_fds, NULL);
229 recv_sock.reset();
230 if (reply_len == -1)
231 return -1;
233 // If we received more file descriptors than caller expected, then we treat
234 // that as an error.
235 if (recv_fds.size() > (result_fd != NULL ? 1 : 0)) {
236 NOTREACHED();
237 return -1;
240 if (result_fd)
241 *result_fd = recv_fds.empty() ? -1 : recv_fds[0]->release();
243 return reply_len;
245 #endif // !defined(__native_client_nonsfi__)