From 0c584f43f1b30ab0ae895f58593c8035d666e16f Mon Sep 17 00:00:00 2001 From: sr Date: Tue, 16 Jul 2013 12:30:06 +0200 Subject: Fix more bugs, remove debug messages --- src/server/image.c | 8 ++------ src/server/net.c | 4 ++++ src/server/server.c | 51 +++++++++++++++++++++++++-------------------------- src/server/server.h | 1 + 4 files changed, 32 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/server/image.c b/src/server/image.c index 4fe465b..4d066ad 100644 --- a/src/server/image.c +++ b/src/server/image.c @@ -70,7 +70,7 @@ int image_is_complete(dnbd3_image_t *image) */ int image_save_cache_map(dnbd3_image_t *image) { - if ( image == NULL ) return TRUE; + if ( image == NULL || image->cache_map == NULL ) return TRUE; char mapfile[strlen( image->path ) + 4 + 1]; int fd; strcpy( mapfile, image->path ); @@ -104,9 +104,7 @@ dnbd3_image_t* image_get(char *name, uint16_t revision) pthread_spin_lock( &_images_lock ); for (i = 0; i < _num_images; ++i) { dnbd3_image_t * const image = _images[i]; - printf( "Comparing '%s' and '%s'..\n", name, image->lower_name ); if ( image == NULL || strcmp( image->lower_name, name ) != 0 ) continue; - printf( "RID %d and %d\n", (int)revision, (int)image->rid ); if ( revision == image->rid ) { candidate = image; break; @@ -126,7 +124,7 @@ dnbd3_image_t* image_get(char *name, uint16_t revision) // Found, see if it works struct stat st; if ( stat( candidate->path, &st ) < 0 ) { - printf( "File '%s' has gone away...\n", candidate->path ); + printf( "[DEBUG] File '%s' has gone away...\n", candidate->path ); candidate->working = FALSE; // No file? OUT! } candidate->users++; @@ -318,7 +316,6 @@ static int image_try_load(char *base, char *path) sprintf( mapFile, "%s.map", path ); int fdMap = open( mapFile, O_RDONLY ); if ( fdMap >= 0 ) { - printf( "Opened %s, cache_map is %p\n", mapFile, cache_map ); size_t map_size = IMGSIZE_TO_MAPBYTES( fileSize ); cache_map = calloc( 1, map_size ); int rd = read( fdMap, cache_map, map_size ); @@ -405,7 +402,6 @@ static int image_try_load(char *base, char *path) image->atime = time( NULL ); } image->working = (image->cache_map == NULL ); - printf( "Map: %p, work: %d\n", image->cache_map, (int)image->working ); pthread_spin_init( &image->lock, PTHREAD_PROCESS_PRIVATE ); // Get rid of cache map if image is complete if ( image->cache_map != NULL && image_is_complete( image ) ) { diff --git a/src/server/net.c b/src/server/net.c index 6aa5d5b..51d8415 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -133,6 +133,9 @@ void *net_client_handler(void *dnbd3_client) dnbd3_server_entry_t server_list[NUMBER_SERVERS]; + // Set to zero to make valgrind happy + memset(&reply, 0, sizeof(reply)); + memset(&payload, 0, sizeof(payload)); reply.magic = dnbd3_packet_magic; // Receive first packet. This must be CMD_SELECT_IMAGE by protocol specification @@ -357,5 +360,6 @@ void *net_client_handler(void *dnbd3_client) exit_client_cleanup: if ( client->sock != -1 ) close( client->sock ); if ( image_file != -1 ) close( image_file ); dnbd3_remove_client( client ); + client = dnbd3_free_client( client ); pthread_exit( (void *)0 ); } diff --git a/src/server/server.c b/src/server/server.c index 85461ff..19dd18d 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -54,7 +54,6 @@ char *_rpc_password = NULL; char *_cache_dir = NULL; static int dnbd3_add_client(dnbd3_client_t *client); -static dnbd3_client_t* dnbd3_free_client(dnbd3_client_t *client); static void dnbd3_load_config(); static void dnbd3_handle_sigpipe(int signum); static void dnbd3_handle_sigterm(int signum); @@ -267,14 +266,14 @@ int main(int argc, char *argv[]) // This has to be done before creating the thread, otherwise a race condition might occur when the new thread dies faster than this thread adds the client to the list after creating the thread if ( !dnbd3_add_client( dnbd3_client ) ) { - dnbd3_free_client( dnbd3_client ); + dnbd3_client = dnbd3_free_client( dnbd3_client ); continue; } if ( 0 != pthread_create( &(dnbd3_client->thread), NULL, net_client_handler, (void *)(uintptr_t)dnbd3_client ) ) { memlogf( "[ERROR] Could not start thread for new client." ); dnbd3_remove_client( dnbd3_client ); - dnbd3_free_client( dnbd3_client ); + dnbd3_client = dnbd3_free_client( dnbd3_client ); continue; } pthread_detach( dnbd3_client->thread ); @@ -331,6 +330,29 @@ void dnbd3_remove_client(dnbd3_client_t *client) pthread_spin_unlock( &_clients_lock ); } +/** + * Free the client struct recursively. + * !! Make sure to call this function after removing the client from _dnbd3_clients !! + * Locks on: _clients[].lock + */ +dnbd3_client_t* dnbd3_free_client(dnbd3_client_t *client) +{ + GSList *it; + pthread_spin_lock(&client->lock); + for (it = client->sendqueue; it; it = it->next) { + free( it->data ); + } + g_slist_free( client->sendqueue ); + if ( client->sock >= 0 ) close( client->sock ); + client->sock = -1; + if ( client->image != NULL ) image_release( client->image ); + client->image = NULL; + pthread_spin_unlock(&client->lock); + pthread_spin_destroy(&client->lock); + free( client ); + return NULL; +} + //###// /** @@ -357,29 +379,6 @@ static int dnbd3_add_client(dnbd3_client_t *client) return TRUE; } -/** - * Free the client struct recursively. - * !! Make sure to call this function after removing the client from _dnbd3_clients !! - * Locks on: _clients[].lock - */ -static dnbd3_client_t* dnbd3_free_client(dnbd3_client_t *client) -{ - GSList *it; - pthread_spin_lock(&client->lock); - for (it = client->sendqueue; it; it = it->next) { - free( it->data ); - } - g_slist_free( client->sendqueue ); - if ( client->sock >= 0 ) close( client->sock ); - client->sock = -1; - if ( client->image != NULL ) image_release( client->image ); - client->image = NULL; - pthread_spin_unlock(&client->lock); - pthread_spin_destroy(&client->lock); - free( client ); - return NULL; -} - static void dnbd3_load_config() { // Load configuration diff --git a/src/server/server.h b/src/server/server.h index 413b2d4..8c068a9 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -43,6 +43,7 @@ extern int _fake_delay; void dnbd3_cleanup(); void dnbd3_remove_client(dnbd3_client_t *client); +dnbd3_client_t* dnbd3_free_client(dnbd3_client_t *client); dnbd3_client_t* dnbd3_init_client(struct sockaddr_storage *client, int fd); #if !defined(_FILE_OFFSET_BITS) || _FILE_OFFSET_BITS != 64 -- cgit v1.2.3-55-g7522