diff --git a/src/bozo/test/testproc.c b/src/bozo/test/testproc.c index 6243cc5b8a..06dec34fa1 100644 --- a/src/bozo/test/testproc.c +++ b/src/bozo/test/testproc.c @@ -9,21 +9,80 @@ #include #include +#include +#include +#include static int ignore = 0; static int sleepTime = 10; +static int run = 1; +static char *file = NULL; -sigproc() +void +trim(char *s) +{ + char *p = strchr(s, '\n'); + if (p) { + *p = '\0'; + } +} + +int +readfile(void) +{ + FILE *fp; + int *bogus = NULL; + char buffer[256]; + if (file) { + if ((fp = fopen(file, "r")) == NULL) { + fprintf(stderr, "Unable to open file %s\n", file); + exit(-1); + } + fgets(buffer, sizeof(buffer), fp); + trim(buffer); + if (strncmp(buffer, "sleep ", 6) == 0) { + int t = atoi(buffer + 6); + if (t) { + sleepTime = t; + } + } + if (strcmp(buffer, "run") == 0) { + run = 1; + } + if (strcmp(buffer, "return") == 0) { + run = 0; + sleepTime = 0; + } + if (strcmp(buffer, "exit") == 0) { + exit(1); + } + if (strcmp(buffer, "crash") == 0) { + *bogus = 1; /* intentional */ + exit(2); /* should not reach */ + } + fclose(fp); + } +} + +void +sigproc(int signo) { printf("testproc received signal\n"); if (ignore) - return 0; + return; exit(0); } -main(argc, argv) - int argc; - char **argv; +void +sigreload(int signo) +{ + readfile(); + return; +} + + +int +main(int argc, char **argv) { int i; @@ -43,19 +102,36 @@ main(argc, argv) #endif signal(SIGTERM, sigproc); signal(SIGQUIT, sigproc); + signal(SIGHUP, sigreload); + for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-ignore") == 0) { ignore = 1; } else if (strcmp(argv[i], "-sleep") == 0) { sleepTime = atoi(argv[i + 1]); i++; + } else if (strcmp(argv[i], "-file") == 0) { + file = argv[i + 1]; + i++; } else { printf("unrecognized option '%s', try one of\n", argv[i]); printf("-ignore ignore SIGTERM signal\n"); printf("-sleep sleep N seconds before exiting\n"); - exit(1); + printf("-file read file for next action\n"); + return 1; } } - sleep(sleepTime); - exit(0); + + while (run) { + if (file) { + readfile(); + } else { + run = 0; + } + if (sleepTime) { + sleep(sleepTime); + } + } + + return 0; }