• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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 "build/build_config.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_file.h"
17 #include "base/location.h"
18 #include "base/pickle.h"
19 #include "base/posix/unix_domain_socket.h"
20 #include "base/single_thread_task_runner.h"
21 #include "base/synchronization/waitable_event.h"
22 #include "base/threading/thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 
25 namespace base {
26 
27 namespace {
28 
29 // Callers should use ASSERT_NO_FATAL_FAILURE with this function, to
30 // ensure that execution is aborted if the function has assertion failure.
CreateSocketPair(int fds[2])31 void CreateSocketPair(int fds[2]) {
32 #if defined(OS_MACOSX)
33   // Mac OS does not support SOCK_SEQPACKET.
34   int flags = SOCK_STREAM;
35 #else
36   int flags = SOCK_SEQPACKET;
37 #endif
38   ASSERT_EQ(0, socketpair(AF_UNIX, flags, 0, fds));
39 #if defined(OS_MACOSX)
40   // On OSX an attempt to read or write to a closed socket may generate a
41   // SIGPIPE rather than returning -1, corrected with SO_NOSIGPIPE option.
42   int nosigpipe = 1;
43   ASSERT_EQ(0, setsockopt(fds[0], SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe,
44                           sizeof(nosigpipe)));
45   ASSERT_EQ(0, setsockopt(fds[1], SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe,
46                           sizeof(nosigpipe)));
47 #endif
48 }
49 
TEST(UnixDomainSocketTest,SendRecvMsgAbortOnReplyFDClose)50 TEST(UnixDomainSocketTest, SendRecvMsgAbortOnReplyFDClose) {
51   Thread message_thread("UnixDomainSocketTest");
52   ASSERT_TRUE(message_thread.Start());
53   int fds[2];
54   ASSERT_NO_FATAL_FAILURE(CreateSocketPair(fds));
55   ScopedFD scoped_fd0(fds[0]);
56   ScopedFD scoped_fd1(fds[1]);
57 
58   // Have the thread send a synchronous message via the socket.
59   Pickle request;
60   message_thread.task_runner()->PostTask(
61       FROM_HERE, BindOnce(IgnoreResult(&UnixDomainSocket::SendRecvMsg), fds[1],
62                           nullptr, 0U, nullptr, request));
63 
64   // Receive the message.
65   std::vector<ScopedFD> message_fds;
66   uint8_t buffer[16];
67   ASSERT_EQ(
68       static_cast<int>(request.size()),
69       UnixDomainSocket::RecvMsg(fds[0], buffer, sizeof(buffer), &message_fds));
70   ASSERT_EQ(1U, message_fds.size());
71 
72   // Close the reply FD.
73   message_fds.clear();
74 
75   // Check that the thread didn't get blocked.
76   WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
77                       WaitableEvent::InitialState::NOT_SIGNALED);
78   message_thread.task_runner()->PostTask(
79       FROM_HERE, BindOnce(&WaitableEvent::Signal, Unretained(&event)));
80   ASSERT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(5000)));
81 }
82 
TEST(UnixDomainSocketTest,SendRecvMsgAvoidsSIGPIPE)83 TEST(UnixDomainSocketTest, SendRecvMsgAvoidsSIGPIPE) {
84   // Make sure SIGPIPE isn't being ignored.
85   struct sigaction act = {}, oldact;
86   act.sa_handler = SIG_DFL;
87   ASSERT_EQ(0, sigaction(SIGPIPE, &act, &oldact));
88   int fds[2];
89   ASSERT_NO_FATAL_FAILURE(CreateSocketPair(fds));
90   ScopedFD scoped_fd1(fds[1]);
91   ASSERT_EQ(0, IGNORE_EINTR(close(fds[0])));
92 
93   // Have the thread send a synchronous message via the socket. Unless the
94   // message is sent with MSG_NOSIGNAL, this shall result in SIGPIPE.
95   Pickle request;
96   ASSERT_EQ(
97       -1, UnixDomainSocket::SendRecvMsg(fds[1], nullptr, 0U, nullptr, request));
98   ASSERT_EQ(EPIPE, errno);
99   // Restore the SIGPIPE handler.
100   ASSERT_EQ(0, sigaction(SIGPIPE, &oldact, nullptr));
101 }
102 
103 // Simple sanity check within a single process that receiving PIDs works.
TEST(UnixDomainSocketTest,RecvPid)104 TEST(UnixDomainSocketTest, RecvPid) {
105   int fds[2];
106   ASSERT_NO_FATAL_FAILURE(CreateSocketPair(fds));
107   ScopedFD recv_sock(fds[0]);
108   ScopedFD send_sock(fds[1]);
109 
110   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
111 
112   static const char kHello[] = "hello";
113   ASSERT_TRUE(UnixDomainSocket::SendMsg(send_sock.get(), kHello, sizeof(kHello),
114                                         std::vector<int>()));
115 
116   // Extra receiving buffer space to make sure we really received only
117   // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
118   char buf[sizeof(kHello) + 1];
119   ProcessId sender_pid;
120   std::vector<ScopedFD> fd_vec;
121   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
122       recv_sock.get(), buf, sizeof(buf), &fd_vec, &sender_pid);
123   ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
124   ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
125   ASSERT_EQ(0U, fd_vec.size());
126 
127   ASSERT_EQ(getpid(), sender_pid);
128 }
129 
130 // Same as above, but send the max number of file descriptors too.
TEST(UnixDomainSocketTest,RecvPidWithMaxDescriptors)131 TEST(UnixDomainSocketTest, RecvPidWithMaxDescriptors) {
132   int fds[2];
133   ASSERT_NO_FATAL_FAILURE(CreateSocketPair(fds));
134   ScopedFD recv_sock(fds[0]);
135   ScopedFD send_sock(fds[1]);
136 
137   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
138 
139   static const char kHello[] = "hello";
140   std::vector<int> send_fds(UnixDomainSocket::kMaxFileDescriptors,
141                             send_sock.get());
142   ASSERT_TRUE(UnixDomainSocket::SendMsg(send_sock.get(), kHello, sizeof(kHello),
143                                         send_fds));
144 
145   // Extra receiving buffer space to make sure we really received only
146   // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer.
147   char buf[sizeof(kHello) + 1];
148   ProcessId sender_pid;
149   std::vector<ScopedFD> recv_fds;
150   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
151       recv_sock.get(), buf, sizeof(buf), &recv_fds, &sender_pid);
152   ASSERT_EQ(sizeof(kHello), static_cast<size_t>(nread));
153   ASSERT_EQ(0, memcmp(buf, kHello, sizeof(kHello)));
154   ASSERT_EQ(UnixDomainSocket::kMaxFileDescriptors, recv_fds.size());
155 
156   ASSERT_EQ(getpid(), sender_pid);
157 }
158 
159 // Check that RecvMsgWithPid doesn't DCHECK fail when reading EOF from a
160 // disconnected socket.
TEST(UnixDomianSocketTest,RecvPidDisconnectedSocket)161 TEST(UnixDomianSocketTest, RecvPidDisconnectedSocket) {
162   int fds[2];
163   ASSERT_NO_FATAL_FAILURE(CreateSocketPair(fds));
164   ScopedFD recv_sock(fds[0]);
165   ScopedFD send_sock(fds[1]);
166 
167   ASSERT_TRUE(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get()));
168 
169   send_sock.reset();
170 
171   char ch;
172   ProcessId sender_pid;
173   std::vector<ScopedFD> recv_fds;
174   const ssize_t nread = UnixDomainSocket::RecvMsgWithPid(
175       recv_sock.get(), &ch, sizeof(ch), &recv_fds, &sender_pid);
176   ASSERT_EQ(0, nread);
177   ASSERT_EQ(-1, sender_pid);
178   ASSERT_EQ(0U, recv_fds.size());
179 }
180 
181 }  // namespace
182 
183 }  // namespace base
184