diff --git a/src/logging.c b/src/logging.c index e47ebf2..1ff3ed1 100644 --- a/src/logging.c +++ b/src/logging.c @@ -51,6 +51,9 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) * */ + extern int terminate_signal; + extern int user_abort; + char** result; char* malloc_result; char message_buffer[MAX_LOG_LINE_CHARS * sizeof( char )]; @@ -326,57 +329,64 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) /* Open the log file for appending. */ fp = fopen( nwipe_options.logfile, "a" ); - if( fp == NULL ) + if( fp != NULL ) { - fprintf( stderr, "nwipe_log: Unable to open '%s' for logging.\n", nwipe_options.logfile ); + + /* Get the file descriptor of the log file. */ + fd = fileno( fp ); + + /* Block and lock. */ + r = flock( fd, LOCK_EX ); + + if( r != 0 ) + { + perror( "nwipe_log: flock:" ); + fprintf( stderr, "nwipe_log: Unable to lock '%s' for logging.\n", nwipe_options.logfile ); + r = pthread_mutex_unlock( &mutex1 ); + if( r != 0 ) + { + fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r ); + + /* Unlock the file. */ + r = flock( fd, LOCK_UN ); + fclose( fp ); + return; + } + } + + fprintf( fp, "%s\n", log_lines[log_current_element] ); + + /* Unlock the file. */ + r = flock( fd, LOCK_UN ); + + if( r != 0 ) + { + perror( "nwipe_log: flock:" ); + fprintf( stderr, "Error: Unable to unlock '%s' after logging.\n", nwipe_options.logfile ); + } + + /* Close the stream. */ + r = fclose( fp ); + + if( r != 0 ) + { + perror( "nwipe_log: fclose:" ); + fprintf( stderr, "Error: Unable to close '%s' after logging.\n", nwipe_options.logfile ); + } + } + else + { + /* Tell user we can't create/open the log and terminate nwipe */ + fprintf( + stderr, "\nERROR:Unable to create/open '%s' for logging, permissions?\n\n", nwipe_options.logfile ); r = pthread_mutex_unlock( &mutex1 ); if( r != 0 ) { fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r ); - return; } - } - - /* Get the file descriptor of the log file. */ - fd = fileno( fp ); - - /* Block and lock. */ - r = flock( fd, LOCK_EX ); - - if( r != 0 ) - { - perror( "nwipe_log: flock:" ); - fprintf( stderr, "nwipe_log: Unable to lock '%s' for logging.\n", nwipe_options.logfile ); - r = pthread_mutex_unlock( &mutex1 ); - if( r != 0 ) - { - fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r ); - - /* Unlock the file. */ - r = flock( fd, LOCK_UN ); - fclose( fp ); - return; - } - } - - fprintf( fp, "%s\n", log_lines[log_current_element] ); - - /* Unlock the file. */ - r = flock( fd, LOCK_UN ); - - if( r != 0 ) - { - perror( "nwipe_log: flock:" ); - fprintf( stderr, "Error: Unable to unlock '%s' after logging.\n", nwipe_options.logfile ); - } - - /* Close the stream. */ - r = fclose( fp ); - - if( r != 0 ) - { - perror( "nwipe_log: fclose:" ); - fprintf( stderr, "Error: Unable to close '%s' after logging.\n", nwipe_options.logfile ); + user_abort = 1; + terminate_signal = 1; + return; } } diff --git a/src/nwipe.c b/src/nwipe.c index 82bb948..d5f3798 100644 --- a/src/nwipe.c +++ b/src/nwipe.c @@ -116,11 +116,18 @@ int main( int argc, char** argv ) /* Parse command line options. */ nwipe_optind = nwipe_options_parse( argc, argv ); + if( nwipe_optind == argc ) { /* File names were not given by the user. Scan for devices. */ nwipe_enumerated = nwipe_device_scan( &c1 ); + if( terminate_signal == 1 ) + { + cleanup(); + exit( 1 ); + } + if( nwipe_enumerated == 0 ) { nwipe_log( NWIPE_LOG_INFO, diff --git a/src/version.c b/src/version.c index b10a123..2bba297 100644 --- a/src/version.c +++ b/src/version.c @@ -4,7 +4,7 @@ * used by configure to dynamically assign those values * to documentation files. */ -const char* version_string = "0.32.027"; +const char* version_string = "0.32.028"; const char* program_name = "nwipe"; const char* author_name = "Martijn van Brummelen"; const char* email_address = "git@brumit.nl"; @@ -14,4 +14,4 @@ Modifications to original dwipe Copyright Andy Beverley \n\ This is free software; see the source for copying conditions.\n\ There is NO warranty; not even for MERCHANTABILITY or FITNESS\n\ FOR A PARTICULAR PURPOSE.\n"; -const char* banner = "nwipe 0.32.027"; +const char* banner = "nwipe 0.32.028";