summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-19 22:46:19 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-19 22:46:19 +0000
commit632c035ad967446f42a546cdf069c04f8b5d1678 (patch)
treecea4def9af5b172d45fffcc9a36b6466ad830d00 /src/common/compat.c
parentc126b79f07a46b506e19733ff312fe6540ae2096 (diff)
downloadtor-632c035ad967446f42a546cdf069c04f8b5d1678.tar.gz
tor-632c035ad967446f42a546cdf069c04f8b5d1678.zip
r18221@catbus: nickm | 2008-02-19 17:46:16 -0500
New debugging code to figure out what is happending with socket counts. svn:r13593
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 018600e251..691a3f6b4c 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -487,6 +487,12 @@ touch_file(const char *fname)
return 0;
}
+#undef DEBUG_SOCKET_COUNTING
+#ifdef DEBUG_SOCKET_COUNTING
+static bitarray_t *open_sockets = NULL;
+static int max_socket = -1;
+#endif
+
/** Count of number of sockets currently open. (Undercounts sockets opened by
* eventdns and libevent.) */
static int n_sockets_open = 0;
@@ -498,6 +504,15 @@ int
tor_close_socket(int s)
{
int r = 0;
+#ifdef DEBUG_SOCKET_COUNTING
+ if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
+ log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
+ "socket(), or that was already closed or something.", s);
+ } else {
+ tor_assert(open_sockets && s <= max_socket);
+ bitarray_clear(open_sockets, s);
+ }
+#endif
/* 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
@@ -536,8 +551,25 @@ int
tor_open_socket(int domain, int type, int protocol)
{
int s = socket(domain, type, protocol);
- if (s >= 0)
+ if (s >= 0) {
++n_sockets_open;
+#ifdef DEBUG_SOCKET_COUNTING
+ if (s > max_socket) {
+ if (max_socket == -1) {
+ open_sockets = bitarray_init_zero(s+128);
+ max_socket = s+128;
+ } else {
+ open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
+ max_socket = s+128;
+ }
+ }
+ if (bitarray_is_set(open_sockets, s)) {
+ log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
+ "gave it to me!", s);
+ }
+ bitarray_set(open_sockets, s);
+#endif
+ }
return s;
}