| 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_FUCHSIA) | 6 #if defined(HOST_OS_FUCHSIA) |
| 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 perror("ioctl(fd, FIONREAD, &available) failed"); | 72 perror("ioctl(fd, FIONREAD, &available) failed"); |
| 78 return result; | 73 return result; |
| 79 } | 74 } |
| 80 ASSERT(available >= 0); | 75 ASSERT(available >= 0); |
| 81 return static_cast<intptr_t>(available); | 76 return static_cast<intptr_t>(available); |
| 82 } | 77 } |
| 83 | 78 |
| 84 | |
| 85 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { | 79 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { |
| 86 #ifdef DEBUG | 80 #ifdef DEBUG |
| 87 bool is_blocking = false; | 81 bool is_blocking = false; |
| 88 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 82 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 89 ASSERT(is_blocking); | 83 ASSERT(is_blocking); |
| 90 #endif | 84 #endif |
| 91 size_t remaining = count; | 85 size_t remaining = count; |
| 92 char* buffer_pos = reinterpret_cast<char*>(buffer); | 86 char* buffer_pos = reinterpret_cast<char*>(buffer); |
| 93 while (remaining > 0) { | 87 while (remaining > 0) { |
| 94 ssize_t bytes_read = NO_RETRY_EXPECTED(read(fd, buffer_pos, remaining)); | 88 ssize_t bytes_read = NO_RETRY_EXPECTED(read(fd, buffer_pos, remaining)); |
| 95 if (bytes_read == 0) { | 89 if (bytes_read == 0) { |
| 96 return count - remaining; | 90 return count - remaining; |
| 97 } else if (bytes_read == -1) { | 91 } else if (bytes_read == -1) { |
| 98 ASSERT(EAGAIN == EWOULDBLOCK); | 92 ASSERT(EAGAIN == EWOULDBLOCK); |
| 99 // Error code EWOULDBLOCK should only happen for non blocking | 93 // Error code EWOULDBLOCK should only happen for non blocking |
| 100 // file descriptors. | 94 // file descriptors. |
| 101 ASSERT(errno != EWOULDBLOCK); | 95 ASSERT(errno != EWOULDBLOCK); |
| 102 return -1; | 96 return -1; |
| 103 } else { | 97 } else { |
| 104 ASSERT(bytes_read > 0); | 98 ASSERT(bytes_read > 0); |
| 105 remaining -= bytes_read; | 99 remaining -= bytes_read; |
| 106 buffer_pos += bytes_read; | 100 buffer_pos += bytes_read; |
| 107 } | 101 } |
| 108 } | 102 } |
| 109 return count; | 103 return count; |
| 110 } | 104 } |
| 111 | 105 |
| 112 | |
| 113 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { | 106 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { |
| 114 #ifdef DEBUG | 107 #ifdef DEBUG |
| 115 bool is_blocking = false; | 108 bool is_blocking = false; |
| 116 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 109 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 117 ASSERT(is_blocking); | 110 ASSERT(is_blocking); |
| 118 #endif | 111 #endif |
| 119 size_t remaining = count; | 112 size_t remaining = count; |
| 120 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); | 113 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); |
| 121 while (remaining > 0) { | 114 while (remaining > 0) { |
| 122 ssize_t bytes_written = NO_RETRY_EXPECTED(write(fd, buffer_pos, remaining)); | 115 ssize_t bytes_written = NO_RETRY_EXPECTED(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 NO_RETRY_EXPECTED(close(fd)); | 135 NO_RETRY_EXPECTED(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_FUCHSIA) | 142 #endif // defined(HOST_OS_FUCHSIA) |
| OLD | NEW |