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"
11 #include <sys/ioctl.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
15 #if defined(OS_SOLARIS)
16 #include <sys/filio.h>
19 #include "base/file_util.h"
20 #include "base/logging.h"
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.
28 const size_t kMaxMessageLength
= static_cast<size_t>(INT_MAX
);
32 const SyncSocket::Handle
SyncSocket::kInvalidHandle
= -1;
34 SyncSocket::SyncSocket() : handle_(kInvalidHandle
) {}
36 SyncSocket::~SyncSocket() {
41 bool 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
);
46 #if defined(OS_MACOSX)
48 #endif // defined(OS_MACOSX)
50 Handle handles
[2] = { kInvalidHandle
, kInvalidHandle
};
51 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, handles
) != 0)
54 #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
)) {
65 // Copy the handles out for successful return.
66 socket_a
->handle_
= handles
[0];
67 socket_b
->handle_
= handles
[1];
72 if (handles
[0] != kInvalidHandle
) {
73 if (HANDLE_EINTR(close(handles
[0])) < 0)
74 DPLOG(ERROR
) << "close";
76 if (handles
[1] != kInvalidHandle
) {
77 if (HANDLE_EINTR(close(handles
[1])) < 0)
78 DPLOG(ERROR
) << "close";
84 bool SyncSocket::Close() {
85 if (handle_
== kInvalidHandle
) {
88 int retval
= HANDLE_EINTR(close(handle_
));
90 DPLOG(ERROR
) << "close";
91 handle_
= kInvalidHandle
;
95 size_t SyncSocket::Send(const void* buffer
, size_t length
) {
96 DCHECK_LE(length
, kMaxMessageLength
);
97 const char* charbuffer
= static_cast<const char*>(buffer
);
98 int len
= file_util::WriteFileDescriptor(handle_
, charbuffer
, length
);
100 return (len
== -1) ? 0 : static_cast<size_t>(len
);
103 size_t SyncSocket::Receive(void* buffer
, size_t length
) {
104 DCHECK_LE(length
, kMaxMessageLength
);
105 char* charbuffer
= static_cast<char*>(buffer
);
106 if (file_util::ReadFromFD(handle_
, charbuffer
, length
))
111 size_t SyncSocket::Peek() {
113 if (-1 == ioctl(handle_
, FIONREAD
, &number_chars
)) {
114 // If there is an error in ioctl, signal that the channel would block.
117 return (size_t) number_chars
;
120 CancelableSyncSocket::CancelableSyncSocket() {}
121 CancelableSyncSocket::CancelableSyncSocket(Handle handle
)
122 : SyncSocket(handle
) {
125 bool CancelableSyncSocket::Shutdown() {
126 return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR
)) >= 0;
129 size_t CancelableSyncSocket::Send(const void* buffer
, size_t length
) {
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
135 fcntl(handle_
, F_SETFL
, flags
| O_NONBLOCK
);
138 size_t len
= SyncSocket::Send(buffer
, length
);
140 if (flags
!= -1 && (flags
& O_NONBLOCK
) == 0) {
141 // Restore the original flags.
142 fcntl(handle_
, F_SETFL
, flags
);
149 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket
* socket_a
,
150 CancelableSyncSocket
* socket_b
) {
151 return SyncSocket::CreatePair(socket_a
, socket_b
);