Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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 "base/sync_socket.h"
      6 
      7 #include <errno.h>
      8 #include <fcntl.h>
      9 #include <limits.h>
     10 #include <poll.h>
     11 #include <stddef.h>
     12 #include <stdio.h>
     13 #include <sys/ioctl.h>
     14 #include <sys/socket.h>
     15 #include <sys/types.h>
     16 
     17 #if defined(OS_SOLARIS)
     18 #include <sys/filio.h>
     19 #endif
     20 
     21 #include "base/files/file_util.h"
     22 #include "base/logging.h"
     23 #include "base/threading/thread_restrictions.h"
     24 #include "build/build_config.h"
     25 
     26 namespace base {
     27 
     28 namespace {
     29 // To avoid users sending negative message lengths to Send/Receive
     30 // we clamp message lengths, which are size_t, to no more than INT_MAX.
     31 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
     32 
     33 // Writes |length| of |buffer| into |handle|.  Returns the number of bytes
     34 // written or zero on error.  |length| must be greater than 0.
     35 size_t SendHelper(SyncSocket::Handle handle,
     36                   const void* buffer,
     37                   size_t length) {
     38   DCHECK_GT(length, 0u);
     39   DCHECK_LE(length, kMaxMessageLength);
     40   DCHECK_NE(handle, SyncSocket::kInvalidHandle);
     41   const char* charbuffer = static_cast<const char*>(buffer);
     42   return WriteFileDescriptor(handle, charbuffer, length)
     43              ? static_cast<size_t>(length)
     44              : 0;
     45 }
     46 
     47 bool CloseHandle(SyncSocket::Handle handle) {
     48   if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) {
     49     DPLOG(ERROR) << "close";
     50     return false;
     51   }
     52 
     53   return true;
     54 }
     55 
     56 }  // namespace
     57 
     58 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
     59 
     60 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
     61 
     62 SyncSocket::~SyncSocket() {
     63   Close();
     64 }
     65 
     66 // static
     67 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
     68   DCHECK_NE(socket_a, socket_b);
     69   DCHECK_EQ(socket_a->handle_, kInvalidHandle);
     70   DCHECK_EQ(socket_b->handle_, kInvalidHandle);
     71 
     72 #if defined(OS_MACOSX)
     73   int nosigpipe = 1;
     74 #endif  // defined(OS_MACOSX)
     75 
     76   Handle handles[2] = { kInvalidHandle, kInvalidHandle };
     77   if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
     78     CloseHandle(handles[0]);
     79     CloseHandle(handles[1]);
     80     return false;
     81   }
     82 
     83 #if defined(OS_MACOSX)
     84   // On OSX an attempt to read or write to a closed socket may generate a
     85   // SIGPIPE rather than returning -1.  setsockopt will shut this off.
     86   if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
     87                       &nosigpipe, sizeof nosigpipe) ||
     88       0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
     89                       &nosigpipe, sizeof nosigpipe)) {
     90     CloseHandle(handles[0]);
     91     CloseHandle(handles[1]);
     92     return false;
     93   }
     94 #endif
     95 
     96   // Copy the handles out for successful return.
     97   socket_a->handle_ = handles[0];
     98   socket_b->handle_ = handles[1];
     99 
    100   return true;
    101 }
    102 
    103 // static
    104 SyncSocket::Handle SyncSocket::UnwrapHandle(
    105     const TransitDescriptor& descriptor) {
    106   return descriptor.fd;
    107 }
    108 
    109 bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle,
    110                                           TransitDescriptor* descriptor) {
    111   descriptor->fd = handle();
    112   descriptor->auto_close = false;
    113   return descriptor->fd != kInvalidHandle;
    114 }
    115 
    116 bool SyncSocket::Close() {
    117   const bool retval = CloseHandle(handle_);
    118   handle_ = kInvalidHandle;
    119   return retval;
    120 }
    121 
    122 size_t SyncSocket::Send(const void* buffer, size_t length) {
    123   AssertBlockingAllowed();
    124   return SendHelper(handle_, buffer, length);
    125 }
    126 
    127 size_t SyncSocket::Receive(void* buffer, size_t length) {
    128   AssertBlockingAllowed();
    129   DCHECK_GT(length, 0u);
    130   DCHECK_LE(length, kMaxMessageLength);
    131   DCHECK_NE(handle_, kInvalidHandle);
    132   char* charbuffer = static_cast<char*>(buffer);
    133   if (ReadFromFD(handle_, charbuffer, length))
    134     return length;
    135   return 0;
    136 }
    137 
    138 size_t SyncSocket::ReceiveWithTimeout(void* buffer,
    139                                       size_t length,
    140                                       TimeDelta timeout) {
    141   AssertBlockingAllowed();
    142   DCHECK_GT(length, 0u);
    143   DCHECK_LE(length, kMaxMessageLength);
    144   DCHECK_NE(handle_, kInvalidHandle);
    145 
    146   // Only timeouts greater than zero and less than one second are allowed.
    147   DCHECK_GT(timeout.InMicroseconds(), 0);
    148   DCHECK_LT(timeout.InMicroseconds(),
    149             TimeDelta::FromSeconds(1).InMicroseconds());
    150 
    151   // Track the start time so we can reduce the timeout as data is read.
    152   TimeTicks start_time = TimeTicks::Now();
    153   const TimeTicks finish_time = start_time + timeout;
    154 
    155   struct pollfd pollfd;
    156   pollfd.fd = handle_;
    157   pollfd.events = POLLIN;
    158   pollfd.revents = 0;
    159 
    160   size_t bytes_read_total = 0;
    161   while (bytes_read_total < length) {
    162     const TimeDelta this_timeout = finish_time - TimeTicks::Now();
    163     const int timeout_ms =
    164         static_cast<int>(this_timeout.InMillisecondsRoundedUp());
    165     if (timeout_ms <= 0)
    166       break;
    167     const int poll_result = poll(&pollfd, 1, timeout_ms);
    168     // Handle EINTR manually since we need to update the timeout value.
    169     if (poll_result == -1 && errno == EINTR)
    170       continue;
    171     // Return if other type of error or a timeout.
    172     if (poll_result <= 0)
    173       return bytes_read_total;
    174 
    175     // poll() only tells us that data is ready for reading, not how much.  We
    176     // must Peek() for the amount ready for reading to avoid blocking.
    177     // At hang up (POLLHUP), the write end has been closed and there might still
    178     // be data to be read.
    179     // No special handling is needed for error (POLLERR); we can let any of the
    180     // following operations fail and handle it there.
    181     DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents;
    182     const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
    183 
    184     // There may be zero bytes to read if the socket at the other end closed.
    185     if (!bytes_to_read)
    186       return bytes_read_total;
    187 
    188     const size_t bytes_received =
    189         Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
    190     bytes_read_total += bytes_received;
    191     if (bytes_received != bytes_to_read)
    192       return bytes_read_total;
    193   }
    194 
    195   return bytes_read_total;
    196 }
    197 
    198 size_t SyncSocket::Peek() {
    199   DCHECK_NE(handle_, kInvalidHandle);
    200   int number_chars = 0;
    201   if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
    202     // If there is an error in ioctl, signal that the channel would block.
    203     return 0;
    204   }
    205   DCHECK_GE(number_chars, 0);
    206   return number_chars;
    207 }
    208 
    209 SyncSocket::Handle SyncSocket::Release() {
    210   Handle r = handle_;
    211   handle_ = kInvalidHandle;
    212   return r;
    213 }
    214 
    215 CancelableSyncSocket::CancelableSyncSocket() = default;
    216 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
    217     : SyncSocket(handle) {
    218 }
    219 
    220 bool CancelableSyncSocket::Shutdown() {
    221   DCHECK_NE(handle_, kInvalidHandle);
    222   return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0;
    223 }
    224 
    225 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
    226   DCHECK_GT(length, 0u);
    227   DCHECK_LE(length, kMaxMessageLength);
    228   DCHECK_NE(handle_, kInvalidHandle);
    229 
    230   const int flags = fcntl(handle_, F_GETFL);
    231   if (flags != -1 && (flags & O_NONBLOCK) == 0) {
    232     // Set the socket to non-blocking mode for sending if its original mode
    233     // is blocking.
    234     fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
    235   }
    236 
    237   const size_t len = SendHelper(handle_, buffer, length);
    238 
    239   if (flags != -1 && (flags & O_NONBLOCK) == 0) {
    240     // Restore the original flags.
    241     fcntl(handle_, F_SETFL, flags);
    242   }
    243 
    244   return len;
    245 }
    246 
    247 // static
    248 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
    249                                       CancelableSyncSocket* socket_b) {
    250   return SyncSocket::CreatePair(socket_a, socket_b);
    251 }
    252 
    253 }  // namespace base
    254