[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame^] | 1 | // Copyright (c) 2011 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> |
| 9 | #include <stdio.h> |
| 10 | #include <sys/types.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> |
| 13 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 14 | #include "base/file_util.h" |
| 15 | #include "base/logging.h" |
| 16 | |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | namespace { |
| 21 | // To avoid users sending negative message lengths to Send/Receive |
| 22 | // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 23 | const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 24 | |
| 25 | static const SyncSocket::Handle kInvalidHandle = -1; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | bool SyncSocket::CreatePair(SyncSocket* pair[2]) { |
| 30 | Handle handles[2] = { kInvalidHandle, kInvalidHandle }; |
| 31 | SyncSocket* tmp_sockets[2] = { NULL, NULL }; |
| 32 | #if defined(OS_MACOSX) |
| 33 | int nosigpipe = 1; |
| 34 | #endif // defined(OS_MACOSX) |
| 35 | |
| 36 | // Create the two SyncSocket objects first to avoid ugly cleanup issues. |
| 37 | tmp_sockets[0] = new SyncSocket(kInvalidHandle); |
| 38 | if (tmp_sockets[0] == NULL) { |
| 39 | goto cleanup; |
| 40 | } |
| 41 | tmp_sockets[1] = new SyncSocket(kInvalidHandle); |
| 42 | if (tmp_sockets[1] == NULL) { |
| 43 | goto cleanup; |
| 44 | } |
| 45 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) { |
| 46 | goto cleanup; |
| 47 | } |
| 48 | #if defined(OS_MACOSX) |
| 49 | // On OSX an attempt to read or write to a closed socket may generate a |
| 50 | // SIGPIPE rather than returning -1. setsockopt will shut this off. |
| 51 | if (0 != setsockopt(handles[0], SOL_SOCKET, SO_NOSIGPIPE, |
| 52 | &nosigpipe, sizeof nosigpipe) || |
| 53 | 0 != setsockopt(handles[1], SOL_SOCKET, SO_NOSIGPIPE, |
| 54 | &nosigpipe, sizeof nosigpipe)) { |
| 55 | goto cleanup; |
| 56 | } |
| 57 | #endif |
| 58 | // Copy the handles out for successful return. |
| 59 | tmp_sockets[0]->handle_ = handles[0]; |
| 60 | pair[0] = tmp_sockets[0]; |
| 61 | tmp_sockets[1]->handle_ = handles[1]; |
| 62 | pair[1] = tmp_sockets[1]; |
| 63 | return true; |
| 64 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 65 | cleanup: |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 66 | if (handles[0] != kInvalidHandle) { |
| 67 | if (HANDLE_EINTR(close(handles[0])) < 0) |
| 68 | PLOG(ERROR) << "close"; |
| 69 | } |
| 70 | if (handles[1] != kInvalidHandle) { |
| 71 | if (HANDLE_EINTR(close(handles[1])) < 0) |
| 72 | PLOG(ERROR) << "close"; |
| 73 | } |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 74 | delete tmp_sockets[0]; |
| 75 | delete tmp_sockets[1]; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | bool SyncSocket::Close() { |
| 80 | if (handle_ == kInvalidHandle) { |
| 81 | return false; |
| 82 | } |
[email protected] | be60b9a | 2011-02-23 21:37:41 | [diff] [blame] | 83 | int retval = HANDLE_EINTR(close(handle_)); |
| 84 | if (retval < 0) |
| 85 | PLOG(ERROR) << "close"; |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 86 | handle_ = kInvalidHandle; |
| 87 | return (retval == 0); |
| 88 | } |
| 89 | |
| 90 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame^] | 91 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 92 | const char* charbuffer = static_cast<const char*>(buffer); |
| 93 | int len = file_util::WriteFileDescriptor(handle_, charbuffer, length); |
| 94 | return static_cast<size_t>(len); |
| 95 | } |
| 96 | |
| 97 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame^] | 98 | DCHECK_LE(length, kMaxMessageLength); |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 99 | char* charbuffer = static_cast<char*>(buffer); |
| 100 | if (file_util::ReadFromFD(handle_, charbuffer, length)) { |
| 101 | return length; |
| 102 | } else { |
| 103 | return -1; |
| 104 | } |
| 105 | } |
| 106 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 107 | size_t SyncSocket::Peek() { |
[email protected] | 1e1f1a7 | 2009-12-06 19:45:08 | [diff] [blame] | 108 | int number_chars; |
| 109 | if (-1 == ioctl(handle_, FIONREAD, &number_chars)) { |
| 110 | // If there is an error in ioctl, signal that the channel would block. |
| 111 | return 0; |
| 112 | } |
| 113 | return (size_t) number_chars; |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 182c44fa | 2009-11-26 00:28:02 | [diff] [blame] | 116 | } // namespace base |