1 // Copyright (c) 2012 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.
5 #include "base/sync_socket.h"
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
17 #if defined(OS_SOLARIS)
18 #include <sys/filio.h>
21 #include "base/files/file_util.h"
22 #include "base/logging.h"
23 #include "base/threading/thread_restrictions.h"
24 #include "build/build_config.h"
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.
31 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX);
33 // Writes |length| of |buffer| into |handle|. Returns the number of bytes
34 // written or zero on error. |length| must be greater than 0.
35 size_t SendHelper(SyncSocket::Handle handle,
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);
42 return WriteFileDescriptor(handle, charbuffer, length)
43 ? static_cast<size_t>(length)
47 bool CloseHandle(SyncSocket::Handle handle) {
48 if (handle != SyncSocket::kInvalidHandle && close(handle) < 0) {
49 DPLOG(ERROR) << "close";
58 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1;
60 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {}
62 SyncSocket::~SyncSocket() {
67 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
68 DCHECK_NE(socket_a, socket_b);
69 DCHECK_EQ(socket_a->handle_, kInvalidHandle);
70 DCHECK_EQ(socket_b->handle_, kInvalidHandle);
72 #if defined(OS_MACOSX)
74 #endif // defined(OS_MACOSX)
76 Handle handles[2] = { kInvalidHandle, kInvalidHandle };
77 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
78 CloseHandle(handles[0]);
79 CloseHandle(handles[1]);
83 #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)) {
90 CloseHandle(handles[0]);
91 CloseHandle(handles[1]);
96 // Copy the handles out for successful return.
97 socket_a->handle_ = handles[0];
98 socket_b->handle_ = handles[1];
104 SyncSocket::Handle SyncSocket::UnwrapHandle(
105 const TransitDescriptor& descriptor) {
106 return descriptor.fd;
109 bool SyncSocket::PrepareTransitDescriptor(ProcessHandle peer_process_handle,
110 TransitDescriptor* descriptor) {
111 descriptor->fd = handle();
112 descriptor->auto_close = false;
113 return descriptor->fd != kInvalidHandle;
116 bool SyncSocket::Close() {
117 const bool retval = CloseHandle(handle_);
118 handle_ = kInvalidHandle;
122 size_t SyncSocket::Send(const void* buffer, size_t length) {
123 AssertBlockingAllowed();
124 return SendHelper(handle_, buffer, length);
127 size_t SyncSocket::Receive(void* buffer, size_t length) {
128 AssertBlockingAllowed();
129 DCHECK_GT(length, 0u);
130 DCHECK_LE(length, kMaxMessageLength);
131 DCHECK_NE(handle_, kInvalidHandle);
132 char* charbuffer = static_cast<char*>(buffer);
133 if (ReadFromFD(handle_, charbuffer, length))
138 size_t SyncSocket::ReceiveWithTimeout(void* buffer,
141 AssertBlockingAllowed();
142 DCHECK_GT(length, 0u);
143 DCHECK_LE(length, kMaxMessageLength);
144 DCHECK_NE(handle_, kInvalidHandle);
146 // Only timeouts greater than zero and less than one second are allowed.
147 DCHECK_GT(timeout.InMicroseconds(), 0);
148 DCHECK_LT(timeout.InMicroseconds(),
149 TimeDelta::FromSeconds(1).InMicroseconds());
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;
155 struct pollfd pollfd;
157 pollfd.events = POLLIN;
160 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());
167 const int poll_result = poll(&pollfd, 1, timeout_ms);
168 // Handle EINTR manually since we need to update the timeout value.
169 if (poll_result == -1 && errno == EINTR)
171 // Return if other type of error or a timeout.
172 if (poll_result <= 0)
173 return bytes_read_total;
175 // poll() only tells us that data is ready for reading, not how much. We
176 // must Peek() for the amount ready for reading to avoid blocking.
177 // 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;
182 const size_t bytes_to_read = std::min(Peek(), length - bytes_read_total);
184 // There may be zero bytes to read if the socket at the other end closed.
186 return bytes_read_total;
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;
195 return bytes_read_total;
198 size_t SyncSocket::Peek() {
199 DCHECK_NE(handle_, kInvalidHandle);
200 int number_chars = 0;
201 if (ioctl(handle_, FIONREAD, &number_chars) == -1) {
202 // If there is an error in ioctl, signal that the channel would block.
205 DCHECK_GE(number_chars, 0);
209 SyncSocket::Handle SyncSocket::Release() {
211 handle_ = kInvalidHandle;
215 CancelableSyncSocket::CancelableSyncSocket() = default;
216 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
217 : SyncSocket(handle) {
220 bool CancelableSyncSocket::Shutdown() {
221 DCHECK_NE(handle_, kInvalidHandle);
222 return HANDLE_EINTR(shutdown(handle_, SHUT_RDWR)) >= 0;
225 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
226 DCHECK_GT(length, 0u);
227 DCHECK_LE(length, kMaxMessageLength);
228 DCHECK_NE(handle_, kInvalidHandle);
230 const int flags = fcntl(handle_, F_GETFL);
231 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
232 // Set the socket to non-blocking mode for sending if its original mode
234 fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
237 const size_t len = SendHelper(handle_, buffer, length);
239 if (flags != -1 && (flags & O_NONBLOCK) == 0) {
240 // Restore the original flags.
241 fcntl(handle_, F_SETFL, flags);
248 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
249 CancelableSyncSocket* socket_b) {
250 return SyncSocket::CreatePair(socket_a, socket_b);