1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

Shell: Do not bail early when printing jobs if waitpid() fails

This fixes running `jobs` in a child process.
Also makes sure that stdout is flushed when writing jobs out.
This commit is contained in:
AnotherTest 2020-10-28 09:46:09 +03:30 committed by Andreas Kling
parent a935a31ecf
commit a46318d414

View file

@ -37,13 +37,9 @@ bool Job::print_status(PrintStatusMode mode)
{ {
int wstatus; int wstatus;
auto rc = waitpid(m_pid, &wstatus, WNOHANG); auto rc = waitpid(m_pid, &wstatus, WNOHANG);
if (rc == -1) {
perror("waitpid");
return false;
}
auto status = "running"; auto status = "running";
if (rc != 0) { if (rc > 0) {
if (WIFEXITED(wstatus)) if (WIFEXITED(wstatus))
status = "exited"; status = "exited";
@ -52,6 +48,15 @@ bool Job::print_status(PrintStatusMode mode)
if (WIFSIGNALED(wstatus)) if (WIFSIGNALED(wstatus))
status = "signaled"; status = "signaled";
} else if (rc < 0) {
// We couldn't waitpid() it, probably because we're not the parent shell.
// just use the old information.
if (exited())
status = "exited";
else if (m_is_suspended)
status = "stopped";
else if (signaled())
status = "signaled";
} }
char background_indicator = '-'; char background_indicator = '-';
@ -72,6 +77,7 @@ bool Job::print_status(PrintStatusMode mode)
outln("[{}] {} {} {} {} {}", m_job_id, background_indicator, m_pid, m_pgid, status, command); outln("[{}] {} {} {} {} {}", m_job_id, background_indicator, m_pid, m_pgid, status, command);
break; break;
} }
fflush(stdout);
return true; return true;
} }