From 48ef50f6c33d7fc7cfadf26fe0dc7034ea93a4f0 Mon Sep 17 00:00:00 2001 From: ltfetch Date: Sun, 26 Oct 2014 20:40:24 -0500 Subject: [PATCH] use pipe to block process exit until all transitive children exit --- src/faketime.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/faketime.c b/src/faketime.c index ab09407..1a31ee6 100644 --- a/src/faketime.c +++ b/src/faketime.c @@ -198,6 +198,8 @@ int main (int argc, char **argv) /* simply pass format string along */ setenv("FAKETIME", argv[curr_opt], true); } + int keepalive_fds[2]; + (void) (pipe(keepalive_fds) + 1); /* we just consumed the timestamp option */ curr_opt++; @@ -334,6 +336,7 @@ int main (int argc, char **argv) /* run command and clean up shared objects */ if (0 == (child_pid = fork())) { + close(keepalive_fds[0]); /* only parent needs to read this */ if (EXIT_SUCCESS != execvp(argv[curr_opt], &argv[curr_opt])) { perror("Running specified command failed"); @@ -343,7 +346,10 @@ int main (int argc, char **argv) else { int ret; + char buf; + close(keepalive_fds[1]); /* only children need keep this open */ waitpid(child_pid, &ret, 0); + (void) (read(keepalive_fds[0], &buf, 1) + 1); /* reads 0B when all children exit */ cleanup_shobjs(); if (WIFSIGNALED(ret)) {