added outputerr/outputstd log functions
PATCH 1/6 added outputerr/outputstd log functions
--- include/log.h | 2 ++ log.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+)
include/log.h +2 -0
@@ -25,6 +25,8 @@unsigned int highest_logfile (void );void synclogs (void );void output (unsigned char level,const char *fmt, ...); +void outputerr (const char *fmt, ...); +void outputstd (const char *fmt, ...);void open_logfiles (void );void close_logfiles (void );
log.c +22 -0
@@ -221,3 +221,25 @@ void output(unsigned char level, const char *fmt, ...)fprintf (handle,"%s %s" , prefix, monobuf); (void )fflush (handle); } + +/* +* Used as a way to consolidated all printf calls if someones one to redirect it to somewhere else. +* note: this function ignores quiet_level since it main purpose is error output. +*/ +void outputerr (const char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +vfprintf (stderr, fmt, args); +va_end (args); +} + +void outputstd (const char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +vfprintf (stdout, fmt, args); +va_end (args); +}
Signature · 1 line
1.8.4
PATCH 2/6 wired outputstd/err functions
--- child.c | 4 +-- children/random-syscalls.c | 2 +- generic-sanitise.c | 2 +- ioctls/ioctls.c | 19 +++++++------- log.c | 18 ++++++------- main.c | 8 +++--- maps.c | 8 +++--- net/protocols.c | 7 ++--- params.c | 50 ++++++++++++++++++------------------ pids.c | 4 +-- seed.c | 4 +-- sockets.c | 6 ++--- syscalls/perf_event_open.c | 7 ++--- tables.c | 64 +++++++++++++++++++++++----------------------- trinity.c | 18 ++++++------- 15 files changed, 112 insertions(+), 109 deletions(-)
child.c +2 -2
@@ -50,7 +50,7 @@ static void reenable_coredumps(void)prctl (PR_SET_DUMPABLE , TRUE);if (setrlimit (RLIMIT_CORE , &oldrlimit) !=0 ) { - printf("[%d] Error restoring rlimits to cur:%d max:%d (%s)\n", + outputerr("[%d] Error restoring rlimits to cur:%d max:%d (%s)\n", getpid(), (unsigned int) oldrlimit.rlim_cur, (unsigned int) oldrlimit.rlim_max,
@@ -72,7 +72,7 @@ static void set_make_it_fail(void)if (write (fd, buf,1 ) ==-1 ) {if (errno !=EPERM ) -printf ("writing to /proc/self/make-it-fail failed! (%s)\n" ,strerror (errno)); +outputerr ("writing to /proc/self/make-it-fail failed! (%s)\n" ,strerror (errno));else shm->do_make_it_fail = TRUE; }
children/random-syscalls.c +1 -1
@@ -112,7 +112,7 @@ int child_random_syscalls(int childno) }if (shm->exit_reason !=STILL_RUNNING ) { -printf ("Main is not running, exiting" ); +outputerr ("Main is not running, exiting" ); goto out; }
generic-sanitise.c +1 -1
@@ -91,7 +91,7 @@ static unsigned long handle_arg_range(unsigned int call, unsigned int argnum) }if (high ==0 ) { -printf ("%s forgets to set hirange!\n" , syscalls[call].entry->name); +outputerr ("%s forgets to set hirange!\n" , syscalls[call].entry->name);BUG ("Fix syscall definition!\n" );return 0 ; }
ioctls/ioctls.c +10 -9
@@ -4,6 +4,7 @@#include <stdio.h> #include "trinity.h" // ARRAY_SIZE +#include "log.h" #include "files.h" #include "shm.h" #include "ioctls.h"
@@ -20,7 +21,7 @@ void register_ioctl_group(const struct ioctl_group *grp)return ;if (grps_cnt ==ARRAY_SIZE (grps)) { -fprintf (stderr,"WARNING: please grow IOCTL_GROUPS_MAX.\n" ); +outputerr ("WARNING: please grow IOCTL_GROUPS_MAX.\n" );return ; }
@@ -100,24 +101,24 @@ void dump_ioctls(void)for (i=0 ; i < grps_cnt; ++i) {if (grps[i]->name) -printf ("- %s:\n" , grps[i]->name); +outputerr ("- %s:\n" , grps[i]->name);else if (grps[i]->devtype) {if (grps[i]->devtype ==DEV_MISC ) -printf ("- misc devices" ); +outputerr ("- misc devices" );else if (grps[i]->devtype ==DEV_CHAR ) -printf ("- char devices" ); +outputerr ("- char devices" );else if (grps[i]->devtype ==DEV_BLOCK ) -printf ("- block devices" ); +outputerr ("- block devices" );for (j=0 ; j < grps[i]->devs_cnt; ++j) -printf ("%s '%s'" , +outputerr ("%s '%s'" , j ==0 ?"" :"," , grps[i]->devs[j]); -printf (":\n" ); +outputerr (":\n" ); }else -printf ("- <unknown>:\n" ); +outputerr ("- <unknown>:\n" );for (j=0 ; j < grps[i]->ioctls_cnt; ++j) { -printf (" - 0x%08x : %s\n" , +outputerr (" - 0x%08x : %s\n" , grps[i]->ioctls[j].request, grps[i]->ioctls[j].name ? :"" ); }
log.c +9 -9
@@ -22,7 +22,7 @@ void open_logfiles(void)unlink (logfilename); mainlogfile =fopen (logfilename,"a" );if (!mainlogfile) { -printf ("## couldn't open logfile %s\n" , logfilename); +outputerr ("## couldn't open logfile %s\n" , logfilename);exit (EXIT_FAILURE ); }
@@ -31,7 +31,7 @@ void open_logfiles(void)unlink (logfilename); shm->logfiles[i] =fopen (logfilename,"a" );if (!shm->logfiles[i]) { -printf ("## couldn't open logfile %s\n" , logfilename); +outputerr ("## couldn't open logfile %s\n" , logfilename);exit (EXIT_FAILURE ); } }
@@ -73,12 +73,12 @@ static FILE * find_logfile_handle(void)if (i !=PIDSLOT_NOT_FOUND )return shm->logfiles[i]; -printf ("[%d] ## Couldn't find logfile for pid %d\n" ,getpid (), pid); +outputerr ("## Couldn't find logfile for pid %d\n" , pid);dump_pid_slots (); -printf ("## Logfiles for pids: " ); +outputerr ("## Logfiles for pids: " );for_each_pidslot (j) -printf ("%p " , shm->logfiles[j]); -printf ("\n" ); +outputerr ("%p " , shm->logfiles[j]); +outputerr ("\n" ); }return NULL ; }
@@ -108,7 +108,7 @@ void synclogs(void)for_each_pidslot (i) { ret =fflush (shm->logfiles[i]);if (ret ==EOF ) { -printf ("## logfile flushing failed! %s\n" ,strerror (errno)); +outputerr ("## logfile flushing failed! %s\n" ,strerror (errno));continue ; }
@@ -116,7 +116,7 @@ void synclogs(void)if (fd !=-1 ) { ret =fsync (fd);if (ret !=0 ) -printf ("## fsyncing logfile %d failed. %s\n" , i,strerror (errno)); +outputerr ("## fsyncing logfile %d failed. %s\n" , i,strerror (errno)); } }
@@ -172,7 +172,7 @@ void output(unsigned char level, const char *fmt, ...)va_end (args);if (n <0 ) { -printf ("## Something went wrong in output() [%d]\n" , n); +outputerr ("## Something went wrong in output() [%d]\n" , n);exit (EXIT_FAILURE ); }
main.c +4 -4
@@ -109,7 +109,7 @@ static void fork_children(void)/* Find a space for it in the pid map */ pidslot =find_pid_slot (EMPTY_PIDSLOT );if (pidslot ==PIDSLOT_NOT_FOUND ) { -printf ("## Pid map was full!\n" ); +outputerr ("## Pid map was full!\n" );dump_pid_slots ();exit (EXIT_FAILURE ); }
@@ -137,7 +137,7 @@ static void fork_children(void) ret =pid_alive (mainpid);if (ret !=0 ) { shm->exit_reason =EXIT_SHM_CORRUPTION ; -printf (BUGTXT "parent (%d) went away!\n" , mainpid); +outputerr (BUGTXT "parent (%d) went away!\n" , mainpid);sleep (20000 ); } }
@@ -236,7 +236,7 @@ static void handle_child(pid_t childpid, int childstatus)if (slot ==PIDSLOT_NOT_FOUND ) {/* If we reaped it, it wouldn't show up, so check that. */ if (shm->last_reaped != childpid) { -printf ("## Couldn't find pid slot for %d\n" , childpid); +outputerr ("## Couldn't find pid slot for %d\n" , childpid); shm->exit_reason =EXIT_LOST_PID_SLOT ;dump_pid_slots (); }
@@ -407,7 +407,7 @@ void do_main_loop(void)while (pidmap_empty () == FALSE)handle_children (); -printf ("Bailing main loop. Exit reason: %s\n" ,decode_exit (shm->exit_reason)); +outputerr ("Bailing main loop. Exit reason: %s\n" ,decode_exit (shm->exit_reason));_exit (EXIT_SUCCESS ); }
maps.c +4 -4
@@ -39,7 +39,7 @@ static struct map * alloc_map(void) newmap =malloc (sizeof (struct map ));if (!newmap) { -printf ("Couldn't allocate maps list!\n" ); +outputerr ("Couldn't allocate maps list!\n" );exit (EXIT_FAILURE ); }memset (newmap,0 ,sizeof (struct map ));
@@ -70,7 +70,7 @@ static void * alloc_zero_map(struct map *map, int prot, const char *name) fd =open ("/dev/zero" ,O_RDWR );if (fd <0 ) { -printf ("open /dev/zero failure. %s\n" ,strerror (errno)); +outputerr ("open /dev/zero failure. %s\n" ,strerror (errno));exit (EXIT_FAILURE ); }
@@ -97,7 +97,7 @@ static void * alloc_zero_map(struct map *map, int prot, const char *name) tmpmap->ptr =mmap (NULL , size, prot,MAP_ANONYMOUS |MAP_SHARED , fd,0 );if (tmpmap->ptr ==MAP_FAILED ) { -printf ("mmap /dev/zero failure\n" ); +outputerr ("mmap /dev/zero failure\n" );exit (EXIT_FAILURE ); }
@@ -105,7 +105,7 @@ static void * alloc_zero_map(struct map *map, int prot, const char *name) tmpmap->name =malloc (80 );if (!tmpmap->name) { -fprintf (stderr,"malloc() failed in %s()." , __func__); +outputerr ("malloc() failed in %s()." , __func__);exit (EXIT_FAILURE ); }
net/protocols.c +4 -3
@@ -6,6 +6,7 @@#include "trinity.h" #include "constants.h" #include "net.h" +#include "log.h" struct protocol {const char *name;
@@ -89,10 +90,10 @@ void find_specific_proto(const char *protoarg) }if (i >TRINITY_PF_MAX ) { -printf ("Protocol unknown. Pass a numeric value [0-%d] or one of " ,TRINITY_PF_MAX ); +outputerr ("Protocol unknown. Pass a numeric value [0-%d] or one of " ,TRINITY_PF_MAX );for (i =0 ; i <ARRAY_SIZE (protocols); i++) -printf ("%s " , protocols[i].name); -printf ("\n" ); +outputerr ("%s " , protocols[i].name); +outputerr ("\n" );exit (EXIT_FAILURE ); }
params.c +25 -25
74 lines not on this page · raw message
pids.c +2 -2
@@ -54,7 +54,7 @@ void dump_pid_slots(void) sptr +=sprintf (sptr,"\n" ); } *sptr ='\0' ; -printf ("%s" , string); +outputerr ("%s" , string); }static pid_t pidmax;
@@ -95,7 +95,7 @@ void pids_init(void)#else pidmax =32768 ;#endif -printf ("Couldn't read pid_max from proc\n" ); +outputerr ("Couldn't read pid_max from proc\n" ); }printf ("[init] Using pid_max = %d\n" , pidmax);
seed.c +2 -2
@@ -19,7 +19,7 @@ unsigned int seed = 0;static void syslog_seed (int seedparam) { -fprintf (stderr,"Randomness reseeded to %u\n" , seedparam); +outputerr ("Randomness reseeded to %u\n" , seedparam);openlog ("trinity" ,LOG_CONS |LOG_PERROR ,LOG_USER );syslog (LOG_CRIT ,"Randomness reseeded to %u\n" , seedparam);closelog ();
@@ -86,7 +86,7 @@ void reseed(void) shm->reseed_counter =0 ;if (getpid () != mainpid) { -output (0 ,"Reseeding should only happen from parent!\n" ); +outputerr ("Reseeding should only happen from parent!\n" );exit (EXIT_FAILURE ); }
sockets.c +3 -3
@@ -116,7 +116,7 @@ static void generate_sockets(void) cachefile =creat (cachefilename,S_IWUSR |S_IRUSR );if (cachefile <0 ) { -printf ("Couldn't open cachefile for writing! (%s)\n" , +outputerr ("Couldn't open cachefile for writing! (%s)\n" ,strerror (errno));exit (EXIT_FAILURE ); }
@@ -152,14 +152,14 @@ static void generate_sockets(void) buffer[2 ] = st.protocol; n =write (cachefile, &buffer,sizeof (int ) *3 );if (n ==-1 ) { -printf ("something went wrong writing the cachefile!\n" ); +outputerr ("something went wrong writing the cachefile!\n" );exit (EXIT_FAILURE ); }if (nr_to_create ==0 ) goto done; }else { -//printf("Couldn't open family:%d (%s)\n", st.family, get_proto_name(st.family)); +//outputerr("Couldn't open family:%d (%s)\n", st.family, get_proto_name(st.family)); } skip:
syscalls/perf_event_open.c +4 -3
@@ -15,6 +15,7 @@#include "compat.h" #include "maps.h" #include "shm.h" +#include "log.h" #define SYSFS "/sys/bus/event_source/devices/"
@@ -97,7 +98,7 @@ static int parse_format(char *string, int *field_type, unsigned long long *mask)if (string[i]=='-' )break ;if (string[i]==',' )break ;if ((string[i]<'0' ) || (string[i]>'9' )) { -fprintf (stderr,"Unknown format char %c\n" ,string[i]); +outputerr ("Unknown format char %c\n" , string[i]);return -1 ; } firstnum*=10 ;
@@ -119,7 +120,7 @@ static int parse_format(char *string, int *field_type, unsigned long long *mask)if (string[i]=='-' )break ;if (string[i]==',' )break ;if ((string[i]<'0' ) || (string[i]>'9' )) { -fprintf (stderr,"Unknown format char %c\n" ,string[i]); +outputerr ("Unknown format char %c\n" , string[i]);return -1 ; } secondnum*=10 ;
@@ -238,7 +239,7 @@ static int parse_generic(int pmu, char *value,if (value[ptr]==',' )break ;if (! ( ((value[ptr]>='0' ) && (value[ptr]<='9' )) || ((value[ptr]>='a' ) && (value[ptr]<='f' ))) ) { -fprintf (stderr,"Unexpected char %c\n" ,value[ptr]); +outputerr ("Unexpected char %c\n" , value[ptr]); } temp*=base;if ((value[ptr]>='0' ) && (value[ptr]<='9' )) {
tables.c +32 -32
178 lines not on this page · raw message
trinity.c +9 -9
@@ -99,7 +99,7 @@ static void setup_shm_postargs(void) shm->max_children =sysconf (_SC_NPROCESSORS_ONLN);if (shm->max_children >MAX_NR_CHILDREN ) { -printf ("Increase MAX_NR_CHILDREN!\n" ); +outputerr ("Increase MAX_NR_CHILDREN!\n" );exit (EXIT_FAILURE ); } }
@@ -145,8 +145,8 @@ static int munge_tables(void)display_enabled_syscalls ();if (validate_syscall_tables () == FALSE) { -printf ("No syscalls were enabled!\n" ); -printf ("Use 32bit:%d 64bit:%d\n" , use_32bit, use_64bit); +outputstd ("No syscalls were enabled!\n" ); +outputstd ("Use 32bit:%d 64bit:%d\n" , use_32bit, use_64bit);return FALSE; }
@@ -230,17 +230,17 @@ int main(int argc, char* argv[])if (getuid () ==0 ) {if (dangerous == TRUE) { -printf ("DANGER: RUNNING AS ROOT.\n" ); -printf ("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n" ); -printf ("or similar which could potentially make this machine unbootable without a firmware reset.\n\n" ); -printf ("ctrl-c now unless you really know what you are doing.\n" ); +outputstd ("DANGER: RUNNING AS ROOT.\n" ); +outputstd ("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n" ); +outputstd ("or similar which could potentially make this machine unbootable without a firmware reset.\n\n" ); +outputstd ("ctrl-c now unless you really know what you are doing.\n" );for (i =10 ; i >0 ; i--) { -printf ("Continuing in %d seconds.\r" , i); +outputstd ("Continuing in %d seconds.\r" , i); (void )fflush (stdout);sleep (1 ); } }else { -printf ("Don't run as root (or pass --dangerous if you know what you are doing).\n" ); +outputstd ("Don't run as root (or pass --dangerous if you know what you are doing).\n" );exit (EXIT_FAILURE ); } }
Signature · 1 line
1.8.4
PATCH 3/6 refactored output function
In reply to Ildar Muslukhov--- log.c | 77 ++++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 29 deletions(-)
log.c +48 -29
@@ -10,7 +10,10 @@#include "pids.h" #include "log.h" +#define BUFSIZE 1024 +FILE *mainlogfile; +bool logfiles_opened = FALSE;void open_logfiles (void ) {
@@ -36,6 +39,7 @@ void open_logfiles(void) } }free (logfilename); + logfiles_opened = TRUE; }void close_logfiles (void )
@@ -124,6 +128,25 @@ void synclogs(void)fsync (fileno (mainlogfile)); } +static FILE *robust_find_logfile_handle (void ) +{ +unsigned int j; +FILE *handle =NULL ; + +if ((logging == TRUE) && (logfiles_opened)) { + handle =find_logfile_handle (); +if (!handle) { +outputerr ("## child logfile handle was null logging to main!\n" ); + (void )fflush (stdout); +for_each_pidslot (j) + shm->logfiles[j] = mainlogfile; +sleep (5 ); + handle =find_logfile_handle (); + } + } +return handle; +} +/* * level defines whether it gets displayed to the screen with printf. * (it always logs).
@@ -139,8 +162,8 @@ void output(unsigned char level, const char *fmt, ...)FILE *handle;unsigned int len, i, j;pid_t pid; -char outputbuf[1024 ]; -char monobuf[1024 ]; +char outputbuf[BUFSIZE ]; +char monobuf[BUFSIZE ];char *prefix =NULL ;char watchdog_prefix[]="[watchdog]" ;char init_prefix[]="[init]" ;
@@ -151,6 +174,7 @@ void output(unsigned char level, const char *fmt, ...)if (logging == FALSE && level >= quiet_level)return ; +/* prefix preparation */ pid =getpid ();if (pid == watchdog_pid) prefix = watchdog_prefix;
@@ -167,6 +191,7 @@ void output(unsigned char level, const char *fmt, ...) prefix = child_prefix; } +/* formatting output */ va_start (args, fmt); n =vsnprintf (outputbuf,sizeof (outputbuf), fmt, args);va_end (args);
@@ -176,49 +201,43 @@ void output(unsigned char level, const char *fmt, ...)exit (EXIT_FAILURE ); } +/* stdout output if needed */ if (quiet_level > level) {printf ("%s %s" , prefix, outputbuf); (void )fflush (stdout); } +/* go on with file logs only if enabled */ if (logging == FALSE)return ; - handle =find_logfile_handle (); -if (!handle) { -printf ("## child logfile handle was null logging to main!\n" ); - (void )fflush (stdout); -for_each_pidslot (j) - shm->logfiles[j] = mainlogfile; -sleep (5 ); + handle =robust_find_logfile_handle (); +if (!handle)return ; - }/* If we've specified monochrome, we can just dump the buffer into * the logfile as is, because there shouldn't be any ANSI codes * in the buffer to be stripped out. */ -if (monochrome == TRUE) { -fprintf (handle,"%s %s" , prefix, outputbuf); - (void )fflush (handle); -return ; - } - -/* copy buffer, sans ANSI codes */ - len =strlen (outputbuf); -for (i =0 , j =0 ; i < len; i++) { -if (outputbuf[i] =='' ) { -if (outputbuf[i +2 ] =='1' ) - i +=6 ;// ANSI_COLOUR -else - i +=3 ;// ANSI_RESET - }else { - monobuf[j] = outputbuf[i]; - j++; +if (monochrome == FALSE) { +/* copy buffer, sans ANSI codes */ + len =strlen (outputbuf); +for (i =0 , j =0 ; (i < len) && (i +2 <BUFSIZE ) && (j <BUFSIZE ); i++) { +if (outputbuf[i] =='' ) { +if (outputbuf[i +2 ] =='1' ) + i +=6 ;// ANSI_COLOUR +else + i +=3 ;// ANSI_RESET + }else { + monobuf[j] = outputbuf[i]; + j++; + } } + monobuf[j] ='\0' ; +fprintf (handle,"%s %s" , prefix, monobuf); + }else { +fprintf (handle,"%s %s" , prefix, outputbuf); } - monobuf[j] ='\0' ; -fprintf (handle,"%s %s" , prefix, monobuf); (void )fflush (handle); }
Signature · 1 line
1.8.4
Re: [PATCH 3/6] refactored output function
Since applying this patch, coverity is picking up an out of bounds write. It looks like it can't happen, but I'm wondering why this code was done this way..
hand-editted diff for clarity:
Quoted 5 lines
(quoted diff) +1
-1/* copy buffer, sans ANSI codes */ len = strlen(outputbuf); - for (i = 0, j = 0; i < len; i++) { + for (i = 0, j = 0; (i < len) && (i + 2 < BUFSIZE) && (j < BUFSIZE); i++) { if (outputbuf[i] == '') {
Review
Quoted 10 lines
(quoted diff) +0
-0if (outputbuf[i + 2] == '1') i += 6; // ANSI_COLOUR else i += 3; // ANSI_RESET } else { monobuf[j] = outputbuf[i]; j++; } } monobuf[j] = '\0';
What's the intent behind this ?
It seems redundant, as everything seems to work fine without this change.
Dave
Re: [PATCH 3/6] refactored output function
Sorry, just noticed this one.
I added it just for robustness in future, as the buffered output was also working till the point when it got overflown. This way buffer won't be overflown.
-Ildar
Quoted 27 lines · Dave Jones
Since applying this patch, coverity is picking up an out of bounds write. It looks like it can't happen, but I'm wondering why this code was done this way..
hand-editted diff for clarity:
Quoted 5 lines
/* copy buffer, sans ANSI codes */ len = strlen(outputbuf);(quoted diff) +1
-1- for (i = 0, j = 0; i < len; i++) { + for (i = 0, j = 0; (i < len) && (i + 2 < BUFSIZE) && (j < BUFSIZE); i++) { if (outputbuf[i] == ' ') {Review
Quoted 10 lines
(quoted diff) +0
-0if (outputbuf[i + 2] == '1') i += 6; // ANSI_COLOUR else i += 3; // ANSI_RESET } else { monobuf[j] = outputbuf[i]; j++; } } monobuf[j] = '\0';What's the intent behind this ?
It seems redundant, as everything seems to work fine without this change.Dave
PATCH 4/6 wired in output function instead of printf (and some missing outputstd)
In reply to Ildar Muslukhov--- children/random-syscalls.c | 9 ++------- net/protocols.c | 4 ++-- pids.c | 2 +- seed.c | 7 +++++-- sockets.c | 10 +++++----- tables.c | 34 +++++++++++++++++----------------- trinity.c | 8 ++++---- watchdog.c | 8 ++++---- 8 files changed, 40 insertions(+), 42 deletions(-)
children/random-syscalls.c +2 -7
@@ -76,7 +76,6 @@ extern int sigwas;int child_random_syscalls (int childno) { -pid_t pid =getpid ();int ret;unsigned int syscallnr;
@@ -139,14 +138,10 @@ int child_random_syscalls(int childno)if (syscalls_todo) {if (shm->total_syscalls_done >= syscalls_todo) { -output (0 ,"[%d] shm->total_syscalls_done (%d) >= syscalls_todo (%d)\n" , - pid, shm->total_syscalls_done,syscalls_todo); +output (0 ,"Reached maximum syscall count (todo = %d, done = %d), exiting...\n" , + syscalls_todo, shm->total_syscalls_done); shm->exit_reason =EXIT_REACHED_COUNT ; } - -if (shm->total_syscalls_done == syscalls_todo) -printf ("[%d] Reached maximum syscall count %ld\n" , - pid, shm->total_syscalls_done); } ret =mkcall (childno);
net/protocols.c +2 -2
@@ -77,7 +77,7 @@ void find_specific_proto(const char *protoarg)for (i =0 ; i <ARRAY_SIZE (protocols); i++) {if (strcmp (protoarg, protocols[i].name) ==0 ) { specific_proto = protocols[i].proto; -printf ("Proto %s = %d\n" , protoarg, specific_proto); +output (2 ,"Proto %s = %d\n" , protoarg, specific_proto);break ; } }
@@ -98,5 +98,5 @@ void find_specific_proto(const char *protoarg)exit (EXIT_FAILURE ); } -printf ("Using protocol %s (%u) for all sockets\n" , protocols[i].name, protocols[i].proto); +output (2 ,"Using protocol %s (%u) for all sockets\n" , protocols[i].name, protocols[i].proto); }
pids.c +1 -1
@@ -98,7 +98,7 @@ void pids_init(void)outputerr ("Couldn't read pid_max from proc\n" ); } -printf ("[init] Using pid_max = %d\n" , pidmax); +output (0 ,"Using pid_max = %d\n" , pidmax); }int pid_is_valid (pid_t pid)
seed.c +5 -2
@@ -50,11 +50,11 @@ unsigned int new_seed(void)unsigned int init_seed (unsigned int seedparam) {if (user_set_seed == TRUE) -printf ("Using user passed random seed: %u\n" , seedparam); +output (0 ,"Using user passed random seed: %u\n" , seedparam);else { seedparam =new_seed (); -printf ("Initial random seed: %u\n" , seedparam); +output (0 ,"Initial random seed: %u\n" , seedparam); }if (do_syslog == TRUE)
@@ -69,6 +69,9 @@ unsigned int init_seed(unsigned int seedparam)*/ void set_seed (unsigned int pidslot) { +pid_t pid =getpid (); +if ((pid != watchdog_pid) && (pid != initpid) && (pid != mainpid)) +output (0 ,"Setting seed: %u\n" , shm->seed + (pidslot +1 ));srand (shm->seed + (pidslot +1 )); shm->seeds[pidslot] = shm->seed; }
sockets.c +5 -5
@@ -190,7 +190,7 @@ static void close_sockets(void) fd = shm->socket_fds[i]; shm->socket_fds[i] =0 ;if (close (fd) !=0 ) { -printf ("failed to close socket.(%s)\n" ,strerror (errno)); +output (1 ,"failed to close socket.(%s)\n" ,strerror (errno)); } }
@@ -211,7 +211,7 @@ void open_sockets(void) cachefile =open (cachefilename,O_RDONLY );if (cachefile <0 ) { -printf ("Couldn't find socket cachefile. Regenerating.\n" ); +output (1 ,"Couldn't find socket cachefile. Regenerating.\n" );generate_sockets ();return ; }
@@ -229,7 +229,7 @@ void open_sockets(void)if (do_specific_proto == TRUE) {if (domain != specific_proto) { -printf ("ignoring socket cachefile due to specific protocol request, and stale data in cachefile.\n" ); +output (1 ,"ignoring socket cachefile due to specific protocol request, and stale data in cachefile.\n" ); regenerate:unlock_cachefile (cachefile);/* drop the reader lock. */ close (cachefile);
@@ -241,7 +241,7 @@ regenerate: fd =open_socket (domain, type, protocol);if (fd <0 ) { -printf ("Cachefile is stale. Need to regenerate.\n" ); +output (1 ,"Cachefile is stale. Need to regenerate.\n" );close_sockets (); goto regenerate; }
@@ -254,7 +254,7 @@ regenerate: }if (nr_sockets <NR_SOCKET_FDS ) { -printf ("Insufficient sockets in cachefile (%d). Regenerating.\n" , nr_sockets); +output (1 ,"Insufficient sockets in cachefile (%d). Regenerating.\n" , nr_sockets); goto regenerate; }
tables.c +17 -17
@@ -52,10 +52,10 @@ static void validate_specific_syscall(const struct syscalltable *table, int callreturn ;if (table[call].entry->flags &AVOID_SYSCALL ) -printf ("%s is marked as AVOID. Skipping\n" , table[call].entry->name); +output (0 ,"%s is marked as AVOID. Skipping\n" , table[call].entry->name);if (table[call].entry->flags &NI_SYSCALL ) -printf ("%s is NI_SYSCALL. Skipping\n" , table[call].entry->name); +output (0 ,"%s is NI_SYSCALL. Skipping\n" , table[call].entry->name); }int validate_specific_syscall_silent (const struct syscalltable *table,int call)
@@ -145,12 +145,12 @@ void deactivate_syscall(unsigned int calln)void count_syscalls_enabled (void ) {if (biarch == TRUE) { -printf ("[init] 32-bit syscalls: %d enabled, %d disabled. " +output (0 ,"32-bit syscalls: %d enabled, %d disabled. " "64-bit syscalls: %d enabled, %d disabled.\n" , shm->nr_active_32bit_syscalls, max_nr_32bit_syscalls - shm->nr_active_32bit_syscalls, shm->nr_active_64bit_syscalls, max_nr_64bit_syscalls - shm->nr_active_64bit_syscalls); }else { -printf ("Enabled %d syscalls. Disabled %d syscalls.\n" , +output (0 ,"Enabled %d syscalls. Disabled %d syscalls.\n" , shm->nr_active_syscalls, max_nr_syscalls - shm->nr_active_syscalls); } }
@@ -380,7 +380,7 @@ static void toggle_syscall_biarch_n(int calln, const struct syscalltable *table, }if ((arch_bits !=0 ) && (calln !=-1 )) -printf ("Marking %d-bit syscall %s (%d) as to be %sabled.\n" , +output (0 ,"Marking %d-bit syscall %s (%d) as to be %sabled.\n" , arch_bits, arg_name, calln, state ?"en" :"dis" ); }
@@ -415,7 +415,7 @@ static void toggle_syscall_biarch(const char *arg, bool state) }if ((specific_syscall64 !=-1 ) && (specific_syscall32 !=-1 )) { -printf ("Marking syscall %s (64bit:%d 32bit:%d) as to be %sabled.\n" , +output (0 ,"Marking syscall %s (64bit:%d 32bit:%d) as to be %sabled.\n" , arg_name, specific_syscall64, specific_syscall32, state ?"en" :"dis" );clear_check_user_specified_arch (arg, &arg_name);
@@ -423,7 +423,7 @@ static void toggle_syscall_biarch(const char *arg, bool state) }if (specific_syscall64 !=-1 ) { -printf ("Marking 64-bit syscall %s (%d) as to be %sabled.\n" , +output (0 ,"Marking 64-bit syscall %s (%d) as to be %sabled.\n" , arg, specific_syscall64, state ?"en" :"dis" );clear_check_user_specified_arch (arg, &arg_name);
@@ -431,7 +431,7 @@ static void toggle_syscall_biarch(const char *arg, bool state) }if (specific_syscall32 !=-1 ) { -printf ("Marking 32-bit syscall %s (%d) as to be %sabled.\n" , +output (0 ,"Marking 32-bit syscall %s (%d) as to be %sabled.\n" , arg, specific_syscall32, state ?"en" :"dis" );clear_check_user_specified_arch (arg, &arg_name);
@@ -456,7 +456,7 @@ static void toggle_syscall_n(int calln, bool state, const char *arg, const char syscalls[calln].entry->flags |=TO_BE_DEACTIVATED ; } -printf ("Marking syscall %s (%d) as to be %sabled.\n" , +output (0 ,"Marking syscall %s (%d) as to be %sabled.\n" , arg_name, calln, state ?"en" :"dis" ); }
@@ -482,14 +482,14 @@ void deactivate_disabled_syscalls(void) {unsigned int i; -printf ("Disabling syscalls marked as disabled by command line options\n" ); +output (0 ,"Disabling syscalls marked as disabled by command line options\n" );if (biarch == TRUE) {for_each_64bit_syscall (i) {if (syscalls_64bit[i].entry->flags &TO_BE_DEACTIVATED ) { syscalls_64bit[i].entry->flags &= ~(ACTIVE |TO_BE_DEACTIVATED );deactivate_syscall64 (i); -printf ("Marked 64-bit syscall %s (%d) as deactivated.\n" , +output (0 ,"Marked 64-bit syscall %s (%d) as deactivated.\n" , syscalls_64bit[i].entry->name, syscalls_64bit[i].entry->number); } }
@@ -497,7 +497,7 @@ void deactivate_disabled_syscalls(void)if (syscalls_32bit[i].entry->flags &TO_BE_DEACTIVATED ) { syscalls_32bit[i].entry->flags &= ~(ACTIVE |TO_BE_DEACTIVATED );deactivate_syscall32 (i); -printf ("Marked 32-bit syscall %s (%d) as deactivated.\n" , +output (0 ,"Marked 32-bit syscall %s (%d) as deactivated.\n" , syscalls_32bit[i].entry->name, syscalls_32bit[i].entry->number); } }
@@ -507,7 +507,7 @@ void deactivate_disabled_syscalls(void)if (syscalls[i].entry->flags &TO_BE_DEACTIVATED ) { syscalls[i].entry->flags &= ~(ACTIVE |TO_BE_DEACTIVATED );deactivate_syscall (i); -printf ("Marked syscall %s (%d) as deactivated.\n" , +output (0 ,"Marked syscall %s (%d) as deactivated.\n" , syscalls[i].entry->name, syscalls[i].entry->number); } }
@@ -704,19 +704,19 @@ void display_enabled_syscalls(void)if (biarch == TRUE) {for_each_64bit_syscall (i) {if (syscalls_64bit[i].entry->flags &ACTIVE ) -printf ("64-bit syscall %d:%s enabled.\n" , i, syscalls_64bit[i].entry->name); +output (0 ,"64-bit syscall %d:%s enabled.\n" , i, syscalls_64bit[i].entry->name); }for_each_32bit_syscall (i) {if (syscalls_32bit[i].entry->flags &ACTIVE ) -printf ("32-bit syscall %d:%s enabled.\n" , i, syscalls_32bit[i].entry->name); +output (0 ,"32-bit syscall %d:%s enabled.\n" , i, syscalls_32bit[i].entry->name); } }else {/* non-biarch */ for_each_syscall (i) {if (syscalls[i].entry->flags &ACTIVE ) -printf ("syscall %d:%s enabled.\n" , i, syscalls[i].entry->name); +output (0 ,"syscall %d:%s enabled.\n" , i, syscalls[i].entry->name); } } }
@@ -766,7 +766,7 @@ void disable_non_net_syscalls(void) {unsigned int i; -printf ("Disabling non networking related syscalls\n" ); +output (0 ,"Disabling non networking related syscalls\n" );if (biarch == TRUE) {for_each_64bit_syscall (i) {
trinity.c +4 -4
@@ -185,7 +185,7 @@ int main(int argc, char* argv[])int childstatus;unsigned int i; - printf("Trinity v" __stringify(VERSION) " Dave Jones <[email protected]>\n"); + outputstd("Trinity v" __stringify(VERSION) " Dave Jones <[email protected]>\n"); progname = argv[0 ];
@@ -200,10 +200,10 @@ int main(int argc, char* argv[])exit (EXIT_FAILURE );parse_args (argc, argv); -printf ("Done parsing arguments.\n" ); +outputstd ("Done parsing arguments.\n" );if (kernel_taint_mask != (int )0xFFFFFFFF ) { -printf ("Custom kernel taint mask has been specified: 0x%08x (%d).\n" , kernel_taint_mask, kernel_taint_mask); +outputstd ("Custom kernel taint mask has been specified: 0x%08x (%d).\n" , kernel_taint_mask, kernel_taint_mask); }setup_shm_postargs ();
@@ -274,7 +274,7 @@ int main(int argc, char* argv[])/* Shutting down. */ waitpid (watchdog_pid, &childstatus,0 ); -printf ("\nRan %ld syscalls. Successes: %ld Failures: %ld\n" , +output (0 ,"\nRan %ld syscalls. Successes: %ld Failures: %ld\n" , shm->total_syscalls_done -1 , shm->successes, shm->failures); ret =EXIT_SUCCESS ;
watchdog.c +4 -4
@@ -188,7 +188,7 @@ static void check_children(void)/* if we wrapped, just reset it, we'll pick it up next time around. */ if (old > (now +3 )) { -printf ("child %d wrapped! old=%ld now=%ld\n" , i, old, now); +output (1 ,"child %d wrapped! old=%ld now=%ld\n" , i, old, now); shm->tv[i].tv_sec = now;continue ; }
@@ -275,7 +275,7 @@ static void watchdog(void)bool watchdog_exit = FALSE;int ret =0 ; -printf ("[watchdog] Watchdog is alive. (pid:%d)\n" , watchdog_pid); +output (0 ,"Watchdog is alive. (pid:%d)\n" , watchdog_pid);prctl (PR_SET_NAME , (unsigned long ) &watchdogname); (void )signal (SIGSEGV ,SIG_DFL );
@@ -303,9 +303,9 @@ static void watchdog(void)if (shm->total_syscalls_done %1000 ==0 )synclogs (); -if ((quiet_level >1 ) && (shm->total_syscalls_done >1 )) { +if (shm->total_syscalls_done >1 ) {if (shm->total_syscalls_done - lastcount >10000 ) { -printf ("[watchdog] %ld iterations. [F:%ld S:%ld]\n" , +output (0 ,"%ld iterations. [F:%ld S:%ld]\n" , shm->total_syscalls_done, shm->failures, shm->successes); lastcount = shm->total_syscalls_done; }
Signature · 1 line
1.8.4
Re: [PATCH 4/6] wired in output function instead of printf (and some missing outputstd)
seed.c unsigned int init_seed(unsigned int seedparam) · line 73
Quoted 4 lines · Ildar Muslukhov
void
set_seed(unsigned int pidslot)
{seed.c +2
-0+pid_t pid =getpid (); +if ((pid != watchdog_pid) && (pid != initpid) && (pid != mainpid))
As a follow-on patch, I suggest splitting this out into an is_child_pid() helper.
Dave
Re: [PATCH 4/6] wired in output function instead of printf (and some missing outputstd)
Quoted 9 lines · Dave Jones
seed.c unsigned int init_seed(unsigned int seedparam) · line 73
Quoted 4 lines · Ildar Muslukhov
void
set_seed(unsigned int pidslot)
{seed.c +2
-0+pid_t pid =getpid (); +if ((pid != watchdog_pid) && (pid != initpid) && (pid != mainpid))As a follow-on patch, I suggest splitting this out into an
is_child_pid()helper.
on second thought, the printing of the seed from the child is a) pointless because we can infer it from the main seed, and b) really noisy (especially when you run a lot of children).
let's drop this chunk
Dave
PATCH 5/6 added bufferless logging functions for syscall pamaters
In reply to Ildar Muslukhov--- include/log.h | 9 ++++++ log.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+)
include/log.h +9 -0
@@ -1,6 +1,8 @@#ifndef _LOG_H#define _LOG_H 1 +#include "types.h" +#define ANSI_RED "[1;31m"#define ANSI_GREEN "[1;32m"#define ANSI_YELLOW "[1;33m"
@@ -21,12 +23,19 @@#define CRESETPTR if (monochrome == FALSE) *sptr += sprintf(*sptr, "%s", ANSI_RESET); +#define REDFD if (mono == FALSE) fprintf(fd, "%s", ANSI_RED); +#define GREENFD if (mono == FALSE) fprintf(fd, "%s", ANSI_GREEN); +#define CRESETFD if (mono == FALSE) fprintf(fd, "%s", ANSI_RESET); +#define MAX_LOGLEVEL 3unsigned int highest_logfile (void );void synclogs (void );void output (unsigned char level,const char *fmt, ...);void outputerr (const char *fmt, ...);void outputstd (const char *fmt, ...); +void output_syscall_prefix (const unsigned int childno,const unsigned int syscallno); +void output_syscall_postfix (unsigned long ret,int errno_saved,bool err); +void open_logfiles (void );void close_logfiles (void );
log.c +101 -0
117 lines not on this page · raw message
Signature · 1 line
1.8.4
Re: [PATCH 5/6] added bufferless logging functions for syscall pamaters
log.c void synclogs(void) · line 137
Quoted 4 lines · Ildar Muslukhov
log.c +4
-0+static void output_arg (unsigned int call,unsigned int argnum,const char *name,unsigned long oldreg,unsigned long reg,int type,FILE *fd,bool mono) +{ +} +
uh, what ?
Quoted 18 lines
log.c +18
-0+ +static void output_syscall_prefix_to_fd (const unsigned int childno,const pid_t pid,const unsigned int syscallno,FILE *fd,bool mono) +{ +fprintf (fd,"[child%d:%d] [%ld] %s" , childno, pid, shm->child_syscall_count[childno], + (shm->do32bit[childno] == TRUE) ?"[32BIT] " :"" ); + +if (syscallno > max_nr_syscalls) +fprintf (fd,"%u" , syscallno); +else +fprintf (fd,"%s" , syscalls[syscallno].entry->name); + +CRESETFD + fprintf(fd, "("); +output_arg (syscallno,1 , syscalls[syscallno].entry->arg1name, shm->previous_a1[childno], shm->a1[childno], + syscalls[syscallno].entry->arg1type, fd, mono); +output_arg (syscallno,2 , syscalls[syscallno].entry->arg2name, shm->previous_a2[childno], shm->a2[childno], + syscalls[syscallno].entry->arg2type, fd, mono); + output_arg(syscallno, 3, syscalls[syscallno].entry->arg3name, shm->previous_a3[childno], shm->a3[childno],
log.c void outputstd(const char *fmt, ...) · line 299
Quoted 10 lines
log.c +10
-0+ syscalls[syscallno].entry->arg3type, fd, mono); +output_arg (syscallno,4 , syscalls[syscallno].entry->arg4name, shm->previous_a4[childno], shm->a4[childno], + syscalls[syscallno].entry->arg4type, fd, mono); +output_arg (syscallno,5 , syscalls[syscallno].entry->arg5name, shm->previous_a5[childno], shm->a5[childno], + syscalls[syscallno].entry->arg5type, fd, mono); +output_arg (syscallno,6 , syscalls[syscallno].entry->arg6name, shm->previous_a6[childno], shm->a6[childno], + syscalls[syscallno].entry->arg6type, fd, mono); +CRESETFD + fprintf(fd, ") "); +}
Dave
PATCH 6/6 wired syscall parameters logging function into syscall (this should fix stack smash bug detected)
In reply to Ildar Muslukhov--- include/log.h | 3 -- log.c | 63 +++++++++++++++++++++++++- syscall.c | 139 +++++----------------------------------------------------- 3 files changed, 72 insertions(+), 133 deletions(-)
include/log.h +0 -3
@@ -21,8 +21,6 @@#define WHITE if (monochrome == FALSE) sptr += sprintf(sptr, "%s", ANSI_WHITE);#define CRESET if (monochrome == FALSE) sptr += sprintf(sptr, "%s", ANSI_RESET); -#define CRESETPTR if (monochrome == FALSE) *sptr += sprintf(*sptr, "%s", ANSI_RESET); -#define REDFD if (mono == FALSE) fprintf(fd, "%s", ANSI_RED);#define GREENFD if (mono == FALSE) fprintf(fd, "%s", ANSI_GREEN);#define CRESETFD if (mono == FALSE) fprintf(fd, "%s", ANSI_RESET);
@@ -48,7 +46,6 @@ void close_logfiles(void);#define BUGTXT ANSI_RED "BUG!: " ANSI_RESET GIT_VERSION#endif -#define BUG (bugtxt) { printf("%s:%s:%d %s", __FILE__, __func__, __LINE__, bugtxt); while(1); }#endif /* _LOG_H */
log.c +62 -1
75 lines not on this page · raw message
syscall.c +10 -129
182 lines not on this page · raw message
Signature · 1 line
1.8.4
Re: [PATCH 6/6] wired syscall parameters logging function into syscall (this should fix stack smash bug detected)
Quoted 5 lines · Ildar Muslukhov
log.c +0
-0
log.c void synclogs(void) · line 141
Quoted 9 lines
log.c +6
-0@@ -133,6 +133,68 @@ void synclogs(void)static void output_arg (unsigned int call,unsigned int argnum,const char *name,unsigned long oldreg,unsigned long reg,int type,FILE *fd,bool mono) { +if (syscalls[call].entry->num_args >= argnum) { +if (!name) +return ; + +if (argnum !=1 ) { +CRESETFD
oh, you add it in the next patch.. this won't work. Patch series need to be functional at every step, or the ability to use git-bisect breaks.
Dave