| 61 | |
| 62 | |
| 63 | int anetSetBlock(char *err, int fd, int non_block) { |
| 64 | #ifdef HAVE_FF_KQUEUE |
| 65 | if (ioctl(fd, FIONBIO, &non_block) == -1) { |
| 66 | anetSetError(err, "ioctl FIONBIO : %s", strerror(errno)); |
| 67 | return ANET_ERR; |
| 68 | } |
| 69 | return ANET_OK; |
| 70 | #else |
| 71 | int flags; |
| 72 | |
| 73 | /* Set the socket blocking (if non_block is zero) or non-blocking. |
| 74 | * Note that fcntl(2) for F_GETFL and F_SETFL can't be |
| 75 | * interrupted by a signal. */ |
| 76 | if ((flags = fcntl(fd, F_GETFL)) == -1) { |
| 77 | anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno)); |
| 78 | return ANET_ERR; |
| 79 | } |
| 80 | |
| 81 | /* Check if this flag has been set or unset, if so, |
| 82 | * then there is no need to call fcntl to set/unset it again. */ |
| 83 | if (!!(flags & O_NONBLOCK) == !!non_block) |
| 84 | return ANET_OK; |
| 85 | |
| 86 | if (non_block) |
| 87 | flags |= O_NONBLOCK; |
| 88 | else |
| 89 | flags &= ~O_NONBLOCK; |
| 90 | |
| 91 | if (fcntl(fd, F_SETFL, flags) == -1) { |
| 92 | anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)); |
| 93 | return ANET_ERR; |
| 94 | } |
| 95 | return ANET_OK; |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | int anetNonBlock(char *err, int fd) { |
| 100 | return anetSetBlock(err,fd,1); |
no test coverage detected