blob: ff1e0e6caa3329a14f0f14d29889268d98aea343 [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>
[email protected]5d272092012-04-19 10:23:038#include <fcntl.h>
[email protected]62558f12013-10-19 22:13:199#include <limits.h>
grunellb464b3e2017-05-23 11:15:2610#include <poll.h>
avi9b6f42932015-12-26 22:15:1411#include <stddef.h>
[email protected]182c44fa2009-11-26 00:28:0212#include <stdio.h>
[email protected]1e1f1a72009-12-06 19:45:0813#include <sys/ioctl.h>
[email protected]182c44fa2009-11-26 00:28:0214#include <sys/socket.h>
[email protected]5d272092012-04-19 10:23:0315#include <sys/types.h>
[email protected]182c44fa2009-11-26 00:28:0216
[email protected]94f8c952011-06-25 04:54:4117#if defined(OS_SOLARIS)
18#include <sys/filio.h>
19#endif
20
[email protected]e3177dd52014-08-13 20:22:1421#include "base/files/file_util.h"
[email protected]182c44fa2009-11-26 00:28:0222#include "base/logging.h"
[email protected]62558f12013-10-19 22:13:1923#include "base/threading/thread_restrictions.h"
avi9b6f42932015-12-26 22:15:1424#include "build/build_config.h"
[email protected]182c44fa2009-11-26 00:28:0225
26namespace base {
27
28namespace {
29// To avoid users sending negative message lengths to Send/Receive
30// we clamp message lengths, which are size_t, to no more than INT_MAX.
31const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
32
[email protected]62558f12013-10-19 22:13:1933// Writes |length| of |buffer| into |handle|. Returns the number of bytes
34// written or zero on error. |length| must be greater than 0.
35size_t SendHelper(SyncSocket::Handle handle,
36 const void* buffer,
37 size_t length) {
38 DCHECK_GT(length, 0u);
39 DCHECK_LE(length, kMaxMessageLength);
40 DCHECK_NE(handle, SyncSocket::kInvalidHandle);
41 const char* charbuffer = static_cast<const char*>(buffer);
chirantan75ea2fd2014-10-07 23:15:3042 return WriteFileDescriptor(handle, charbuffer, length)
43 ? static_cast<size_t>(length)
44 : 0;
[email protected]62558f12013-10-19 22:13:1945}
46
[email protected]1da08e72013-11-01 19:58:1247bool CloseHandle(SyncSocket::Handle handle) {
48 if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) {
49 DPLOG(ERROR) << "close";
50 return false;
51 }
52
53 return true;
54}
55
[email protected]182c44fa2009-11-26 00:28:0256} // namespace
57
[email protected]5895ee12011-12-22 19:33:2758const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
59
[email protected]532e9bd2012-01-25 12:04:1760SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
61
62SyncSocket::~SyncSocket() {
63 Close();
64}
65
66// static
67bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
[email protected]62558f12013-10-19 22:13:1968 DCHECK_NE(socket_a, socket_b);
69 DCHECK_EQ(socket_a->handle_, kInvalidHandle);
70 DCHECK_EQ(socket_b->handle_, kInvalidHandle);
[email protected]532e9bd2012-01-25 12:04:1771
[email protected]182c44fa2009-11-26 00:28:0272#if defined(OS_MACOSX)
73 int nosigpipe = 1;
74#endif // defined(OS_MACOSX)
75
[email protected]532e9bd2012-01-25 12:04:1776 Handle handles[2] = { kInvalidHandle, kInvalidHandle };
[email protected]1da08e72013-11-01 19:58:1277 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
78 CloseHandle(handles[0]);
79 CloseHandle(handles[1]);
80 return false;
81 }
[email protected]532e9bd2012-01-25 12:04:1782
[email protected]182c44fa2009-11-26 00:28:0283#if defined(OS_MACOSX)
84 // On OSX an attempt to read or write to a closed socket may generate a
85 // SIGPIPE rather than returning -1. setsockopt will shut this off.
86 if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE,
87 &nosigpipe, sizeof nosigpipe) ||
88 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE,
89 &nosigpipe, sizeof nosigpipe)) {
[email protected]1da08e72013-11-01 19:58:1290 CloseHandle(handles[0]);
91 CloseHandle(handles[1]);
92 return false;
[email protected]182c44fa2009-11-26 00:28:0293 }
94#endif
[email protected]532e9bd2012-01-25 12:04:1795
[email protected]182c44fa2009-11-26 00:28:0296 // Copy the handles out for successful return.
[email protected]532e9bd2012-01-25 12:04:1797 socket_a->handle_ = handles[0];
98 socket_b->handle_ = handles[1];
99
[email protected]182c44fa2009-11-26 00:28:02100 return true;
[email protected]182c44fa2009-11-26 00:28:02101}
102
burnik3d670052014-09-08 06:58:22103// static
104SyncSocket::Handle SyncSocket::UnwrapHandle(
105 const TransitDescriptor& descriptor) {
106 return descriptor.fd;
107}
108
109bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle,
110 TransitDescriptor* descriptor) {
111 descriptor->fd = handle();
112 descriptor->auto_close = false;
113 return descriptor->fd != kInvalidHandle;
114}
115
[email protected]182c44fa2009-11-26 00:28:02116bool SyncSocket::Close() {
[email protected]1da08e72013-11-01 19:58:12117 const bool retval = CloseHandle(handle_);
[email protected]182c44fa2009-11-26 00:28:02118 handle_ = kInvalidHandle;
[email protected]1da08e72013-11-01 19:58:12119 return retval;
[email protected]182c44fa2009-11-26 00:28:02120}
121
122size_t SyncSocket::Send(const void* buffer, size_t length) {
Francois Doray66bdfd82017-10-20 13:50:37123 AssertBlockingAllowed();
[email protected]62558f12013-10-19 22:13:19124 return SendHelper(handle_, buffer, length);
[email protected]182c44fa2009-11-26 00:28:02125}
126
127size_t SyncSocket::Receive(void* buffer, size_t length) {
Francois Doray66bdfd82017-10-20 13:50:37128 AssertBlockingAllowed();
[email protected]62558f12013-10-19 22:13:19129 DCHECK_GT(length, 0u);
[email protected]d7a93ad2011-04-22 13:13:07130 DCHECK_LE(length, kMaxMessageLength);
[email protected]62558f12013-10-19 22:13:19131 DCHECK_NE(handle_, kInvalidHandle);
[email protected]182c44fa2009-11-26 00:28:02132 char* charbuffer = static_cast<char*>(buffer);
[email protected]b264eab2013-11-27 23:22:08133 if (ReadFromFD(handle_, charbuffer, length))
[email protected]182c44fa2009-11-26 00:28:02134 return length;
[email protected]532e9bd2012-01-25 12:04:17135 return 0;
[email protected]182c44fa2009-11-26 00:28:02136}
137
[email protected]62558f12013-10-19 22:13:19138size_t SyncSocket::ReceiveWithTimeout(void* buffer,
139 size_t length,
140 TimeDelta timeout) {
Francois Doray66bdfd82017-10-20 13:50:37141 AssertBlockingAllowed();
[email protected]62558f12013-10-19 22:13:19142 DCHECK_GT(length, 0u);
143 DCHECK_LE(length, kMaxMessageLength);
144 DCHECK_NE(handle_, kInvalidHandle);
[email protected]1bc4379e2013-11-05 00:14:27145
[email protected]62558f12013-10-19 22:13:19146 // Only timeouts greater than zero and less than one second are allowed.
147 DCHECK_GT(timeout.InMicroseconds(), 0);
148 DCHECK_LT(timeout.InMicroseconds(),
grunellb464b3e2017-05-23 11:15:26149 TimeDelta::FromSeconds(1).InMicroseconds());
[email protected]62558f12013-10-19 22:13:19150
151 // Track the start time so we can reduce the timeout as data is read.
152 TimeTicks start_time = TimeTicks::Now();
153 const TimeTicks finish_time = start_time + timeout;
154
grunellb464b3e2017-05-23 11:15:26155 struct pollfd pollfd;
156 pollfd.fd = handle_;
157 pollfd.events = POLLIN;
158 pollfd.revents = 0;
[email protected]62558f12013-10-19 22:13:19159
grunellb464b3e2017-05-23 11:15:26160 size_t bytes_read_total = 0;
161 while (bytes_read_total < length) {
162 const TimeDelta this_timeout = finish_time - TimeTicks::Now();
163 const int timeout_ms =
164 static_cast<int>(this_timeout.InMillisecondsRoundedUp());
165 if (timeout_ms <= 0)
166 break;
167 const int poll_result = poll(&pollfd, 1, timeout_ms);
[email protected]62558f12013-10-19 22:13:19168 // Handle EINTR manually since we need to update the timeout value.
grunellb464b3e2017-05-23 11:15:26169 if (poll_result == -1 && errno == EINTR)
[email protected]62558f12013-10-19 22:13:19170 continue;
grunellb464b3e2017-05-23 11:15:26171 // Return if other type of error or a timeout.
172 if (poll_result <= 0)
[email protected]62558f12013-10-19 22:13:19173 return bytes_read_total;
174
grunellb464b3e2017-05-23 11:15:26175 // poll() only tells us that data is ready for reading, not how much. We
[email protected]62558f12013-10-19 22:13:19176 // must Peek() for the amount ready for reading to avoid blocking.
grunellb464b3e2017-05-23 11:15:26177 // At hang up (POLLHUP), the write end has been closed and there might still
178 // be data to be read.
179 // No special handling is needed for error (POLLERR); we can let any of the
180 // following operations fail and handle it there.
181 DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents;
[email protected]62558f12013-10-19 22:13:19182 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
183
184 // There may be zero bytes to read if the socket at the other end closed.
185 if (!bytes_to_read)
186 return bytes_read_total;
187
188 const size_t bytes_received =
189 Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
190 bytes_read_total += bytes_received;
191 if (bytes_received != bytes_to_read)
192 return bytes_read_total;
193 }
194
195 return bytes_read_total;
196}
197
[email protected]d8b65912009-12-04 22:53:22198size_t SyncSocket::Peek() {
[email protected]62558f12013-10-19 22:13:19199 DCHECK_NE(handle_, kInvalidHandle);
200 int number_chars = 0;
201 if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
[email protected]1e1f1a72009-12-06 19:45:08202 // If there is an error in ioctl, signal that the channel would block.
203 return 0;
204 }
[email protected]62558f12013-10-19 22:13:19205 DCHECK_GE(number_chars, 0);
206 return number_chars;
[email protected]d8b65912009-12-04 22:53:22207}
208
maxmorind4bcb112017-04-13 11:43:13209SyncSocket::Handle SyncSocket::Release() {
210 Handle r = handle_;
211 handle_ = kInvalidHandle;
212 return r;
213}
214
Chris Watkinsbb7211c2017-11-29 07:16:38215CancelableSyncSocket::CancelableSyncSocket() = default;
[email protected]532e9bd2012-01-25 12:04:17216CancelableSyncSocket::CancelableSyncSocket(Handle handle)
217 : SyncSocket(handle) {
218}
219
220bool CancelableSyncSocket::Shutdown() {
[email protected]62558f12013-10-19 22:13:19221 DCHECK_NE(handle_, kInvalidHandle);
222 return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0;
[email protected]532e9bd2012-01-25 12:04:17223}
224
[email protected]5d272092012-04-19 10:23:03225size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
[email protected]62558f12013-10-19 22:13:19226 DCHECK_GT(length, 0u);
227 DCHECK_LE(length, kMaxMessageLength);
228 DCHECK_NE(handle_, kInvalidHandle);
229
tfarina5967e5a2016-03-09 14:40:13230 const int flags = fcntl(handle_, F_GETFL);
[email protected]5d272092012-04-19 10:23:03231 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
232 // Set the socket to non-blocking mode for sending if its original mode
233 // is blocking.
234 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
235 }
236
[email protected]62558f12013-10-19 22:13:19237 const size_t len = SendHelper(handle_, buffer, length);
[email protected]5d272092012-04-19 10:23:03238
239 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
240 // Restore the original flags.
241 fcntl(handle_, F_SETFL, flags);
242 }
243
244 return len;
245}
246
[email protected]532e9bd2012-01-25 12:04:17247// static
248bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
249 CancelableSyncSocket* socket_b) {
250 return SyncSocket::CreatePair(socket_a, socket_b);
251}
252
[email protected]182c44fa2009-11-26 00:28:02253} // namespace base