From f77ff938b7774d45f106b0fa9d6b6a8bab176b72 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 28 Nov 2004 05:48:02 +0000 Subject: [PATCH] remember; tor_socket_errno has side effects! svn:r2997 --- src/common/compat.c | 4 ++++ src/or/buffers.c | 6 ++++-- src/or/connection.c | 5 +++-- src/or/main.c | 6 +++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/common/compat.c b/src/common/compat.c index 5b70ad7dbf..4cf481fba5 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -582,6 +582,10 @@ void tor_mutex_release(tor_mutex_t *m) * get your errors from WSAGetLastError, not errno. (If you supply a * socket of -1, we check WSAGetLastError, but don't correct * WSAEWOULDBLOCKs.) + * + * The upshot of all of this is that when a socket call fails, you + * should call tor_socket_errno at most once on the failing + * socket to get the error. */ #ifdef MS_WINDOWS int tor_socket_errno(int sock) diff --git a/src/or/buffers.c b/src/or/buffers.c index 47b82fed40..99b8779f28 100644 --- a/src/or/buffers.c +++ b/src/or/buffers.c @@ -184,7 +184,8 @@ int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof) { // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most); read_result = recv(s, buf->mem+buf->datalen, at_most, 0); if (read_result < 0) { - if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */ + int e = tor_socket_errno(s); + if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */ return -1; } return 0; /* would block. */ @@ -254,7 +255,8 @@ int flush_buf(int s, buf_t *buf, size_t *buf_flushlen) write_result = send(s, buf->mem, *buf_flushlen, 0); if (write_result < 0) { - if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */ + int e = tor_socket_errno(s); + if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */ return -1; } log_fn(LOG_DEBUG,"write() would block, returning."); diff --git a/src/or/connection.c b/src/or/connection.c index a261210449..4b48844fa9 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -538,10 +538,11 @@ int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_ log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port); if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) { - if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) { + int e = tor_socket_errno(s); + if(!ERRNO_IS_CONN_EINPROGRESS(e)) { /* yuck. kill it. */ log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port, - tor_socket_strerror(tor_socket_errno(s))); + tor_socket_strerror(e)); tor_close_socket(s); return -1; } else { diff --git a/src/or/main.c b/src/or/main.c index d22fc94426..dd05484411 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -869,11 +869,11 @@ static int do_main_loop(void) { /* let catch() handle things like ^c, and otherwise don't worry about it */ if (poll_result < 0) { + int e = tor_socket_errno(-1); /* let the program survive things like ^z */ - if(tor_socket_errno(-1) != EINTR) { + if(e != EINTR) { log_fn(LOG_ERR,"poll failed: %s [%d]", - tor_socket_strerror(tor_socket_errno(-1)), - tor_socket_errno(-1)); + tor_socket_strerror(e), e); return -1; } else { log_fn(LOG_DEBUG,"poll interrupted.");