diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-01-21 12:18:11 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-01-21 12:18:11 -0500 |
commit | ac5b70c700b211008853b5f212100a867f508dfd (patch) | |
tree | 53e83027711dd6f7dd4328a960efc615294dc926 /src/common/compat_threads.c | |
parent | a52e549124adb09ad0b49b7d2b5b3fb79bfe7aeb (diff) | |
download | tor-ac5b70c700b211008853b5f212100a867f508dfd.tar.gz tor-ac5b70c700b211008853b5f212100a867f508dfd.zip |
handle EINTR in compat_*threads.c
Diffstat (limited to 'src/common/compat_threads.c')
-rw-r--r-- | src/common/compat_threads.c | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c index 79440070a2..3b79292cdb 100644 --- a/src/common/compat_threads.c +++ b/src/common/compat_threads.c @@ -88,12 +88,59 @@ in_main_thread(void) return main_thread_id == tor_get_thread_id(); } +#if defined(HAVE_EVENTFD) || defined(HAVE_PIPE) +/* non-interruptable versions */ +static int +write_ni(int fd, const void *buf, size_t n) +{ + int r; + again: + r = write(fd, buf, n); + if (r < 0 && errno == EINTR) + goto again; + return r; +} +static int +read_ni(int fd, void *buf, size_t n) +{ + int r; + again: + r = read(fd, buf, n); + if (r < 0 && errno == EINTR) + goto again; + return r; +} +#endif + +/* non-interruptable versions */ +static int +send_ni(int fd, const void *buf, size_t n, int flags) +{ + int r; + again: + r = send(fd, buf, n, flags); + if (r < 0 && errno == EINTR) + goto again; + return r; +} + +static int +recv_ni(int fd, void *buf, size_t n, int flags) +{ + int r; + again: + r = recv(fd, buf, n, flags); + if (r < 0 && errno == EINTR) + goto again; + return r; +} + #ifdef HAVE_EVENTFD static int eventfd_alert(int fd) { uint64_t u = 1; - int r = write(fd, (void*)&u, sizeof(u)); + int r = write_ni(fd, (void*)&u, sizeof(u)); if (r < 0 && errno != EAGAIN) return -1; return 0; @@ -103,7 +150,7 @@ static int eventfd_drain(int fd) { uint64_t u = 0; - int r = read(fd, (void*)&u, sizeof(u)); + int r = read_ni(fd, (void*)&u, sizeof(u)); if (r < 0 && errno != EAGAIN) return -1; return 0; @@ -136,7 +183,7 @@ pipe_drain(int fd) static int sock_alert(tor_socket_t fd) { - ssize_t r = send(fd, "x", 1, 0); + ssize_t r = send_ni(fd, "x", 1, 0); if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd))) return -1; return 0; @@ -147,7 +194,7 @@ sock_drain(tor_socket_t fd) { char buf[32]; ssize_t r; - while ((r = recv(fd, buf, sizeof(buf), 0)) >= 0) + while ((r = recv_ni(fd, buf, sizeof(buf), 0)) >= 0) ; if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd))) return -1; |