blob: 257916df3357a21962a4f6b9fe7561a09c286d50 [file] [log] [blame]
[email protected]532e9bd2012-01-25 12:04:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]182c44fa2009-11-26 00:28:022// 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>
[email protected]5d272092012-04-19 10:23:039#include <fcntl.h>
[email protected]182c44fa2009-11-26 00:28:0210#include <stdio.h>
[email protected]1e1f1a72009-12-06 19:45:0811#include <sys/ioctl.h>
[email protected]182c44fa2009-11-26 00:28:0212#include <sys/socket.h>
[email protected]5d272092012-04-19 10:23:0313#include <sys/types.h>
[email protected]182c44fa2009-11-26 00:28:0214
[email protected]94f8c952011-06-25 04:54:4115#if defined(OS_SOLARIS)
16#include <sys/filio.h>
17#endif
18
[email protected]182c44fa2009-11-26 00:28:0219#include "base/file_util.h"
20#include "base/logging.h"
21
22
23namespace base {
24
25namespace {
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.
28const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
29
[email protected]182c44fa2009-11-26 00:28:0230} // namespace
31
[email protected]5895ee12011-12-22 19:33:2732const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
33
[email protected]532e9bd2012-01-25 12:04:1734SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
35
36SyncSocket::~SyncSocket() {
37 Close();
38}
39
40// static
41bool 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
[email protected]182c44fa2009-11-26 00:28:0246#if defined(OS_MACOSX)
47 int nosigpipe = 1;
48#endif // defined(OS_MACOSX)
49
[email protected]532e9bd2012-01-25 12:04:1750 Handle handles[2] = { kInvalidHandle, kInvalidHandle };
51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0)
[email protected]182c44fa2009-11-26 00:28:0252 goto cleanup;
[email protected]532e9bd2012-01-25 12:04:1753
[email protected]182c44fa2009-11-26 00:28:0254#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
[email protected]532e9bd2012-01-25 12:04:1764
[email protected]182c44fa2009-11-26 00:28:0265 // Copy the handles out for successful return.
[email protected]532e9bd2012-01-25 12:04:1766 socket_a->handle_ = handles[0];
67 socket_b->handle_ = handles[1];
68
[email protected]182c44fa2009-11-26 00:28:0269 return true;
70
[email protected]2fdc86a2010-01-26 23:08:0271 cleanup:
[email protected]be60b9a2011-02-23 21:37:4172 if (handles[0] != kInvalidHandle) {
73 if (HANDLE_EINTR(close(handles[0])) < 0)
[email protected]a42d4632011-10-26 21:48:0074 DPLOG(ERROR) << "close";
[email protected]be60b9a2011-02-23 21:37:4175 }
76 if (handles[1] != kInvalidHandle) {
77 if (HANDLE_EINTR(close(handles[1])) < 0)
[email protected]a42d4632011-10-26 21:48:0078 DPLOG(ERROR) << "close";
[email protected]be60b9a2011-02-23 21:37:4179 }
[email protected]532e9bd2012-01-25 12:04:1780
[email protected]182c44fa2009-11-26 00:28:0281 return false;
82}
83
84bool SyncSocket::Close() {
85 if (handle_ == kInvalidHandle) {
86 return false;
87 }
[email protected]be60b9a2011-02-23 21:37:4188 int retval = HANDLE_EINTR(close(handle_));
89 if (retval < 0)
[email protected]a42d4632011-10-26 21:48:0090 DPLOG(ERROR) << "close";
[email protected]182c44fa2009-11-26 00:28:0291 handle_ = kInvalidHandle;
92 return (retval == 0);
93}
94
95size_t SyncSocket::Send(const void* buffer, size_t length) {
[email protected]d7a93ad2011-04-22 13:13:0796 DCHECK_LE(length, kMaxMessageLength);
[email protected]182c44fa2009-11-26 00:28:0297 const char* charbuffer = static_cast<const char*>(buffer);
98 int len = file_util::WriteFileDescriptor(handle_, charbuffer, length);
[email protected]5d272092012-04-19 10:23:0399
100 return (len == -1) ? 0 : static_cast<size_t>(len);
[email protected]182c44fa2009-11-26 00:28:02101}
102
103size_t SyncSocket::Receive(void* buffer, size_t length) {
[email protected]d7a93ad2011-04-22 13:13:07104 DCHECK_LE(length, kMaxMessageLength);
[email protected]182c44fa2009-11-26 00:28:02105 char* charbuffer = static_cast<char*>(buffer);
[email protected]532e9bd2012-01-25 12:04:17106 if (file_util::ReadFromFD(handle_, charbuffer, length))
[email protected]182c44fa2009-11-26 00:28:02107 return length;
[email protected]532e9bd2012-01-25 12:04:17108 return 0;
[email protected]182c44fa2009-11-26 00:28:02109}
110
[email protected]d8b65912009-12-04 22:53:22111size_t SyncSocket::Peek() {
[email protected]1e1f1a72009-12-06 19:45:08112 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;
[email protected]d8b65912009-12-04 22:53:22118}
119
[email protected]532e9bd2012-01-25 12:04:17120CancelableSyncSocket::CancelableSyncSocket() {}
121CancelableSyncSocket::CancelableSyncSocket(Handle handle)
122 : SyncSocket(handle) {
123}
124
125bool CancelableSyncSocket::Shutdown() {
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
127}
128
[email protected]5d272092012-04-19 10:23:03129size_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
[email protected]532e9bd2012-01-25 12:04:17148// static
149bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
150 CancelableSyncSocket* socket_b) {
151 return SyncSocket::CreatePair(socket_a, socket_b);
152}
153
[email protected]182c44fa2009-11-26 00:28:02154} // namespace base