summaryrefslogtreecommitdiffstats
path: root/piper.c
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2015-10-29 00:28:48 +0100
committerMarius Halden <marius.h@lden.org>2015-10-29 00:28:48 +0100
commitdceb90521adabea6271947f26f3fe059f95b48e1 (patch)
treeeddde4ea5fa923df0c2562069485239d43b73796 /piper.c
parentd327ae4f04c98337ee63bbcb727d1d6ac87b3734 (diff)
downloadpiper-dceb90521adabea6271947f26f3fe059f95b48e1.tar.gz
piper-dceb90521adabea6271947f26f3fe059f95b48e1.tar.bz2
piper-dceb90521adabea6271947f26f3fe059f95b48e1.tar.xz
Add more error checking
Diffstat (limited to 'piper.c')
-rw-r--r--piper.c53
1 files changed, 32 insertions, 21 deletions
diff --git a/piper.c b/piper.c
index 6eb8ad8..5d47880 100644
--- a/piper.c
+++ b/piper.c
@@ -74,11 +74,11 @@ gen_argv(struct proc *proc)
// XXX: Check for overflow in argv
argv = malloc(sizeof(char*) * 1024);
if (argv == NULL)
- fail(1, "malloc");
+ err(1, "malloc");
cmd = strdup(proc->cmd);
if (cmd == NULL)
- fail(1, "strdup");
+ err(1, "strdup");
head = argv;
for (i = 0; i < 1024; i++)
@@ -96,7 +96,7 @@ gen_argv(struct proc *proc)
proc->argv = argv;
}
-int
+void
run_cmd(struct proc *proc)
{
pid_t cpid;
@@ -104,30 +104,34 @@ run_cmd(struct proc *proc)
cpid = fork();
if (cpid == 0) {
if (proc->_stdin != -1) {
- close(0);
- dup2(proc->_stdin, 0);
+ if (close(0) == -1)
+ fail(1, "close");
+ if (dup2(proc->_stdin, 0) == -1)
+ fail(1, "dup2");
}
if (proc->_stdout != -1) {
- close(1);
- dup2(proc->_stdout, 1);
+ if (close(1) == -1)
+ fail(1, "close");
+ if (dup2(proc->_stdout, 1) == -1)
+ fail(1, "dup2");
}
if (proc->_stderr != -1) {
- close(2);
- dup2(proc->_stderr, 2);
+ if (close(2) == -1)
+ fail(1, "close");
+ if (dup2(proc->_stderr, 2) == -1)
+ fail(1, "dup2");
}
close(fds[0]);
close(fds[1]);
execvp(proc->argv[0], proc->argv);
+
fail(1, "execvp");
} else if (cpid == -1) {
- // error D:
- return -1;
+ fail(1, "fork");
} else {
proc->pid = cpid;
}
-
- return 0;
}
int
@@ -193,13 +197,18 @@ main(int argc, char **argv)
act.sa_sigaction = &handle_fatal;
act.sa_flags = SA_SIGINFO;
- sigaction(SIGINT, &act, NULL);
- sigaction(SIGHUP, &act, NULL);
- sigaction(SIGTERM, &act, NULL);
+ if (sigaction(SIGINT, &act, NULL) == -1)
+ err(1, "sigaction");
+ if (sigaction(SIGHUP, &act, NULL) == -1)
+ err(1, "sigaction");
+ if (sigaction(SIGTERM, &act, NULL) == -1)
+ err(1, "sigaction");
act.sa_sigaction = &handle_nonfatal;
- sigaction(SIGUSR1, &act, NULL);
- sigaction(SIGUSR2, &act, NULL);
+ if (sigaction(SIGUSR1, &act, NULL) == -1)
+ err(1, "sigaction");
+ if (sigaction(SIGUSR2, &act, NULL) == -1)
+ err(1, "sigaction");
procs[0].cmd = strdup(argv[1]);
procs[1].cmd = strdup(argv[2]);
@@ -220,7 +229,7 @@ main(int argc, char **argv)
procs[1]._stdin = fds[0];
run_cmd(&procs[0]);
- run_cmd(&procs[1]); // XXX: Check return codes
+ run_cmd(&procs[1]);
while (have_child()) {
int status;
@@ -233,11 +242,13 @@ main(int argc, char **argv)
if (pid == -1 && errno == EINTR)
continue;
- struct proc *p = proc_by_pid(pid); // XXX: Check return code
+ struct proc *p = proc_by_pid(pid);
+ if (p == NULL)
+ continue; // XXX: Log something here?
p->pid = -1;
if (!terminate)
- run_cmd(p); // XXX: Check return code
+ run_cmd(p);
}
return 0;