+/* Set the status of JOB and perform any necessary cleanup if the job is
+ marked as JDEAD.
+
+ Currently, the cleanup activity is restricted to handling any SIGINT
+ received while waiting for a foreground job to finish. */
+static int
+set_job_status_and_cleanup (job)
+ int job;
+{
+ PROCESS *child;
+ int tstatus, job_state, any_stopped, any_tstped, call_set_current;
+ SigHandler *temp_handler;
+
+ child = jobs[job]->pipe;
+ jobs[job]->flags &= ~J_NOTIFIED;
+
+ call_set_current = 0;
+
+ /*
+ * COMPUTE JOB STATUS
+ */
+
+ /* If all children are not running, but any of them is stopped, then
+ the job is stopped, not dead. */
+ job_state = any_stopped = any_tstped = 0;
+ do
+ {
+ job_state |= PRUNNING (child);
+#if 0
+ if (PEXITED (child) && (WIFSTOPPED (child->status)))
+#else
+ /* Only checking for WIFSTOPPED now, not for PS_DONE */
+ if (PSTOPPED (child))
+#endif
+ {
+ any_stopped = 1;
+ any_tstped |= job_control && (WSTOPSIG (child->status) == SIGTSTP);
+ }
+ child = child->next;
+ }
+ while (child != jobs[job]->pipe);
+
+ /* If job_state != 0, the job is still running, so don't bother with
+ setting the process exit status and job state unless we're
+ transitioning from stopped to running. */
+ if (job_state != 0 && JOBSTATE(job) != JSTOPPED)
+ return 0;
+
+ /*
+ * SET JOB STATUS
+ */
+
+ /* The job is either stopped or dead. Set the state of the job accordingly. */
+ if (any_stopped)
+ {
+ jobs[job]->state = JSTOPPED;
+ jobs[job]->flags &= ~J_FOREGROUND;
+ call_set_current++;
+ /* Suspending a job with SIGTSTP breaks all active loops. */
+ if (any_tstped && loop_level)
+ breaking = loop_level;
+ }
+ else if (job_state != 0) /* was stopped, now running */
+ {
+ jobs[job]->state = JRUNNING;
+ call_set_current++;
+ }
+ else
+ {
+ jobs[job]->state = JDEAD;
+ js.j_ndead++;
+
+#if 0
+ if (IS_FOREGROUND (job))
+ setjstatus (job);
+#endif
+
+ /* If this job has a cleanup function associated with it, call it
+ with `cleanarg' as the single argument, then set the function
+ pointer to NULL so it is not inadvertently called twice. The
+ cleanup function is responsible for deallocating cleanarg. */
+ if (jobs[job]->j_cleanup)
+ {
+ (*jobs[job]->j_cleanup) (jobs[job]->cleanarg);
+ jobs[job]->j_cleanup = (sh_vptrfunc_t *)NULL;
+ }
+ }
+
+ /*
+ * CLEANUP
+ *
+ * Currently, we just do special things if we got a SIGINT while waiting
+ * for a foreground job to complete
+ */
+
+ if (JOBSTATE (job) == JDEAD)
+ {
+ /* If we're running a shell script and we get a SIGINT with a
+ SIGINT trap handler, but the foreground job handles it and
+ does not exit due to SIGINT, run the trap handler but do not
+ otherwise act as if we got the interrupt. */
+ if (wait_sigint_received && interactive_shell == 0 &&
+ child_caught_sigint && IS_FOREGROUND (job) &&
+ signal_is_trapped (SIGINT))
+ {
+ int old_frozen;
+ wait_sigint_received = 0;
+ last_command_exit_value = process_exit_status (child->status);
+
+ old_frozen = jobs_list_frozen;
+ jobs_list_frozen = 1;
+ tstatus = maybe_call_trap_handler (SIGINT);
+ jobs_list_frozen = old_frozen;
+ }
+
+ /* If the foreground job is killed by SIGINT when job control is not
+ active, we need to perform some special handling.
+
+ The check of wait_sigint_received is a way to determine if the
+ SIGINT came from the keyboard (in which case the shell has already
+ seen it, and wait_sigint_received is non-zero, because keyboard
+ signals are sent to process groups) or via kill(2) to the foreground
+ process by another process (or itself). If the shell did receive the
+ SIGINT, it needs to perform normal SIGINT processing. */
+ else if (wait_sigint_received &&
+ child_caught_sigint == 0 &&
+ IS_FOREGROUND (job) && IS_JOBCONTROL (job) == 0)
+ {
+ int old_frozen;
+
+ wait_sigint_received = 0;
+
+ /* If SIGINT is trapped, set the exit status so that the trap
+ handler can see it. */
+ if (signal_is_trapped (SIGINT))
+ last_command_exit_value = process_exit_status (child->status);
+
+ /* If the signal is trapped, let the trap handler get it no matter
+ what and simply return if the trap handler returns.
+ maybe_call_trap_handler() may cause dead jobs to be removed from
+ the job table because of a call to execute_command. We work
+ around this by setting JOBS_LIST_FROZEN. */
+ old_frozen = jobs_list_frozen;
+ jobs_list_frozen = 1;
+ tstatus = maybe_call_trap_handler (SIGINT);
+ jobs_list_frozen = old_frozen;
+ if (tstatus == 0 && old_sigint_handler != INVALID_SIGNAL_HANDLER)
+ {
+ /* wait_sigint_handler () has already seen SIGINT and
+ allowed the wait builtin to jump out. We need to
+ call the original SIGINT handler, if necessary. If
+ the original handler is SIG_DFL, we need to resend
+ the signal to ourselves. */
+
+ temp_handler = old_sigint_handler;
+
+ /* Bogus. If we've reset the signal handler as the result
+ of a trap caught on SIGINT, then old_sigint_handler
+ will point to trap_handler, which now knows nothing about
+ SIGINT (if we reset the sighandler to the default).
+ In this case, we have to fix things up. What a crock. */
+ if (temp_handler == trap_handler && signal_is_trapped (SIGINT) == 0)
+ temp_handler = trap_to_sighandler (SIGINT);
+ restore_sigint_handler ();
+ if (temp_handler == SIG_DFL)
+ termsig_handler (SIGINT); /* XXX */
+ else if (temp_handler != SIG_IGN)
+ (*temp_handler) (SIGINT);
+ }
+ }
+ }
+
+ return call_set_current;
+}
+
+/* Build the array of values for the $PIPESTATUS variable from the set of
+ exit statuses of all processes in the job J. */
+static void
+setjstatus (j)
+ int j;
+{
+#if defined (ARRAY_VARS)
+ register int i;
+ register PROCESS *p;
+
+ for (i = 1, p = jobs[j]->pipe; p->next != jobs[j]->pipe; p = p->next, i++)
+ ;
+ i++;
+ if (statsize < i)
+ {
+ pstatuses = (int *)xrealloc (pstatuses, i * sizeof (int));
+ statsize = i;
+ }
+ i = 0;
+ p = jobs[j]->pipe;
+ do
+ {
+ pstatuses[i++] = process_exit_status (p->status);
+ p = p->next;
+ }
+ while (p != jobs[j]->pipe);
+
+ pstatuses[i] = -1; /* sentinel */
+ set_pipestatus_array (pstatuses, i);
+#endif
+}
+
+void
+run_sigchld_trap (nchild)
+ int nchild;
+{
+ char *trap_command;
+ int i;
+
+ /* Turn off the trap list during the call to parse_and_execute ()
+ to avoid potentially infinite recursive calls. Preserve the
+ values of last_command_exit_value, last_made_pid, and the_pipeline
+ around the execution of the trap commands. */
+ trap_command = savestring (trap_list[SIGCHLD]);
+
+ begin_unwind_frame ("SIGCHLD trap");
+ unwind_protect_int (last_command_exit_value);
+ unwind_protect_int (last_command_exit_signal);
+ unwind_protect_var (last_made_pid);
+ unwind_protect_int (interrupt_immediately);
+ unwind_protect_int (jobs_list_frozen);
+ unwind_protect_pointer (the_pipeline);
+ unwind_protect_pointer (subst_assign_varlist);
+ unwind_protect_pointer (this_shell_builtin);
+
+ /* We have to add the commands this way because they will be run
+ in reverse order of adding. We don't want maybe_set_sigchld_trap ()
+ to reference freed memory. */
+ add_unwind_protect (xfree, trap_command);
+ add_unwind_protect (maybe_set_sigchld_trap, trap_command);
+
+ subst_assign_varlist = (WORD_LIST *)NULL;
+ the_pipeline = (PROCESS *)NULL;
+
+ running_trap = SIGCHLD + 1;
+
+ set_impossible_sigchld_trap ();
+ jobs_list_frozen = 1;
+ for (i = 0; i < nchild; i++)
+ {
+#if 0
+ interrupt_immediately = 1;
+#endif
+ parse_and_execute (savestring (trap_command), "trap", SEVAL_NOHIST|SEVAL_RESETLINE);
+ }
+
+ run_unwind_frame ("SIGCHLD trap");
+ running_trap = 0;
+}
+