blob: 4f30690eeae3df6c517bbfbd00e363486fb6ff28 [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
Hans Wennborgc3cffa62020-04-27 10:09:1221#include "base/check_op.h"
[email protected]e3177dd52014-08-13 20:22:1422#include "base/files/file_util.h"
Etienne Pierre-Doray3879b052018-09-17 14:17:2223#include "base/threading/scoped_blocking_call.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]182c44fa2009-11-26 00:28:0247} // namespace
48
[email protected]532e9bd2012-01-25 12:04:1749// static
50bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
[email protected]62558f12013-10-19 22:13:1951 DCHECK_NE(socket_a, socket_b);
Robert Sesek6ab73b022020-02-13 16:42:3952 DCHECK(!socket_a->IsValid());
53 DCHECK(!socket_b->IsValid());
[email protected]532e9bd2012-01-25 12:04:1754
[email protected]182c44fa2009-11-26 00:28:0255#if defined(OS_MACOSX)
56 int nosigpipe = 1;
57#endif // defined(OS_MACOSX)
58
Robert Sesek6ab73b022020-02-13 16:42:3959 ScopedHandle handles[2];
60
61 {
62 Handle raw_handles[2] = {kInvalidHandle, kInvalidHandle};
63 if (socketpair(AF_UNIX, SOCK_STREAM, 0, raw_handles) != 0) {
64 return false;
65 }
66 handles[0].reset(raw_handles[0]);
67 handles[1].reset(raw_handles[1]);
[email protected]1da08e72013-11-01 19:58:1268 }
[email protected]532e9bd2012-01-25 12:04:1769
[email protected]182c44fa2009-11-26 00:28:0270#if defined(OS_MACOSX)
71 // On OSX an attempt to read or write to a closed socket may generate a
72 // SIGPIPE rather than returning -1. setsockopt will shut this off.
Robert Sesek6ab73b022020-02-13 16:42:3973 if (0 != setsockopt(handles[0].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe,
74 sizeof(nosigpipe)) ||
75 0 != setsockopt(handles[1].get(), SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe,
76 sizeof(nosigpipe))) {
[email protected]1da08e72013-11-01 19:58:1277 return false;
[email protected]182c44fa2009-11-26 00:28:0278 }
79#endif
[email protected]532e9bd2012-01-25 12:04:1780
[email protected]182c44fa2009-11-26 00:28:0281 // Copy the handles out for successful return.
Robert Sesek6ab73b022020-02-13 16:42:3982 socket_a->handle_ = std::move(handles[0]);
83 socket_b->handle_ = std::move(handles[1]);
[email protected]532e9bd2012-01-25 12:04:1784
[email protected]182c44fa2009-11-26 00:28:0285 return true;
[email protected]182c44fa2009-11-26 00:28:0286}
87
Robert Sesek6ab73b022020-02-13 16:42:3988void SyncSocket::Close() {
89 handle_.reset();
[email protected]182c44fa2009-11-26 00:28:0290}
91
92size_t SyncSocket::Send(const void* buffer, size_t length) {
Etienne Bergeron436d42212019-02-26 17:15:1293 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
Robert Sesek6ab73b022020-02-13 16:42:3994 return SendHelper(handle(), buffer, length);
[email protected]182c44fa2009-11-26 00:28:0295}
96
97size_t SyncSocket::Receive(void* buffer, size_t length) {
Etienne Bergeron436d42212019-02-26 17:15:1298 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
[email protected]62558f12013-10-19 22:13:1999 DCHECK_GT(length, 0u);
[email protected]d7a93ad2011-04-22 13:13:07100 DCHECK_LE(length, kMaxMessageLength);
Robert Sesek6ab73b022020-02-13 16:42:39101 DCHECK(IsValid());
[email protected]182c44fa2009-11-26 00:28:02102 char* charbuffer = static_cast<char*>(buffer);
Robert Sesek6ab73b022020-02-13 16:42:39103 if (ReadFromFD(handle(), charbuffer, length))
[email protected]182c44fa2009-11-26 00:28:02104 return length;
[email protected]532e9bd2012-01-25 12:04:17105 return 0;
[email protected]182c44fa2009-11-26 00:28:02106}
107
[email protected]62558f12013-10-19 22:13:19108size_t SyncSocket::ReceiveWithTimeout(void* buffer,
109 size_t length,
110 TimeDelta timeout) {
Etienne Bergeron436d42212019-02-26 17:15:12111 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
[email protected]62558f12013-10-19 22:13:19112 DCHECK_GT(length, 0u);
113 DCHECK_LE(length, kMaxMessageLength);
Robert Sesek6ab73b022020-02-13 16:42:39114 DCHECK(IsValid());
[email protected]1bc4379e2013-11-05 00:14:27115
[email protected]62558f12013-10-19 22:13:19116 // Only timeouts greater than zero and less than one second are allowed.
117 DCHECK_GT(timeout.InMicroseconds(), 0);
118 DCHECK_LT(timeout.InMicroseconds(),
grunellb464b3e2017-05-23 11:15:26119 TimeDelta::FromSeconds(1).InMicroseconds());
[email protected]62558f12013-10-19 22:13:19120
121 // Track the start time so we can reduce the timeout as data is read.
122 TimeTicks start_time = TimeTicks::Now();
123 const TimeTicks finish_time = start_time + timeout;
124
grunellb464b3e2017-05-23 11:15:26125 struct pollfd pollfd;
Robert Sesek6ab73b022020-02-13 16:42:39126 pollfd.fd = handle();
grunellb464b3e2017-05-23 11:15:26127 pollfd.events = POLLIN;
128 pollfd.revents = 0;
[email protected]62558f12013-10-19 22:13:19129
grunellb464b3e2017-05-23 11:15:26130 size_t bytes_read_total = 0;
131 while (bytes_read_total < length) {
132 const TimeDelta this_timeout = finish_time - TimeTicks::Now();
133 const int timeout_ms =
134 static_cast<int>(this_timeout.InMillisecondsRoundedUp());
135 if (timeout_ms <= 0)
136 break;
137 const int poll_result = poll(&pollfd, 1, timeout_ms);
[email protected]62558f12013-10-19 22:13:19138 // Handle EINTR manually since we need to update the timeout value.
grunellb464b3e2017-05-23 11:15:26139 if (poll_result == -1 && errno == EINTR)
[email protected]62558f12013-10-19 22:13:19140 continue;
grunellb464b3e2017-05-23 11:15:26141 // Return if other type of error or a timeout.
142 if (poll_result <= 0)
[email protected]62558f12013-10-19 22:13:19143 return bytes_read_total;
144
grunellb464b3e2017-05-23 11:15:26145 // poll() only tells us that data is ready for reading, not how much. We
[email protected]62558f12013-10-19 22:13:19146 // must Peek() for the amount ready for reading to avoid blocking.
grunellb464b3e2017-05-23 11:15:26147 // At hang up (POLLHUP), the write end has been closed and there might still
148 // be data to be read.
149 // No special handling is needed for error (POLLERR); we can let any of the
150 // following operations fail and handle it there.
151 DCHECK(pollfd.revents & (POLLIN | POLLHUP | POLLERR)) << pollfd.revents;
[email protected]62558f12013-10-19 22:13:19152 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
153
154 // There may be zero bytes to read if the socket at the other end closed.
155 if (!bytes_to_read)
156 return bytes_read_total;
157
158 const size_t bytes_received =
159 Receive(static_cast<char*>(buffer) + bytes_read_total, bytes_to_read);
160 bytes_read_total += bytes_received;
161 if (bytes_received != bytes_to_read)
162 return bytes_read_total;
163 }
164
165 return bytes_read_total;
166}
167
[email protected]d8b65912009-12-04 22:53:22168size_t SyncSocket::Peek() {
Robert Sesek6ab73b022020-02-13 16:42:39169 DCHECK(IsValid());
[email protected]62558f12013-10-19 22:13:19170 int number_chars = 0;
Robert Sesek6ab73b022020-02-13 16:42:39171 if (ioctl(handle_.get(), FIONREAD, &number_chars) == -1) {
[email protected]1e1f1a72009-12-06 19:45:08172 // If there is an error in ioctl, signal that the channel would block.
173 return 0;
174 }
[email protected]62558f12013-10-19 22:13:19175 DCHECK_GE(number_chars, 0);
176 return number_chars;
[email protected]d8b65912009-12-04 22:53:22177}
178
Robert Sesek6ab73b022020-02-13 16:42:39179bool SyncSocket::IsValid() const {
180 return handle_.is_valid();
maxmorind4bcb112017-04-13 11:43:13181}
182
Robert Sesek6ab73b022020-02-13 16:42:39183SyncSocket::Handle SyncSocket::handle() const {
184 return handle_.get();
185}
186
187SyncSocket::Handle SyncSocket::Release() {
188 return handle_.release();
[email protected]532e9bd2012-01-25 12:04:17189}
190
191bool CancelableSyncSocket::Shutdown() {
Robert Sesek6ab73b022020-02-13 16:42:39192 DCHECK(IsValid());
193 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0;
[email protected]532e9bd2012-01-25 12:04:17194}
195
[email protected]5d272092012-04-19 10:23:03196size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
[email protected]62558f12013-10-19 22:13:19197 DCHECK_GT(length, 0u);
198 DCHECK_LE(length, kMaxMessageLength);
Robert Sesek6ab73b022020-02-13 16:42:39199 DCHECK(IsValid());
[email protected]62558f12013-10-19 22:13:19200
Robert Sesek6ab73b022020-02-13 16:42:39201 const int flags = fcntl(handle(), F_GETFL);
[email protected]5d272092012-04-19 10:23:03202 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
203 // Set the socket to non-blocking mode for sending if its original mode
204 // is blocking.
Robert Sesek6ab73b022020-02-13 16:42:39205 fcntl(handle(), F_SETFL, flags | O_NONBLOCK);
[email protected]5d272092012-04-19 10:23:03206 }
207
Robert Sesek6ab73b022020-02-13 16:42:39208 const size_t len = SendHelper(handle(), buffer, length);
[email protected]5d272092012-04-19 10:23:03209
210 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
211 // Restore the original flags.
Robert Sesek6ab73b022020-02-13 16:42:39212 fcntl(handle(), F_SETFL, flags);
[email protected]5d272092012-04-19 10:23:03213 }
214
215 return len;
216}
217
[email protected]532e9bd2012-01-25 12:04:17218// static
219bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
220 CancelableSyncSocket* socket_b) {
221 return SyncSocket::CreatePair(socket_a, socket_b);
222}
223
[email protected]182c44fa2009-11-26 00:28:02224} // namespace base