diff --git a/src/logging.c b/src/logging.c index 71b1dea..8d79a7a 100644 --- a/src/logging.c +++ b/src/logging.c @@ -188,7 +188,7 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) } } - /* Increase the current log element pointer - we will write here */ + /* Increase the current log element pointer - we will write here, deallocation is done in cleanup() in nwipe.c */ if (log_current_element == log_elements_allocated) { log_elements_allocated++; result = realloc (log_lines, (log_elements_allocated) * sizeof(char *)); @@ -200,6 +200,7 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) } log_lines = result; + /* Allocate memory for storing a single log message, deallocation is done in cleanup() in nwipe.c */ message_buffer_length = strlen( message_buffer ) * sizeof(char); malloc_result = malloc((message_buffer_length + 1) * sizeof(char)); if (malloc_result == NULL) diff --git a/src/nwipe.c b/src/nwipe.c index 31e6a01..c1a5828 100644 --- a/src/nwipe.c +++ b/src/nwipe.c @@ -102,6 +102,7 @@ int main( int argc, char** argv ) nwipe_enumerated = nwipe_device_get( &c1, argv, argc ); if ( nwipe_enumerated == 0 ) { + cleanup(); exit(1); } @@ -210,6 +211,7 @@ int main( int argc, char** argv ) if( nwipe_options.nogui ) { printf("--nogui option must be used with autonuke option\n"); + cleanup(); exit(1); } else @@ -481,6 +483,8 @@ int main( int argc, char** argv ) printf("%s\n", log_lines[i]); } + cleanup(); + /* Success. */ return 0; @@ -604,8 +608,7 @@ void *signal_hand(void *ptr) printf("Program interrupted (caught signal %d)\n", sig); - // Cleanup - // TODO: All other cleanup required + cleanup(); exit(0); @@ -619,5 +622,20 @@ void *signal_hand(void *ptr) } /* end of signal_hand */ +int cleanup() +{ + /* Deallocate memory used by logging */ + int idx; + for ( idx=0; idx < log_elements_allocated; idx++ ) + { + free ( log_lines[idx] ); + } + log_elements_allocated=0; /* zeroed just in case cleanup is called twice */ + free ( log_lines ); + + /* TODO: All other cleanup required */ + + return 0; +} /* eof */ diff --git a/src/nwipe.h b/src/nwipe.h index b5a79da..33ab1e0 100644 --- a/src/nwipe.h +++ b/src/nwipe.h @@ -20,6 +20,9 @@ * */ +/* Function prototypes */ +int cleanup(); + #ifndef NWIPE_H_ #define NWIPE_H_