diff options
-rw-r--r-- | piper.c | 67 |
1 files changed, 57 insertions, 10 deletions
@@ -1,5 +1,7 @@ +#include <err.h> #include <errno.h> #include <signal.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -24,27 +26,72 @@ volatile sig_atomic_t terminate = 0; volatile sig_atomic_t sendsignal = 0; void +reap_all() +{ + int i; + for (i = 0; i < NUM_PROCS; i++) { + if (procs[i].pid != -1) + kill(procs[i].pid, SIGTERM); + } + + sleep(5); + + for (i = 0; i < NUM_PROCS; i++) { + if (procs[i].pid != -1) + kill(procs[i].pid, SIGKILL); + } +} + +void +fail(int exit, const char *msg, ...) +{ + reap_all(); + + va_list args; + va_start(args, msg); + verr(exit, msg, args); + va_end(args); // Not reached? +} + +void +failx(int exit, const char *msg, ...) +{ + reap_all(); + + va_list args; + va_start(args, msg); + verrx(exit, msg, args); + va_end(args); // Not reached? +} + +void gen_argv(struct proc *proc) { int i; - char *saveptr, *tmp; - char **argv = malloc(sizeof(char*) * 1024); - char **head = argv; - char *cmd2 = strdup(proc->cmd); - // XXX: Check malloc/strdup return - // XXX: Check for overflow in tmp - + char *saveptr, *tmp, *cmd; + char **argv, **head; + + // XXX: Check for overflow in argv + argv = malloc(sizeof(char*) * 1024); + if (argv == NULL) + fail(1, "malloc"); + + cmd = strdup(proc->cmd); + if (cmd == NULL) + fail(1, "strdup"); + + head = argv; for (i = 0; i < 1024; i++) argv[i] = NULL; - tmp = strtok_r(cmd2, " \t", &saveptr); + tmp = strtok_r(cmd, " \t", &saveptr); while (tmp != NULL) { *(head++) = strdup(tmp); tmp = strtok_r(NULL, " \t", &saveptr); } *head = NULL; - free(cmd2); + free(cmd); proc->argv = argv; } @@ -72,7 +119,7 @@ run_cmd(struct proc *proc) close(fds[1]); execvp(proc->argv[0], proc->argv); - perror("execvp"); + fail(1, "execvp"); } else if (cpid == -1) { // error D: return -1; |