From 354ebb48db8e66a853a58379a4808d5dcd1ceac3 Mon Sep 17 00:00:00 2001 From: Wanlong Gao Date: Fri, 7 Dec 2012 10:10:04 +0800 Subject: cleanup code indent Cleanup the code indent using: find . -name *.c -exec Lindent {} \; It's really a big change, but can fix almost all of the indent problem in C code, although we can't ensure all of the changes are right, but the error changes are really few. Signed-off-by: Wanlong Gao --- pan/debug.c | 56 +- pan/ltp-bump.c | 44 +- pan/ltp-pan.c | 2186 +++++++++++++++++++++++++++-------------------------- pan/ltp-scanner.c | 43 +- pan/reporter.c | 327 ++++---- pan/splitstr.c | 223 +++--- pan/symbol.c | 530 +++++++------ pan/tag_report.c | 659 ++++++++-------- pan/zoolib.c | 618 ++++++++------- 9 files changed, 2345 insertions(+), 2341 deletions(-) (limited to 'pan') diff --git a/pan/debug.c b/pan/debug.c index 75f7d4707..9027f9bac 100644 --- a/pan/debug.c +++ b/pan/debug.c @@ -35,7 +35,6 @@ #include #include "reporter.h" - #ifdef DEBUGGING int Debug[MAXDEBUG]; /* Debug level in their areas */ #endif @@ -45,42 +44,41 @@ int Debug[MAXDEBUG]; /* Debug level in their areas */ * * Syntax: area[,area]:level[,area[,area]:level]... */ -int -set_debug(char *optarg) +int set_debug(char *optarg) { #ifdef DEBUGGING - /* pointers to the debug area and level in the option's arguments */ - char *d_area, *d_level; - /* debug area and level after converted to integers */ - int db_area, db_level; + /* pointers to the debug area and level in the option's arguments */ + char *d_area, *d_level; + /* debug area and level after converted to integers */ + int db_area, db_level; - d_area = optarg; + d_area = optarg; - while (*d_area) { - d_level= strchr(d_area,':'); - *d_level++ = '\0'; - db_level= atoi(d_level); - db_area=atoi(d_area); + while (*d_area) { + d_level = strchr(d_area, ':'); + *d_level++ = '\0'; + db_level = atoi(d_level); + db_area = atoi(d_area); - if (db_area > MAXDEBUG) { - printf("Error - Debug area %s > maximum of %d\n", d_area, - MAXDEBUG); - exit(-1); - } + if (db_area > MAXDEBUG) { + printf("Error - Debug area %s > maximum of %d\n", + d_area, MAXDEBUG); + exit(-1); + } - while (d_area != NULL) { - db_area = atoi(d_area); - printf("Debug area %d set to %d\n", db_area, db_level); - Debug[db_area] = db_level; - if ((d_area = strchr(d_area, ',')) != NULL) - d_area++; + while (d_area != NULL) { + db_area = atoi(d_area); + printf("Debug area %d set to %d\n", db_area, db_level); + Debug[db_area] = db_level; + if ((d_area = strchr(d_area, ',')) != NULL) + d_area++; + } + if ((d_area = strchr(d_level, ',')) == NULL) + break; } - if ((d_area = strchr(d_level, ',')) == NULL) - break; - } #else - printf("Debugging is not enabled. -D has been ignored\n"); + printf("Debugging is not enabled. -D has been ignored\n"); #endif - return 0; + return 0; } diff --git a/pan/ltp-bump.c b/pan/ltp-bump.c index 26fd92ffe..5966c2e84 100644 --- a/pan/ltp-bump.c +++ b/pan/ltp-bump.c @@ -40,7 +40,7 @@ #include "zoolib.h" -pid_t read_active(FILE *fp, char *name); +pid_t read_active(FILE * fp, char *name); int main(int argc, char **argv) { @@ -51,27 +51,28 @@ int main(int argc, char **argv) int sig = SIGINT; while ((c = getopt(argc, argv, "a:s:12")) != -1) { - switch(c) { - case 'a': - active = (char*)malloc(strlen(optarg)+1); - strcpy( active, optarg ); - break; - case 's': - sig = atoi( optarg ); - break; - case '1': - sig = SIGUSR1; - break; - case '2': - sig = SIGUSR2; - break; + switch (c) { + case 'a': + active = (char *)malloc(strlen(optarg) + 1); + strcpy(active, optarg); + break; + case 's': + sig = atoi(optarg); + break; + case '1': + sig = SIGUSR1; + break; + case '2': + sig = SIGUSR2; + break; } } if (active == NULL) { active = zoo_getname(); if (active == NULL) { - fprintf(stderr, "ltp-bump: Must supply -a or set ZOO env variable\n"); + fprintf(stderr, + "ltp-bump: Must supply -a or set ZOO env variable\n"); exit(1); } } @@ -88,18 +89,21 @@ int main(int argc, char **argv) } while (optind < argc) { - /*printf("argv[%d] = (%s)\n", optind, argv[optind] );*/ + /*printf("argv[%d] = (%s)\n", optind, argv[optind] ); */ nanny = zoo_getpid(zoo, argv[optind]); if (nanny == -1) { fprintf(stderr, "ltp-bump: Did not find tag '%s'\n", argv[optind]); } else { - if (kill( nanny, sig ) == -1) { + if (kill(nanny, sig) == -1) { if (errno == ESRCH) { - fprintf(stderr,"ltp-bump: Tag %s (pid %d) seems to be dead already.\n", + fprintf(stderr, + "ltp-bump: Tag %s (pid %d) seems to be dead already.\n", argv[optind], nanny); if (zoo_clear(zoo, nanny)) - fprintf(stderr,"ltp-bump: %s\n", zoo_error); + fprintf(stderr, + "ltp-bump: %s\n", + zoo_error); } } } diff --git a/pan/ltp-pan.c b/pan/ltp-pan.c index 73c464184..6a1a79742 100644 --- a/pan/ltp-pan.c +++ b/pan/ltp-pan.c @@ -69,33 +69,29 @@ #include "zoolib.h" /* One entry in the command line collection. */ -struct coll_entry -{ - char *name; /* tag name */ - char *cmdline; /* command line */ - char *pcnt_f; /* location of %f in the command line args, flag */ - struct coll_entry *next; +struct coll_entry { + char *name; /* tag name */ + char *cmdline; /* command line */ + char *pcnt_f; /* location of %f in the command line args, flag */ + struct coll_entry *next; }; -struct collection -{ - int cnt; - struct coll_entry **ary; +struct collection { + int cnt; + struct coll_entry **ary; }; -struct tag_pgrp -{ - int pgrp; - int stopping; - time_t mystime; - struct coll_entry *cmd; - char output[PATH_MAX]; +struct tag_pgrp { + int pgrp; + int stopping; + time_t mystime; + struct coll_entry *cmd; + char output[PATH_MAX]; }; -struct orphan_pgrp -{ - int pgrp; - struct orphan_pgrp *next; +struct orphan_pgrp { + int pgrp; + struct orphan_pgrp *next; }; static pid_t run_child(struct coll_entry *colle, struct tag_pgrp *active, @@ -132,8 +128,8 @@ zoo_t zoofile; static char *reporttype = NULL; /* zoolib */ -int rec_signal; /* received signal */ -int send_signal; /* signal to send */ +int rec_signal; /* received signal */ +int send_signal; /* signal to send */ /* Debug Bits */ int Debug = 0; @@ -146,741 +142,794 @@ int Debug = 0; #define Dstart 0x000002 /* started command */ #define Dwait 0x000001 /* wait interrupted */ -int -main(int argc, char **argv) +int main(int argc, char **argv) { - extern char *optarg; - extern int optind; - char *zooname = NULL; /* name of the zoo file to use */ - char *filename = "/dev/null"; /* filename to read test tags from */ - char *logfilename = NULL; - char *failcmdfilename = NULL; - char *outputfilename = NULL; - struct collection *coll = NULL; - struct tag_pgrp *running; - struct orphan_pgrp *orphans, *orph; + extern char *optarg; + extern int optind; + char *zooname = NULL; /* name of the zoo file to use */ + char *filename = "/dev/null"; /* filename to read test tags from */ + char *logfilename = NULL; + char *failcmdfilename = NULL; + char *outputfilename = NULL; + struct collection *coll = NULL; + struct tag_pgrp *running; + struct orphan_pgrp *orphans, *orph; struct utsname unamebuf; - FILE *logfile = NULL; - FILE *failcmdfile = NULL; - int keep_active = 1; - int num_active = 0; - int failcnt = 0; /* count of total testcases that failed. */ - int err, i; - int starts = -1; - int timed = 0; - int run_time = -1; char modifier = 'm'; int ret = 0; - int stop; - int go_idle; - int has_brakes = 0; /* stop everything if a test case fails */ - int sequential = 0; /* run tests sequentially */ - int fork_in_road = 0; - int exit_stat; - int track_exit_stats = 0; /* exit non-zero if any test exits non-zero */ - int fmt_print = 0; /* enables formatted printing of logfiles. */ - int quiet_mode = 0; /* supresses test start and test end tags. */ - int c; - pid_t cpid; - struct sigaction sa; - - while ((c = getopt(argc, argv, "AO:Sa:C:d:ef:hl:n:o:pqr:s:t:x:y")) != -1) { - switch (c) { - case 'A': /* all-stop flag */ - has_brakes = 1; - track_exit_stats = 1; - break; - case 'O': /* output buffering directory */ - test_out_dir = strdup(optarg); - break; - case 'S': /* run tests sequentially */ - sequential = 1; - break; - case 'a': /* name of the zoo file to use */ - zooname = strdup(optarg); - break; - case 'C': /* name of the file where all failed commands will be */ - failcmdfilename = strdup(optarg); - break; - case 'd': /* debug options */ - sscanf(optarg, "%i", &Debug); - break; - case 'e': /* exit non-zero if any test exists non-zero */ - track_exit_stats = 1; - break; - case 'f': /* filename to read test tags from */ - filename = strdup(optarg); - break; - case 'h': /* help */ - fprintf(stdout, "Usage: pan -n name [ -SyAehpq ] [ -s starts ]" - " [-t time[s|m|h|d] [ -x nactive ] [ -l logfile ]\n\t" - "[ -a active-file ] [ -f command-file ] " - "[ -C fail-command-file ] " - "[ -d debug-level ]\n\t[-o output-file] " - "[-O output-buffer-directory] [cmd]\n"); - exit(0); - case 'l': /* log file */ - logfilename = strdup(optarg); - break; - case 'n': /* tag given to pan */ - panname = strdup(optarg); - break; - case 'o': /* send test output here */ - outputfilename = strdup(optarg); - break; - case 'p': /* formatted printing. */ - fmt_print = 1; - break; - case 'q': /* supress test start and test end messages */ - quiet_mode = 1; - break; - case 'r': /* reporting type: none, rts */ - reporttype = strdup(optarg); - break; - case 's': /* number of tags to run */ - starts = atoi(optarg); - break; - case 't': /* run_time to run */ - ret = sscanf(optarg, "%d%c", &run_time, &modifier); - if (ret == 0) { fprintf(stderr, "Need proper time input: ####x where" - "x is one of s,m,h,d\n"); break; } - else if (ret == 1) { fprintf(stderr, "Only got a time value of %d " - "modifiers need to come immediately after #" - " assuming %c\n", run_time, modifier); } - else - { - switch (modifier) - { - case 's': run_time = run_time; break; - case 'm': run_time = run_time * 60; break; - case 'h': run_time = run_time * 60 * 60; break; - case 'd': run_time = run_time * 60 * 60 * 24; break; - default: - fprintf(stderr, "Invalid time modifier, try: s|h|m|d\n"); exit(-1); - } - if (!quiet_mode) - printf("PAN will run for %d seconds\n", run_time); - } - timed = 1; //-t implies run as many starts as possible, by default - break; - case 'x': /* number of tags to keep running */ - keep_active = atoi(optarg); - break; - case 'y': /* restart on failure or signal */ - fork_in_road = 1; - break; + FILE *logfile = NULL; + FILE *failcmdfile = NULL; + int keep_active = 1; + int num_active = 0; + int failcnt = 0; /* count of total testcases that failed. */ + int err, i; + int starts = -1; + int timed = 0; + int run_time = -1; + char modifier = 'm'; + int ret = 0; + int stop; + int go_idle; + int has_brakes = 0; /* stop everything if a test case fails */ + int sequential = 0; /* run tests sequentially */ + int fork_in_road = 0; + int exit_stat; + int track_exit_stats = 0; /* exit non-zero if any test exits non-zero */ + int fmt_print = 0; /* enables formatted printing of logfiles. */ + int quiet_mode = 0; /* supresses test start and test end tags. */ + int c; + pid_t cpid; + struct sigaction sa; + + while ((c = + getopt(argc, argv, "AO:Sa:C:d:ef:hl:n:o:pqr:s:t:x:y")) != -1) { + switch (c) { + case 'A': /* all-stop flag */ + has_brakes = 1; + track_exit_stats = 1; + break; + case 'O': /* output buffering directory */ + test_out_dir = strdup(optarg); + break; + case 'S': /* run tests sequentially */ + sequential = 1; + break; + case 'a': /* name of the zoo file to use */ + zooname = strdup(optarg); + break; + case 'C': /* name of the file where all failed commands will be */ + failcmdfilename = strdup(optarg); + break; + case 'd': /* debug options */ + sscanf(optarg, "%i", &Debug); + break; + case 'e': /* exit non-zero if any test exists non-zero */ + track_exit_stats = 1; + break; + case 'f': /* filename to read test tags from */ + filename = strdup(optarg); + break; + case 'h': /* help */ + fprintf(stdout, + "Usage: pan -n name [ -SyAehpq ] [ -s starts ]" + " [-t time[s|m|h|d] [ -x nactive ] [ -l logfile ]\n\t" + "[ -a active-file ] [ -f command-file ] " + "[ -C fail-command-file ] " + "[ -d debug-level ]\n\t[-o output-file] " + "[-O output-buffer-directory] [cmd]\n"); + exit(0); + case 'l': /* log file */ + logfilename = strdup(optarg); + break; + case 'n': /* tag given to pan */ + panname = strdup(optarg); + break; + case 'o': /* send test output here */ + outputfilename = strdup(optarg); + break; + case 'p': /* formatted printing. */ + fmt_print = 1; + break; + case 'q': /* supress test start and test end messages */ + quiet_mode = 1; + break; + case 'r': /* reporting type: none, rts */ + reporttype = strdup(optarg); + break; + case 's': /* number of tags to run */ + starts = atoi(optarg); + break; + case 't': /* run_time to run */ + ret = sscanf(optarg, "%d%c", &run_time, &modifier); + if (ret == 0) { + fprintf(stderr, + "Need proper time input: ####x where" + "x is one of s,m,h,d\n"); + break; + } else if (ret == 1) { + fprintf(stderr, "Only got a time value of %d " + "modifiers need to come immediately after #" + " assuming %c\n", run_time, modifier); + } else { + switch (modifier) { + case 's': + run_time = run_time; + break; + case 'm': + run_time = run_time * 60; + break; + case 'h': + run_time = run_time * 60 * 60; + break; + case 'd': + run_time = run_time * 60 * 60 * 24; + break; + default: + fprintf(stderr, + "Invalid time modifier, try: s|h|m|d\n"); + exit(-1); + } + if (!quiet_mode) + printf("PAN will run for %d seconds\n", + run_time); + } + timed = 1; //-t implies run as many starts as possible, by default + break; + case 'x': /* number of tags to keep running */ + keep_active = atoi(optarg); + break; + case 'y': /* restart on failure or signal */ + fork_in_road = 1; + break; + } + } + + if (panname == NULL) { + fprintf(stderr, "pan: Must supply -n\n"); + exit(1); } - } - - if (panname == NULL) { - fprintf(stderr, "pan: Must supply -n\n"); - exit(1); - } - if (zooname == NULL) { - zooname = zoo_getname(); if (zooname == NULL) { - fprintf(stderr, - "pan(%s): Must supply -a or set ZOO env variable\n", - panname); - exit(1); + zooname = zoo_getname(); + if (zooname == NULL) { + fprintf(stderr, + "pan(%s): Must supply -a or set ZOO env variable\n", + panname); + exit(1); + } } - } - if (reporttype) { - /* make sure we understand the report type */ - if (strcasecmp(reporttype, "rts") - && strcasecmp(reporttype, "none") - /* && strcasecmp(reporttype, "xml")*/) - reporttype = "rts"; - } else { - /* set the default */ - reporttype = "rts"; - } - - if (logfilename != NULL) { - time_t startup; - char *s; - - if (!strcmp(logfilename, "-")) { - logfile = stdout; + if (reporttype) { + /* make sure we understand the report type */ + if (strcasecmp(reporttype, "rts") + && strcasecmp(reporttype, "none") + /* && strcasecmp(reporttype, "xml") */ + ) + reporttype = "rts"; } else { - if ((logfile = fopen(logfilename, "a+")) == NULL) { + /* set the default */ + reporttype = "rts"; + } + + if (logfilename != NULL) { + time_t startup; + char *s; + + if (!strcmp(logfilename, "-")) { + logfile = stdout; + } else { + if ((logfile = fopen(logfilename, "a+")) == NULL) { + fprintf(stderr, + "pan(%s): Error %s (%d) opening log file '%s'\n", + panname, strerror(errno), errno, + logfilename); + exit(1); + } + } + + time(&startup); + s = ctime(&startup); + *(s + strlen(s) - 1) = '\0'; + if (!fmt_print) + fprintf(logfile, "startup='%s'\n", s); + else { + fprintf(logfile, "Test Start Time: %s\n", s); + fprintf(logfile, + "-----------------------------------------\n"); + fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", + "Testcase", "Result", "Exit Value"); + fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", + "--------", "------", "------------"); + } + fflush(logfile); + } + + coll = get_collection(filename, optind, argc, argv); + if (!coll) + exit(1); + if (coll->cnt == 0) { fprintf(stderr, - "pan(%s): Error %s (%d) opening log file '%s'\n", - panname, strerror(errno), errno, logfilename); + "pan(%s): Must supply a file collection or a command\n", + panname); exit(1); - } } - time(&startup); - s = ctime(&startup); - *(s + strlen(s) - 1) = '\0'; - if (!fmt_print) - fprintf(logfile, "startup='%s'\n", s); - else - { - fprintf(logfile, "Test Start Time: %s\n", s); - fprintf(logfile, "-----------------------------------------\n"); - fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", - "Testcase", "Result", "Exit Value"); - fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", - "--------", "------", "------------"); + if (Debug & Dsetup) + dump_coll(coll); + + /* a place to store the pgrps we're watching */ + running = + (struct tag_pgrp *)malloc((keep_active + 1) * + sizeof(struct tag_pgrp)); + if (running == NULL) { + fprintf(stderr, "pan(%s): Failed to allocate memory: %s\n", + panname, strerror(errno)); + exit(2); } - fflush(logfile); - } - - coll = get_collection(filename, optind, argc, argv); - if (!coll) - exit(1); - if (coll->cnt == 0) { - fprintf(stderr, - "pan(%s): Must supply a file collection or a command\n", - panname); - exit(1); - } - - if (Debug & Dsetup) - dump_coll(coll); - - /* a place to store the pgrps we're watching */ - running = (struct tag_pgrp *)malloc((keep_active + 1) * sizeof(struct tag_pgrp)); - if (running == NULL) { - fprintf(stderr, "pan(%s): Failed to allocate memory: %s\n", panname, - strerror(errno)); - exit(2); - } - memset(running, 0, keep_active * sizeof(struct tag_pgrp)); - running[keep_active].pgrp = -1; /* end sentinel */ - - /* a head to the orphaned pgrp list */ - orphans = (struct orphan_pgrp *) malloc(sizeof(struct orphan_pgrp)); - memset(orphans, 0, sizeof(struct orphan_pgrp)); - - srand48(time(NULL) ^ (getpid() + (getpid() << 15))); - - /* Supply a default for starts. If we are in sequential mode, use - * the number of commands available; otherwise 1. - */ - if (timed == 1 && starts == -1) { /* timed, infinite by default */ - starts = -1; - } else if (starts == -1) { - if (sequential) { - starts = coll->cnt; - } else { - starts = 1; + memset(running, 0, keep_active * sizeof(struct tag_pgrp)); + running[keep_active].pgrp = -1; /* end sentinel */ + + /* a head to the orphaned pgrp list */ + orphans = (struct orphan_pgrp *)malloc(sizeof(struct orphan_pgrp)); + memset(orphans, 0, sizeof(struct orphan_pgrp)); + + srand48(time(NULL) ^ (getpid() + (getpid() << 15))); + + /* Supply a default for starts. If we are in sequential mode, use + * the number of commands available; otherwise 1. + */ + if (timed == 1 && starts == -1) { /* timed, infinite by default */ + starts = -1; + } else if (starts == -1) { + if (sequential) { + starts = coll->cnt; + } else { + starts = 1; + } + } else if (starts == 0) { /* if the user specified infinite, set it */ + starts = -1; + } else { /* else, make sure we are starting at least keep_active processes */ + if (starts < keep_active) + starts = keep_active; } - } else if (starts == 0) { /* if the user specified infinite, set it */ - starts = -1; - } else { /* else, make sure we are starting at least keep_active processes */ - if (starts < keep_active) - starts = keep_active; - } - - /* if we're buffering output, but we're only running on process at a time, - * then essentially "turn off buffering" - */ - if (test_out_dir && (keep_active == 1)) { - free(test_out_dir); - test_out_dir = NULL; - } - - if (test_out_dir) { - struct stat sbuf; - if (stat(test_out_dir, &sbuf) < 0) { - fprintf(stderr, - "pan(%s): stat of -O arg '%s' failed. errno: %d %s\n", - panname, test_out_dir, errno, strerror(errno)); - exit(1); + /* if we're buffering output, but we're only running on process at a time, + * then essentially "turn off buffering" + */ + if (test_out_dir && (keep_active == 1)) { + free(test_out_dir); + test_out_dir = NULL; } - if (!S_ISDIR(sbuf.st_mode)) { - fprintf(stderr, "pan(%s): -O arg '%s' must be a directory.\n", - panname, test_out_dir); - exit(1); + + if (test_out_dir) { + struct stat sbuf; + + if (stat(test_out_dir, &sbuf) < 0) { + fprintf(stderr, + "pan(%s): stat of -O arg '%s' failed. errno: %d %s\n", + panname, test_out_dir, errno, strerror(errno)); + exit(1); + } + if (!S_ISDIR(sbuf.st_mode)) { + fprintf(stderr, + "pan(%s): -O arg '%s' must be a directory.\n", + panname, test_out_dir); + exit(1); + } + if (access(test_out_dir, W_OK | R_OK | X_OK) < 0) { + fprintf(stderr, + "pan(%s): permission denied on -O arg '%s'. errno: %d %s\n", + panname, test_out_dir, errno, strerror(errno)); + exit(1); + } } - if (access(test_out_dir, W_OK | R_OK | X_OK) < 0) { - fprintf(stderr, - "pan(%s): permission denied on -O arg '%s'. errno: %d %s\n", - panname, test_out_dir, errno, strerror(errno)); - exit(1); + + if (outputfilename) { + if (!freopen(outputfilename, "a+", stdout)) { + fprintf(stderr, + "pan(%s): Error %s (%d) opening output file '%s'\n", + panname, strerror(errno), errno, + outputfilename); + exit(1); + } } - } - - if (outputfilename) { - if (!freopen(outputfilename, "a+", stdout)) { - fprintf(stderr, - "pan(%s): Error %s (%d) opening output file '%s'\n", - panname, strerror(errno), errno, outputfilename); - exit(1); + + if (failcmdfilename) { + if (!(failcmdfile = fopen(failcmdfilename, "a+"))) { + fprintf(stderr, + "pan(%s): Error %s (%d) opening fail cmd file '%s'\n", + panname, strerror(errno), errno, + failcmdfilename); + exit(1); + } } - } - - if (failcmdfilename) { - if (!(failcmdfile = fopen(failcmdfilename, "a+"))) { - fprintf(stderr, - "pan(%s): Error %s (%d) opening fail cmd file '%s'\n", - panname, strerror(errno), errno, failcmdfilename); - exit(1); - } - } - - if ((zoofile = zoo_open(zooname)) == NULL) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - exit(1); - } - if (zoo_mark_args(zoofile, getpid(), panname, argc, argv)) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - exit(1); - } - - /* Allocate N spaces for max-arg commands. - * this is an "active file cleanliness" thing - */ - { - char *av[2], bigarg[82]; - - memset(bigarg, '.', 81); - bigarg[81] = '\0'; - av[0] = bigarg; - av[1] = NULL; - - for (c = 0; c < keep_active; c++) { - if (zoo_mark_cmdline(zoofile, c, panname, "")) { + + if ((zoofile = zoo_open(zooname)) == NULL) { fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); exit(1); - } } - for (c = 0; c < keep_active; c++) { - if (zoo_clear(zoofile, c)) { + if (zoo_mark_args(zoofile, getpid(), panname, argc, argv)) { fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); exit(1); - } } - } - rec_signal = send_signal = 0; - if (run_time != -1) { alarm(run_time); } + /* Allocate N spaces for max-arg commands. + * this is an "active file cleanliness" thing + */ + { + char *av[2], bigarg[82]; + + memset(bigarg, '.', 81); + bigarg[81] = '\0'; + av[0] = bigarg; + av[1] = NULL; + + for (c = 0; c < keep_active; c++) { + if (zoo_mark_cmdline(zoofile, c, panname, "")) { + fprintf(stderr, "pan(%s): %s\n", panname, + zoo_error); + exit(1); + } + } + for (c = 0; c < keep_active; c++) { + if (zoo_clear(zoofile, c)) { + fprintf(stderr, "pan(%s): %s\n", panname, + zoo_error); + exit(1); + } + } + } + + rec_signal = send_signal = 0; + if (run_time != -1) { + alarm(run_time); + } - sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - sa.sa_handler = wait_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = wait_handler; + + sigaction(SIGALRM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGUSR1, &sa, NULL); /* ignore fork_in_road */ + sigaction(SIGUSR2, &sa, NULL); /* stop the scheduler */ + + c = 0; /* in this loop, c is the command index */ + stop = 0; + exit_stat = 0; + go_idle = 0; + while (1) { + + while ((num_active < keep_active) && (starts != 0)) { + if (stop || rec_signal || go_idle) + break; + + if (!sequential) + c = lrand48() % coll->cnt; + + /* find a slot for the child */ + for (i = 0; i < keep_active; ++i) { + if (running[i].pgrp == 0) + break; + } + if (i == keep_active) { + fprintf(stderr, + "pan(%s): Aborting: i == keep_active = %d\n", + panname, i); + wait_handler(SIGINT); + exit_stat++; + break; + } - sigaction(SIGALRM, &sa, NULL); - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); - sigaction(SIGHUP, &sa, NULL); - sigaction(SIGUSR1, &sa, NULL); /* ignore fork_in_road */ - sigaction(SIGUSR2, &sa, NULL); /* stop the scheduler */ + cpid = + run_child(coll->ary[c], running + i, quiet_mode, + &failcnt, fmt_print, logfile); + if (cpid != -1) + ++num_active; + if ((cpid != -1 || sequential) && starts > 0) + --starts; - c = 0; /* in this loop, c is the command index */ - stop = 0; - exit_stat = 0; - go_idle = 0; - while (1) { + if (sequential) + if (++c >= coll->cnt) + c = 0; - while ((num_active < keep_active) && (starts != 0)) { - if (stop || rec_signal || go_idle) - break; + } /* while ((num_active < keep_active) && (starts != 0)) */ - if (!sequential) - c = lrand48() % coll->cnt; + if (starts == 0) { + if (!quiet_mode) + printf("incrementing stop\n"); + ++stop; + } else if (starts == -1) //wjh + { + FILE *f = (FILE *) - 1; + if ((f = fopen(PAN_STOP_FILE, "r")) != 0) { + printf("Got %s Stopping!\n", PAN_STOP_FILE); + fclose(f); + unlink(PAN_STOP_FILE); + stop++; + } + } - /* find a slot for the child */ - for (i = 0; i < keep_active; ++i) { - if (running[i].pgrp == 0) - break; - } - if (i == keep_active) { - fprintf(stderr, "pan(%s): Aborting: i == keep_active = %d\n", - panname, i); - wait_handler(SIGINT); - exit_stat++; - break; - } - - cpid = run_child(coll->ary[c], running + i, quiet_mode, &failcnt, - fmt_print, logfile); - if (cpid != -1) - ++num_active; - if ((cpid != -1 || sequential) && starts > 0) - --starts; - - if (sequential) - if (++c >= coll->cnt) - c = 0; - - } /* while ((num_active < keep_active) && (starts != 0)) */ - - if (starts == 0) - { - if (!quiet_mode) - printf("incrementing stop\n"); - ++stop; - } - else if (starts == -1) //wjh - { - FILE *f = (FILE*)-1; - if ((f = fopen(PAN_STOP_FILE, "r")) != 0) - { printf("Got %s Stopping!\n", PAN_STOP_FILE); - fclose(f); unlink(PAN_STOP_FILE); stop++; - } - } + if (rec_signal) { + /* propagate everything except sigusr2 */ + + if (rec_signal == SIGUSR2) { + if (fork_in_road) + ++go_idle; + else + ++stop; + rec_signal = send_signal = 0; + } else { + if (rec_signal == SIGUSR1) + fork_in_road = 0; + propagate_signal(running, keep_active, orphans); + if (fork_in_road) + ++go_idle; + else + ++stop; + } + } - if (rec_signal) { - /* propagate everything except sigusr2 */ + err = check_pids(running, &num_active, keep_active, logfile, + failcmdfile, orphans, fmt_print, &failcnt, + quiet_mode); + if (Debug & Drunning) { + pids_running(running, keep_active); + orphans_running(orphans); + } + if (err) { + if (fork_in_road) + ++go_idle; + if (track_exit_stats) + exit_stat++; + if (has_brakes) { + fprintf(stderr, "pan(%s): All stop!%s\n", + panname, go_idle ? " (idling)" : ""); + wait_handler(SIGINT); + } + } - if (rec_signal == SIGUSR2) { - if (fork_in_road) - ++go_idle; - else - ++stop; - rec_signal = send_signal = 0; - } else { - if (rec_signal == SIGUSR1) - fork_in_road = 0; - propagate_signal(running, keep_active, orphans); - if (fork_in_road) - ++go_idle; - else - ++stop; - } - } + if (stop && (num_active == 0)) + break; - err = check_pids(running, &num_active, keep_active, logfile, - failcmdfile, orphans, fmt_print, &failcnt, quiet_mode); - if (Debug & Drunning) { - pids_running(running, keep_active); - orphans_running(orphans); - } - if (err) { - if (fork_in_road) - ++go_idle; - if (track_exit_stats) - exit_stat++; - if (has_brakes) { - fprintf(stderr, "pan(%s): All stop!%s\n", panname, - go_idle ? " (idling)" : ""); - wait_handler(SIGINT); - } + if (go_idle && (num_active == 0)) { + go_idle = 0; /* It is idle, now resume scheduling. */ + wait_handler(0); /* Reset the signal ratchet. */ + } } - if (stop && (num_active == 0)) - break; - - if (go_idle && (num_active == 0)) { - go_idle = 0; /* It is idle, now resume scheduling. */ - wait_handler(0); /* Reset the signal ratchet. */ + /* Wait for orphaned pgrps */ + while (1) { + for (orph = orphans; orph != NULL; orph = orph->next) { + if (orph->pgrp == 0) + continue; + /* Yes, we have orphaned pgrps */ + sleep(5); + if (!rec_signal) { + /* force an artificial signal, move us + * through the signal ratchet. + */ + wait_handler(SIGINT); + } + propagate_signal(running, keep_active, orphans); + if (Debug & Drunning) + orphans_running(orphans); + break; + } + if (orph == NULL) + break; } - } - /* Wait for orphaned pgrps */ - while (1) { - for (orph = orphans; orph != NULL; orph = orph->next) { - if (orph->pgrp == 0) - continue; - /* Yes, we have orphaned pgrps */ - sleep(5); - if (!rec_signal) { - /* force an artificial signal, move us - * through the signal ratchet. - */ - wait_handler(SIGINT); - } - propagate_signal(running, keep_active, orphans); - if (Debug & Drunning) - orphans_running(orphans); - break; + if (zoo_clear(zoofile, getpid())) { + fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); + ++exit_stat; } - if (orph == NULL) - break; - } - - if (zoo_clear(zoofile, getpid())) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - ++exit_stat; - } - fclose(zoofile); - if (logfile && fmt_print) - { + fclose(zoofile); + if (logfile && fmt_print) { if (uname(&unamebuf) == -1) - fprintf(stderr, "ERROR: uname(): %s\n", strerror(errno)); - fprintf(logfile, "\n-----------------------------------------------\n"); + fprintf(stderr, "ERROR: uname(): %s\n", + strerror(errno)); + fprintf(logfile, + "\n-----------------------------------------------\n"); fprintf(logfile, "Total Tests: %d\n", coll->cnt); fprintf(logfile, "Total Failures: %d\n", failcnt); fprintf(logfile, "Kernel Version: %s\n", unamebuf.release); - fprintf(logfile, "Machine Architecture: %s\n", unamebuf.machine); + fprintf(logfile, "Machine Architecture: %s\n", + unamebuf.machine); fprintf(logfile, "Hostname: %s\n\n", unamebuf.nodename); } - if (logfile && (logfile != stdout)) - fclose(logfile); + if (logfile && (logfile != stdout)) + fclose(logfile); - exit(exit_stat); + exit(exit_stat); } - - static void propagate_signal(struct tag_pgrp *running, int keep_active, struct orphan_pgrp *orphans) { - int i; + int i; - if (Debug & Dshutdown) - fprintf(stderr, "pan was signaled with sig %d...\n", rec_signal); + if (Debug & Dshutdown) + fprintf(stderr, "pan was signaled with sig %d...\n", + rec_signal); - if (rec_signal == SIGALRM) - { - printf("PAN stop Alarm was received\n"); - rec_signal = SIGTERM; - } + if (rec_signal == SIGALRM) { + printf("PAN stop Alarm was received\n"); + rec_signal = SIGTERM; + } - for (i = 0; i < keep_active; ++i) { - if (running[i].pgrp == 0) - continue; + for (i = 0; i < keep_active; ++i) { + if (running[i].pgrp == 0) + continue; - if (Debug & Dshutdown) - fprintf(stderr, " propagating sig %d to %d\n", - send_signal, -running[i].pgrp); - if (kill(-running[i].pgrp, send_signal) != 0) { - fprintf(stderr, - "pan(%s): kill(%d,%d) failed on tag (%s). errno:%d %s\n", - panname, -running[i].pgrp, send_signal, - running[i].cmd->name, errno, strerror(errno)); + if (Debug & Dshutdown) + fprintf(stderr, " propagating sig %d to %d\n", + send_signal, -running[i].pgrp); + if (kill(-running[i].pgrp, send_signal) != 0) { + fprintf(stderr, + "pan(%s): kill(%d,%d) failed on tag (%s). errno:%d %s\n", + panname, -running[i].pgrp, send_signal, + running[i].cmd->name, errno, strerror(errno)); + } + running[i].stopping = 1; } - running[i].stopping = 1; - } - check_orphans(orphans, send_signal); + check_orphans(orphans, send_signal); - rec_signal = send_signal = 0; + rec_signal = send_signal = 0; } - static int check_pids(struct tag_pgrp *running, int *num_active, int keep_active, FILE * logfile, FILE * failcmdfile, struct orphan_pgrp *orphans, int fmt_print, int *failcnt, int quiet_mode) { - int w; - pid_t cpid; - int stat_loc; - int ret = 0; - int i; - time_t t; - char *status; - int signaled = 0; - struct tms tms1, tms2; - clock_t tck; - - check_orphans(orphans, 0); - - tck = times(&tms1); - if (tck == -1) { - fprintf(stderr, "pan(%s): times(&tms1) failed. errno:%d %s\n", - panname, errno, strerror(errno)); - } - cpid = wait(&stat_loc); - tck = times(&tms2); - if (tck == -1) { - fprintf(stderr, "pan(%s): times(&tms2) failed. errno:%d %s\n", - panname, errno, strerror(errno)); - } - - if (cpid < 0) { - if (errno == EINTR) { - if (Debug) - fprintf(stderr, "pan(%s): wait() interrupted\n", panname); - } else if (errno != ECHILD) { - fprintf(stderr, "pan(%s): wait() failed. errno:%d %s\n", - panname, errno, strerror(errno)); + int w; + pid_t cpid; + int stat_loc; + int ret = 0; + int i; + time_t t; + char *status; + int signaled = 0; + struct tms tms1, tms2; + clock_t tck; + + check_orphans(orphans, 0); + + tck = times(&tms1); + if (tck == -1) { + fprintf(stderr, "pan(%s): times(&tms1) failed. errno:%d %s\n", + panname, errno, strerror(errno)); } - } else if (cpid > 0) { - - if (WIFSIGNALED(stat_loc)) { - w = WTERMSIG(stat_loc); - status = "signaled"; - if (Debug & Dexit) - fprintf(stderr, "child %d terminated with signal %d\n", cpid, - w); - --*num_active; - signaled = 1; - } else if (WIFEXITED(stat_loc)) { - w = WEXITSTATUS(stat_loc); - status = "exited"; - if (Debug & Dexit) - fprintf(stderr, "child %d exited with status %d\n", cpid, w); - --*num_active; - if (w != 0) - ret++; - } else if (WIFSTOPPED(stat_loc)) { /* should never happen */ - w = WSTOPSIG(stat_loc); - status = "stopped"; - ret++; - } else { /* should never happen */ - w = 0; - status = "unknown"; - ret++; + cpid = wait(&stat_loc); + tck = times(&tms2); + if (tck == -1) { + fprintf(stderr, "pan(%s): times(&tms2) failed. errno:%d %s\n", + panname, errno, strerror(errno)); } - for (i = 0; i < keep_active; ++i) { - if (running[i].pgrp == cpid) { - if ((w == 130) && running[i].stopping && - (strcmp(status, "exited") == 0)) { - /* The child received sigint, but - * did not trap for it? Compensate - * for it here. - */ - w = 0; - ret--; /* undo */ - if (Debug & Drunning) + if (cpid < 0) { + if (errno == EINTR) { + if (Debug) + fprintf(stderr, "pan(%s): wait() interrupted\n", + panname); + } else if (errno != ECHILD) { fprintf(stderr, - "pan(%s): tag=%s exited 130, known to be signaled; will give it an exit 0.\n", - panname, running[i].cmd->name); - } - time(&t); - if (logfile != NULL) { - if (!fmt_print) - fprintf(logfile, - "tag=%s stime=%d dur=%d exit=%s stat=%d core=%s cu=%d cs=%d\n", - running[i].cmd->name, (int) (running[i].mystime), - (int) (t - running[i].mystime), status, w, - (stat_loc & 0200) ? "yes" : "no", - (int) (tms2.tms_cutime - tms1.tms_cutime), - (int) (tms2.tms_cstime - tms1.tms_cstime)); - else - { - if (w != 0) - ++*failcnt; - fprintf(logfile, "%-30.30s %-10.10s %-5d\n", - running[i].cmd->name, ((w != 0) ? "FAIL" : "PASS"), - w); - } - - fflush(logfile); - } - - if ((failcmdfile != NULL) && (w !=0)) { - fprintf(failcmdfile, "%s %s\n", running[i].cmd->name, running[i].cmd->cmdline); - } - - if (running[i].stopping) - status = "driver_interrupt"; - - if (test_out_dir) { - if (!quiet_mode) - write_test_start(running+i); - copy_buffered_output(running + i); - unlink(running[i].output); + "pan(%s): wait() failed. errno:%d %s\n", + panname, errno, strerror(errno)); } - if (!quiet_mode) - write_test_end(running+i, "ok", t, status, - stat_loc, w, &tms1, &tms2); - - /* If signaled and we weren't expecting - * this to be stopped then the proc - * had a problem. - */ - if (signaled && !running[i].stopping) - ret++; - - running[i].pgrp = 0; - if (zoo_clear(zoofile, cpid)) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - exit(1); + } else if (cpid > 0) { + + if (WIFSIGNALED(stat_loc)) { + w = WTERMSIG(stat_loc); + status = "signaled"; + if (Debug & Dexit) + fprintf(stderr, + "child %d terminated with signal %d\n", + cpid, w); + --*num_active; + signaled = 1; + } else if (WIFEXITED(stat_loc)) { + w = WEXITSTATUS(stat_loc); + status = "exited"; + if (Debug & Dexit) + fprintf(stderr, + "child %d exited with status %d\n", + cpid, w); + --*num_active; + if (w != 0) + ret++; + } else if (WIFSTOPPED(stat_loc)) { /* should never happen */ + w = WSTOPSIG(stat_loc); + status = "stopped"; + ret++; + } else { /* should never happen */ + w = 0; + status = "unknown"; + ret++; } - /* Check for orphaned pgrps */ - if ((kill(-cpid, 0) == 0) || (errno == EPERM)) { - if (zoo_mark_cmdline(zoofile, cpid, "panorphan", - running[i].cmd->cmdline)) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - exit(1); - } - mark_orphan(orphans, cpid); - /* status of kill doesn't matter */ - kill(-cpid, SIGTERM); + for (i = 0; i < keep_active; ++i) { + if (running[i].pgrp == cpid) { + if ((w == 130) && running[i].stopping && + (strcmp(status, "exited") == 0)) { + /* The child received sigint, but + * did not trap for it? Compensate + * for it here. + */ + w = 0; + ret--; /* undo */ + if (Debug & Drunning) + fprintf(stderr, + "pan(%s): tag=%s exited 130, known to be signaled; will give it an exit 0.\n", + panname, + running[i].cmd->name); + } + time(&t); + if (logfile != NULL) { + if (!fmt_print) + fprintf(logfile, + "tag=%s stime=%d dur=%d exit=%s stat=%d core=%s cu=%d cs=%d\n", + running[i].cmd->name, + (int)(running[i]. + mystime), + (int)(t - + running[i]. + mystime), status, + w, + (stat_loc & 0200) ? + "yes" : "no", + (int)(tms2.tms_cutime - + tms1.tms_cutime), + (int)(tms2.tms_cstime - + tms1.tms_cstime)); + else { + if (w != 0) + ++ * failcnt; + fprintf(logfile, + "%-30.30s %-10.10s %-5d\n", + running[i].cmd->name, + ((w != + 0) ? "FAIL" : "PASS"), + w); + } + + fflush(logfile); + } + + if ((failcmdfile != NULL) && (w != 0)) { + fprintf(failcmdfile, "%s %s\n", + running[i].cmd->name, + running[i].cmd->cmdline); + } + + if (running[i].stopping) + status = "driver_interrupt"; + + if (test_out_dir) { + if (!quiet_mode) + write_test_start(running + i); + copy_buffered_output(running + i); + unlink(running[i].output); + } + if (!quiet_mode) + write_test_end(running + i, "ok", t, + status, stat_loc, w, + &tms1, &tms2); + + /* If signaled and we weren't expecting + * this to be stopped then the proc + * had a problem. + */ + if (signaled && !running[i].stopping) + ret++; + + running[i].pgrp = 0; + if (zoo_clear(zoofile, cpid)) { + fprintf(stderr, "pan(%s): %s\n", + panname, zoo_error); + exit(1); + } + + /* Check for orphaned pgrps */ + if ((kill(-cpid, 0) == 0) || (errno == EPERM)) { + if (zoo_mark_cmdline + (zoofile, cpid, "panorphan", + running[i].cmd->cmdline)) { + fprintf(stderr, "pan(%s): %s\n", + panname, zoo_error); + exit(1); + } + mark_orphan(orphans, cpid); + /* status of kill doesn't matter */ + kill(-cpid, SIGTERM); + } + + break; + } } - - break; - } } - } - return ret; + return ret; } - static pid_t run_child(struct coll_entry *colle, struct tag_pgrp *active, int quiet_mode, int *failcnt, int fmt_print, FILE * logfile) { - ssize_t errlen; - int cpid; - int c_stdout = -1; /* child's stdout, stderr */ - int capturing = 0; /* output is going to a file instead of stdout */ - char *c_cmdline; - static long cmdno = 0; - int errpipe[2]; /* way to communicate to parent that the tag */ - char errbuf[1024]; /* didn't actually start */ - - /* Try to open the file that will be stdout for the test */ - if (test_out_dir) { - capturing = 1; - do { - sprintf(active->output, "%s/%s.%ld", - test_out_dir, colle->name, cmdno++); - c_stdout = open(active->output, O_CREAT | O_RDWR | O_EXCL | O_SYNC, 0666); - } while (c_stdout < 0 && errno == EEXIST); - if (c_stdout < 0) { - fprintf(stderr, - "pan(%s): open of stdout file failed (tag %s). errno: %d %s\n file: %s\n", - panname, colle->name, errno, strerror(errno), - active->output); - return -1; + ssize_t errlen; + int cpid; + int c_stdout = -1; /* child's stdout, stderr */ + int capturing = 0; /* output is going to a file instead of stdout */ + char *c_cmdline; + static long cmdno = 0; + int errpipe[2]; /* way to communicate to parent that the tag */ + char errbuf[1024]; /* didn't actually start */ + + /* Try to open the file that will be stdout for the test */ + if (test_out_dir) { + capturing = 1; + do { + sprintf(active->output, "%s/%s.%ld", + test_out_dir, colle->name, cmdno++); + c_stdout = + open(active->output, + O_CREAT | O_RDWR | O_EXCL | O_SYNC, 0666); + } while (c_stdout < 0 && errno == EEXIST); + if (c_stdout < 0) { + fprintf(stderr, + "pan(%s): open of stdout file failed (tag %s). errno: %d %s\n file: %s\n", + panname, colle->name, errno, strerror(errno), + active->output); + return -1; + } } - } - - /* get the tag's command line arguments ready. subst_pcnt_f() uses a - * static counter, that's why we do it here instead of after we fork. - */ - if (colle->pcnt_f) { - c_cmdline = subst_pcnt_f(colle); - } else { - c_cmdline = colle->cmdline; - } - - if (pipe(errpipe) < 0) { - fprintf(stderr, "pan(%s): pipe() failed. errno:%d %s\n", + + /* get the tag's command line arguments ready. subst_pcnt_f() uses a + * static counter, that's why we do it here instead of after we fork. + */ + if (colle->pcnt_f) { + c_cmdline = subst_pcnt_f(colle); + } else { + c_cmdline = colle->cmdline; + } + + if (pipe(errpipe) < 0) { + fprintf(stderr, "pan(%s): pipe() failed. errno:%d %s\n", panname, errno, strerror(errno)); - if (capturing) { - close(c_stdout); - unlink(active->output); - } - return -1; - } - - time(&active->mystime); - active->cmd = colle; - - if (!test_out_dir) - if (!quiet_mode) - write_test_start(active); - - if ((cpid = fork()) == -1) { - fprintf(stderr, "pan(%s): fork failed (tag %s). errno:%d %s\n", - panname, colle->name, errno, strerror(errno)); - if (capturing) { - unlink(active->output); - close(c_stdout); - } - close(errpipe[0]); - close(errpipe[1]); - return -1; - } else if (cpid == 0) { - /* child */ + if (capturing) { + close(c_stdout); + unlink(active->output); + } + return -1; + } - fclose(zoofile); - close(errpipe[0]); - fcntl(errpipe[1], F_SETFD, 1); /* close the pipe if we succeed */ - setpgrp(); + time(&active->mystime); + active->cmd = colle; - umask(0); + if (!test_out_dir) + if (!quiet_mode) + write_test_start(active); + + if ((cpid = fork()) == -1) { + fprintf(stderr, + "pan(%s): fork failed (tag %s). errno:%d %s\n", + panname, colle->name, errno, strerror(errno)); + if (capturing) { + unlink(active->output); + close(c_stdout); + } + close(errpipe[0]); + close(errpipe[1]); + return -1; + } else if (cpid == 0) { + /* child */ + + fclose(zoofile); + close(errpipe[0]); + fcntl(errpipe[1], F_SETFD, 1); /* close the pipe if we succeed */ + setpgrp(); + + umask(0); #define WRITE_OR_DIE(fd, buf, buflen) do { \ if (write((fd), (buf), (buflen)) != (buflen)) { \ @@ -889,462 +938,467 @@ run_child(struct coll_entry *colle, struct tag_pgrp *active, int quiet_mode, } \ } while(0) - /* if we're putting output into a buffer file, we need to do the - * redirection now. If we fail - */ - if (capturing) { - if (dup2(c_stdout, fileno(stdout)) == -1) { - errlen = sprintf(errbuf, "pan(%s): couldn't redirect stdout for tag %s. errno:%d %s", - panname, colle->name, errno, strerror(errno)); - WRITE_OR_DIE(errpipe[1], &errlen, sizeof(errlen)); - WRITE_OR_DIE(errpipe[1], errbuf, errlen); - exit(2); - } - if (dup2(c_stdout, fileno(stderr)) == -1) { - errlen = sprintf(errbuf, "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", - panname, colle->name, errno, strerror(errno)); - WRITE_OR_DIE(errpipe[1], &errlen, sizeof(errlen)); - WRITE_OR_DIE(errpipe[1], errbuf, errlen); - exit(2); - } - } else { /* stderr still needs to be redirected */ - if (dup2(fileno(stdout), fileno(stderr)) == -1) { - errlen = sprintf(errbuf, "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", - panname, colle->name, errno, strerror(errno)); + /* if we're putting output into a buffer file, we need to do the + * redirection now. If we fail + */ + if (capturing) { + if (dup2(c_stdout, fileno(stdout)) == -1) { + errlen = + sprintf(errbuf, + "pan(%s): couldn't redirect stdout for tag %s. errno:%d %s", + panname, colle->name, errno, + strerror(errno)); + WRITE_OR_DIE(errpipe[1], &errlen, + sizeof(errlen)); + WRITE_OR_DIE(errpipe[1], errbuf, errlen); + exit(2); + } + if (dup2(c_stdout, fileno(stderr)) == -1) { + errlen = + sprintf(errbuf, + "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", + panname, colle->name, errno, + strerror(errno)); + WRITE_OR_DIE(errpipe[1], &errlen, + sizeof(errlen)); + WRITE_OR_DIE(errpipe[1], errbuf, errlen); + exit(2); + } + } else { /* stderr still needs to be redirected */ + if (dup2(fileno(stdout), fileno(stderr)) == -1) { + errlen = + sprintf(errbuf, + "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", + panname, colle->name, errno, + strerror(errno)); + WRITE_OR_DIE(errpipe[1], &errlen, + sizeof(errlen)); + WRITE_OR_DIE(errpipe[1], errbuf, errlen); + exit(2); + } + } + /* If there are any shell-type characters in the cmdline + * such as '>', '<', '$', '|', etc, then we exec a shell and + * run the cmd under a shell. + * + * Otherwise, break the cmdline at white space and exec the + * cmd directly. + */ + if (strpbrk(c_cmdline, "\"';|<>$\\")) { + execlp("sh", "sh", "-c", c_cmdline, (char *)0); + errlen = sprintf(errbuf, + "pan(%s): execlp of '%s' (tag %s) failed. errno:%d %s", + panname, c_cmdline, colle->name, errno, + strerror(errno)); + } else { + char **arg_v; + + arg_v = (char **)splitstr(c_cmdline, NULL, NULL); + + execvp(arg_v[0], arg_v); + errlen = sprintf(errbuf, + "pan(%s): execvp of '%s' (tag %s) failed. errno:%d %s", + panname, arg_v[0], colle->name, errno, + strerror(errno)); + } WRITE_OR_DIE(errpipe[1], &errlen, sizeof(errlen)); WRITE_OR_DIE(errpipe[1], errbuf, errlen); - exit(2); - } + exit(errno); } - /* If there are any shell-type characters in the cmdline - * such as '>', '<', '$', '|', etc, then we exec a shell and - * run the cmd under a shell. - * - * Otherwise, break the cmdline at white space and exec the - * cmd directly. + + /* parent */ + + /* subst_pcnt_f() allocates the command line dynamically + * free the malloc to prevent a memory leak */ - if (strpbrk(c_cmdline, "\"';|<>$\\")) { - execlp("sh", "sh", "-c", c_cmdline, (char*)0); - errlen = sprintf(errbuf, - "pan(%s): execlp of '%s' (tag %s) failed. errno:%d %s", - panname, c_cmdline, colle->name, errno, strerror(errno)); - } else { - char **arg_v; + if (colle->pcnt_f) + free(c_cmdline); - arg_v = (char **)splitstr(c_cmdline, NULL, NULL); + close(errpipe[1]); - execvp(arg_v[0], arg_v); - errlen = sprintf(errbuf, - "pan(%s): execvp of '%s' (tag %s) failed. errno:%d %s", - panname, arg_v[0], colle->name, errno, strerror(errno)); - } - WRITE_OR_DIE(errpipe[1], &errlen, sizeof(errlen)); - WRITE_OR_DIE(errpipe[1], errbuf, errlen); - exit(errno); - } - - /* parent */ - - /* subst_pcnt_f() allocates the command line dynamically - * free the malloc to prevent a memory leak - */ - if (colle->pcnt_f) - free(c_cmdline); - - close(errpipe[1]); - - /* if the child couldn't go through with the exec, - * clean up the mess, note it, and move on - */ - if (read(errpipe[0], &errlen, sizeof(errlen))) { - int status; - time_t end_time; - int termid; - char *termtype; - struct tms notime = {0, 0, 0, 0}; - - read(errpipe[0], errbuf, errlen); - close(errpipe[0]); - errbuf[errlen] = '\0'; - /* fprintf(stderr, "%s", errbuf); */ - waitpid(cpid, &status, 0); - if (WIFSIGNALED(status)) { - termid = WTERMSIG(status); - termtype = "signaled"; - } else if (WIFEXITED(status)) { - termid = WEXITSTATUS(status); - termtype = "exited"; - } else if (WIFSTOPPED(status)) { - termid = WSTOPSIG(status); - termtype = "stopped"; - } else { - termid = 0; - termtype = "unknown"; - } - time(&end_time); - if (logfile != NULL) { - if (!fmt_print) { - fprintf(logfile, - "tag=%s stime=%d dur=%d exit=%s " - "stat=%d core=%s cu=%d cs=%d\n", - colle->name, (int)(active->mystime), - (int) (end_time - active->mystime), termtype, - termid, (status & 0200) ? "yes" : "no", - 0, 0); + /* if the child couldn't go through with the exec, + * clean up the mess, note it, and move on + */ + if (read(errpipe[0], &errlen, sizeof(errlen))) { + int status; + time_t end_time; + int termid; + char *termtype; + struct tms notime = { 0, 0, 0, 0 }; + + read(errpipe[0], errbuf, errlen); + close(errpipe[0]); + errbuf[errlen] = '\0'; + /* fprintf(stderr, "%s", errbuf); */ + waitpid(cpid, &status, 0); + if (WIFSIGNALED(status)) { + termid = WTERMSIG(status); + termtype = "signaled"; + } else if (WIFEXITED(status)) { + termid = WEXITSTATUS(status); + termtype = "exited"; + } else if (WIFSTOPPED(status)) { + termid = WSTOPSIG(status); + termtype = "stopped"; } else { - if (termid != 0) - ++*failcnt; + termid = 0; + termtype = "unknown"; + } + time(&end_time); + if (logfile != NULL) { + if (!fmt_print) { + fprintf(logfile, + "tag=%s stime=%d dur=%d exit=%s " + "stat=%d core=%s cu=%d cs=%d\n", + colle->name, (int)(active->mystime), + (int)(end_time - active->mystime), + termtype, termid, + (status & 0200) ? "yes" : "no", 0, 0); + } else { + if (termid != 0) + ++ * failcnt; + + fprintf(logfile, "%-30.30s %-10.10s %-5d\n", + colle->name, + ((termid != 0) ? "FAIL" : "PASS"), + termid); + } + fflush(logfile); + } - fprintf(logfile, "%-30.30s %-10.10s %-5d\n", - colle->name, ((termid != 0) ? "FAIL" : "PASS"), - termid); + if (!quiet_mode) { + //write_test_start(active, errbuf); + write_test_end(active, errbuf, end_time, termtype, + status, termid, ¬ime, ¬ime); } - fflush(logfile); + if (capturing) { + close(c_stdout); + unlink(active->output); + } + return -1; } - if (!quiet_mode) - { - //write_test_start(active, errbuf); - write_test_end(active, errbuf, end_time, termtype, status, - termid, ¬ime, ¬ime); - } - if (capturing) { - close(c_stdout); - unlink(active->output); - } - return -1; - } - - close(errpipe[0]); - if (capturing) close(c_stdout); - - active->pgrp = cpid; - active->stopping = 0; - - if (zoo_mark_cmdline(zoofile, cpid, colle->name, colle->cmdline)) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); - exit(1); - } - - if (Debug & Dstartup) - fprintf(stderr, "started %s cpid=%d at %s", - colle->name, cpid, ctime(&active->mystime)); - - if (Debug & Dstart) { - fprintf(stderr, "Executing test = %s as %s", colle->name, colle->cmdline); + close(errpipe[0]); if (capturing) - fprintf(stderr, "with output file = %s\n", active->output); - else - fprintf(stderr, "\n"); - } + close(c_stdout); - return cpid; -} + active->pgrp = cpid; + active->stopping = 0; + + if (zoo_mark_cmdline(zoofile, cpid, colle->name, colle->cmdline)) { + fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); + exit(1); + } + if (Debug & Dstartup) + fprintf(stderr, "started %s cpid=%d at %s", + colle->name, cpid, ctime(&active->mystime)); -static char * -subst_pcnt_f(struct coll_entry *colle) + if (Debug & Dstart) { + fprintf(stderr, "Executing test = %s as %s", colle->name, + colle->cmdline); + if (capturing) + fprintf(stderr, "with output file = %s\n", + active->output); + else + fprintf(stderr, "\n"); + } + + return cpid; +} + +static char *subst_pcnt_f(struct coll_entry *colle) { - static int counter = 1; - char pid_and_counter[20]; - char new_cmdline[1024]; + static int counter = 1; + char pid_and_counter[20]; + char new_cmdline[1024]; - /* if we get called falsely, do the right thing anyway */ - if (!colle->pcnt_f) - return colle->cmdline; + /* if we get called falsely, do the right thing anyway */ + if (!colle->pcnt_f) + return colle->cmdline; - snprintf(pid_and_counter, 20, "%d_%d", getpid(), counter++); - snprintf(new_cmdline, 1024, colle->cmdline, pid_and_counter); - return strdup(new_cmdline); + snprintf(pid_and_counter, 20, "%d_%d", getpid(), counter++); + snprintf(new_cmdline, 1024, colle->cmdline, pid_and_counter); + return strdup(new_cmdline); } -static struct collection * -get_collection(char *file, int optind, int argc, char **argv) +static struct collection *get_collection(char *file, int optind, int argc, + char **argv) { - char *buf, *a, *b; - struct coll_entry *head, *p, *n; - struct collection *coll; - int i; - - buf = slurp(file); - if (!buf) - return NULL; - - coll = (struct collection *) malloc(sizeof(struct collection)); - coll->cnt = 0; - - head = p = n = NULL; - a = b = buf; - while (a) { - /* set b to the start of the next line and add a NULL character - * to separate the two lines */ - if ((b = strchr(a, '\n')) != NULL) - *b++ = '\0'; - - /* If this is line isn't a comment */ - if ((*a != '#') && (*a != '\0') && (*a != ' ')) { - n = (struct coll_entry *) malloc(sizeof(struct coll_entry)); - if ((n->pcnt_f = strstr(a, "%f"))) { - n->pcnt_f[1] = 's'; - } - n->name = strdup(strsep(&a, " \t")); - n->cmdline = strdup(a); - n->next = NULL; - - if (p) { - p->next = n; - } - if (head == NULL) { - head = n; - } - p = n; - coll->cnt++; + char *buf, *a, *b; + struct coll_entry *head, *p, *n; + struct collection *coll; + int i; + + buf = slurp(file); + if (!buf) + return NULL; + + coll = (struct collection *)malloc(sizeof(struct collection)); + coll->cnt = 0; + + head = p = n = NULL; + a = b = buf; + while (a) { + /* set b to the start of the next line and add a NULL character + * to separate the two lines */ + if ((b = strchr(a, '\n')) != NULL) + *b++ = '\0'; + + /* If this is line isn't a comment */ + if ((*a != '#') && (*a != '\0') && (*a != ' ')) { + n = (struct coll_entry *) + malloc(sizeof(struct coll_entry)); + if ((n->pcnt_f = strstr(a, "%f"))) { + n->pcnt_f[1] = 's'; + } + n->name = strdup(strsep(&a, " \t")); + n->cmdline = strdup(a); + n->next = NULL; + + if (p) { + p->next = n; + } + if (head == NULL) { + head = n; + } + p = n; + coll->cnt++; + } + a = b; } - a = b; - } - free(buf); - - /* is there something on the commandline to be counted? */ - if (optind < argc) { - char workstr[1024] = ""; - int workstr_left = 1023; - - /* fill arg list */ - for (i = 0; optind < argc; ++optind, ++i) { - strncat(workstr, argv[optind], workstr_left); - workstr_left = workstr_left - strlen(argv[optind]); - strncat(workstr, " ", workstr_left); - workstr_left--; + free(buf); + + /* is there something on the commandline to be counted? */ + if (optind < argc) { + char workstr[1024] = ""; + int workstr_left = 1023; + + /* fill arg list */ + for (i = 0; optind < argc; ++optind, ++i) { + strncat(workstr, argv[optind], workstr_left); + workstr_left = workstr_left - strlen(argv[optind]); + strncat(workstr, " ", workstr_left); + workstr_left--; + } + + n = (struct coll_entry *)malloc(sizeof(struct coll_entry)); + if ((n->pcnt_f = strstr(workstr, "%f"))) { + n->pcnt_f[1] = 's'; + } + n->cmdline = strdup(workstr); + n->name = "cmdln"; + n->next = NULL; + if (p) { + p->next = n; + } + if (head == NULL) { + head = n; + } + coll->cnt++; } - n = (struct coll_entry *) malloc(sizeof(struct coll_entry)); - if ((n->pcnt_f = strstr(workstr, "%f"))) { - n->pcnt_f[1] = 's'; + /* get an array */ + coll->ary = (struct coll_entry **)malloc(coll->cnt * + sizeof(struct coll_entry *)); + + /* fill the array */ + i = 0; + n = head; + while (n != NULL) { + coll->ary[i] = n; + n = n->next; + ++i; } - n->cmdline = strdup(workstr); - n->name = "cmdln"; - n->next = NULL; - if (p) { - p->next = n; + if (i != coll->cnt) + fprintf(stderr, "pan(%s): i doesn't match cnt\n", panname); + + return coll; +} + +static char *slurp(char *file) +{ + char *buf; + int fd; + struct stat sbuf; + + if ((fd = open(file, O_RDONLY)) < 0) { + fprintf(stderr, + "pan(%s): open(%s,O_RDONLY) failed. errno:%d %s\n", + panname, file, errno, strerror(errno)); + return NULL; } - if (head == NULL) { - head = n; + + if (fstat(fd, &sbuf) < 0) { + fprintf(stderr, "pan(%s): fstat(%s) failed. errno:%d %s\n", + panname, file, errno, strerror(errno)); + return NULL; } - coll->cnt++; - } - - /* get an array */ - coll->ary = (struct coll_entry **) malloc(coll->cnt * - sizeof(struct coll_entry *)); - - /* fill the array */ - i = 0; - n = head; - while (n != NULL) { - coll->ary[i] = n; - n = n->next; - ++i; - } - if (i != coll->cnt) - fprintf(stderr, "pan(%s): i doesn't match cnt\n", panname); - - return coll; -} + buf = (char *)malloc(sbuf.st_size + 1); + if (read(fd, buf, sbuf.st_size) != sbuf.st_size) { + fprintf(stderr, "pan(%s): slurp failed. errno:%d %s\n", + panname, errno, strerror(errno)); + return NULL; + } + buf[sbuf.st_size] = '\0'; -static char * -slurp(char *file) -{ - char *buf; - int fd; - struct stat sbuf; - - if ((fd = open(file, O_RDONLY)) < 0) { - fprintf(stderr, "pan(%s): open(%s,O_RDONLY) failed. errno:%d %s\n", - panname, file, errno, strerror(errno)); - return NULL; - } - - if (fstat(fd, &sbuf) < 0) { - fprintf(stderr, "pan(%s): fstat(%s) failed. errno:%d %s\n", - panname, file, errno, strerror(errno)); - return NULL; - } - - buf = (char *) malloc(sbuf.st_size + 1); - if (read(fd, buf, sbuf.st_size) != sbuf.st_size) { - fprintf(stderr, "pan(%s): slurp failed. errno:%d %s\n", - panname, errno, strerror(errno)); - return NULL; - } - buf[sbuf.st_size] = '\0'; - - close(fd); - return buf; + close(fd); + return buf; } -static void -check_orphans(struct orphan_pgrp *orphans, int sig) +static void check_orphans(struct orphan_pgrp *orphans, int sig) { - struct orphan_pgrp *orph; + struct orphan_pgrp *orph; - for (orph = orphans; orph != NULL; orph = orph->next) { - if (orph->pgrp == 0) - continue; + for (orph = orphans; orph != NULL; orph = orph->next) { + if (orph->pgrp == 0) + continue; - if (Debug & Dshutdown) - fprintf(stderr, " propagating sig %d to orphaned pgrp %d\n", - sig, -(orph->pgrp)); - if (kill(-(orph->pgrp), sig) != 0) { - if (errno == ESRCH) { - /* This pgrp is now empty */ - if (zoo_clear(zoofile, orph->pgrp)) { - fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); + if (Debug & Dshutdown) + fprintf(stderr, + " propagating sig %d to orphaned pgrp %d\n", + sig, -(orph->pgrp)); + if (kill(-(orph->pgrp), sig) != 0) { + if (errno == ESRCH) { + /* This pgrp is now empty */ + if (zoo_clear(zoofile, orph->pgrp)) { + fprintf(stderr, "pan(%s): %s\n", + panname, zoo_error); + } + orph->pgrp = 0; + } else { + fprintf(stderr, + "pan(%s): kill(%d,%d) on orphaned pgrp failed. errno:%d %s\n", + panname, -(orph->pgrp), sig, errno, + strerror(errno)); + } } - orph->pgrp = 0; - } else { - fprintf(stderr, - "pan(%s): kill(%d,%d) on orphaned pgrp failed. errno:%d %s\n", - panname, -(orph->pgrp), sig, errno, strerror(errno)); - } } - } } - -static void -mark_orphan(struct orphan_pgrp *orphans, pid_t cpid) +static void mark_orphan(struct orphan_pgrp *orphans, pid_t cpid) { - struct orphan_pgrp *orph; - - for (orph = orphans; orph != NULL; orph = orph->next) { - if (orph->pgrp == 0) - break; - } - if (orph == NULL) { - /* make a new struct */ - orph = (struct orphan_pgrp *) malloc(sizeof(struct orphan_pgrp)); - - /* plug in the new struct just after the head */ - orph->next = orphans->next; - orphans->next = orph; - } - orph->pgrp = cpid; -} + struct orphan_pgrp *orph; + for (orph = orphans; orph != NULL; orph = orph->next) { + if (orph->pgrp == 0) + break; + } + if (orph == NULL) { + /* make a new struct */ + orph = (struct orphan_pgrp *)malloc(sizeof(struct orphan_pgrp)); + /* plug in the new struct just after the head */ + orph->next = orphans->next; + orphans->next = orph; + } + orph->pgrp = cpid; +} -static void -copy_buffered_output(struct tag_pgrp *running) +static void copy_buffered_output(struct tag_pgrp *running) { - char *tag_output; - - tag_output = slurp(running->output); - if (tag_output) { - printf("%s", tag_output); - /* make sure the output ends with a newline */ - if (tag_output[strlen(tag_output) - 1] != '\n') - printf("\n"); - fflush(stdout); - free(tag_output); - } + char *tag_output; + + tag_output = slurp(running->output); + if (tag_output) { + printf("%s", tag_output); + /* make sure the output ends with a newline */ + if (tag_output[strlen(tag_output) - 1] != '\n') + printf("\n"); + fflush(stdout); + free(tag_output); + } } - -static void -write_test_start(struct tag_pgrp *running) +static void write_test_start(struct tag_pgrp *running) { - if (!strcmp(reporttype, "rts")) { - - printf("%s\ntag=%s stime=%ld\ncmdline=\"%s\"\ncontacts=\"%s\"\nanalysis=%s\n%s\n", - "<<>>", - running->cmd->name, running->mystime, running->cmd->cmdline, "", - "exit", - "<<>>"); - } - fflush(stdout); -} + if (!strcmp(reporttype, "rts")) { + printf + ("%s\ntag=%s stime=%ld\ncmdline=\"%s\"\ncontacts=\"%s\"\nanalysis=%s\n%s\n", + "<<>>", running->cmd->name, running->mystime, + running->cmd->cmdline, "", "exit", "<<>>"); + } + fflush(stdout); +} static void write_test_end(struct tag_pgrp *running, const char *init_status, - time_t exit_time, char *term_type, int stat_loc, - int term_id, struct tms *tms1, struct tms *tms2) + time_t exit_time, char *term_type, int stat_loc, + int term_id, struct tms *tms1, struct tms *tms2) { - if (!strcmp(reporttype, "rts")) { - printf("%s\ninitiation_status=\"%s\"\nduration=%ld termination_type=%s " - "termination_id=%d corefile=%s\ncutime=%d cstime=%d\n%s\n", - "<<>>", init_status, - (long) (exit_time - running->mystime), - term_type, term_id, (stat_loc & 0200) ? "yes" : "no", - (int) (tms2->tms_cutime - tms1->tms_cutime), - (int) (tms2->tms_cstime - tms1->tms_cstime), - "<<>>"); - } - fflush(stdout); + if (!strcmp(reporttype, "rts")) { + printf + ("%s\ninitiation_status=\"%s\"\nduration=%ld termination_type=%s " + "termination_id=%d corefile=%s\ncutime=%d cstime=%d\n%s\n", + "<<>>", init_status, + (long)(exit_time - running->mystime), term_type, term_id, + (stat_loc & 0200) ? "yes" : "no", + (int)(tms2->tms_cutime - tms1->tms_cutime), + (int)(tms2->tms_cstime - tms1->tms_cstime), + "<<>>"); + } + fflush(stdout); } /* The functions below are all debugging related */ -static void -pids_running(struct tag_pgrp *running, int keep_active) +static void pids_running(struct tag_pgrp *running, int keep_active) { - int i; - - fprintf(stderr, "pids still running: "); - for (i = 0; i < keep_active; ++i) { - if (running[i].pgrp != 0) - fprintf(stderr, "%d ", running[i].pgrp); - } - fprintf(stderr, "\n"); + int i; + + fprintf(stderr, "pids still running: "); + for (i = 0; i < keep_active; ++i) { + if (running[i].pgrp != 0) + fprintf(stderr, "%d ", running[i].pgrp); + } + fprintf(stderr, "\n"); } -static void -orphans_running(struct orphan_pgrp *orphans) +static void orphans_running(struct orphan_pgrp *orphans) { - struct orphan_pgrp *orph; - - fprintf(stderr, "orphans still running: "); - for (orph = orphans; orph != NULL; orph = orph->next) { - if (orph->pgrp != 0) - fprintf(stderr, "%d ", -(orph->pgrp)); - } - fprintf(stderr, "\n"); + struct orphan_pgrp *orph; + + fprintf(stderr, "orphans still running: "); + for (orph = orphans; orph != NULL; orph = orph->next) { + if (orph->pgrp != 0) + fprintf(stderr, "%d ", -(orph->pgrp)); + } + fprintf(stderr, "\n"); } -static void -dump_coll(struct collection *coll) +static void dump_coll(struct collection *coll) { - int i; + int i; - for (i = 0; i < coll->cnt; ++i) { - fprintf(stderr, "coll %d\n", i); - fprintf(stderr, " name=%s cmdline=%s\n", coll->ary[i]->name, - coll->ary[i]->cmdline); - } + for (i = 0; i < coll->cnt; ++i) { + fprintf(stderr, "coll %d\n", i); + fprintf(stderr, " name=%s cmdline=%s\n", coll->ary[i]->name, + coll->ary[i]->cmdline); + } } -void -wait_handler( int sig ) +void wait_handler(int sig) { - static int lastsent = 0; - - if (sig == 0) { - lastsent = 0; - } else { - rec_signal = sig; - if (sig == SIGUSR2) - return; - if (lastsent == 0) - send_signal = sig; - else if (lastsent == SIGUSR1) - send_signal = SIGINT; - else if (lastsent == sig) - send_signal = SIGTERM; - else if (lastsent == SIGTERM) - send_signal = SIGHUP; - else if (lastsent == SIGHUP) - send_signal = SIGKILL; - lastsent = send_signal; - } + static int lastsent = 0; + + if (sig == 0) { + lastsent = 0; + } else { + rec_signal = sig; + if (sig == SIGUSR2) + return; + if (lastsent == 0) + send_signal = sig; + else if (lastsent == SIGUSR1) + send_signal = SIGINT; + else if (lastsent == sig) + send_signal = SIGTERM; + else if (lastsent == SIGTERM) + send_signal = SIGHUP; + else if (lastsent == SIGHUP) + send_signal = SIGKILL; + lastsent = send_signal; + } } diff --git a/pan/ltp-scanner.c b/pan/ltp-scanner.c index ce3c1fbf5..afdd75775 100644 --- a/pan/ltp-scanner.c +++ b/pan/ltp-scanner.c @@ -131,32 +131,33 @@ #include "reporter.h" #include "symbol.h" -char *cnf; /* current filename */ -int extended=0; /* -e option */ +char *cnf; /* current filename */ +int extended = 0; /* -e option */ int main(int argc, char *argv[]) { - SYM tags; /* tag data */ + SYM tags; /* tag data */ int c; while ((c = getopt(argc, argv, "D:ehi")) != -1) { - switch(c) { - case 'i': - set_iscanner(); - break; - case 'D': - set_debug(optarg); - break; - case 'e': - extended++; - break; - case 'h': - fprintf(stderr, "%s [-e] [-i] [ -D area, level ] input-filenames\n", - argv[0]); - exit(0); - default: - fprintf(stderr, "invalid argument, %c\n", c); - exit(1); + switch (c) { + case 'i': + set_iscanner(); + break; + case 'D': + set_debug(optarg); + break; + case 'e': + extended++; + break; + case 'h': + fprintf(stderr, + "%s [-e] [-i] [ -D area, level ] input-filenames\n", + argv[0]); + exit(0); + default: + fprintf(stderr, "invalid argument, %c\n", c); + exit(1); } } @@ -166,7 +167,7 @@ int main(int argc, char *argv[]) scanner(tags); #ifdef DEBUGGING DEBUG(D_INIT, 1) - sym_dump_s(tags, 0); + sym_dump_s(tags, 0); #endif reporter(tags); diff --git a/pan/reporter.c b/pan/reporter.c index ee9dad0d0..d1dee3acd 100644 --- a/pan/reporter.c +++ b/pan/reporter.c @@ -49,13 +49,13 @@ * Report Generation * ************************************************************************/ -static int scanner_reporter( SYM ); -static int iscanner_reporter( SYM ); -static int scanner_test_end( SYM, SYM, SYM ); -static int iscanner_test_end( SYM, SYM, SYM ); +static int scanner_reporter(SYM); +static int iscanner_reporter(SYM); +static int scanner_test_end(SYM, SYM, SYM); +static int iscanner_test_end(SYM, SYM, SYM); -static int (*reporter_func)( SYM ) = scanner_reporter; -static int (*test_end_func)( SYM, SYM, SYM ) = scanner_test_end; +static int (*reporter_func) (SYM) = scanner_reporter; +static int (*test_end_func) (SYM, SYM, SYM) = scanner_test_end; /* * Do the report generation. @@ -79,94 +79,93 @@ static int (*test_end_func)( SYM, SYM, SYM ) = scanner_test_end; * (4) go thru all tags and report each as described at the beginning of * this file */ -static int -scanner_reporter(tags) - SYM tags; +static int scanner_reporter(tags) +SYM tags; { - DBT Key, Data; - SYM Tag, Keys; - - time_t clock; - struct tm *tm; - - /* a list of tags, a count of the number of tags allocated to the list, - and a pointer to go thru the list */ - char **taglist, **tl; - int ntags; - int tagcount; /* how many tags used */ - - char key_get[KEYSIZE]; - char *info; - - - /* - * extract tag names from data - */ - ntags=NTAGS_START; - taglist= (char **)malloc(sizeof(char *) * ntags); - tagcount=0; - - tl = taglist; - sym_seq(tags, &Key, &Data, R_FIRST); - do { - if (tagcount == ntags) { - /* exceeded tag array size -- realloc */ - ntags += NTAGS_START; - taglist= (char **)realloc(taglist, sizeof(char *) * ntags); - tl = taglist+tagcount; - } - - *tl++ = Key.data; - tagcount++; - } while (sym_seq(tags, &Key, &Data, R_NEXT)==0); - - if (tagcount == ntags) { - /* exceeded tag array size -- realloc */ - ntags += NTAGS_START; - taglist= (char **)realloc(taglist, sizeof(char *) * ntags); - tl = taglist+tagcount; - } - - *tl++ = NULL; - ntags = tagcount; - /* Retrieve one "stime" to get the date. */ - for (tl=taglist; *tl != NULL; tl++) { - strcpy(key_get, *tl); - strcat(key_get, ",_keys,stime"); - if ((info = (char *)sym_get(tags, key_get)) != NULL) { - clock = atoi(info); - tm = gmtime(&clock); - strftime(key_get, KEYSIZE, "%x", tm); - sym_put(tags, strdup("_RTS,date"), strdup(key_get), 0); - break; - } - } - - print_header(tags); - - /* - * The way that I am using 'Keys' and 'Tag' makes assumptions about the - * internals of the sym_* data structure. - */ - /* dump 'em all */ - for (tl=taglist; *tl != NULL; tl++) { - if (!strcmp(*tl, "_RTS")) - continue; - - strcpy(key_get, *tl); - strcat(key_get, ",_keys"); - if ((Keys = sym_get(tags, key_get)) == NULL) { - return 0; - } - - strcpy(key_get, *tl); - if ((Tag = sym_get(tags, key_get)) != NULL) { - tag_report(NULL, Tag, Keys); - } - } - free(taglist); - - return 0; + DBT Key, Data; + SYM Tag, Keys; + + time_t clock; + struct tm *tm; + + /* a list of tags, a count of the number of tags allocated to the list, + and a pointer to go thru the list */ + char **taglist, **tl; + int ntags; + int tagcount; /* how many tags used */ + + char key_get[KEYSIZE]; + char *info; + + /* + * extract tag names from data + */ + ntags = NTAGS_START; + taglist = (char **)malloc(sizeof(char *) * ntags); + tagcount = 0; + + tl = taglist; + sym_seq(tags, &Key, &Data, R_FIRST); + do { + if (tagcount == ntags) { + /* exceeded tag array size -- realloc */ + ntags += NTAGS_START; + taglist = + (char **)realloc(taglist, sizeof(char *) * ntags); + tl = taglist + tagcount; + } + + *tl++ = Key.data; + tagcount++; + } while (sym_seq(tags, &Key, &Data, R_NEXT) == 0); + + if (tagcount == ntags) { + /* exceeded tag array size -- realloc */ + ntags += NTAGS_START; + taglist = (char **)realloc(taglist, sizeof(char *) * ntags); + tl = taglist + tagcount; + } + + *tl++ = NULL; + ntags = tagcount; + /* Retrieve one "stime" to get the date. */ + for (tl = taglist; *tl != NULL; tl++) { + strcpy(key_get, *tl); + strcat(key_get, ",_keys,stime"); + if ((info = (char *)sym_get(tags, key_get)) != NULL) { + clock = atoi(info); + tm = gmtime(&clock); + strftime(key_get, KEYSIZE, "%x", tm); + sym_put(tags, strdup("_RTS,date"), strdup(key_get), 0); + break; + } + } + + print_header(tags); + + /* + * The way that I am using 'Keys' and 'Tag' makes assumptions about the + * internals of the sym_* data structure. + */ + /* dump 'em all */ + for (tl = taglist; *tl != NULL; tl++) { + if (!strcmp(*tl, "_RTS")) + continue; + + strcpy(key_get, *tl); + strcat(key_get, ",_keys"); + if ((Keys = sym_get(tags, key_get)) == NULL) { + return 0; + } + + strcpy(key_get, *tl); + if ((Tag = sym_get(tags, key_get)) != NULL) { + tag_report(NULL, Tag, Keys); + } + } + free(taglist); + + return 0; } /* @@ -180,100 +179,96 @@ scanner_reporter(tags) * under another key tree with almost zero brainwork because a SYM * is what the DATA area points to. */ -static int -scanner_test_end(alltags, ctag, keys) - SYM alltags, ctag, keys; +static int scanner_test_end(alltags, ctag, keys) +SYM alltags, ctag, keys; { - static int notag=0; /* counter for records with no tag (error) */ - char tagname[KEYSIZE]; /* used when creating name (see above) */ - char *tag; /* tag name to look things up in */ - char *status; /* initiation status of old tag */ - SYM rm; /* pointer to old tag -- to remove it */ - - - if (alltags == NULL || keys == NULL || ctag == NULL) - return -1; /* for really messed up test output */ - - /* insert keys into tag */ - sym_put(ctag, "_keys", (void *)keys, 0); - - /* get the tag, or build a new one */ - if ((tag=(char *)sym_get(keys, "tag")) == NULL) { - /* this is an "impossible" situation: test_output checks for this - * and creates a dummy tag. */ - sprintf(tagname, "no_tag_%d", notag++); - fprintf(stderr, "No TAG key! Using %s\n", tagname); - sym_put(keys, "tag", strdup(tagname), 0); - tag=strdup(tagname); - } - - /* - * Special case: duplicate tag that has an initiation_status failure - * is thrown away. - */ - if ((rm=(SYM)sym_get(alltags, tag)) != NULL) { - if ((status=(char *)sym_get(keys, "initiation_status")) != NULL) { - if (strcmp(status, "ok")) { - /* do not take new data. remove new data */ - sym_rm(ctag, RM_KEY | RM_DATA); - return 1; - } else { - /* remove old data in alltags */ - sym_rm(rm, RM_KEY | RM_DATA); - } - } else { - /* new data does not have an initiation_status -- throw it away */ - sym_rm(ctag, RM_KEY | RM_DATA); - return 1; - } - } - - /* put new data.. replaces existing "tag" key if it exists - * (it's data should have been removed above) */ - sym_put(alltags, tag, ctag, PUT_REPLACE); - - return 0; + static int notag = 0; /* counter for records with no tag (error) */ + char tagname[KEYSIZE]; /* used when creating name (see above) */ + char *tag; /* tag name to look things up in */ + char *status; /* initiation status of old tag */ + SYM rm; /* pointer to old tag -- to remove it */ + + if (alltags == NULL || keys == NULL || ctag == NULL) + return -1; /* for really messed up test output */ + + /* insert keys into tag */ + sym_put(ctag, "_keys", (void *)keys, 0); + + /* get the tag, or build a new one */ + if ((tag = (char *)sym_get(keys, "tag")) == NULL) { + /* this is an "impossible" situation: test_output checks for this + * and creates a dummy tag. */ + sprintf(tagname, "no_tag_%d", notag++); + fprintf(stderr, "No TAG key! Using %s\n", tagname); + sym_put(keys, "tag", strdup(tagname), 0); + tag = strdup(tagname); + } + + /* + * Special case: duplicate tag that has an initiation_status failure + * is thrown away. + */ + if ((rm = (SYM) sym_get(alltags, tag)) != NULL) { + if ((status = + (char *)sym_get(keys, "initiation_status")) != NULL) { + if (strcmp(status, "ok")) { + /* do not take new data. remove new data */ + sym_rm(ctag, RM_KEY | RM_DATA); + return 1; + } else { + /* remove old data in alltags */ + sym_rm(rm, RM_KEY | RM_DATA); + } + } else { + /* new data does not have an initiation_status -- throw it away */ + sym_rm(ctag, RM_KEY | RM_DATA); + return 1; + } + } + + /* put new data.. replaces existing "tag" key if it exists + * (it's data should have been removed above) */ + sym_put(alltags, tag, ctag, PUT_REPLACE); + + return 0; } -static int -iscanner_reporter(tags) - SYM tags; +static int iscanner_reporter(tags) +SYM tags; { - return 0; + return 0; } -static int -iscanner_test_end(alltags, ctag, keys) - SYM alltags, ctag, keys; +static int iscanner_test_end(alltags, ctag, keys) +SYM alltags, ctag, keys; { - if (alltags == NULL || keys == NULL || ctag == NULL) - return -1; /* for really messed up test output */ + if (alltags == NULL || keys == NULL || ctag == NULL) + return -1; /* for really messed up test output */ - /* insert keys into tag */ - sym_put(ctag, "_keys", (void *)keys, 0); + /* insert keys into tag */ + sym_put(ctag, "_keys", (void *)keys, 0); - - return tag_report(alltags, ctag, keys); + return tag_report(alltags, ctag, keys); } -int reporter( SYM s ) +int reporter(SYM s) { - return reporter_func( s ); + return reporter_func(s); } -int test_end( SYM a, SYM b, SYM c ) +int test_end(SYM a, SYM b, SYM c) { - return test_end_func( a, b, c ); + return test_end_func(a, b, c); } void set_scanner(void) { - reporter_func = scanner_reporter; - test_end_func = scanner_test_end; + reporter_func = scanner_reporter; + test_end_func = scanner_test_end; } void set_iscanner(void) { - reporter_func = iscanner_reporter; - test_end_func = iscanner_test_end; + reporter_func = iscanner_reporter; + test_end_func = iscanner_test_end; } diff --git a/pan/splitstr.c b/pan/splitstr.c index 864a26205..4706323a8 100644 --- a/pan/splitstr.c +++ b/pan/splitstr.c @@ -52,96 +52,88 @@ */ #include #include -#include /* for string functions */ +#include /* for string functions */ #ifdef UNIT_TEST #include #endif /* UNIT_TEST */ #include "splitstr.h" -const char ** -splitstr(const char *str, const char *separator, int *argcount) +const char **splitstr(const char *str, const char *separator, int *argcount) { - char *arg_string =NULL, - **arg_array =NULL, - *cur_tok =NULL; - - int num_toks =0, - max_toks =20, - i; - - /* - * In most recoverable errors, if argcount is not NULL, - * set argcount to 0. Then return NULL. - */ - if (str == NULL) - { - if (argcount != NULL) - *argcount = 0; - return(NULL); - } - - /* - * set aside temporary space to work on the string. - */ - arg_string = strdup( str ); - - if (arg_string == NULL) - { - if (argcount != NULL) - *argcount = 0; - return(NULL); - } - - /* - * set aside an initial char ** array for string array. - */ - arg_array = (char **)malloc( sizeof(char *) * max_toks ); - - if (arg_array == NULL) - { - if (argcount != NULL) - *argcount = 0; - return(NULL); - } - - if (separator==NULL) - separator = " \t"; - - /* - * Use strtok() to parse 'arg_string', placing pointers to the - * individual tokens into the elements of 'arg_array'. Expand - * 'arg_array' if necessary. - */ - cur_tok = strtok(arg_string, separator); - while (cur_tok != NULL) - { - arg_array[num_toks++] = cur_tok; - cur_tok = strtok(NULL, separator); - if (num_toks == max_toks) - { - max_toks += 20; - arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*max_toks ); - } - } - arg_array[num_toks] = NULL; - - /* - * If there are any spaces left in our array, make them NULL - */ - for (i=num_toks+1;imagic = SYM_MAGIC; - h->sym = NULL; - h->cursor = NULL; - return(h); + h->magic = SYM_MAGIC; + h->sym = NULL; + h->cursor = NULL; + return (h); } -static struct sym * -mknode(struct sym *next, char *key, void *data) +static struct sym *mknode(struct sym *next, char *key, void *data) { - struct sym *n; - - if ((n=(struct sym *)malloc(sizeof(struct sym))) == NULL) { - sym_error="sym node malloc failed!"; - return(NULL); - } - - n->next = next; - n->key = strdup(key); - n->data = data; - - if (n->key == NULL) { - sym_error="sym node strdup(key) failed!"; - return(NULL); - } - return(n); + struct sym *n; + + if ((n = (struct sym *)malloc(sizeof(struct sym))) == NULL) { + sym_error = "sym node malloc failed!"; + return (NULL); + } + + n->next = next; + n->key = strdup(key); + n->data = data; + + if (n->key == NULL) { + sym_error = "sym node strdup(key) failed!"; + return (NULL); + } + return (n); } /* * Search for a key in a single-level symbol table hierarchy. */ -static struct sym * -find_key1(struct sym *sym, char *key) +static struct sym *find_key1(struct sym *sym, char *key) { - while (sym != NULL) - if (strcmp(sym->key, key) == 0) - return(sym); - else - sym=sym->next; - return(NULL); + while (sym != NULL) + if (strcmp(sym->key, key) == 0) + return (sym); + else + sym = sym->next; + return (NULL); } /* * Create a new key node, add it to the *end* of this list */ -static int -add_key(SYM sym, char *key, void *data) +static int add_key(SYM sym, char *key, void *data) { - register struct sym *sn; - - if (sym->sym == NULL) - { - sym->sym = mknode(NULL, key, data); - if (sym->sym == NULL) - { - return(-1); - } - } - else - { - for (sn=sym->sym; sn!=NULL && sn->next != NULL; sn=sn->next); - sn->next = mknode(NULL, key, data); - assert(sn->next != NULL); - if (sn->next == NULL) - return(-1); - } - return(0); + register struct sym *sn; + + if (sym->sym == NULL) { + sym->sym = mknode(NULL, key, data); + if (sym->sym == NULL) { + return (-1); + } + } else { + for (sn = sym->sym; sn != NULL && sn->next != NULL; + sn = sn->next) ; + sn->next = mknode(NULL, key, data); + assert(sn->next != NULL); + if (sn->next == NULL) + return (-1); + } + return (0); } /* @@ -164,10 +158,9 @@ add_key(SYM sym, char *key, void *data) */ SYM sym_open(int flags, int mode, int openinfo) { - return(newsym()); + return (newsym()); } - /* * Add (key, data) to an existing symbol table * @@ -197,68 +190,67 @@ SYM sym_open(int flags, int mode, int openinfo) * last node gets 'data'. * */ -int -sym_put(SYM sym, char *key, void *data, int flags) +int sym_put(SYM sym, char *key, void *data, int flags) { - const char **keys; /* key split into a 2d string array */ - char **kk; - char *nkey; /* copy of 'key' -- before split */ - SYM csym, ncsym; /* search: current symbol table */ - struct sym *nsym = NULL; /* search: found symbol entry */ - - if (sym == NULL) - return(EINVAL); - - nkey = strdup(key); - keys = splitstr(key, ",",NULL); - - if (keys == NULL) - return(EINVAL); - - for (kk=(char **)keys, csym = sym; - *kk != NULL && (nsym=find_key1(csym->sym, *kk)) != NULL; - csym=nsym->data) { - - if (*++kk == NULL) - break; - - if (nsym->data == NULL) { /* fatal error */ - free(nkey); - splitstr_free(keys); - return(ENOTDIR); - } - if (((SYM) (nsym->data))->magic != SYM_MAGIC) { - free(nkey); - splitstr_free(keys); - return(ENOTDIR); + const char **keys; /* key split into a 2d string array */ + char **kk; + char *nkey; /* copy of 'key' -- before split */ + SYM csym, ncsym; /* search: current symbol table */ + struct sym *nsym = NULL; /* search: found symbol entry */ + + if (sym == NULL) + return (EINVAL); + + nkey = strdup(key); + keys = splitstr(key, ",", NULL); + + if (keys == NULL) + return (EINVAL); + + for (kk = (char **)keys, csym = sym; + *kk != NULL && (nsym = find_key1(csym->sym, *kk)) != NULL; + csym = nsym->data) { + + if (*++kk == NULL) + break; + + if (nsym->data == NULL) { /* fatal error */ + free(nkey); + splitstr_free(keys); + return (ENOTDIR); + } + if (((SYM) (nsym->data))->magic != SYM_MAGIC) { + free(nkey); + splitstr_free(keys); + return (ENOTDIR); + } } - } - if (*kk == NULL) { /* found a complete match */ - free(nkey); - splitstr_free(keys); + if (*kk == NULL) { /* found a complete match */ + free(nkey); + splitstr_free(keys); - if (flags == PUT_REPLACE) { - nsym->data = data; - return(0); - } else { - return(EEXIST); + if (flags == PUT_REPLACE) { + nsym->data = data; + return (0); + } else { + return (EEXIST); + } } - } - /* csym is a ptr to a list */ - for (;*kk != NULL; kk++) { - if (*(kk+1) != NULL) { - add_key(csym, *kk, (void *)(ncsym=newsym())); - csym = ncsym; - } else { - add_key(csym, *kk, data); /* last key */ + /* csym is a ptr to a list */ + for (; *kk != NULL; kk++) { + if (*(kk + 1) != NULL) { + add_key(csym, *kk, (void *)(ncsym = newsym())); + csym = ncsym; + } else { + add_key(csym, *kk, data); /* last key */ + } } - } - free(nkey); - splitstr_free(keys); - return(0); + free(nkey); + splitstr_free(keys); + return (0); } /* @@ -267,50 +259,50 @@ sym_put(SYM sym, char *key, void *data, int flags) * "key" is not modified. * If the key cannot be found, NULL is returned */ -void * sym_get(SYM sym, char *key) +void *sym_get(SYM sym, char *key) { - char *nkey; - const char **keys; /* key split into a 2d string array */ - char **kk; - SYM csym; /* search: current symbol table */ - struct sym *nsym = NULL; /* search: found symbol entry */ - - if (sym == NULL) - return(NULL); - - nkey=strdup(key); - keys = splitstr(nkey, ",", NULL); - if (keys == NULL) - return(NULL); - - for (kk=(char **)keys, csym = sym; - *kk != NULL && (nsym=find_key1(csym->sym, *kk)) != NULL; - csym=nsym->data) { - - if (*++kk == NULL) - break; - - if (nsym->data == NULL) { /* fatal error */ - free(nkey); - splitstr_free(keys); - return(NULL); - } - if (((SYM)(nsym->data))->magic != SYM_MAGIC) { - free(nkey); - splitstr_free(keys); - return(NULL); + char *nkey; + const char **keys; /* key split into a 2d string array */ + char **kk; + SYM csym; /* search: current symbol table */ + struct sym *nsym = NULL; /* search: found symbol entry */ + + if (sym == NULL) + return (NULL); + + nkey = strdup(key); + keys = splitstr(nkey, ",", NULL); + if (keys == NULL) + return (NULL); + + for (kk = (char **)keys, csym = sym; + *kk != NULL && (nsym = find_key1(csym->sym, *kk)) != NULL; + csym = nsym->data) { + + if (*++kk == NULL) + break; + + if (nsym->data == NULL) { /* fatal error */ + free(nkey); + splitstr_free(keys); + return (NULL); + } + if (((SYM) (nsym->data))->magic != SYM_MAGIC) { + free(nkey); + splitstr_free(keys); + return (NULL); + } } - } - if (*kk == NULL) { /* found a complete match */ - splitstr_free(keys); - free(nkey); - return(nsym->data); - } else { - splitstr_free(keys); - free(nkey); - return(NULL); - } + if (*kk == NULL) { /* found a complete match */ + splitstr_free(keys); + free(nkey); + return (nsym->data); + } else { + splitstr_free(keys); + free(nkey); + return (NULL); + } } /* @@ -319,63 +311,62 @@ void * sym_get(SYM sym, char *key) * The cursor must be set by R_CURSOR, R_FIRST before using R_NEXT. * NULL is returned when no more items are available. */ -int - sym_seq(SYM sym, DBT *key, DBT *data, int flags) +int sym_seq(SYM sym, DBT * key, DBT * data, int flags) { - SYM csym; - - switch(flags) { - /* - * A number of ways to do this: - * specificly: sym_seq( .., "key,key") sets to Nth element of the 2nd - * level symbol table - * sym_seq(.., "key,key,") sets to the first element of the 3rd - * level symbol table - * - * sym_seq(.., "key,key") where both must be complete keys, sets - * cursor to the first element of the 3rd level symbol table; - * if there is no 3rd level, return an error. - */ - case R_CURSOR: - csym = (SYM) sym_get(sym, (char *)key->data); - if (csym == NULL || csym->magic != SYM_MAGIC) { - return(2); + SYM csym; + + switch (flags) { + /* + * A number of ways to do this: + * specificly: sym_seq( .., "key,key") sets to Nth element of the 2nd + * level symbol table + * sym_seq(.., "key,key,") sets to the first element of the 3rd + * level symbol table + * + * sym_seq(.., "key,key") where both must be complete keys, sets + * cursor to the first element of the 3rd level symbol table; + * if there is no 3rd level, return an error. + */ + case R_CURSOR: + csym = (SYM) sym_get(sym, (char *)key->data); + if (csym == NULL || csym->magic != SYM_MAGIC) { + return (2); + } + sym->cursor = csym->sym; + if (sym->cursor == NULL) + return (1); + key->data = sym->cursor->key; + data->data = sym->cursor->data; + + return (0); + + case R_FIRST: + sym->cursor = sym->sym; + if (sym->cursor == NULL) + return (1); + key->data = sym->cursor->key; + data->data = sym->cursor->data; + + return (0); + + case R_NEXT: + if (sym->cursor == NULL) + return (1); + sym->cursor = sym->cursor->next; + + if (sym->cursor == NULL) + return (1); + + key->data = sym->cursor->key; + data->data = sym->cursor->data; + + return (0); + + case R_LAST: + case R_PREV: + default: + return (-1); } - sym->cursor = csym->sym; - if (sym->cursor == NULL) - return(1); - key->data = sym->cursor->key; - data->data = sym->cursor->data; - - return(0); - - case R_FIRST: - sym->cursor = sym->sym; - if (sym->cursor == NULL) - return(1); - key->data = sym->cursor->key; - data->data = sym->cursor->data; - - return(0); - - case R_NEXT: - if (sym->cursor == NULL) - return(1); - sym->cursor = sym->cursor->next; - - if (sym->cursor == NULL) - return(1); - - key->data = sym->cursor->key; - data->data = sym->cursor->data; - - return(0); - - case R_LAST: - case R_PREV: - default: - return(-1); - } } /* @@ -383,90 +374,91 @@ int * Handles hierarchies, using a double quote to indicate depth, one * double quote for each level. */ -int -sym_dump(SYM sym, int depth) +int sym_dump(SYM sym, int depth) { - register struct sym *se; /* symbol entry */ - register int d; + register struct sym *se; /* symbol entry */ + register int d; - if (sym == NULL || sym->magic != SYM_MAGIC) - return -1; + if (sym == NULL || sym->magic != SYM_MAGIC) + return -1; - for (se=sym->sym;se != NULL;se=se->next) { - for (d=0;d < depth; d++) { - putchar('"'); putchar(' '); + for (se = sym->sym; se != NULL; se = se->next) { + for (d = 0; d < depth; d++) { + putchar('"'); + putchar(' '); + } + printf("%s\n", se->key); + sym_dump((SYM) se->data, depth + 1); } - printf("%s\n", se->key); - sym_dump((SYM)se->data, depth+1); - } - return 0; + return 0; } /* * sym dump, but data is _always_ a string (print it) */ -int -sym_dump_s(SYM sym, int depth) +int sym_dump_s(SYM sym, int depth) { - register struct sym *se; /* symbol entry */ - register int d; + register struct sym *se; /* symbol entry */ + register int d; - if (sym == NULL) - return 0; + if (sym == NULL) + return 0; - if (sym->magic != SYM_MAGIC) { - for (d=0;d < depth; d++) { - putchar('"'); putchar(' '); + if (sym->magic != SYM_MAGIC) { + for (d = 0; d < depth; d++) { + putchar('"'); + putchar(' '); + } + printf(" = %s\n", (char *)sym); + return 0; } - printf(" = %s\n", (char *)sym); - return 0; - } - for (se=sym->sym;se != NULL;se=se->next) { - for (d=0;d < depth; d++) { - putchar('"'); putchar(' '); - } - printf("%s", se->key); - if (((SYM)se->data)->magic == SYM_MAGIC) { - putchar('\n'); - sym_dump_s((SYM)se->data, depth+1); - } else { - printf("(%p) = %s (%p)\n", se->key, (char *)se->data, se->data); + for (se = sym->sym; se != NULL; se = se->next) { + for (d = 0; d < depth; d++) { + putchar('"'); + putchar(' '); + } + printf("%s", se->key); + if (((SYM) se->data)->magic == SYM_MAGIC) { + putchar('\n'); + sym_dump_s((SYM) se->data, depth + 1); + } else { + printf("(%p) = %s (%p)\n", se->key, (char *)se->data, + se->data); + } } - } - return 0; + return 0; } /* * Remove an entire symbol table (done bottom up) */ -int -sym_rm(SYM sym, int flags) +int sym_rm(SYM sym, int flags) { - register struct sym *se, *nse; /* symbol entry */ + register struct sym *se, *nse; /* symbol entry */ - if (sym == NULL) - return 0; + if (sym == NULL) + return 0; - if (sym->magic != SYM_MAGIC) { - if (!(flags&RM_DATA)) - free(sym); + if (sym->magic != SYM_MAGIC) { + if (!(flags & RM_DATA)) + free(sym); + return 0; + } + + for (se = sym->sym; se != NULL;) { + sym_rm((SYM) se->data, flags); + nse = se->next; + if (flags & RM_KEY) + free(se->key); + if (flags & RM_DATA) + free(se->data); + free(se); + se = nse; + } + if (!(flags & RM_DATA)) + free(sym); return 0; - } - - for (se=sym->sym;se != NULL;) { - sym_rm((SYM)se->data, flags); - nse=se->next; - if (flags & RM_KEY) - free(se->key); - if (flags & RM_DATA) - free(se->data); - free(se); - se=nse; - } - if (!(flags&RM_DATA)) - free(sym); - return 0; } diff --git a/pan/tag_report.c b/pan/tag_report.c index c4517b729..473bed1d1 100644 --- a/pan/tag_report.c +++ b/pan/tag_report.c @@ -36,7 +36,7 @@ #include "reporter.h" #include "splitstr.h" -static char * worst_case(char *, char *); +static char *worst_case(char *, char *); /************************************************************************ * Report Generation * @@ -52,51 +52,49 @@ static char * worst_case(char *, char *); * This is the central results reporting function. All standard report * format results are printed thru test_result. */ -int -test_result(tag, tcid, tc, result, tags) - char *tag, *tcid, *tc; - char *result; - SYM tags; +int test_result(tag, tcid, tc, result, tags) +char *tag, *tcid, *tc; +char *result; +SYM tags; { - char *expert, expkey[KEYSIZE]; - register char *c; - char **cont; - const char **cont_save; - - if (tcid == NULL) - tcid = "-"; - if (tc == NULL) - tc = "-"; - if (tag == NULL) - tag = "test_result: no tag"; - if (result == NULL) - result = "(RESULT IS NULL)"; - - strcpy(expkey, "contacts"); - /* note: the sym_get here does _not_ change the "cursor" */ - if ((expert = (char *)sym_get(tags, expkey)) == NULL) { - expert = "UNKNOWN"; - } - - /* ' tr " " "_" ' */ - for (c = result; *c; c++) { - if (*c == ' ') { - *c = '_'; + char *expert, expkey[KEYSIZE]; + register char *c; + char **cont; + const char **cont_save; + + if (tcid == NULL) + tcid = "-"; + if (tc == NULL) + tc = "-"; + if (tag == NULL) + tag = "test_result: no tag"; + if (result == NULL) + result = "(RESULT IS NULL)"; + + strcpy(expkey, "contacts"); + /* note: the sym_get here does _not_ change the "cursor" */ + if ((expert = (char *)sym_get(tags, expkey)) == NULL) { + expert = "UNKNOWN"; } - } - if (*result == '\0') - result = "?"; - - /* split contacts on "," and print out a line for each */ - cont_save = splitstr(expert, ",", NULL); - for (cont=(char **)cont_save;*cont != NULL; cont++) { - printf(FORMAT, tag, tcid, tc, result, *cont); - } - splitstr_free(cont_save); - - return 0; -} + /* ' tr " " "_" ' */ + for (c = result; *c; c++) { + if (*c == ' ') { + *c = '_'; + } + } + if (*result == '\0') + result = "?"; + + /* split contacts on "," and print out a line for each */ + cont_save = splitstr(expert, ",", NULL); + for (cont = (char **)cont_save; *cont != NULL; cont++) { + printf(FORMAT, tag, tcid, tc, result, *cont); + } + splitstr_free(cont_save); + + return 0; +} /* * CUTS test reporting. @@ -111,98 +109,100 @@ test_result(tag, tcid, tc, result, tags) * report an error if they don't match. */ -int -cuts_report(tags, keys, at, tag ) - SYM tags, keys; char *at, *tag; +int cuts_report(tags, keys, at, tag) +SYM tags, keys; +char *at, *tag; { - DBT Key, Data; - - /* analysis type: count of CUTS test cases */ - const char **ant; - char *dat; /* strdup(at) */ - int tccount; /* expected count of testcases */ - int tcnum; /* seen count of testcases */ - - /* a list of tcids */ - char **taglist, **tl; - int ntags, tagcount; - - char key_get[255]; - - char *result = "", *worst_case(); /* overall result */ - - /* parse analysis type: cuts:tc-count */ - ant = splitstr((dat=strdup(at)), ":",NULL); - if (ant[1] != NULL) - tccount=atoi(ant[1]); - else - tccount=0; - free(dat); - splitstr_free(ant); - - /* extract tcids */ - ntags=NTCID_START; - taglist= (char **)malloc(sizeof(char *) * ntags); - tagcount=0; - - tl = taglist; - sym_seq(tags, &Key, &Data, R_FIRST); - do { - if (tagcount == ntags) { - /* exceeded tag array size -- realloc */ - ntags += NTCID_START; - taglist= (char **)realloc(taglist, sizeof(char *) * ntags); - tl = taglist+tagcount; - } + DBT Key, Data; + + /* analysis type: count of CUTS test cases */ + const char **ant; + char *dat; /* strdup(at) */ + int tccount; /* expected count of testcases */ + int tcnum; /* seen count of testcases */ + + /* a list of tcids */ + char **taglist, **tl; + int ntags, tagcount; + + char key_get[255]; + + char *result = "", *worst_case(); /* overall result */ + + /* parse analysis type: cuts:tc-count */ + ant = splitstr((dat = strdup(at)), ":", NULL); + if (ant[1] != NULL) + tccount = atoi(ant[1]); + else + tccount = 0; + free(dat); + splitstr_free(ant); + + /* extract tcids */ + ntags = NTCID_START; + taglist = (char **)malloc(sizeof(char *) * ntags); + tagcount = 0; + + tl = taglist; + sym_seq(tags, &Key, &Data, R_FIRST); + do { + if (tagcount == ntags) { + /* exceeded tag array size -- realloc */ + ntags += NTCID_START; + taglist = + (char **)realloc(taglist, sizeof(char *) * ntags); + tl = taglist + tagcount; + } - if (strcmp((char *)Key.data, "_keys")==0) - continue; - DEBUG(D_REPORT, 10) - printf("cuts_report: tcid %s\n", (char *)Key.data); - *tl++ = Key.data; - tagcount++; - } while (sym_seq(tags, &Key, &Data, R_NEXT)==0); + if (strcmp((char *)Key.data, "_keys") == 0) + continue; + DEBUG(D_REPORT, 10) + printf("cuts_report: tcid %s\n", (char *)Key.data); + *tl++ = Key.data; + tagcount++; + } while (sym_seq(tags, &Key, &Data, R_NEXT) == 0); - if (tagcount == ntags) { - /* exceeded tag array size -- realloc */ - ntags++; /* need just one more */ - taglist= (char **)realloc(taglist, sizeof(char *) * ntags); - tl = taglist+tagcount; - } + if (tagcount == ntags) { + /* exceeded tag array size -- realloc */ + ntags++; /* need just one more */ + taglist = (char **)realloc(taglist, sizeof(char *) * ntags); + tl = taglist + tagcount; + } - *tl++ = NULL; + *tl++ = NULL; - ntags = tagcount; + ntags = tagcount; - /* dump all found records */ - tcnum=0; - for (tl=taglist; *tl != NULL; tl++) { + /* dump all found records */ + tcnum = 0; + for (tl = taglist; *tl != NULL; tl++) { - strcpy(key_get, *tl); - Key.data = (void *) key_get; + strcpy(key_get, *tl); + Key.data = (void *)key_get; - /*sym_dump_s(sym_get(tags, key_get), 0);*/ + /*sym_dump_s(sym_get(tags, key_get), 0); */ - sym_seq(tags, &Key, &Data, R_CURSOR); - do { - DEBUG(D_REPORT, 10) - printf("cuts_report: tc %s = %s\n", (char *)Key.data, - (char *)Data.data); - result = worst_case(result, (char *)Data.data); - test_result(tag, *tl, (char *)Key.data, (char *)Data.data, keys); - if (atoi((char *)Key.data)) - tcnum++; - } while (sym_seq(tags, &Key, &Data, R_NEXT)==0); - } + sym_seq(tags, &Key, &Data, R_CURSOR); + do { + DEBUG(D_REPORT, 10) + printf("cuts_report: tc %s = %s\n", + (char *)Key.data, (char *)Data.data); + result = worst_case(result, (char *)Data.data); + test_result(tag, *tl, (char *)Key.data, + (char *)Data.data, keys); + if (atoi((char *)Key.data)) + tcnum++; + } while (sym_seq(tags, &Key, &Data, R_NEXT) == 0); + } - test_result(tag, "*", "*", result, keys); + test_result(tag, "*", "*", result, keys); - if (tccount != 0 && tccount != tcnum) - test_result(tag, "-", "-", "TC count wrong", keys); + if (tccount != 0 && tccount != tcnum) + test_result(tag, "-", "-", "TC count wrong", keys); - free(taglist); + free(taglist); - return 0; + return 0; } /* @@ -227,189 +227,193 @@ cuts_report(tags, keys, at, tag ) * (4) go thru all tags and report each as described at the beginning of * this file */ -int -tag_report(alltags, ctag, keys) - SYM alltags, ctag, keys; +int tag_report(alltags, ctag, keys) +SYM alltags, ctag, keys; { - extern int extended; - - char key_get[KEYSIZE]; - char *info; - - /* retrieved _keys values: initation status, start time, duration, - * termination type, termination id, start line, end line. */ - char *tag, *contact, *is, *mystime, *duration, *tt, *ti, *sl, *el; - - /* Check all driver-level status first */ - strcpy(key_get, "tag"); - if ((tag = (char *)sym_get(keys, key_get)) == NULL) { - return -1; - } - - /* Check all driver-level status first */ - strcpy(key_get, "initiation_status"); - if ((is = (char *)sym_get(keys, key_get)) == NULL) { - test_result(tag, NULL, NULL, "no init status", keys); - return -1; - } - - if (strcmp(is, "ok")) { - test_result(tag, NULL, NULL, is, keys); - } else { - - strcpy(key_get, "corefile"); - if ((info = (char *)sym_get(keys, key_get)) != NULL) - if (strcmp(info, "no") != 0) { - test_result(tag, NULL, NULL, "coredump", keys); - } - - strcpy(key_get, "termination_type"); - if ((tt = (char *)sym_get(keys, key_get))==NULL) { - test_result(tag, NULL, NULL, "no Term Type", keys); - return -1; + extern int extended; + + char key_get[KEYSIZE]; + char *info; + + /* retrieved _keys values: initation status, start time, duration, + * termination type, termination id, start line, end line. */ + char *tag, *contact, *is, *mystime, *duration, *tt, *ti, *sl, *el; + + /* Check all driver-level status first */ + strcpy(key_get, "tag"); + if ((tag = (char *)sym_get(keys, key_get)) == NULL) { + return -1; } - if (strcmp(tt, "exited")) { - test_result(tag, NULL, NULL, tt, keys); + /* Check all driver-level status first */ + strcpy(key_get, "initiation_status"); + if ((is = (char *)sym_get(keys, key_get)) == NULL) { + test_result(tag, NULL, NULL, "no init status", keys); + return -1; } - strcpy(key_get, "analysis"); - if ((info = (char *)sym_get(keys, key_get)) == NULL) { - test_result(tag, NULL, NULL, "no Analysis Type", keys); - return -1; + if (strcmp(is, "ok")) { + test_result(tag, NULL, NULL, is, keys); + } else { + + strcpy(key_get, "corefile"); + if ((info = (char *)sym_get(keys, key_get)) != NULL) + if (strcmp(info, "no") != 0) { + test_result(tag, NULL, NULL, "coredump", keys); + } + + strcpy(key_get, "termination_type"); + if ((tt = (char *)sym_get(keys, key_get)) == NULL) { + test_result(tag, NULL, NULL, "no Term Type", keys); + return -1; + } + + if (strcmp(tt, "exited")) { + test_result(tag, NULL, NULL, tt, keys); + } + + strcpy(key_get, "analysis"); + if ((info = (char *)sym_get(keys, key_get)) == NULL) { + test_result(tag, NULL, NULL, "no Analysis Type", keys); + return -1; + } + + /* Getting here indicates that there were no fatal driver-level + * errors. Do the kind of reporting requested by the test. + */ + + if (strncmp(info, "none", 4) == 0) { + /* + * If analysis is 'none', alway report the test as + * a pass regardless of output or exit status. + */ + test_result(tag, NULL, NULL, "pass", keys); + + } else if (strncmp(info, "cuts", 4)) { + + /* + * If analysis is not cuts, assume it is 'exit', thus + * the termination_id is used to determine pass/fail result. + */ + if (strcmp(tt, "timeout")) { + strcpy(key_get, "termination_id"); + if ((info = + (char *)sym_get(keys, key_get)) == NULL) { + test_result(tag, NULL, NULL, + "no_Term_Id", keys); + } else { + if (strcmp(info, "0")) { + test_result(tag, NULL, NULL, + "fail", keys); + } else { + test_result(tag, NULL, NULL, + "pass", keys); + } + } + } + } else { + cuts_report(ctag, keys, info, tag); + } } - /* Getting here indicates that there were no fatal driver-level - * errors. Do the kind of reporting requested by the test. + /* + * Extended Format: + * - tcid+tc = "!" + * - tab separated fields + * - no field widths + * - fields 6 - ~ are: + * start-time (time_t) + * duration + * termination_id + * termination_type + * Start Line (of test results in output file) + * End Line */ - if (strncmp(info, "none", 4) == 0) { - /* - * If analysis is 'none', alway report the test as - * a pass regardless of output or exit status. - */ - test_result(tag, NULL, NULL, "pass", keys); - - } else if (strncmp(info, "cuts", 4)) { - - /* - * If analysis is not cuts, assume it is 'exit', thus - * the termination_id is used to determine pass/fail result. - */ - if (strcmp(tt, "timeout")) { - strcpy(key_get, "termination_id"); - if ((info = (char *)sym_get(keys, key_get)) == NULL) { - test_result(tag, NULL, NULL, "no_Term_Id", keys); - } else { - if (strcmp(info, "0")) { - test_result(tag, NULL, NULL, "fail", keys); - } else { - test_result(tag, NULL, NULL, "pass", keys); - } + if (extended) { + + strcpy(key_get, "termination_id"); + if ((ti = (char *)sym_get(keys, key_get)) == NULL) { + ti = "No_Termination_ID"; } - } - } else { - cuts_report(ctag, keys, info, tag); - } - } - - /* - * Extended Format: - * - tcid+tc = "!" - * - tab separated fields - * - no field widths - * - fields 6 - ~ are: - * start-time (time_t) - * duration - * termination_id - * termination_type - * Start Line (of test results in output file) - * End Line - */ - - if (extended) { - - strcpy(key_get, "termination_id"); - if ((ti = (char *)sym_get(keys, key_get)) == NULL) { - ti = "No_Termination_ID"; - } - strcpy(key_get, "termination_type"); - if ((tt = (char *)sym_get(keys, key_get)) == NULL) { - tt = "No_Termination_Type"; - } + strcpy(key_get, "termination_type"); + if ((tt = (char *)sym_get(keys, key_get)) == NULL) { + tt = "No_Termination_Type"; + } - strcpy(key_get, "duration"); - if ((duration = (char *)sym_get(keys, key_get)) == NULL) { - duration = "No_Duration"; - } + strcpy(key_get, "duration"); + if ((duration = (char *)sym_get(keys, key_get)) == NULL) { + duration = "No_Duration"; + } - strcpy(key_get, "_Start_line"); - if ((sl = (char *)sym_get(keys, key_get)) == NULL) { - sl = "No_Start_line"; - } + strcpy(key_get, "_Start_line"); + if ((sl = (char *)sym_get(keys, key_get)) == NULL) { + sl = "No_Start_line"; + } - strcpy(key_get, "_End_line"); - if ((el = (char *)sym_get(keys, key_get)) == NULL) { - el = "No_End_line"; - } + strcpy(key_get, "_End_line"); + if ((el = (char *)sym_get(keys, key_get)) == NULL) { + el = "No_End_line"; + } - strcpy(key_get, "contacts"); - if ((contact = (char *)sym_get(keys, key_get)) == NULL) { - contact = "No_Contacts"; - } + strcpy(key_get, "contacts"); + if ((contact = (char *)sym_get(keys, key_get)) == NULL) { + contact = "No_Contacts"; + } - strcpy(key_get, "stime"); - if ((mystime = (char *)sym_get(keys, key_get)) == NULL) { - mystime = "No_stime"; - } + strcpy(key_get, "stime"); + if ((mystime = (char *)sym_get(keys, key_get)) == NULL) { + mystime = "No_stime"; + } - printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n", - tag, "!", "!", is, contact, mystime, duration, - ti, tt, sl, el); - } + printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n", + tag, "!", "!", is, contact, mystime, duration, + ti, tt, sl, el); + } - return 0; + return 0; } /* * Print a header made up of the RTS keywords * In "extended" mode, print the header to stderr. */ -int -print_header(tags) - SYM tags; +int print_header(tags) +SYM tags; { - DBT Key, Data; - char key_get[255]; - - FILE *out; - - extern int extended; - - if (extended) - out = stderr; - else - out = stdout; - - fprintf(out, "System Configuration:\n"); - /* build header out of RTS keywords */ - sprintf(key_get, "_RTS"); - Key.data = (void *) key_get; - if (sym_seq(tags, &Key, &Data, R_CURSOR) == 0) { - do { - if (strcmp((char *)Key.data, "PATH")==0) - continue; - fprintf(out, "%-20.20s %s\n", (char *)Key.data, (char *)Data.data); - } while (sym_seq(tags, &Key, &Data, R_NEXT)==0); - } + DBT Key, Data; + char key_get[255]; + + FILE *out; + + extern int extended; + + if (extended) + out = stderr; + else + out = stdout; + + fprintf(out, "System Configuration:\n"); + /* build header out of RTS keywords */ + sprintf(key_get, "_RTS"); + Key.data = (void *)key_get; + if (sym_seq(tags, &Key, &Data, R_CURSOR) == 0) { + do { + if (strcmp((char *)Key.data, "PATH") == 0) + continue; + fprintf(out, "%-20.20s %s\n", (char *)Key.data, + (char *)Data.data); + } while (sym_seq(tags, &Key, &Data, R_NEXT) == 0); + } - fprintf(out, "\n"); - fprintf(out, FORMAT, "tag", "tcid", "testcase", "status", "contact"); - fprintf(out, "-------------------------------------------------------------------------------\n"); + fprintf(out, "\n"); + fprintf(out, FORMAT, "tag", "tcid", "testcase", "status", "contact"); + fprintf(out, + "-------------------------------------------------------------------------------\n"); - return 0; + return 0; } /* @@ -418,71 +422,66 @@ print_header(tags) * This is passed s SYM for the current tag and the initiation keys. * The text seen by lex is in yytext (global). */ -int -cuts_testcase(tag, keys) - SYM tag, keys; +int cuts_testcase(tag, keys) +SYM tag, keys; { - char *cuts_info[5]; - char key[KEYSIZE]; - char *oldresult, *newresult, *worst_case(); - int tok_num=0; - extern char yytext[]; + char *cuts_info[5]; + char key[KEYSIZE]; + char *oldresult, *newresult, *worst_case(); + int tok_num = 0; + extern char yytext[]; - cuts_info[tok_num] = strtok(yytext, "\t "); - while (tok_num < 5 && - (cuts_info[++tok_num] = strtok(NULL, "\t ")) != NULL ) - ; + cuts_info[tok_num] = strtok(yytext, "\t "); + while (tok_num < 5 && + (cuts_info[++tok_num] = strtok(NULL, "\t ")) != NULL) ; - strcpy(key, cuts_info[0]); - strcat(key, ","); - strcat(key, cuts_info[1]); + strcpy(key, cuts_info[0]); + strcat(key, ","); + strcat(key, cuts_info[1]); #ifdef DEBUGGING - DEBUG(D_SCAN_CUTS, 1) { - printf("cuts_testcase: TCID=%s TC=%s Result=%s\n", cuts_info[0], - cuts_info[1], cuts_info[2]); - printf("cuts_testcase: %d %s\n", tok_num, key); - } + DEBUG(D_SCAN_CUTS, 1) { + printf("cuts_testcase: TCID=%s TC=%s Result=%s\n", cuts_info[0], + cuts_info[1], cuts_info[2]); + printf("cuts_testcase: %d %s\n", tok_num, key); + } #endif - if ((oldresult=(char *)sym_get(tag, key)) != NULL) { - /* Duplicate -- assume mulitple runs */ - /* keep "worst case" */ - newresult = worst_case(oldresult, cuts_info[2]); - sym_put(tag, key, strdup(newresult), PUT_REPLACE); - free(oldresult); /* remove the "data" portion of the key */ - } else { - sym_put(tag, key, strdup(cuts_info[2]), 0); - } - return 0; + if ((oldresult = (char *)sym_get(tag, key)) != NULL) { + /* Duplicate -- assume mulitple runs */ + /* keep "worst case" */ + newresult = worst_case(oldresult, cuts_info[2]); + sym_put(tag, key, strdup(newresult), PUT_REPLACE); + free(oldresult); /* remove the "data" portion of the key */ + } else { + sym_put(tag, key, strdup(cuts_info[2]), 0); + } + return 0; } /* * Determine a "worst case" status from two given statuses. */ -static char * -worst_case(t1, t2) - char *t1, *t2; +static char *worst_case(t1, t2) +char *t1, *t2; { - /* NULL-terminated table, ordered from worst-case to best-case */ - static char *worst[] = { - "FAIL", "BROK", "PASS", "CONF", - "WARN", "INFO", NULL, - }; - - char **w1, **w2; - - /* Search the table for each status, then use the index to determine - which has a lower precedence */ - for (w1=worst; *w1 != NULL && strcmp(t1,*w1); w1++) - ; - - for (w2=worst; *w2 != NULL && strcmp(t2,*w2); w2++) - ; - - if (w1 < w2) - return(t1); - else - return(t2); + /* NULL-terminated table, ordered from worst-case to best-case */ + static char *worst[] = { + "FAIL", "BROK", "PASS", "CONF", + "WARN", "INFO", NULL, + }; + + char **w1, **w2; + + /* Search the table for each status, then use the index to determine + which has a lower precedence */ + for (w1 = worst; *w1 != NULL && strcmp(t1, *w1); w1++) ; + + for (w2 = worst; *w2 != NULL && strcmp(t2, *w2); w2++) ; + + if (w1 < w2) + return (t1); + else + return (t2); } diff --git a/pan/zoolib.c b/pan/zoolib.c index f7343e4bc..568ffffaf 100644 --- a/pan/zoolib.c +++ b/pan/zoolib.c @@ -50,7 +50,7 @@ */ #include -#include /* for getenv */ +#include /* for getenv */ #include #include "zoolib.h" @@ -58,8 +58,8 @@ char zoo_error[ZELEN]; #ifdef __linux__ /* glibc2.2 definition needs -D_XOPEN_SOURCE, which breaks other things. */ -extern int sighold (int __sig); -extern int sigrelse (int __sig); +extern int sighold(int __sig); +extern int sigrelse(int __sig); #endif /* zoo_mark(): private function to make an entry to the zoo @@ -70,406 +70,378 @@ static int zoo_unlock(zoo_t z); /* cat_args(): helper function to make cmdline from argc, argv */ char *cat_args(int argc, char **argv); - /* zoo_getname(): create a filename to use for the zoo */ -char * -zoo_getname() +char *zoo_getname() { - char buf[1024]; - char *zoo; - - zoo = getenv( "ZOO" ); - if (zoo) { - snprintf(buf, 1024, "%s/%s", zoo, "active"); - return strdup(buf); - } else { - /* if there is no environment variable, we don't know where to put it */ - return NULL; - } -} - + char buf[1024]; + char *zoo; -/* zoo_open(): open a zoo for use */ -zoo_t -zoo_open(char *zooname) -{ - zoo_t new_zoo; - - new_zoo = (zoo_t)fopen(zooname, "r+"); - if (!new_zoo) { - if (errno == ENOENT) { - /* file doesn't exist, try fopen(xxx, "a+") */ - new_zoo = (zoo_t)fopen(zooname, "a+"); - if (!new_zoo) { - /* total failure */ - snprintf(zoo_error, ZELEN, - "Could not open zoo as \"%s\", errno:%d %s", - zooname, errno, strerror(errno)); - return 0; - } - fclose(new_zoo); - new_zoo = fopen(zooname, "r+"); + zoo = getenv("ZOO"); + if (zoo) { + snprintf(buf, 1024, "%s/%s", zoo, "active"); + return strdup(buf); } else { - snprintf(zoo_error, ZELEN, - "Could not open zoo as \"%s\", errno:%d %s", - zooname, errno, strerror(errno)); + /* if there is no environment variable, we don't know where to put it */ + return NULL; } - } - return new_zoo; } -int -zoo_close(zoo_t z) +/* zoo_open(): open a zoo for use */ +zoo_t zoo_open(char *zooname) { - int ret; - - ret = fclose(z); - if (ret) { - snprintf(zoo_error, ZELEN, - "closing zoo caused error, errno:%d %s", - errno, strerror(errno)); - } - return ret; + zoo_t new_zoo; + + new_zoo = (zoo_t) fopen(zooname, "r+"); + if (!new_zoo) { + if (errno == ENOENT) { + /* file doesn't exist, try fopen(xxx, "a+") */ + new_zoo = (zoo_t) fopen(zooname, "a+"); + if (!new_zoo) { + /* total failure */ + snprintf(zoo_error, ZELEN, + "Could not open zoo as \"%s\", errno:%d %s", + zooname, errno, strerror(errno)); + return 0; + } + fclose(new_zoo); + new_zoo = fopen(zooname, "r+"); + } else { + snprintf(zoo_error, ZELEN, + "Could not open zoo as \"%s\", errno:%d %s", + zooname, errno, strerror(errno)); + } + } + return new_zoo; } - -static int -zoo_mark(zoo_t z, char *entry) +int zoo_close(zoo_t z) { - FILE *fp = (FILE *)z; - int found = 0; - long pos; - char buf[BUFLEN]; + int ret; - if (fp == NULL) - return -1; - - if (zoo_lock(z)) - return -1; - - /* first fit */ - rewind(fp); + ret = fclose(z); + if (ret) { + snprintf(zoo_error, ZELEN, + "closing zoo caused error, errno:%d %s", + errno, strerror(errno)); + } + return ret; +} - do { - pos = ftell(fp); +static int zoo_mark(zoo_t z, char *entry) +{ + FILE *fp = (FILE *) z; + int found = 0; + long pos; + char buf[BUFLEN]; - if (fgets(buf, BUFLEN, fp) == NULL) - break; + if (fp == NULL) + return -1; - if (buf[0] == '#') { - rewind(fp); - if (fseek(fp, pos, SEEK_SET)) { - /* error */ - snprintf(zoo_error, ZELEN, - "seek error while writing to zoo file, errno:%d %s", - errno, strerror(errno)); + if (zoo_lock(z)) return -1; - } - /* write the entry, left justified, and padded/truncated to the - * same size as the previous entry */ - fprintf(fp, "%-*.*s\n", (int)strlen(buf)-1, (int)strlen(buf)-1, entry); - found = 1; - break; - } - } while (1); - - if (!found) { - if (fseek(fp, 0, SEEK_END)) { - snprintf(zoo_error, ZELEN, - "error seeking to end of zoo file, errno:%d %s", - errno, strerror(errno)); - return -1; + + /* first fit */ + rewind(fp); + + do { + pos = ftell(fp); + + if (fgets(buf, BUFLEN, fp) == NULL) + break; + + if (buf[0] == '#') { + rewind(fp); + if (fseek(fp, pos, SEEK_SET)) { + /* error */ + snprintf(zoo_error, ZELEN, + "seek error while writing to zoo file, errno:%d %s", + errno, strerror(errno)); + return -1; + } + /* write the entry, left justified, and padded/truncated to the + * same size as the previous entry */ + fprintf(fp, "%-*.*s\n", (int)strlen(buf) - 1, + (int)strlen(buf) - 1, entry); + found = 1; + break; + } + } while (1); + + if (!found) { + if (fseek(fp, 0, SEEK_END)) { + snprintf(zoo_error, ZELEN, + "error seeking to end of zoo file, errno:%d %s", + errno, strerror(errno)); + return -1; + } + fprintf(fp, "%-*.*s\n", 79, 79, entry); } - fprintf(fp, "%-*.*s\n", 79, 79, entry); - } - fflush(fp); + fflush(fp); - if (zoo_unlock(z)) - return -1; - return 0; + if (zoo_unlock(z)) + return -1; + return 0; } -int -zoo_mark_cmdline(zoo_t z, pid_t p, char *tag, char *cmdline) +int zoo_mark_cmdline(zoo_t z, pid_t p, char *tag, char *cmdline) { - char new_entry[BUFLEN]; + char new_entry[BUFLEN]; - snprintf(new_entry, 80, "%d,%s,%s", p, tag, cmdline); - return zoo_mark(z, new_entry); + snprintf(new_entry, 80, "%d,%s,%s", p, tag, cmdline); + return zoo_mark(z, new_entry); } -int -zoo_mark_args(zoo_t z, pid_t p, char *tag, int ac, char **av) +int zoo_mark_args(zoo_t z, pid_t p, char *tag, int ac, char **av) { - char *cmdline; - int ret; + char *cmdline; + int ret; - cmdline = cat_args(ac, av); - ret = zoo_mark_cmdline(z, p, tag, cmdline); + cmdline = cat_args(ac, av); + ret = zoo_mark_cmdline(z, p, tag, cmdline); - free(cmdline); - return ret; + free(cmdline); + return ret; } -int -zoo_clear(zoo_t z, pid_t p) +int zoo_clear(zoo_t z, pid_t p) { - FILE *fp = (FILE *)z; - long pos; - char buf[BUFLEN]; - pid_t that_pid; - int found = 0; - - - if (fp == NULL) - return -1; - - if (zoo_lock(z)) - return -1; - rewind(fp); + FILE *fp = (FILE *) z; + long pos; + char buf[BUFLEN]; + pid_t that_pid; + int found = 0; - do { - pos = ftell(fp); - - if (fgets(buf, BUFLEN, fp) == NULL) - break; + if (fp == NULL) + return -1; - if (buf[0] == '#') - continue; + if (zoo_lock(z)) + return -1; + rewind(fp); + + do { + pos = ftell(fp); + + if (fgets(buf, BUFLEN, fp) == NULL) + break; + + if (buf[0] == '#') + continue; + + that_pid = atoi(buf); + if (that_pid == p) { + if (fseek(fp, pos, SEEK_SET)) { + /* error */ + snprintf(zoo_error, ZELEN, + "seek error while writing to zoo file, errno:%d %s", + errno, strerror(errno)); + return -1; + } + if (ftell(fp) != pos) { + printf("fseek failed\n"); + } + fputs("#", fp); + found = 1; + break; + } + } while (1); + + fflush(fp); + + /* FIXME: unlock zoo file */ + if (zoo_unlock(z)) + return -1; - that_pid = atoi(buf); - if (that_pid == p) { - if (fseek(fp, pos, SEEK_SET)) { - /* error */ + if (!found) { snprintf(zoo_error, ZELEN, - "seek error while writing to zoo file, errno:%d %s", - errno, strerror(errno)); - return -1; - } - if (ftell(fp) != pos) { - printf("fseek failed\n"); - } - fputs("#", fp); - found = 1; - break; + "zoo_clear() did not find pid(%d)", p); + return 1; } - } while (1); - - fflush( fp ); - - /* FIXME: unlock zoo file */ - if (zoo_unlock(z)) - return -1; - - if (!found) { - snprintf(zoo_error, ZELEN, - "zoo_clear() did not find pid(%d)", - p); - return 1; - } - return 0; + return 0; } -pid_t -zoo_getpid(zoo_t z, char *tag) +pid_t zoo_getpid(zoo_t z, char *tag) { - FILE *fp = (FILE *)z; - char buf[BUFLEN], *s; - pid_t this_pid = -1; + FILE *fp = (FILE *) z; + char buf[BUFLEN], *s; + pid_t this_pid = -1; + if (fp == NULL) + return -1; - if (fp == NULL) - return -1; - - if (zoo_lock(z)) - return -1; + if (zoo_lock(z)) + return -1; - rewind(fp); - do { - if (fgets(buf, BUFLEN, fp) == NULL) - break; + rewind(fp); + do { + if (fgets(buf, BUFLEN, fp) == NULL) + break; - if (buf[0] == '#') - continue; /* recycled line */ + if (buf[0] == '#') + continue; /* recycled line */ - if ((s = strchr(buf, ',')) == NULL) - continue; /* line was not expected format */ + if ((s = strchr(buf, ',')) == NULL) + continue; /* line was not expected format */ - if (strncmp(s+1, tag, strlen(tag))) - continue; /* tag does not match */ + if (strncmp(s + 1, tag, strlen(tag))) + continue; /* tag does not match */ - this_pid = atoi(buf); - break; - } while (1); + this_pid = atoi(buf); + break; + } while (1); - if (zoo_unlock(z)) - return -1; - return this_pid; + if (zoo_unlock(z)) + return -1; + return this_pid; } -int -zoo_lock(zoo_t z) +int zoo_lock(zoo_t z) { - FILE *fp = (FILE *)z; - struct flock zlock; - sigset_t block_these; - int ret; - - if (fp == NULL) - return -1; - - zlock.l_whence = zlock.l_start = zlock.l_len = 0; - zlock.l_type = F_WRLCK; - - sigemptyset(&block_these); - sigaddset(&block_these, SIGINT); - sigaddset(&block_these, SIGTERM); - sigaddset(&block_these, SIGHUP); - sigaddset(&block_these, SIGUSR1); - sigaddset(&block_these, SIGUSR2); - sigprocmask(SIG_BLOCK, &block_these, NULL); - - do { - ret = fcntl(fileno(fp), F_SETLKW, &zlock); - } while (ret == -1 && errno == EINTR); - - sigprocmask(SIG_UNBLOCK, &block_these, NULL); - if (ret == -1) { - snprintf(zoo_error, ZELEN, - "failed to unlock zoo file, errno:%d %s", - errno, strerror(errno)); - return -1; - } - return 0; + FILE *fp = (FILE *) z; + struct flock zlock; + sigset_t block_these; + int ret; -} + if (fp == NULL) + return -1; + + zlock.l_whence = zlock.l_start = zlock.l_len = 0; + zlock.l_type = F_WRLCK; + + sigemptyset(&block_these); + sigaddset(&block_these, SIGINT); + sigaddset(&block_these, SIGTERM); + sigaddset(&block_these, SIGHUP); + sigaddset(&block_these, SIGUSR1); + sigaddset(&block_these, SIGUSR2); + sigprocmask(SIG_BLOCK, &block_these, NULL); + + do { + ret = fcntl(fileno(fp), F_SETLKW, &zlock); + } while (ret == -1 && errno == EINTR); + + sigprocmask(SIG_UNBLOCK, &block_these, NULL); + if (ret == -1) { + snprintf(zoo_error, ZELEN, + "failed to unlock zoo file, errno:%d %s", + errno, strerror(errno)); + return -1; + } + return 0; -int -zoo_unlock(zoo_t z) -{ - FILE *fp = (FILE *)z; - struct flock zlock; - sigset_t block_these; - int ret; - - if (fp == NULL) - return -1; - - zlock.l_whence = zlock.l_start = zlock.l_len = 0; - zlock.l_type = F_UNLCK; - - sigemptyset(&block_these); - sigaddset(&block_these, SIGINT); - sigaddset(&block_these, SIGTERM); - sigaddset(&block_these, SIGHUP); - sigaddset(&block_these, SIGUSR1); - sigaddset(&block_these, SIGUSR2); - sigprocmask(SIG_BLOCK, &block_these, NULL); - - do { - ret = fcntl(fileno(fp), F_SETLKW, &zlock); - } while (ret == -1 && errno == EINTR); - - sigprocmask(SIG_UNBLOCK, &block_these, NULL); - - if (ret == -1) { - snprintf(zoo_error, ZELEN, - "failed to lock zoo file, errno:%d %s", - errno, strerror(errno)); - return -1; - } - return 0; } -char * -cat_args(int argc, char **argv) +int zoo_unlock(zoo_t z) { - int a, size; - char *cmd; - - for (size = a = 0; a < argc; a++) { - size += strlen(argv[a]); - size++; - } - - if ((cmd = (char *)malloc(size)) == NULL) { - snprintf(zoo_error, ZELEN, - "Malloc Error, %s/%d", - __FILE__, __LINE__); - return NULL; - } - - *cmd='\0'; - for (a = 0; a < argc ; a++) { - if (a != 0) - strcat(cmd, " "); - strcat(cmd, argv[a]); - } - - return cmd; -} + FILE *fp = (FILE *) z; + struct flock zlock; + sigset_t block_these; + int ret; -#if defined(UNIT_TEST) + if (fp == NULL) + return -1; + zlock.l_whence = zlock.l_start = zlock.l_len = 0; + zlock.l_type = F_UNLCK; -void -zt_add(zoo_t z, int n) -{ - char cmdline[200]; - char tag[10]; + sigemptyset(&block_these); + sigaddset(&block_these, SIGINT); + sigaddset(&block_these, SIGTERM); + sigaddset(&block_these, SIGHUP); + sigaddset(&block_these, SIGUSR1); + sigaddset(&block_these, SIGUSR2); + sigprocmask(SIG_BLOCK, &block_these, NULL); + + do { + ret = fcntl(fileno(fp), F_SETLKW, &zlock); + } while (ret == -1 && errno == EINTR); - snprintf(tag, 10, "%s%d", "test", n); - snprintf(cmdline, 200, "%s%d %s %s %s", "runtest", n, "one", "two", "three"); + sigprocmask(SIG_UNBLOCK, &block_these, NULL); - zoo_mark_cmdline(z, n, tag, cmdline); + if (ret == -1) { + snprintf(zoo_error, ZELEN, + "failed to lock zoo file, errno:%d %s", + errno, strerror(errno)); + return -1; + } + return 0; } -int -main(int argc, char *argv[]) +char *cat_args(int argc, char **argv) { + int a, size; + char *cmd; - char *zooname; - zoo_t test_zoo; - char *test_tag = "unittest"; - int i,j; + for (size = a = 0; a < argc; a++) { + size += strlen(argv[a]); + size++; + } + if ((cmd = (char *)malloc(size)) == NULL) { + snprintf(zoo_error, ZELEN, + "Malloc Error, %s/%d", __FILE__, __LINE__); + return NULL; + } - zooname = zoo_getname(); + *cmd = '\0'; + for (a = 0; a < argc; a++) { + if (a != 0) + strcat(cmd, " "); + strcat(cmd, argv[a]); + } - if (!zooname) { - zooname = strdup("test_zoo"); - } - printf("Test zoo filename is %s\n", zooname); + return cmd; +} - if ((test_zoo = zoo_open(zooname)) == NULL) { - printf("Error opennning zoo\n"); - exit(-1); - } +#if defined(UNIT_TEST) +void zt_add(zoo_t z, int n) +{ + char cmdline[200]; + char tag[10]; - zoo_mark_args(test_zoo, getpid(), test_tag, argc, argv); + snprintf(tag, 10, "%s%d", "test", n); + snprintf(cmdline, 200, "%s%d %s %s %s", "runtest", n, "one", "two", + "three"); + zoo_mark_cmdline(z, n, tag, cmdline); +} - for (j = 0; j < 5; j++) { - for (i = 0; i < 20; i++) { - zt_add(test_zoo, i); - } +int main(int argc, char *argv[]) +{ - for (; i >=0; i--) { - zoo_clear(test_zoo, i); - } - } + char *zooname; + zoo_t test_zoo; + char *test_tag = "unittest"; + int i, j; - zoo_clear(test_zoo, getpid()); + zooname = zoo_getname(); + if (!zooname) { + zooname = strdup("test_zoo"); + } + printf("Test zoo filename is %s\n", zooname); - return 0; -} + if ((test_zoo = zoo_open(zooname)) == NULL) { + printf("Error opennning zoo\n"); + exit(-1); + } + zoo_mark_args(test_zoo, getpid(), test_tag, argc, argv); + for (j = 0; j < 5; j++) { + for (i = 0; i < 20; i++) { + zt_add(test_zoo, i); + } + for (; i >= 0; i--) { + zoo_clear(test_zoo, i); + } + } + zoo_clear(test_zoo, getpid()); + return 0; +} #endif -- cgit v1.2.3