blob: d4a302e57b8f27068941af0d429df3d3c0eeacd6 [file] [log] [blame]
sehr@google.comfdc90062009-11-26 09:28:02 +09001// 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>
sehr@google.comb72918f2009-12-07 04:45:08 +090011#include <sys/ioctl.h>
sehr@google.comfdc90062009-11-26 09:28:02 +090012#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
erg@google.combf6ce9f2010-01-27 08:08:02 +090066 cleanup:
sehr@google.comfdc90062009-11-26 09:28:02 +090067 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
cpu@chromium.org750392c2009-12-05 07:53:22 +0900102size_t SyncSocket::Peek() {
sehr@google.comb72918f2009-12-07 04:45:08 +0900103 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;
cpu@chromium.org750392c2009-12-05 07:53:22 +0900109}
110
sehr@google.comfdc90062009-11-26 09:28:02 +0900111} // namespace base