use pipe to block process exit until all transitive children exit

This commit is contained in:
ltfetch
2014-10-26 20:40:24 -05:00
parent 035add4eff
commit 48ef50f6c3

View File

@@ -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))
{