From 07a604e3d6c1800374c9713c0ef954eb7d18a40a Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Fri, 19 Jan 2007 17:50:36 +0000 Subject: [PATCH] Debug file descriptors --- Makefile | 2 +- opentracker.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 525b3cb..94b8e05 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC?=gcc -FEATURES=#-DWANT_IP_FROM_QUERY_STRING -DWANT_BLACKLIST -DWANT_CLOSED_TRACKER +FEATURES=#-D_DEBUG_FDS -DWANT_IP_FROM_QUERY_STRING -DWANT_BLACKLIST -DWANT_CLOSED_TRACKER #DEBUG_OPTS=-g -ggdb -pg # -fprofile-arcs -ftest-coverage DEBUG_OPTS=-s -Os CFLAGS+=-I../libowfat -Wall -pipe -m64 # -pedantic -ansi diff --git a/opentracker.c b/opentracker.c index a6c57e7..374824f 100644 --- a/opentracker.c +++ b/opentracker.c @@ -35,6 +35,10 @@ static const size_t SUCCESS_HTTP_SIZE_OFF = 17; /* To always have space for error messages ;) */ static char static_scratch[8192]; +#ifdef _DEBUG_FDS +static char fd_debug_space[0x10000]; +#endif + static void carp(const char* routine) { buffer_puts(buffer_2,routine); buffer_puts(buffer_2,": "); @@ -70,10 +74,18 @@ void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) { if( h ) array_reset(&h->r); written_size = write( s, buffer, size ); if( ( written_size < 0 ) || ( written_size == size ) ) { +#ifdef _DEBUG_FDS + if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); + fd_debug_space[s] = 0; +#endif free(h); io_close( s ); } else { /* here we would take a copy of the buffer and remember it */ fprintf( stderr, "Should have handled this.\n" ); +#ifdef _DEBUG_FDS + if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); + fd_debug_space[s] = 0; +#endif free(h); io_close( s ); } } @@ -337,6 +349,10 @@ e404: senddata( s, h, static_scratch + reply_off, reply_size ); } else { if( h ) array_reset(&h->r); +#ifdef _DEBUG_FDS + if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); + fd_debug_space[s] = 0; +#endif free( h ); io_close( s ); } } @@ -349,6 +365,15 @@ void graceful( int s ) { } } +#ifdef _DEBUG_FDS +void count_fds( int s ) { + int i, count = 0; + for( i=0; ir); free(h); } +#ifdef _DEBUG_FDS + if( !fd_debug_space[clientsocket] ) fprintf( stderr, "close on non-open fd\n" ); + fd_debug_space[clientsocket] = 0; +#endif io_close(clientsocket); return; } @@ -423,6 +452,11 @@ void handle_accept( int64 serversocket ) { continue; } +#ifdef _DEBUG_FDS + if( fd_debug_space[i] ) fprintf( stderr, "double use of fd: %i\n", (int)i ); + fd_debug_space[i] = 1; +#endif + io_wantread( i ); byte_zero(h,sizeof(struct http_data)); @@ -448,6 +482,10 @@ void handle_timeouted( ) { array_reset( &h->r ); free( h ); } +#ifdef _DEBUG_FDS + if( !fd_debug_space[i] ) fprintf( stderr, "close on non-open fd\n" ); + fd_debug_space[i] = 0; +#endif io_close(i); } } @@ -521,7 +559,10 @@ int main( int argc, char **argv ) { panic("io_fd"); signal( SIGPIPE, SIG_IGN ); - signal( SIGINT, graceful ); + signal( SIGINT, graceful ); +#ifdef _DEBUG_FDS + signal( SIGINFO, count_fds ); +#endif if( init_logic( serverdir ) == -1 ) panic("Logic not started");