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