fixed --exit-with-child

This commit is contained in:
Albert S. 2019-04-27 23:00:12 +02:00
parent fe7f6a0cc0
commit 27fdabc591

View File

@ -61,14 +61,14 @@ struct ignorelist *ignorelist_head = NULL;
struct ignorelist **ignorelist_current = &ignorelist_head; struct ignorelist **ignorelist_current = &ignorelist_head;
/* Write once globals. Set from process_arguments*/ /* Write-once globals. Set from process_arguments*/
bool silent = false; bool silent = false;
bool noenv = false; bool noenv = false;
bool fromstdin = false; bool fromstdin = false;
bool forkbombcheck = true; bool forkbombcheck = true;
bool daemonize = false; bool daemonize = false;
bool exit_with_child = false; bool exit_with_child = false;
int exitcode = 0; int awaited_child_exit_code = -1;
uint32_t mask = 0; uint32_t mask = 0;
char *prog = NULL; char *prog = NULL;
@ -530,8 +530,7 @@ void parse_options(int argc, char **argv)
exit_with_child = true; exit_with_child = true;
if(optarg) if(optarg)
{ {
exitcode = atoi(optarg); awaited_child_exit_code = atoi(optarg);
} }
break; break;
} }
@ -637,11 +636,26 @@ void child_handler(int signum, siginfo_t *info, void *context)
} }
if(exit_with_child) if(exit_with_child)
{ {
if(status == exitcode) int adhocify_exit_code = 0;
if(WIFEXITED(status))
{ {
logwrite("child exited with specified exit code, exiting too"); adhocify_exit_code = WEXITSTATUS(status);
exit(exitcode); if(awaited_child_exit_code > -1)
{
if(adhocify_exit_code == awaited_child_exit_code)
{
logwrite("child exited with specified exit code, exiting too\n");
exit(adhocify_exit_code);
}
return; //not the exit code we wanted, keep running
}
} }
if(WIFSIGNALED(status))
{
adhocify_exit_code = 128 + WTERMSIG(status); //copy bash's behaviour
}
//TODO: coredump?
exit(adhocify_exit_code);
} }
} }