From 4acf8d2fd3b9d55ac57d2b733631f87e2d23c04d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 14 Nov 2013 10:12:03 +0100 Subject: [SERVER] Create client threads detached instead of detaching them after creation to prevent a race condition where a thread dies faster than we can call pthread_detach, which leads to a use-after-free --- src/server/server.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/server/server.c b/src/server/server.c index 2d1448c..f5204fc 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -329,9 +329,9 @@ int main(int argc, char *argv[]) //pthread_t thread_rpc; //pthread_create(&(thread_rpc), NULL, &dnbd3_rpc_mainloop, NULL); - // setup the job thread (query other servers, delete old images etc.) - //pthread_t thread_job; - //pthread_create(&(thread_job), NULL, &dnbd3_job_thread, NULL); + pthread_attr_t threadAttrs; + pthread_attr_init( &threadAttrs ); + pthread_attr_setdetachstate( &threadAttrs, PTHREAD_CREATE_DETACHED ); memlogf( "[INFO] Server is ready..." ); @@ -362,13 +362,12 @@ int main(int argc, char *argv[]) continue; } - if ( 0 != pthread_create( &(dnbd3_client->thread), NULL, net_client_handler, (void *)(uintptr_t)dnbd3_client ) ) { + if ( 0 != pthread_create( &(dnbd3_client->thread), &threadAttrs, net_client_handler, (void *)(uintptr_t)dnbd3_client ) ) { memlogf( "[ERROR] Could not start thread for new client." ); dnbd3_remove_client( dnbd3_client ); dnbd3_client = dnbd3_free_client( dnbd3_client ); continue; } - pthread_detach( dnbd3_client->thread ); } } -- cgit v1.2.3-55-g7522