+/* Wait for any background job started by this shell to finish. Very
+ similar to wait_for_background_pids(). Returns the exit status of
+ the next exiting job, -1 if there are no background jobs. The caller
+ is responsible for translating -1 into the right return value. */
+int
+wait_for_any_job ()
+{
+ pid_t pid;
+ int i, r, waited_for;
+ sigset_t set, oset;
+
+ if (jobs_list_frozen)
+ return -1;
+
+ /* First see if there are any unnotified dead jobs that we can report on */
+ BLOCK_CHILD (set, oset);
+ for (i = 0; i < js.j_jobslots; i++)
+ {
+ if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i) == 0)
+ {
+return_job:
+ r = job_exit_status (i);
+ notify_of_job_status (); /* XXX */
+ delete_job (i, 0);
+#if defined (COPROCESS_SUPPORT)
+ coproc_reap ();
+#endif
+ UNBLOCK_CHILD (oset);
+ return r;
+ }
+ }
+ UNBLOCK_CHILD (oset);
+
+ /* At this point, we have no dead jobs in the jobs table. Wait until we
+ get one, even if it takes multiple pids exiting. */
+ for (waited_for = 0;;)
+ {
+ /* Make sure there is a background job to wait for */
+ BLOCK_CHILD (set, oset);
+ for (i = 0; i < js.j_jobslots; i++)
+ if (jobs[i] && RUNNING (i) && IS_FOREGROUND (i) == 0)
+ break;
+ if (i == js.j_jobslots)
+ {
+ UNBLOCK_CHILD (oset);
+ return -1;
+ }
+
+ UNBLOCK_CHILD (oset);
+
+ QUIT;
+ CHECK_TERMSIG;
+ CHECK_WAIT_INTR;
+
+ errno = 0;
+ r = wait_for (ANY_PID); /* special sentinel value for wait_for */
+ if (r == -1 && errno == ECHILD)
+ mark_all_jobs_as_dead ();
+
+ /* Now we see if we have any dead jobs and return the first one */
+ BLOCK_CHILD (set, oset);
+ for (i = 0; i < js.j_jobslots; i++)
+ if (jobs[i] && DEADJOB (i))
+ goto return_job;
+ UNBLOCK_CHILD (oset);
+ }
+
+ return -1;
+}
+