mirror of
https://github.com/martijnvanbrummelen/nwipe.git
synced 2026-02-20 13:42:14 +00:00
Merge pull request #410 from PartialVolume/fix_segfault_when_logfile_cannot_be_created_due_to_permissions
Fix obscure segfault when --logfile option used with a non writable directory.
This commit is contained in:
100
src/logging.c
100
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 <andy@andybev.com>\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";
|
||||
|
||||
Reference in New Issue
Block a user