sync_socket_posix.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2009 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 <stdio.h>
10#include <sys/types.h>
11#include <sys/ioctl.h>
12#include <sys/socket.h>
13
14#include "base/file_util.h"
15#include "base/logging.h"
16
17
18namespace base {
19
20namespace {
21// To avoid users sending negative message lengths to Send/Receive
22// we clamp message lengths, which are size_t, to no more than INT_MAX.
23const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
24
25static const SyncSocket::Handle kInvalidHandle = -1;
26
27}  // namespace
28
29bool SyncSocket::CreatePair(SyncSocket* pair[2]) {
30  Handle handles[2] = { kInvalidHandle, kInvalidHandle };
31  SyncSocket* tmp_sockets[2] = { NULL, NULL };
32#if defined(OS_MACOSX)
33  int nosigpipe = 1;
34#endif  // defined(OS_MACOSX)
35
36  // Create the two SyncSocket objects first to avoid ugly cleanup issues.
37  tmp_sockets[0] = new SyncSocket(kInvalidHandle);
38  if (tmp_sockets[0] == NULL) {
39    goto cleanup;
40  }
41  tmp_sockets[1] = new SyncSocket(kInvalidHandle);
42  if (tmp_sockets[1] == NULL) {
43    goto cleanup;
44  }
45  if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
46    goto cleanup;
47  }
48#if defined(OS_MACOSX)
49  // On OSX an attempt to read or write to a closed socket may generate a
50  // SIGPIPE rather than returning -1.  setsockopt will shut this off.
51  if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
52                      &nosigpipe, sizeof nosigpipe) ||
53      0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
54                      &nosigpipe, sizeof nosigpipe)) {
55    goto cleanup;
56  }
57#endif
58  // Copy the handles out for successful return.
59  tmp_sockets[0]->handle_ = handles[0];
60  pair[0] = tmp_sockets[0];
61  tmp_sockets[1]->handle_ = handles[1];
62  pair[1] = tmp_sockets[1];
63  return true;
64
65 cleanup:
66  if (handles[0] != kInvalidHandle)
67    (void) close(handles[0]);
68  if (handles[1] != kInvalidHandle)
69    (void) close(handles[1]);
70  delete tmp_sockets[0];
71  delete tmp_sockets[1];
72  return false;
73}
74
75bool SyncSocket::Close() {
76  if (handle_ == kInvalidHandle) {
77    return false;
78  }
79  int retval = close(handle_);
80  handle_ = kInvalidHandle;
81  return (retval == 0);
82}
83
84size_t SyncSocket::Send(const void* buffer, size_t length) {
85  DCHECK(length <= kMaxMessageLength);
86  const char* charbuffer = static_cast<const char*>(buffer);
87  int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
88  return static_cast<size_t>(len);
89}
90
91size_t SyncSocket::Receive(void* buffer, size_t length) {
92  DCHECK(length <= kMaxMessageLength);
93  char* charbuffer = static_cast<char*>(buffer);
94  if (file_util::ReadFromFD(handle_, charbuffer, length)) {
95    return length;
96  } else {
97    return -1;
98  }
99}
100
101size_t SyncSocket::Peek() {
102  int number_chars;
103  if (-1 == ioctl(handle_, FIONREAD, &number_chars)) {
104    // If there is an error in ioctl, signal that the channel would block.
105    return 0;
106  }
107  return (size_t) number_chars;
108}
109
110}  // namespace base
111