]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
remember; tor_socket_errno has side effects!
authorNick Mathewson <nickm@torproject.org>
Sun, 28 Nov 2004 05:48:02 +0000 (05:48 +0000)
committerNick Mathewson <nickm@torproject.org>
Sun, 28 Nov 2004 05:48:02 +0000 (05:48 +0000)
svn:r2997

src/common/compat.c
src/or/buffers.c
src/or/connection.c
src/or/main.c

index 5b70ad7dbf0fffebc8d8e1b24c71a3126f63f7b4..4cf481fba56ef426a165e16fea5f57f90574d0ad 100644 (file)
@@ -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 <em>at most once</em> on the failing
+ * socket to get the error.
  */
 #ifdef MS_WINDOWS
 int tor_socket_errno(int sock)
index 47b82fed40167a5770e3f83d758d5023831eea81..99b8779f287e8e830063a9ccaa1275ca1d1a54e3 100644 (file)
@@ -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.");
index a26121044939dcab6ca2bf9a371a12d79baf3433..4b48844fa96a0f0c95e9a52302e5140561605069 100644 (file)
@@ -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 {
index d22fc9442684ed55c19e1c3d39f29bd6c371a480..dd05484411a358f483f50e3334cdf598315bfcbe 100644 (file)
@@ -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.");