Reap N-number of children per SIGCHLD

This commit is contained in:
Lester Hightower 2024-07-02 09:33:43 -04:00
parent 250e2ae24f
commit 5236c8d947

View File

@ -706,41 +706,47 @@ void child_handler(int signum, siginfo_t *info, void *context)
return; return;
} }
int status; while (1) {
pid_t p = waitpid(-1, &status, WNOHANG); int status;
if(p == -1) pid_t p = waitpid(-1, &status, WNOHANG);
{ if(p == 0) // No more children to reap
logerror("waitpid failed when handling child exit\n");
exit(EXIT_FAILURE);
}
int adhocify_exit_code = 0;
if(WIFEXITED(status))
{
adhocify_exit_code = WEXITSTATUS(status);
if(adhocify_exit_code == 127)
{ {
logwrite("command not found, exiting\n"); return; // exits infinite while loop and child_handler() function
exit(adhocify_exit_code);
} }
if(exit_with_child && awaited_child_exit_code > -1) if(p == -1) // waitpid error
{ {
bool must_exit = adhocify_exit_code == awaited_child_exit_code; logerror("waitpid failed when handling child exit\n");
if(negate_child_exit_code) exit(EXIT_FAILURE);
}
int adhocify_exit_code = 0;
if(WIFEXITED(status))
{
adhocify_exit_code = WEXITSTATUS(status);
if(adhocify_exit_code == 127)
{ {
must_exit = !must_exit; logwrite("command not found, exiting\n");
}
if(must_exit)
{
logwrite("command exited with specified exit code, exiting too\n");
exit(adhocify_exit_code); exit(adhocify_exit_code);
} }
if(exit_with_child && awaited_child_exit_code > -1)
{
bool must_exit = adhocify_exit_code == awaited_child_exit_code;
if(negate_child_exit_code)
{
must_exit = !must_exit;
}
if(must_exit)
{
logwrite("command exited with specified exit code, exiting too\n");
exit(adhocify_exit_code);
}
}
}
if(exit_with_child && WIFSIGNALED(status))
{
adhocify_exit_code = 128 + WTERMSIG(status); // copy bash's behaviour
exit(adhocify_exit_code);
} }
}
if(exit_with_child && WIFSIGNALED(status))
{
adhocify_exit_code = 128 + WTERMSIG(status); // copy bash's behaviour
exit(adhocify_exit_code);
} }
} }