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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <sys/ioctl.h> | 8 #include <sys/ioctl.h> |
9 | 9 |
10 #include "bin/fdutils.h" | 10 #include "bin/fdutils.h" |
11 | 11 |
12 | 12 |
13 bool FDUtils::SetNonBlocking(intptr_t fd) { | 13 static bool SetBlockingHelper(intptr_t fd, bool blocking) { |
14 intptr_t status; | 14 intptr_t status; |
15 status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)); | 15 status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)); |
16 if (status < 0) { | 16 if (status < 0) { |
17 perror("fcntl F_GETFL failed"); | 17 perror("fcntl F_GETFL failed"); |
18 return false; | 18 return false; |
19 } | 19 } |
20 status = (status | O_NONBLOCK); | 20 status = blocking ? (status & ~O_NONBLOCK) : (status | O_NONBLOCK); |
21 if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFL, status)) < 0) { | 21 if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFL, status)) < 0) { |
22 perror("fcntl F_SETFL failed"); | 22 perror("fcntl F_SETFL failed"); |
23 return false; | 23 return false; |
24 } | 24 } |
25 return true; | 25 return true; |
26 } | 26 } |
27 | 27 |
28 | 28 |
| 29 bool FDUtils::SetNonBlocking(intptr_t fd) { |
| 30 return SetBlockingHelper(fd, false); |
| 31 } |
| 32 |
| 33 |
| 34 bool FDUtils::SetBlocking(intptr_t fd) { |
| 35 return SetBlockingHelper(fd, true); |
| 36 } |
| 37 |
| 38 |
29 bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { | 39 bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { |
30 intptr_t status; | 40 intptr_t status; |
31 status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)); | 41 status = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL)); |
32 if (status < 0) { | 42 if (status < 0) { |
33 perror("fcntl F_GETFL failed"); | 43 perror("fcntl F_GETFL failed"); |
34 return false; | 44 return false; |
35 } | 45 } |
36 *is_blocking = (status & O_NONBLOCK) == 0; | 46 *is_blocking = (status & O_NONBLOCK) == 0; |
37 return true; | 47 return true; |
38 } | 48 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 ASSERT(errno != EWOULDBLOCK); | 109 ASSERT(errno != EWOULDBLOCK); |
100 return -1; | 110 return -1; |
101 } else { | 111 } else { |
102 ASSERT(bytes_written > 0); | 112 ASSERT(bytes_written > 0); |
103 remaining -= bytes_written; | 113 remaining -= bytes_written; |
104 buffer_pos += bytes_written; | 114 buffer_pos += bytes_written; |
105 } | 115 } |
106 } | 116 } |
107 return count; | 117 return count; |
108 } | 118 } |
OLD | NEW |