mirror of
https://github.com/martijnvanbrummelen/nwipe.git
synced 2026-02-20 05:32:14 +00:00
Formatting logging module.
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
|||||||
- name: verifying code style
|
- name: verifying code style
|
||||||
# TODO use check-format when all the code has been formatted.
|
# TODO use check-format when all the code has been formatted.
|
||||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/mt19937ar-cok.h src/mt19937ar-cok.c src/isaac_rand.c src/isaac_rand.h src/isaac_standard.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/logging.c src/logging.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
||||||
|
|||||||
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
|||||||
- name: verifying code style
|
- name: verifying code style
|
||||||
# TODO use check-format when all the code has been formatted.
|
# TODO use check-format when all the code has been formatted.
|
||||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/mt19937ar-cok.h src/mt19937ar-cok.c src/isaac_rand.c src/isaac_rand.h src/isaac_standard.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/logging.c src/logging.h src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
||||||
|
|||||||
732
src/logging.c
732
src/logging.c
@@ -2,7 +2,7 @@
|
|||||||
* logging.c: Logging facilities for nwipe.
|
* logging.c: Logging facilities for nwipe.
|
||||||
*
|
*
|
||||||
* Copyright Darik Horn <dajhorn-dban@vanadac.com>.
|
* Copyright Darik Horn <dajhorn-dban@vanadac.com>.
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it under
|
* This program is free software; you can redistribute it and/or modify it under
|
||||||
* the terms of the GNU General Public License as published by the Free Software
|
* the terms of the GNU General Public License as published by the Free Software
|
||||||
* Foundation, version 2.
|
* Foundation, version 2.
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program; if not, write to the Free Software Foundation, Inc.,
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -37,407 +37,433 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
/* Global array to hold log values to print when logging to STDOUT */
|
/* Global array to hold log values to print when logging to STDOUT */
|
||||||
char **log_lines;
|
char** log_lines;
|
||||||
int log_current_element = 0;
|
int log_current_element = 0;
|
||||||
int log_elements_allocated = 0;
|
int log_elements_allocated = 0;
|
||||||
int log_elements_displayed = 0;
|
int log_elements_displayed = 0;
|
||||||
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
|
||||||
void nwipe_log( nwipe_log_t level, const char* format, ... )
|
void nwipe_log( nwipe_log_t level, const char* format, ... )
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Writes a message to the program log file.
|
* Writes a message to the program log file.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char **result;
|
char** result;
|
||||||
char *malloc_result;
|
char* malloc_result;
|
||||||
char message_buffer[MAX_LOG_LINE_CHARS * sizeof(char)];
|
char message_buffer[MAX_LOG_LINE_CHARS * sizeof( char )];
|
||||||
int chars_written;
|
int chars_written;
|
||||||
|
|
||||||
int message_buffer_length;
|
|
||||||
int r; /* result buffer */
|
|
||||||
|
|
||||||
/* A time buffer. */
|
int message_buffer_length;
|
||||||
time_t t;
|
int r; /* result buffer */
|
||||||
|
|
||||||
/* A pointer to the system time struct. */
|
/* A time buffer. */
|
||||||
struct tm* p;
|
time_t t;
|
||||||
r = pthread_mutex_lock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_lock failed. Code %i \n", r );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get the current time. */
|
/* A pointer to the system time struct. */
|
||||||
t = time( NULL );
|
struct tm* p;
|
||||||
p = localtime( &t );
|
r = pthread_mutex_lock( &mutex1 );
|
||||||
|
if( r != 0 )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: pthread_mutex_lock failed. Code %i \n", r );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Position of writing to current log string */
|
/* Get the current time. */
|
||||||
int line_current_pos = 0;
|
t = time( NULL );
|
||||||
|
p = localtime( &t );
|
||||||
/* Print the date. The rc script uses the same format. */
|
|
||||||
chars_written = snprintf( message_buffer, MAX_LOG_LINE_CHARS, "[%i/%02i/%02i %02i:%02i:%02i] nwipe: ", \
|
|
||||||
1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec );
|
|
||||||
|
|
||||||
/* Has the end of the buffer been reached ?, snprintf returns the number of characters that would have been
|
|
||||||
* written if MAX_LOG_LINE_CHARS had not been reached, it does not return the actual characters written in
|
|
||||||
* all circumstances, hence why we need to check whether it's greater than MAX_LOG_LINE_CHARS and if so set
|
|
||||||
* it to MAX_LOG_LINE_CHARS, preventing a buffer overrun further down this function.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* check if there was a complete failure to write this part of the message, in which case return */
|
/* Position of writing to current log string */
|
||||||
if ( chars_written < 0 )
|
int line_current_pos = 0;
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( (line_current_pos + chars_written) > MAX_LOG_LINE_CHARS )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n", MAX_LOG_LINE_CHARS );
|
|
||||||
line_current_pos = MAX_LOG_LINE_CHARS;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
line_current_pos += chars_written;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( line_current_pos < MAX_LOG_LINE_CHARS )
|
/* Print the date. The rc script uses the same format. */
|
||||||
{
|
chars_written = snprintf( message_buffer,
|
||||||
switch( level )
|
MAX_LOG_LINE_CHARS,
|
||||||
{
|
"[%i/%02i/%02i %02i:%02i:%02i] nwipe: ",
|
||||||
|
1900 + p->tm_year,
|
||||||
|
1 + p->tm_mon,
|
||||||
|
p->tm_mday,
|
||||||
|
p->tm_hour,
|
||||||
|
p->tm_min,
|
||||||
|
p->tm_sec );
|
||||||
|
|
||||||
case NWIPE_LOG_NONE:
|
/*
|
||||||
/* Do nothing. */
|
* Has the end of the buffer been reached ?, snprintf returns the number of characters that would have been
|
||||||
break;
|
* written if MAX_LOG_LINE_CHARS had not been reached, it does not return the actual characters written in
|
||||||
|
* all circumstances, hence why we need to check whether it's greater than MAX_LOG_LINE_CHARS and if so set
|
||||||
|
* it to MAX_LOG_LINE_CHARS, preventing a buffer overrun further down this function.
|
||||||
|
*/
|
||||||
|
|
||||||
case NWIPE_LOG_DEBUG:
|
/* check if there was a complete failure to write this part of the message, in which case return */
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "debug: " );
|
if( chars_written < 0 )
|
||||||
break;
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
||||||
case NWIPE_LOG_INFO:
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "info: " );
|
if( r != 0 )
|
||||||
break;
|
{
|
||||||
|
|
||||||
case NWIPE_LOG_NOTICE:
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "notice: " );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NWIPE_LOG_WARNING:
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "warning: " );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NWIPE_LOG_ERROR:
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "error: " );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NWIPE_LOG_FATAL:
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "fatal: " );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NWIPE_LOG_SANITY:
|
|
||||||
/* TODO: Request that the user report the log. */
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "sanity: " );
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
chars_written = snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "level %i: ", level );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Has the end of the buffer been reached ?
|
|
||||||
*/
|
|
||||||
if ( chars_written < 0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( (line_current_pos + chars_written) > MAX_LOG_LINE_CHARS )
|
if( ( line_current_pos + chars_written ) > MAX_LOG_LINE_CHARS )
|
||||||
{
|
{
|
||||||
fprintf( stderr, "nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n", MAX_LOG_LINE_CHARS );
|
fprintf( stderr,
|
||||||
line_current_pos = MAX_LOG_LINE_CHARS;
|
"nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n",
|
||||||
}
|
MAX_LOG_LINE_CHARS );
|
||||||
else
|
line_current_pos = MAX_LOG_LINE_CHARS;
|
||||||
{
|
}
|
||||||
line_current_pos += chars_written;
|
else
|
||||||
}
|
{
|
||||||
}
|
line_current_pos += chars_written;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* The variable argument pointer. */
|
if( line_current_pos < MAX_LOG_LINE_CHARS )
|
||||||
va_list ap;
|
{
|
||||||
|
switch( level )
|
||||||
|
{
|
||||||
|
|
||||||
/* Fetch the argument list. */
|
case NWIPE_LOG_NONE:
|
||||||
va_start( ap, format );
|
/* Do nothing. */
|
||||||
|
break;
|
||||||
|
|
||||||
/* Print the event. */
|
|
||||||
if ( line_current_pos < MAX_LOG_LINE_CHARS )
|
|
||||||
{
|
|
||||||
chars_written = vsnprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos -1, format, ap );
|
|
||||||
|
|
||||||
if ( chars_written < 0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
va_end( ap );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( (line_current_pos + chars_written) > MAX_LOG_LINE_CHARS )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n", MAX_LOG_LINE_CHARS );
|
|
||||||
line_current_pos = MAX_LOG_LINE_CHARS;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
line_current_pos += chars_written;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fflush(stdout);
|
case NWIPE_LOG_DEBUG:
|
||||||
/* Increase the current log element pointer - we will write here, deallocation is done in cleanup() in nwipe.c */
|
chars_written =
|
||||||
if (log_current_element == log_elements_allocated) {
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "debug: " );
|
||||||
log_elements_allocated++;
|
break;
|
||||||
result = realloc (log_lines, (log_elements_allocated) * sizeof(char *));
|
|
||||||
if ( result == NULL )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: realloc failed when adding a log line.\n" );
|
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
va_end( ap );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log_lines = result;
|
|
||||||
|
|
||||||
/* Allocate memory for storing a single log message, deallocation is done in cleanup() in nwipe.c */
|
case NWIPE_LOG_INFO:
|
||||||
message_buffer_length = strlen( message_buffer ) * sizeof(char);
|
chars_written =
|
||||||
malloc_result = malloc((message_buffer_length + 1) * sizeof(char));
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "info: " );
|
||||||
if (malloc_result == NULL)
|
break;
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: malloc failed when adding a log line.\n" );
|
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
va_end( ap );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log_lines[log_current_element] = malloc_result;
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy ( log_lines[log_current_element], message_buffer );
|
case NWIPE_LOG_NOTICE:
|
||||||
|
chars_written =
|
||||||
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "notice: " );
|
||||||
|
break;
|
||||||
|
|
||||||
/*
|
case NWIPE_LOG_WARNING:
|
||||||
if( level >= NWIPE_LOG_WARNING )
|
chars_written =
|
||||||
{
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "warning: " );
|
||||||
vfprintf( stderr, format, ap );
|
break;
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Release the argument list. */
|
case NWIPE_LOG_ERROR:
|
||||||
va_end( ap );
|
chars_written =
|
||||||
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "error: " );
|
||||||
|
break;
|
||||||
|
|
||||||
/*
|
case NWIPE_LOG_FATAL:
|
||||||
if( level >= NWIPE_LOG_WARNING )
|
chars_written =
|
||||||
{
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "fatal: " );
|
||||||
fprintf( stderr, "\n" );
|
break;
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* The log file pointer. */
|
case NWIPE_LOG_SANITY:
|
||||||
FILE* fp;
|
/* TODO: Request that the user report the log. */
|
||||||
|
chars_written =
|
||||||
|
snprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "sanity: " );
|
||||||
|
break;
|
||||||
|
|
||||||
/* The log file descriptor. */
|
default:
|
||||||
int fd;
|
chars_written = snprintf(
|
||||||
|
message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos, "level %i: ", level );
|
||||||
|
}
|
||||||
|
|
||||||
if (nwipe_options.logfile[0] == '\0')
|
/*
|
||||||
{
|
* Has the end of the buffer been reached ?
|
||||||
if (nwipe_options.nogui)
|
*/
|
||||||
{
|
if( chars_written < 0 )
|
||||||
printf( "%s\n", log_lines[log_current_element] );
|
{
|
||||||
log_elements_displayed++;
|
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
||||||
}
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
} else
|
if( r != 0 )
|
||||||
{
|
{
|
||||||
/* Open the log file for appending. */
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
fp = fopen( nwipe_options.logfile, "a" );
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( ( line_current_pos + chars_written ) > MAX_LOG_LINE_CHARS )
|
||||||
|
{
|
||||||
|
fprintf( stderr,
|
||||||
|
"nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n",
|
||||||
|
MAX_LOG_LINE_CHARS );
|
||||||
|
line_current_pos = MAX_LOG_LINE_CHARS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
line_current_pos += chars_written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if( fp == NULL )
|
/* The variable argument pointer. */
|
||||||
{
|
va_list ap;
|
||||||
fprintf( stderr, "nwipe_log: Unable to open '%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 );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get the file descriptor of the log file. */
|
|
||||||
fd = fileno( fp );
|
|
||||||
|
|
||||||
/* Block and lock. */
|
/* Fetch the argument list. */
|
||||||
r = flock( fd, LOCK_EX );
|
va_start( ap, format );
|
||||||
|
|
||||||
if( r != 0 )
|
/* Print the event. */
|
||||||
{
|
if( line_current_pos < MAX_LOG_LINE_CHARS )
|
||||||
perror( "nwipe_log: flock:" );
|
{
|
||||||
fprintf( stderr, "nwipe_log: Unable to lock '%s' for logging.\n", nwipe_options.logfile );
|
chars_written =
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
vsnprintf( message_buffer + line_current_pos, MAX_LOG_LINE_CHARS - line_current_pos - 1, format, ap );
|
||||||
if ( r !=0 )
|
|
||||||
{
|
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
|
|
||||||
/* Unlock the file. */
|
if( chars_written < 0 )
|
||||||
r = flock( fd, LOCK_UN );
|
{
|
||||||
fclose( fp );
|
fprintf( stderr, "nwipe_log: snprintf error when writing log line to memory.\n" );
|
||||||
return;
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
}
|
if( r != 0 )
|
||||||
}
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
|
va_end( ap );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( ( line_current_pos + chars_written ) > MAX_LOG_LINE_CHARS )
|
||||||
|
{
|
||||||
|
fprintf( stderr,
|
||||||
|
"nwipe_log: Warning! The log line has been truncated as it exceeded %i characters\n",
|
||||||
|
MAX_LOG_LINE_CHARS );
|
||||||
|
line_current_pos = MAX_LOG_LINE_CHARS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
line_current_pos += chars_written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fprintf( fp, "%s\n", log_lines[log_current_element] );
|
fflush( stdout );
|
||||||
|
/* 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* ) );
|
||||||
|
if( result == NULL )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: realloc failed when adding a log line.\n" );
|
||||||
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
|
if( r != 0 )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
|
va_end( ap );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log_lines = result;
|
||||||
|
|
||||||
/* Unlock the file. */
|
/* Allocate memory for storing a single log message, deallocation is done in cleanup() in nwipe.c */
|
||||||
r = flock( fd, LOCK_UN );
|
message_buffer_length = strlen( message_buffer ) * sizeof( char );
|
||||||
|
malloc_result = malloc( ( message_buffer_length + 1 ) * sizeof( char ) );
|
||||||
|
if( malloc_result == NULL )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: malloc failed when adding a log line.\n" );
|
||||||
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
|
if( r != 0 )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
|
va_end( ap );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log_lines[log_current_element] = malloc_result;
|
||||||
|
}
|
||||||
|
|
||||||
if( r != 0 )
|
strcpy( log_lines[log_current_element], message_buffer );
|
||||||
{
|
|
||||||
perror( "nwipe_log: flock:" );
|
|
||||||
fprintf( stderr, "Error: Unable to unlock '%s' after logging.\n", nwipe_options.logfile );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Close the stream. */
|
/*
|
||||||
r = fclose( fp );
|
if( level >= NWIPE_LOG_WARNING )
|
||||||
|
{
|
||||||
|
vfprintf( stderr, format, ap );
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if( r != 0 )
|
/* Release the argument list. */
|
||||||
{
|
va_end( ap );
|
||||||
perror( "nwipe_log: fclose:" );
|
|
||||||
fprintf( stderr, "Error: Unable to close '%s' after logging.\n", nwipe_options.logfile );
|
/*
|
||||||
}
|
if( level >= NWIPE_LOG_WARNING )
|
||||||
}
|
{
|
||||||
|
fprintf( stderr, "\n" );
|
||||||
log_current_element++;
|
}
|
||||||
|
*/
|
||||||
r = pthread_mutex_unlock( &mutex1 );
|
|
||||||
if ( r !=0 )
|
/* The log file pointer. */
|
||||||
{
|
FILE* fp;
|
||||||
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
|
||||||
}
|
/* The log file descriptor. */
|
||||||
return;
|
int fd;
|
||||||
|
|
||||||
|
if( nwipe_options.logfile[0] == '\0' )
|
||||||
|
{
|
||||||
|
if( nwipe_options.nogui )
|
||||||
|
{
|
||||||
|
printf( "%s\n", log_lines[log_current_element] );
|
||||||
|
log_elements_displayed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Open the log file for appending. */
|
||||||
|
fp = fopen( nwipe_options.logfile, "a" );
|
||||||
|
|
||||||
|
if( fp == NULL )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: Unable to open '%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 );
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log_current_element++;
|
||||||
|
|
||||||
|
r = pthread_mutex_unlock( &mutex1 );
|
||||||
|
if( r != 0 )
|
||||||
|
{
|
||||||
|
fprintf( stderr, "nwipe_log: pthread_mutex_unlock failed. Code %i \n", r );
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
} /* nwipe_log */
|
} /* nwipe_log */
|
||||||
|
|
||||||
|
|
||||||
void nwipe_perror( int nwipe_errno, const char* f, const char* s )
|
void nwipe_perror( int nwipe_errno, const char* f, const char* s )
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Wrapper for perror().
|
* Wrapper for perror().
|
||||||
*
|
*
|
||||||
* We may wish to tweak or squelch this later.
|
* We may wish to tweak or squelch this later.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
nwipe_log( NWIPE_LOG_ERROR, "%s: %s: %s", f, s, strerror( nwipe_errno ) );
|
nwipe_log( NWIPE_LOG_ERROR, "%s: %s: %s", f, s, strerror( nwipe_errno ) );
|
||||||
|
|
||||||
} /* nwipe_perror */
|
} /* nwipe_perror */
|
||||||
|
|
||||||
int nwipe_log_sysinfo()
|
int nwipe_log_sysinfo()
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE* fp;
|
||||||
char path[256];
|
char path[256];
|
||||||
char cmd[50];
|
char cmd[50];
|
||||||
int len;
|
int len;
|
||||||
int r; /* A result buffer. */
|
int r; // A result buffer.
|
||||||
|
|
||||||
/* Remove or add keywords to be searched, depending on what information is to
|
/*
|
||||||
be logged, making sure the last entry in the array is a NULL string. To remove
|
* Remove or add keywords to be searched, depending on what information is to
|
||||||
an entry simply comment out the keyword with // */
|
* be logged, making sure the last entry in the array is a NULL string. To remove
|
||||||
char dmidecode_keywords[][24] = {
|
* an entry simply comment out the keyword with //
|
||||||
"bios-version",
|
*/
|
||||||
"bios-release-date",
|
char dmidecode_keywords[][24] = {
|
||||||
"system-manufacturer",
|
"bios-version",
|
||||||
"system-product-name",
|
"bios-release-date",
|
||||||
"system-version",
|
"system-manufacturer",
|
||||||
"system-serial-number",
|
"system-product-name",
|
||||||
"system-uuid",
|
"system-version",
|
||||||
"baseboard-manufacturer",
|
"system-serial-number",
|
||||||
"baseboard-product-name",
|
"system-uuid",
|
||||||
"baseboard-version",
|
"baseboard-manufacturer",
|
||||||
"baseboard-serial-number",
|
"baseboard-product-name",
|
||||||
"baseboard-asset-tag",
|
"baseboard-version",
|
||||||
"chassis-manufacturer",
|
"baseboard-serial-number",
|
||||||
"chassis-type",
|
"baseboard-asset-tag",
|
||||||
"chassis-version",
|
"chassis-manufacturer",
|
||||||
"chassis-serial-number",
|
"chassis-type",
|
||||||
"chassis-asset-tag",
|
"chassis-version",
|
||||||
"processor-family",
|
"chassis-serial-number",
|
||||||
"processor-manufacturer",
|
"chassis-asset-tag",
|
||||||
"processor-version",
|
"processor-family",
|
||||||
"processor-frequency",
|
"processor-manufacturer",
|
||||||
"" //terminates the keyword array. DO NOT REMOVE
|
"processor-version",
|
||||||
};
|
"processor-frequency",
|
||||||
unsigned int keywords_idx;
|
"" // terminates the keyword array. DO NOT REMOVE
|
||||||
|
};
|
||||||
|
unsigned int keywords_idx;
|
||||||
|
|
||||||
keywords_idx = 0;
|
keywords_idx = 0;
|
||||||
|
|
||||||
/* Run the dmidecode command to retrieve each dmidecode keyword, one at a time */
|
/* Run the dmidecode command to retrieve each dmidecode keyword, one at a time */
|
||||||
while ( dmidecode_keywords[keywords_idx][0] != 0 )
|
while( dmidecode_keywords[keywords_idx][0] != 0 )
|
||||||
{
|
{
|
||||||
sprintf(cmd,"dmidecode -s %s", &dmidecode_keywords[keywords_idx][0] );
|
sprintf( cmd, "dmidecode -s %s", &dmidecode_keywords[keywords_idx][0] );
|
||||||
fp = popen(cmd, "r");
|
fp = popen( cmd, "r" );
|
||||||
if (fp == NULL ) {
|
if( fp == NULL )
|
||||||
nwipe_log( NWIPE_LOG_INFO, "nwipe_log_sysinfo: Failed to create stream to %s", cmd );
|
{
|
||||||
return 1;
|
nwipe_log( NWIPE_LOG_INFO, "nwipe_log_sysinfo: Failed to create stream to %s", cmd );
|
||||||
}
|
return 1;
|
||||||
/* Read the output a line at a time - output it. */
|
}
|
||||||
while (fgets(path, sizeof(path)-1, fp) != NULL)
|
/* Read the output a line at a time - output it. */
|
||||||
{
|
while( fgets( path, sizeof( path ) - 1, fp ) != NULL )
|
||||||
/* Remove any trailing return from the string, as nwipe_log automatically adds a return */
|
{
|
||||||
len = strlen(path);
|
/* Remove any trailing return from the string, as nwipe_log automatically adds a return */
|
||||||
if( path[len-1] == '\n' ) {
|
len = strlen( path );
|
||||||
path[len-1] = 0;
|
if( path[len - 1] == '\n' )
|
||||||
}
|
{
|
||||||
nwipe_log( NWIPE_LOG_INFO, "%s = %s", &dmidecode_keywords[keywords_idx][0], path );
|
path[len - 1] = 0;
|
||||||
}
|
}
|
||||||
/* close */
|
nwipe_log( NWIPE_LOG_INFO, "%s = %s", &dmidecode_keywords[keywords_idx][0], path );
|
||||||
r = pclose(fp);
|
}
|
||||||
if( r > 0 )
|
/* close */
|
||||||
{
|
r = pclose( fp );
|
||||||
nwipe_log( NWIPE_LOG_INFO, "nwipe_log_sysinfo(): dmidecode failed, \"%s\" exit status = %u", cmd, WEXITSTATUS( r ));
|
if( r > 0 )
|
||||||
return 1;
|
{
|
||||||
}
|
nwipe_log( NWIPE_LOG_INFO,
|
||||||
keywords_idx++;
|
"nwipe_log_sysinfo(): dmidecode failed, \"%s\" exit status = %u",
|
||||||
}
|
cmd,
|
||||||
return 0;
|
WEXITSTATUS( r ) );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
keywords_idx++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* eof */
|
|
||||||
|
|||||||
@@ -14,38 +14,29 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program; if not, write to the Free Software Foundation, Inc.,
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef LOGGING_H_
|
#ifndef LOGGING_H_
|
||||||
#define LOGGING_H_
|
#define LOGGING_H_
|
||||||
|
|
||||||
/* Maximum size a log message can be */
|
/* Maximum size of a log message */
|
||||||
#define MAX_LOG_LINE_CHARS 512
|
#define MAX_LOG_LINE_CHARS 512
|
||||||
|
|
||||||
typedef enum nwipe_log_t_
|
typedef enum nwipe_log_t_ {
|
||||||
{
|
NWIPE_LOG_NONE = 0,
|
||||||
NWIPE_LOG_NONE = 0,
|
NWIPE_LOG_DEBUG, // TODO: Very verbose logging.
|
||||||
NWIPE_LOG_DEBUG, /* TODO: Very verbose logging. */
|
NWIPE_LOG_INFO, // TODO: Verbose logging.
|
||||||
NWIPE_LOG_INFO, /* TODO: Verbose logging. */
|
NWIPE_LOG_NOTICE, // Most logging happens at this level.
|
||||||
NWIPE_LOG_NOTICE, /* Most logging happens at this level. */
|
NWIPE_LOG_WARNING, // Things that the user should know about.
|
||||||
NWIPE_LOG_WARNING, /* Things that the user should know about. */
|
NWIPE_LOG_ERROR, // Non-fatal errors that result in failure.
|
||||||
NWIPE_LOG_ERROR, /* Non-fatal errors that result in failure. */
|
NWIPE_LOG_FATAL, // Errors that cause the program to exit.
|
||||||
NWIPE_LOG_FATAL, /* Errors that cause the program to exit. */
|
NWIPE_LOG_SANITY // Programming errors.
|
||||||
NWIPE_LOG_SANITY /* Programming errors. */
|
|
||||||
} nwipe_log_t;
|
} nwipe_log_t;
|
||||||
|
|
||||||
void nwipe_log( nwipe_log_t level, const char* format, ... );
|
void nwipe_log( nwipe_log_t level, const char* format, ... );
|
||||||
void nwipe_perror( int nwipe_errno, const char* f, const char* s );
|
void nwipe_perror( int nwipe_errno, const char* f, const char* s );
|
||||||
int nwipe_log_sysinfo();
|
int nwipe_log_sysinfo();
|
||||||
|
|
||||||
/* Global array to hold log values to print when logging to STDOUT */
|
|
||||||
//extern char **log_lines;
|
|
||||||
//extern int log_current_element;
|
|
||||||
//extern int log_elements_allocated;
|
|
||||||
|
|
||||||
#endif /* LOGGING_H_ */
|
#endif /* LOGGING_H_ */
|
||||||
|
|
||||||
/* eof */
|
|
||||||
|
|||||||
Reference in New Issue
Block a user