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 <limits.h>
      9 #include <fcntl.h>
     10 #include <stdio.h>
     11 #include <sys/ioctl.h>
     12 #include <sys/socket.h>
     13 #include <sys/types.h>
     14 
     15 #if defined(OS_SOLARIS)
     16 #include <sys/filio.h>
     17 #endif
     18 
     19 #include "base/file_util.h"
     20 #include "base/logging.h"
     21 
     22 
     23 namespace base {
     24 
     25 namespace {
     26 // To avoid users sending negative message lengths to Send/Receive
     27 // we clamp message lengths, which are size_t, to no more than INT_MAX.
     28 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
     29 
     30 }  // namespace
     31 
     32 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
     33 
     34 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
     35 
     36 SyncSocket::~SyncSocket() {
     37   Close();
     38 }
     39 
     40 // static
     41 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
     42   DCHECK(socket_a != socket_b);
     43   DCHECK(socket_a->handle_ == kInvalidHandle);
     44   DCHECK(socket_b->handle_ == kInvalidHandle);
     45 
     46 #if defined(OS_MACOSX)
     47   int nosigpipe = 1;
     48 #endif  // defined(OS_MACOSX)
     49 
     50   Handle handles[2] = { kInvalidHandle, kInvalidHandle };
     51   if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0)
     52     goto cleanup;
     53 
     54 #if defined(OS_MACOSX)
     55   // On OSX an attempt to read or write to a closed socket may generate a
     56   // SIGPIPE rather than returning -1.  setsockopt will shut this off.
     57   if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
     58                       &nosigpipe, sizeof nosigpipe) ||
     59       0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
     60                       &nosigpipe, sizeof nosigpipe)) {
     61     goto cleanup;
     62   }
     63 #endif
     64 
     65   // Copy the handles out for successful return.
     66   socket_a->handle_ = handles[0];
     67   socket_b->handle_ = handles[1];
     68 
     69   return true;
     70 
     71  cleanup:
     72   if (handles[0] != kInvalidHandle) {
     73     if (HANDLE_EINTR(close(handles[0])) < 0)
     74       DPLOG(ERROR) << "close";
     75   }
     76   if (handles[1] != kInvalidHandle) {
     77     if (HANDLE_EINTR(close(handles[1])) < 0)
     78       DPLOG(ERROR) << "close";
     79   }
     80 
     81   return false;
     82 }
     83 
     84 bool SyncSocket::Close() {
     85   if (handle_ == kInvalidHandle) {
     86     return false;
     87   }
     88   int retval = HANDLE_EINTR(close(handle_));
     89   if (retval < 0)
     90     DPLOG(ERROR) << "close";
     91   handle_ = kInvalidHandle;
     92   return (retval == 0);
     93 }
     94 
     95 size_t SyncSocket::Send(const void* buffer, size_t length) {
     96   DCHECK_LE(length, kMaxMessageLength);
     97   const char* charbuffer = static_cast<const char*>(buffer);
     98   int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
     99 
    100   return (len == -1) ? 0 : static_cast<size_t>(len);
    101 }
    102 
    103 size_t SyncSocket::Receive(void* buffer, size_t length) {
    104   DCHECK_LE(length, kMaxMessageLength);
    105   char* charbuffer = static_cast<char*>(buffer);
    106   if (file_util::ReadFromFD(handle_, charbuffer, length))
    107     return length;
    108   return 0;
    109 }
    110 
    111 size_t SyncSocket::Peek() {
    112   int number_chars;
    113   if (-1 == ioctl(handle_, FIONREAD, &number_chars)) {
    114     // If there is an error in ioctl, signal that the channel would block.
    115     return 0;
    116   }
    117   return (size_t) number_chars;
    118 }
    119 
    120 CancelableSyncSocket::CancelableSyncSocket() {}
    121 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
    122     : SyncSocket(handle) {
    123 }
    124 
    125 bool CancelableSyncSocket::Shutdown() {
    126   return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
    127 }
    128 
    129 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
    130   long flags = 0;
    131   flags = fcntl(handle_, F_GETFL, NULL);
    132   if (flags != -1 && (flags & O_NONBLOCK) == 0) {
    133     // Set the socket to non-blocking mode for sending if its original mode
    134     // is blocking.
    135     fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
    136   }
    137 
    138   size_t len = SyncSocket::Send(buffer, length);
    139 
    140   if (flags != -1 && (flags & O_NONBLOCK) == 0) {
    141     // Restore the original flags.
    142     fcntl(handle_, F_SETFL, flags);
    143   }
    144 
    145   return len;
    146 }
    147 
    148 // static
    149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
    150                                       CancelableSyncSocket* socket_b) {
    151   return SyncSocket::CreatePair(socket_a, socket_b);
    152 }
    153 
    154 }  // namespace base
    155