diff options
author | antirez <antirez@gmail.com> | 2014-10-17 16:30:11 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2014-10-17 16:30:32 +0200 |
commit | 74f90c61232859f35db4eabf5b0bf1c8e4123bf0 (patch) | |
tree | 07b174676ff1e2cd24fc26e2f66b5a5118de6d41 /src/anet.c | |
parent | 10aafdad56fa79bd7f95d9b190054b2e56b6cddd (diff) | |
download | redis-74f90c61232859f35db4eabf5b0bf1c8e4123bf0.tar.gz |
anet.c: API to set sockets back to blocking mode.
Diffstat (limited to 'src/anet.c')
-rw-r--r-- | src/anet.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/anet.c b/src/anet.c index e3c155940..9be32fda4 100644 --- a/src/anet.c +++ b/src/anet.c @@ -57,24 +57,37 @@ static void anetSetError(char *err, const char *fmt, ...) va_end(ap); } -int anetNonBlock(char *err, int fd) -{ +int anetSetBlock(char *err, int fd, int non_block) { int flags; - /* Set the socket non-blocking. + /* Set the socket blocking (if non_block is zero) or non-blocking. * Note that fcntl(2) for F_GETFL and F_SETFL can't be * interrupted by a signal. */ if ((flags = fcntl(fd, F_GETFL)) == -1) { anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno)); return ANET_ERR; } - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + + if (non_block) + flags |= O_NONBLOCK; + else + flags &= ~O_NONBLOCK; + + if (fcntl(fd, F_SETFL, flags) == -1) { anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)); return ANET_ERR; } return ANET_OK; } +int anetNonBlock(char *err, int fd) { + return anetSetBlock(err,fd,1); +} + +int anetBlock(char *err, int fd) { + return anetSetBlock(err,fd,0); +} + /* Set TCP keep alive option to detect dead peers. The interval option * is only used for Linux as we are using Linux-specific APIs to set * the probe send time, interval, and count. */ |