Skip to content

Commit

Permalink
Shell: Do not bail early when printing jobs if waitpid() fails
Browse files Browse the repository at this point in the history
This fixes running `jobs` in a child process.
Also makes sure that stdout is flushed when writing jobs out.
  • Loading branch information
alimpfard authored and awesomekling committed Oct 29, 2020
1 parent a935a31 commit a46318d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Shell/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ bool Job::print_status(PrintStatusMode mode)
{
int wstatus;
auto rc = waitpid(m_pid, &wstatus, WNOHANG);
if (rc == -1) {
perror("waitpid");
return false;
}
auto status = "running";

if (rc != 0) {
if (rc > 0) {
if (WIFEXITED(wstatus))
status = "exited";

Expand All @@ -52,6 +48,15 @@ bool Job::print_status(PrintStatusMode mode)

if (WIFSIGNALED(wstatus))
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 = '-';
Expand All @@ -72,6 +77,7 @@ bool Job::print_status(PrintStatusMode mode)
outln("[{}] {} {} {} {} {}", m_job_id, background_indicator, m_pid, m_pgid, status, command);
break;
}
fflush(stdout);

return true;
}
Expand Down

0 comments on commit a46318d

Please sign in to comment.