aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsin <sin@2f30.org>2014-02-07 11:14:38 +0000
committersin <sin@2f30.org>2014-02-07 11:14:38 +0000
commitfc4fd37ba81aa101b9790a875511456b4e57d66c (patch)
tree4d4f68277c8f70126fdcc2c6db905ffdcee5738c
parenteae5382f6ac183086944ff964eeaa95fd7dbe936 (diff)
Use the double fork trick to properly reap spawned processes
-rw-r--r--sinit.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/sinit.c b/sinit.c
index 374a59d..b96ef1e 100644
--- a/sinit.c
+++ b/sinit.c
@@ -34,7 +34,7 @@ main(void)
sigset_t set;
pid_t pid;
fd_set rfds;
- int c, fd, n;
+ int status, fd, n;
if (getpid() != 1)
return EXIT_FAILURE;
@@ -48,7 +48,7 @@ main(void)
return EXIT_FAILURE;
if (pid > 0)
for (;;)
- wait(&c);
+ wait(&status);
sigprocmask(SIG_UNBLOCK, &set, 0);
@@ -102,17 +102,24 @@ dispatchcmd(int fd)
static void
spawn(const Arg *arg)
{
+ int status;
pid_t pid;
char *const *p = arg->v;
pid = fork();
- if (pid < 0)
+ if (pid < 0) {
weprintf("sinit: fork:");
- if (pid == 0) {
+ } else if (pid == 0) {
+ pid = fork();
+ if (pid < 0)
+ weprintf("sinit: fork:");
+ else if (pid > 0)
+ exit(0);
setsid();
setpgid(0, 0);
execvp(*p, p);
weprintf("sinit: execvp %s:", p);
_exit(errno == ENOENT ? 127 : 126);
}
+ waitpid(pid, &status, 0);
}