From a00fc76b27c92a593b86fa131f1c06c8deec1e16 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 11 Feb 2021 01:21:21 +0900 Subject: [PATCH 1/2] clean up fg dead jobs in trap (Option 3) --- jobs.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/jobs.c b/jobs.c index 2c91fb0e..e8f6b79e 100644 --- a/jobs.c +++ b/jobs.c @@ -1226,8 +1226,32 @@ cleanup_dead_jobs () if (i > js.j_lastj && jobs[i]) INTERNAL_DEBUG(("cleanup_dead_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj)); - if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i)) - delete_job (i, 0); + if (jobs[i] && DEADJOB (i)) + { + if (IS_NOTIFIED (i)) + delete_job (i, 0); + else if (IS_FOREGROUND (i)) + { + /* Usually, foreground dead jobs may be immediately + deleted even when they are not flagged as + `IS_NOTIFIED (i)'. However, signaled ones may be + later notified in `notify_of_job_status ()', so do + not delete them now. The following condition is + copied from `notify_of_job_status ()'. */ + WAIT s; + int termsig; + s = raw_job_exit_status (i); + termsig = WTERMSIG (s); +#if !defined (DONT_REPORT_SIGPIPE) + if (termsig && WIFSIGNALED (s) && termsig != SIGINT) +#else + if (termsig && WIFSIGNALED (s) && termsig != SIGINT && termsig != SIGPIPE) +#endif + continue; + + delete_job (i, 0); + } + } } #if defined (PROCESS_SUBSTITUTION) @@ -2073,7 +2097,8 @@ print_job (job, format, state, job_index) JOB *job; int format, state, job_index; { - if (state == -1 || (JOB_STATE)state == job->state) + if ((state == -1 && !(DEADJOB (job_index) && IS_FOREGROUND (job_index))) || + (JOB_STATE)state == job->state) pretty_print_job (job_index, format, stdout); return (0); } @@ -3283,7 +3308,7 @@ wait_for_any_job (flags, ps) { if ((flags & JWAIT_WAITING) && jobs[i] && IS_WAITING (i) == 0) continue; /* if we don't want it, skip it */ - if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i) == 0) + if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i) == 0 && IS_FOREGROUND (i) == 0) { return_job: r = job_exit_status (i); -- 2.36.1