| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "platform/globals.h" |
| 6 | #if defined(HOST_OS_FUCHSIA) |
| 7 | |
| 8 | #include "bin/fdutils.h" |
| 9 | |
| 10 | #include <errno.h> // NOLINT |
| 11 | #include <fcntl.h> // NOLINT |
| 12 | #include <sys/ioctl.h> // NOLINT |
| 13 | #include <unistd.h> // NOLINT |
| 14 | |
| 15 | #include "platform/signal_blocker.h" |
| 16 | |
| 17 | namespace dart { |
| 18 | namespace bin { |
| 19 | |
| 20 | bool FDUtils::SetCloseOnExec(intptr_t fd) { |
| 21 | intptr_t status; |
| 22 | status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFD)); |
| 23 | if (status < 0) { |
| 24 | perror("fcntl(F_GETFD) failed" ); |
| 25 | return false; |
| 26 | } |
| 27 | status |= FD_CLOEXEC; |
| 28 | if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFD, status)) < 0) { |
| 29 | perror("fcntl(F_SETFD, FD_CLOEXEC) failed" ); |
| 30 | return false; |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | static bool SetBlockingHelper(intptr_t fd, bool blocking) { |
| 36 | intptr_t status; |
| 37 | status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); |
| 38 | if (status < 0) { |
| 39 | perror("fcntl(F_GETFL) failed" ); |
| 40 | return false; |
| 41 | } |
| 42 | status = blocking ? (status & ~O_NONBLOCK) : (status | O_NONBLOCK); |
| 43 | if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFL, status)) < 0) { |
| 44 | perror("fcntl(F_SETFL, O_NONBLOCK) failed" ); |
| 45 | return false; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool FDUtils::SetNonBlocking(intptr_t fd) { |
| 51 | return SetBlockingHelper(fd, false); |
| 52 | } |
| 53 | |
| 54 | bool FDUtils::SetBlocking(intptr_t fd) { |
| 55 | return SetBlockingHelper(fd, true); |
| 56 | } |
| 57 | |
| 58 | bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { |
| 59 | intptr_t status; |
| 60 | status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); |
| 61 | if (status < 0) { |
| 62 | return false; |
| 63 | } |
| 64 | *is_blocking = (status & O_NONBLOCK) == 0; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | intptr_t FDUtils::AvailableBytes(intptr_t fd) { |
| 69 | int available; // ioctl for FIONREAD expects an 'int*' argument. |
| 70 | int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); |
| 71 | if (result < 0) { |
| 72 | return result; |
| 73 | } |
| 74 | ASSERT(available >= 0); |
| 75 | return static_cast<intptr_t>(available); |
| 76 | } |
| 77 | |
| 78 | ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { |
| 79 | #ifdef DEBUG |
| 80 | bool is_blocking = false; |
| 81 | ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 82 | ASSERT(is_blocking); |
| 83 | #endif |
| 84 | size_t remaining = count; |
| 85 | char* buffer_pos = reinterpret_cast<char*>(buffer); |
| 86 | while (remaining > 0) { |
| 87 | ssize_t bytes_read = NO_RETRY_EXPECTED(read(fd, buffer_pos, remaining)); |
| 88 | if (bytes_read == 0) { |
| 89 | return count - remaining; |
| 90 | } else if (bytes_read == -1) { |
| 91 | ASSERT(EAGAIN == EWOULDBLOCK); |
| 92 | // Error code EWOULDBLOCK should only happen for non blocking |
| 93 | // file descriptors. |
| 94 | ASSERT(errno != EWOULDBLOCK); |
| 95 | return -1; |
| 96 | } else { |
| 97 | ASSERT(bytes_read > 0); |
| 98 | remaining -= bytes_read; |
| 99 | buffer_pos += bytes_read; |
| 100 | } |
| 101 | } |
| 102 | return count; |
| 103 | } |
| 104 | |
| 105 | ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { |
| 106 | #ifdef DEBUG |
| 107 | bool is_blocking = false; |
| 108 | ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 109 | ASSERT(is_blocking); |
| 110 | #endif |
| 111 | size_t remaining = count; |
| 112 | char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); |
| 113 | while (remaining > 0) { |
| 114 | ssize_t bytes_written = NO_RETRY_EXPECTED(write(fd, buffer_pos, remaining)); |
| 115 | if (bytes_written == 0) { |
| 116 | return count - remaining; |
| 117 | } else if (bytes_written == -1) { |
| 118 | ASSERT(EAGAIN == EWOULDBLOCK); |
| 119 | // Error code EWOULDBLOCK should only happen for non blocking |
| 120 | // file descriptors. |
| 121 | ASSERT(errno != EWOULDBLOCK); |
| 122 | return -1; |
| 123 | } else { |
| 124 | ASSERT(bytes_written > 0); |
| 125 | remaining -= bytes_written; |
| 126 | buffer_pos += bytes_written; |
| 127 | } |
| 128 | } |
| 129 | return count; |
| 130 | } |
| 131 | |
| 132 | void FDUtils::SaveErrorAndClose(intptr_t fd) { |
| 133 | int err = errno; |
| 134 | NO_RETRY_EXPECTED(close(fd)); |
| 135 | errno = err; |
| 136 | } |
| 137 | |
| 138 | } // namespace bin |
| 139 | } // namespace dart |
| 140 | |
| 141 | #endif // defined(HOST_OS_FUCHSIA) |
| 142 | |