[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 2 | // 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] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 9 | #include <fcntl.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 10 | #include <stdio.h> |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 11 | #include <sys/ioctl.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 12 | #include <sys/socket.h> |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 13 | #include <sys/types.h> |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 14 | |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 15 | #if defined(OS_SOLARIS) |
| 16 | #include <sys/filio.h> |
| 17 | #endif |
| 18 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 19 | #include "base/file_util.h" |
| 20 | #include "base/logging.h" |
| 21 | |
| 22 | |
| 23 | namespace base { |
| 24 | |
| 25 | namespace { |
| 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); |
| 29 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 30 | } // namespace |
| 31 | |
[email protected] | 5895ee1 | 2011-12-22 19:33:27 | [diff] [blame] | 32 | const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 33 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 34 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 35 | |
| 36 | SyncSocket::~SyncSocket() { |
| 37 | Close(); |
| 38 | } |
| 39 | |
| 40 | // static |
| 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); |
| 45 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 46 | #if defined(OS_MACOSX) |
| 47 | int nosigpipe = 1; |
| 48 | #endif // defined(OS_MACOSX) |
| 49 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 50 | Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 51 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 52 | goto cleanup; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 53 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 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)) { |
| 61 | goto cleanup; |
| 62 | } |
| 63 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 64 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 65 | // Copy the handles out for successful return. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 66 | socket_a->handle_ = handles[0]; |
| 67 | socket_b->handle_ = handles[1]; |
| 68 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 69 | return true; |
| 70 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 71 | cleanup: |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 72 | if (handles[0] != kInvalidHandle) { |
| 73 | if (HANDLE_EINTR(close(handles[0])) < 0) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 74 | DPLOG(ERROR) << "close"; |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 75 | } |
| 76 | if (handles[1] != kInvalidHandle) { |
| 77 | if (HANDLE_EINTR(close(handles[1])) < 0) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 78 | DPLOG(ERROR) << "close"; |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 79 | } |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 80 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
| 84 | bool SyncSocket::Close() { |
| 85 | if (handle_ == kInvalidHandle) { |
| 86 | return false; |
| 87 | } |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 88 | int retval = HANDLE_EINTR(close(handle_)); |
| 89 | if (retval < 0) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 90 | DPLOG(ERROR) << "close"; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 91 | handle_ = kInvalidHandle; |
| 92 | return (retval == 0); |
| 93 | } |
| 94 | |
| 95 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 96 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 97 | const char* charbuffer = static_cast<const char*>(buffer); |
| 98 | int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 99 | |
| 100 | return (len == -1) ? 0 : static_cast<size_t>(len); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 104 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 105 | char* charbuffer = static_cast<char*>(buffer); |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 106 | if (file_util::ReadFromFD(handle_, charbuffer, length)) |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 107 | return length; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 108 | return 0; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 111 | size_t SyncSocket::Peek() { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 112 | 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] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 120 | CancelableSyncSocket::CancelableSyncSocket() {} |
| 121 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 122 | : SyncSocket(handle) { |
| 123 | } |
| 124 | |
| 125 | bool CancelableSyncSocket::Shutdown() { |
| 126 | return HANDLE_EINTR(shutdown(handle(), SHUT_RDWR)) >= 0; |
| 127 | } |
| 128 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 129 | size_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] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 148 | // static |
| 149 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 150 | CancelableSyncSocket* socket_b) { |
| 151 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 152 | } |
| 153 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 154 | } // namespace base |