diff options
Diffstat (limited to 'piper.c')
-rw-r--r-- | piper.c | 66 |
1 files changed, 40 insertions, 26 deletions
@@ -9,7 +9,7 @@ #include <sys/wait.h> #include <unistd.h> -#define NUM_PROCS 2 +//#define NUM_PROCS 2 struct proc { pid_t pid; @@ -18,9 +18,13 @@ struct proc { int _stdin; int _stdout; int _stderr; -} procs[NUM_PROCS]; +} *procs; -int fds[2]; +int num_procs = 0; + +struct fd { + int fds[2]; +} *fds; volatile sig_atomic_t terminate = 0; volatile sig_atomic_t sendsignal = 0; @@ -29,14 +33,14 @@ void reap_all() { int i; - for (i = 0; i < NUM_PROCS; 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++) { + for (i = 0; i < num_procs; i++) { if (procs[i].pid != -1) kill(procs[i].pid, SIGKILL); } @@ -142,8 +146,12 @@ run_cmd(struct proc *proc) if (dup2(proc->_stderr, 2) == -1) fail(1, "dup2"); } - close(fds[0]); // These aren't very important if they fail - close(fds[1]); + + //int i; + //for (i = 0; i < num_procs - 1; i++) { + // close(fds[i].fds[0]); // These aren't very important if they fail + // close(fds[i].fds[1]); + //} execvp(proc->argv[0], proc->argv); @@ -159,7 +167,7 @@ int have_children() { int i; - for (i = 0; i < NUM_PROCS; i++) { + for (i = 0; i < num_procs; i++) { if (procs[i].pid != -1) return 1; } @@ -171,7 +179,7 @@ struct proc * proc_by_pid(pid_t pid) { int i; - for (i = 0; i < NUM_PROCS; i++) { + for (i = 0; i < num_procs; i++) { if (procs[i].pid == pid) return &procs[i]; } @@ -183,7 +191,7 @@ void signal_procs() { int i; - for (i = 0; i < NUM_PROCS; i++) { + for (i = 0; i < num_procs; i++) { if (procs[i].pid != -1) kill(procs[i].pid, sendsignal); //XXX: Check return? } @@ -231,7 +239,7 @@ void start_missing_procs() { int i; - for (i = 0; i < NUM_PROCS; i++) { + for (i = 0; i < num_procs; i++) { if (procs[i].pid == -1) run_cmd(&procs[i]); } @@ -241,7 +249,7 @@ void print_argv() { int i; - for (i = 0; i < NUM_PROCS; i++) { + for (i = 0; i < num_procs; i++) { char **tmp = procs[i].argv; while (*tmp) @@ -254,23 +262,23 @@ print_argv() int main(int argc, char **argv) { + int i; + if (argc < 2) { - fprintf(stderr, "Usage: piper <cmd1> <cmd2>\n"); + fprintf(stderr, "Usage: piper <cmd1> <cmd2> …\n"); return 1; } setup_signals(); - procs[0].cmd = strdup(argv[1]); - if (procs[0].cmd == NULL) - err(1, "strdup"); + num_procs = argc - 1; + procs = malloc(sizeof(struct proc) * num_procs); - procs[1].cmd = strdup(argv[2]); - if (procs[1].cmd == NULL) - err(1, "strdup"); + for (i = 0; i < num_procs; i++) { + procs[i].cmd = strdup(argv[i + 1]); + if (procs[i].cmd == NULL) + err(1, "strdup"); - int i; - for (i = 0; i < NUM_PROCS; i++) { procs[i].pid = -1; procs[i]._stdin = -1; procs[i]._stdout = -1; @@ -278,12 +286,18 @@ main(int argc, char **argv) gen_argv(&procs[i]); } - if (pipe(fds) == -1) - err(1, "pipe"); + fds = malloc(sizeof(struct fd) * (num_procs - 1)); + if (fds == NULL) + err(1, "malloc"); - procs[0]._stdout = fds[1]; - procs[0]._stderr = fds[1]; - procs[1]._stdin = fds[0]; + for (i = 0; i < num_procs - 1; i++) { + if (pipe(fds[i].fds) == -1) + err(1, "pipe"); + + procs[i]._stdout = fds[i].fds[1]; + procs[i]._stderr = fds[i].fds[1]; + procs[i + 1]._stdin = fds[i].fds[0]; + } while (!terminate || have_children()) { pid_t pid; |