| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(HOST_OS_ANDROID) | 6 #if defined(HOST_OS_ANDROID) |
| 7 | 7 |
| 8 #include "bin/fdutils.h" | 8 #include "bin/fdutils.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 return false; | 25 return false; |
| 26 } | 26 } |
| 27 status |= FD_CLOEXEC; | 27 status |= FD_CLOEXEC; |
| 28 if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFD, status)) < 0) { | 28 if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFD, status)) < 0) { |
| 29 perror("fcntl(F_SETFD, FD_CLOEXEC) failed"); | 29 perror("fcntl(F_SETFD, FD_CLOEXEC) failed"); |
| 30 return false; | 30 return false; |
| 31 } | 31 } |
| 32 return true; | 32 return true; |
| 33 } | 33 } |
| 34 | 34 |
| 35 | |
| 36 static bool SetBlockingHelper(intptr_t fd, bool blocking) { | 35 static bool SetBlockingHelper(intptr_t fd, bool blocking) { |
| 37 intptr_t status; | 36 intptr_t status; |
| 38 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); | 37 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); |
| 39 if (status < 0) { | 38 if (status < 0) { |
| 40 perror("fcntl(F_GETFL) failed"); | 39 perror("fcntl(F_GETFL) failed"); |
| 41 return false; | 40 return false; |
| 42 } | 41 } |
| 43 status = blocking ? (status & ~O_NONBLOCK) : (status | O_NONBLOCK); | 42 status = blocking ? (status & ~O_NONBLOCK) : (status | O_NONBLOCK); |
| 44 if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFL, status)) < 0) { | 43 if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFL, status)) < 0) { |
| 45 perror("fcntl(F_SETFL, O_NONBLOCK) failed"); | 44 perror("fcntl(F_SETFL, O_NONBLOCK) failed"); |
| 46 return false; | 45 return false; |
| 47 } | 46 } |
| 48 return true; | 47 return true; |
| 49 } | 48 } |
| 50 | 49 |
| 51 | |
| 52 bool FDUtils::SetNonBlocking(intptr_t fd) { | 50 bool FDUtils::SetNonBlocking(intptr_t fd) { |
| 53 return SetBlockingHelper(fd, false); | 51 return SetBlockingHelper(fd, false); |
| 54 } | 52 } |
| 55 | 53 |
| 56 | |
| 57 bool FDUtils::SetBlocking(intptr_t fd) { | 54 bool FDUtils::SetBlocking(intptr_t fd) { |
| 58 return SetBlockingHelper(fd, true); | 55 return SetBlockingHelper(fd, true); |
| 59 } | 56 } |
| 60 | 57 |
| 61 | |
| 62 bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { | 58 bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { |
| 63 intptr_t status; | 59 intptr_t status; |
| 64 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); | 60 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); |
| 65 if (status < 0) { | 61 if (status < 0) { |
| 66 return false; | 62 return false; |
| 67 } | 63 } |
| 68 *is_blocking = (status & O_NONBLOCK) == 0; | 64 *is_blocking = (status & O_NONBLOCK) == 0; |
| 69 return true; | 65 return true; |
| 70 } | 66 } |
| 71 | 67 |
| 72 | |
| 73 intptr_t FDUtils::AvailableBytes(intptr_t fd) { | 68 intptr_t FDUtils::AvailableBytes(intptr_t fd) { |
| 74 int available; // ioctl for FIONREAD expects an 'int*' argument. | 69 int available; // ioctl for FIONREAD expects an 'int*' argument. |
| 75 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); | 70 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); |
| 76 if (result < 0) { | 71 if (result < 0) { |
| 77 return result; | 72 return result; |
| 78 } | 73 } |
| 79 ASSERT(available >= 0); | 74 ASSERT(available >= 0); |
| 80 return static_cast<intptr_t>(available); | 75 return static_cast<intptr_t>(available); |
| 81 } | 76 } |
| 82 | 77 |
| 83 | |
| 84 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { | 78 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { |
| 85 #ifdef DEBUG | 79 #ifdef DEBUG |
| 86 bool is_blocking = false; | 80 bool is_blocking = false; |
| 87 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 81 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 88 ASSERT(is_blocking); | 82 ASSERT(is_blocking); |
| 89 #endif | 83 #endif |
| 90 size_t remaining = count; | 84 size_t remaining = count; |
| 91 char* buffer_pos = reinterpret_cast<char*>(buffer); | 85 char* buffer_pos = reinterpret_cast<char*>(buffer); |
| 92 while (remaining > 0) { | 86 while (remaining > 0) { |
| 93 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining)); | 87 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining)); |
| 94 if (bytes_read == 0) { | 88 if (bytes_read == 0) { |
| 95 return count - remaining; | 89 return count - remaining; |
| 96 } else if (bytes_read == -1) { | 90 } else if (bytes_read == -1) { |
| 97 ASSERT(EAGAIN == EWOULDBLOCK); | 91 ASSERT(EAGAIN == EWOULDBLOCK); |
| 98 // Error code EWOULDBLOCK should only happen for non blocking | 92 // Error code EWOULDBLOCK should only happen for non blocking |
| 99 // file descriptors. | 93 // file descriptors. |
| 100 ASSERT(errno != EWOULDBLOCK); | 94 ASSERT(errno != EWOULDBLOCK); |
| 101 return -1; | 95 return -1; |
| 102 } else { | 96 } else { |
| 103 ASSERT(bytes_read > 0); | 97 ASSERT(bytes_read > 0); |
| 104 remaining -= bytes_read; | 98 remaining -= bytes_read; |
| 105 buffer_pos += bytes_read; | 99 buffer_pos += bytes_read; |
| 106 } | 100 } |
| 107 } | 101 } |
| 108 return count; | 102 return count; |
| 109 } | 103 } |
| 110 | 104 |
| 111 | |
| 112 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { | 105 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { |
| 113 #ifdef DEBUG | 106 #ifdef DEBUG |
| 114 bool is_blocking = false; | 107 bool is_blocking = false; |
| 115 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 108 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 116 ASSERT(is_blocking); | 109 ASSERT(is_blocking); |
| 117 #endif | 110 #endif |
| 118 size_t remaining = count; | 111 size_t remaining = count; |
| 119 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); | 112 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); |
| 120 while (remaining > 0) { | 113 while (remaining > 0) { |
| 121 ssize_t bytes_written = | 114 ssize_t bytes_written = |
| 122 TEMP_FAILURE_RETRY(write(fd, buffer_pos, remaining)); | 115 TEMP_FAILURE_RETRY(write(fd, buffer_pos, remaining)); |
| 123 if (bytes_written == 0) { | 116 if (bytes_written == 0) { |
| 124 return count - remaining; | 117 return count - remaining; |
| 125 } else if (bytes_written == -1) { | 118 } else if (bytes_written == -1) { |
| 126 ASSERT(EAGAIN == EWOULDBLOCK); | 119 ASSERT(EAGAIN == EWOULDBLOCK); |
| 127 // Error code EWOULDBLOCK should only happen for non blocking | 120 // Error code EWOULDBLOCK should only happen for non blocking |
| 128 // file descriptors. | 121 // file descriptors. |
| 129 ASSERT(errno != EWOULDBLOCK); | 122 ASSERT(errno != EWOULDBLOCK); |
| 130 return -1; | 123 return -1; |
| 131 } else { | 124 } else { |
| 132 ASSERT(bytes_written > 0); | 125 ASSERT(bytes_written > 0); |
| 133 remaining -= bytes_written; | 126 remaining -= bytes_written; |
| 134 buffer_pos += bytes_written; | 127 buffer_pos += bytes_written; |
| 135 } | 128 } |
| 136 } | 129 } |
| 137 return count; | 130 return count; |
| 138 } | 131 } |
| 139 | 132 |
| 140 | |
| 141 void FDUtils::SaveErrorAndClose(intptr_t fd) { | 133 void FDUtils::SaveErrorAndClose(intptr_t fd) { |
| 142 int err = errno; | 134 int err = errno; |
| 143 VOID_TEMP_FAILURE_RETRY(close(fd)); | 135 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 144 errno = err; | 136 errno = err; |
| 145 } | 137 } |
| 146 | 138 |
| 147 } // namespace bin | 139 } // namespace bin |
| 148 } // namespace dart | 140 } // namespace dart |
| 149 | 141 |
| 150 #endif // defined(HOST_OS_ANDROID) | 142 #endif // defined(HOST_OS_ANDROID) |
| OLD | NEW |