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