diff --git a/fiche.c b/fiche.c index 54b609e..887274e 100644 --- a/fiche.c +++ b/fiche.c @@ -33,6 +33,7 @@ Use netcat to push text - example: #include #include +#include #include #include #include @@ -543,12 +544,18 @@ static void dispatch_connection(int socket, Fiche_Settings *settings) { // Spawn a new thread to handle this connection pthread_t id; + pthread_attr_t attr; - if ( pthread_create(&id, NULL, &handle_connection, c) != 0 ) { + if ( (errno = pthread_attr_init(&attr)) || + (errno = pthread_attr_setstacksize(&attr, 128*1024)) || + (errno = pthread_create(&id, &attr, &handle_connection, c)) ) { + pthread_attr_destroy(&attr); print_error("Couldn't spawn a thread!"); return; } + pthread_attr_destroy(&attr); + // Detach thread if created succesfully // TODO: consider using pthread_tryjoin_np pthread_detach(id); @@ -557,6 +564,8 @@ static void dispatch_connection(int socket, Fiche_Settings *settings) { static void *handle_connection(void *args) { + char *slug = NULL; + uint8_t *buffer = NULL; // Cast args to it's previous type struct fiche_connection *c = (struct fiche_connection *) args; @@ -584,22 +593,20 @@ static void *handle_connection(void *args) { } // Create a buffer - uint8_t buffer[c->settings->buffer_len]; - memset(buffer, 0, c->settings->buffer_len); + buffer = calloc(c->settings->buffer_len, 1); + if (!buffer) { + print_error("Couldn't allocate the buffer!"); + print_separator(); + + goto exit; + } - const int r = recv(c->socket, buffer, sizeof(buffer), MSG_WAITALL); + const int r = recv(c->socket, buffer, c->settings->buffer_len, MSG_WAITALL); if (r <= 0) { print_error("No data received from the client!"); print_separator(); - // Close the socket - close(c->socket); - - // Cleanup - free(c); - pthread_exit(NULL); - - return 0; + goto exit; } // - Check if request was performed with a known protocol @@ -621,7 +628,6 @@ static void *handle_connection(void *args) { } // Generate slug and use it to create an url - char *slug; uint8_t extra = 0; do { @@ -650,7 +656,7 @@ static void *handle_connection(void *args) { free(c); free(slug); pthread_exit(NULL); - return NULL; + goto exit; } } @@ -662,12 +668,7 @@ static void *handle_connection(void *args) { print_error("Couldn't generate a slug!"); print_separator(); - close(c->socket); - - // Cleanup - free(c); - pthread_exit(NULL); - return NULL; + goto exit; } @@ -676,13 +677,7 @@ static void *handle_connection(void *args) { print_error("Couldn't save a file!"); print_separator(); - close(c->socket); - - // Cleanup - free(c); - free(slug); - pthread_exit(NULL); - return NULL; + goto exit; } // Write a response to the user @@ -704,15 +699,15 @@ static void *handle_connection(void *args) { // TODO: log unsuccessful and rejected connections log_entry(c->settings, ip, hostname, slug); +exit: // Close the connection close(c->socket); // Perform cleanup of values used in this thread + free(buffer); free(slug); free(c); - pthread_exit(NULL); - return NULL; }