[or-cvs] r13580: Try to *fix* the socket counting problem, and add an info lo (in tor/trunk: . src/common)

nickm at seul.org nickm at seul.org
Tue Feb 19 19:30:42 UTC 2008


Author: nickm
Date: 2008-02-19 14:30:41 -0500 (Tue, 19 Feb 2008)
New Revision: 13580

Modified:
   tor/trunk/
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
Log:
 r18198 at catbus:  nickm | 2008-02-19 14:30:30 -0500
 Try to *fix* the socket counting problem, and add an info log to detect whether we really fixed it



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r18198] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2008-02-19 19:30:37 UTC (rev 13579)
+++ tor/trunk/src/common/compat.c	2008-02-19 19:30:41 UTC (rev 13580)
@@ -492,10 +492,12 @@
 static int n_sockets_open = 0;
 
 /** As close(), but guaranteed to work for sockets across platforms (including
- * Windows, where close()ing a socket doesn't work. */
-void
+ * Windows, where close()ing a socket doesn't work.  Returns 0 on success, -1
+ * on failure. */
+int
 tor_close_socket(int s)
 {
+  int r = 0;
   /* On Windows, you have to call close() on fds returned by open(),
    * and closesocket() on fds returned by socket().  On Unix, everything
    * gets close()'d.  We abstract this difference by always using
@@ -503,14 +505,28 @@
    * files.
    */
 #ifdef USE_BSOCKETS
-  bclose(s);
+  r = bclose(s);
 #elif defined(MS_WINDOWS)
-  closesocket(s);
+  r = closesocket(s);
 #else
-  close(s);
+  r = close(s);
 #endif
+  if (r == 0) {
+    --n_sockets_open;
+  } else {
+    int err = tor_socket_errno(-1);
+    log_info(LD_NET, "Close returned an error: %s", tor_socket_strerror(err));
+#ifdef WIN32
+    if (err != WSAENOTSOCK)
+      --n_sockets_open;
+#else
+    if (err != EBADF)
+      --n_sockets_open;
+#endif
+    r = -1;
+  }
   tor_assert(n_sockets_open > 0);
-  --n_sockets_open;
+  return r;
 }
 
 /** As socket(), but counts the number of open sockets. */

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2008-02-19 19:30:37 UTC (rev 13579)
+++ tor/trunk/src/common/compat.h	2008-02-19 19:30:41 UTC (rev 13580)
@@ -255,7 +255,7 @@
 
 /* ===== Net compatibility */
 
-void tor_close_socket(int s);
+int tor_close_socket(int s);
 int tor_open_socket(int domain, int type, int protocol);
 int get_n_open_sockets(void);
 



More information about the tor-commits mailing list