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