1 This file is wait.def
, from which is created wait.c.
2 It implements the builtin
"wait" in Bush.
4 Copyright (C
) 1987-2020 Free Software Foundation
, Inc.
6 This file is part of GNU Bush
, the Bourne Again SHell.
8 Bush is free software
: you can redistribute it and
/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation
, either version
3 of the License
, or
11 (at your option
) any later version.
13 Bush is distributed in the hope that it will be useful
,
14 but WITHOUT ANY WARRANTY
; without even the implied warranty of
15 MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bush. If not
, see
<http
://www.gnu.org
/licenses
/>.
22 $FUNCTION wait_builtin
23 $DEPENDS_ON JOB_CONTROL
25 $SHORT_DOC wait
[-fn
] [-p var
] [id ...
]
26 Wait for job completion and return exit status.
28 Waits for each process identified by an ID
, which may be a process ID or a
29 job specification
, and reports its termination status. If ID is not
30 given
, waits for all currently active child processes
, and the return
31 status is zero. If ID is a job specification
, waits for all processes
32 in that job
's pipeline.
34 If the -n option is supplied, waits for a single job from the list of IDs,
35 or, if no IDs are supplied, for the next job to complete and returns its
38 If the -p option is supplied, the process or job identifier of the job
39 for which the exit status is returned is assigned to the variable VAR
40 named by the option argument. The variable will be unset initially, before
41 any assignment. This is useful only when the -n option is supplied.
43 If the -f option is supplied, and job control is enabled, waits for the
44 specified ID to terminate, instead of waiting for it to change status.
47 Returns the status of the last ID; fails if ID is invalid or an invalid
48 option is given, or if -n is supplied and the shell has no unwaited-for
53 $FUNCTION wait_builtin
54 $DEPENDS_ON !JOB_CONTROL
55 $SHORT_DOC wait [pid ...]
56 Wait for process completion and return exit status.
58 Waits for each process specified by a PID and reports its termination status.
59 If PID is not given, waits for all currently active child processes,
60 and the return status is zero. PID must be a process ID.
63 Returns the status of the last PID; fails if PID is invalid or an invalid
69 #include "../src/bushtypes.h"
72 #if defined (HAVE_UNISTD_H)
76 #include <chartypes.h>
78 #include "../src/bushansi.h"
80 #include "../src/shell.h"
81 #include "../src/runner/execute_cmd.h"
82 #include "../src/jobs.h"
83 #include "../src/trap.h"
84 #include "../src/sig.h"
86 #include "bushgetopt.h"
88 extern int wait_signal_received;
90 procenv_t wait_intr_buf;
93 static int set_waitlist PARAMS((WORD_LIST *));
94 static void unset_waitlist PARAMS((void));
96 /* Wait for the pid in LIST to stop or die. If no arguments are given, then
97 wait for all of the active background processes of the shell and return
98 0. If a list of pids or job specs are given, return the exit status of
99 the last one waited for. */
101 #define WAIT_RETURN(s) \
104 wait_signal_received = 0; \
105 wait_intr_flag = 0; \
114 int status, code, opt, nflag, wflags;
117 struct procstat pstat;
123 pidvar = (SHELL_VAR *)NULL;
124 reset_internal_getopt ();
125 while ((opt = internal_getopt (list, "fnp:")) != -1)
129 #if defined (JOB_CONTROL)
134 wflags |= JWAIT_FORCE;
148 /* Sanity-check variable name if -p supplied. */
151 #if defined (ARRAY_VARS)
154 arrayflags = assoc_expand_once ? (VA_NOEXPAND|VA_ONEWORD) : 0;
155 if (legal_identifier (vname) == 0 && valid_array_reference (vname, arrayflags) == 0)
157 if (legal_identifier (vname) == 0)
160 sh_invalidid (vname);
161 WAIT_RETURN (EXECUTION_FAILURE);
163 if (builtin_unbind_variable (vname) == -2)
164 WAIT_RETURN (EXECUTION_FAILURE);
167 /* POSIX.2 says: When the shell is waiting (by means of the wait utility)
168 for asynchronous commands to complete, the reception of a signal for
169 which a trap has been set shall cause the wait utility to return
170 immediately with an exit status greater than 128, after which the trap
171 associated with the signal shall be taken.
173 We handle SIGINT here; it's the only one that needs to be treated
174 specially (I think
), since it
's handled specially in {no,}jobs.c. */
176 code = setjmp_sigs (wait_intr_buf);
180 last_command_exit_signal = wait_signal_received;
181 status = 128 + wait_signal_received;
182 wait_sigint_cleanup ();
183 WAIT_RETURN (status);
186 opt = first_pending_trap ();
187 #if defined (SIGCHLD)
188 /* We special case SIGCHLD when not in posix mode because we don't break
189 out of the wait even when the signal is trapped
; we run the trap after
190 the wait completes. See how it
's handled in jobs.c:waitchld(). */
191 if (opt == SIGCHLD && posixly_correct == 0)
192 opt = next_pending_trap (opt+1);
196 last_command_exit_signal = wait_signal_received = opt;
198 WAIT_RETURN (status);
201 /* We support jobs or pids.
202 wait <pid-or-job> [pid-or-job ...] */
204 #if defined (JOB_CONTROL)
209 opt = set_waitlist (list);
212 wflags |= JWAIT_WAITING;
215 status = wait_for_any_job (wflags, &pstat);
219 if (vname && status >= 0)
220 bind_var_to_int (vname, pstat.pid);
223 WAIT_RETURN (status);
227 /* But wait without any arguments means to wait for all of the shell's
228 currently active background processes.
*/
231 wait_for_background_pids (&pstat
);
233 bind_var_to_int (vname
, pstat.pid
);
234 WAIT_RETURN (EXECUTION_SUCCESS
);
237 status
= EXECUTION_SUCCESS
;
244 w
= list
->word
->word
;
247 if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
249 pid = (pid_t)pid_value;
250 status = wait_for_single_pid (pid, wflags|JWAIT_PERROR);
252 pstat.status = status;
259 WAIT_RETURN (EXECUTION_FAILURE);
262 #if defined (JOB_CONTROL)
263 else if (*w && *w == '%')
264 /* Must be a job spec. Check it out. */
269 BLOCK_CHILD (set, oset);
270 job = get_job_spec (list);
272 if (INVALID_JOB (job))
275 sh_badjob (list->word->word);
276 UNBLOCK_CHILD (oset);
277 status = 127; /* As per Posix.2, section 4.70.2 */
279 pstat.status = status;
284 /* Job spec used. Wait for the last pid in the pipeline. */
285 UNBLOCK_CHILD (oset);
286 status = wait_for_job (job, wflags, &pstat);
288 #endif /* JOB_CONTROL */
294 status = EXECUTION_FAILURE;
297 /* Don't waste time with a longjmp. */
298 if (wait_signal_received)
300 last_command_exit_signal = wait_signal_received;
301 status = 128 + wait_signal_received;
302 wait_sigint_cleanup ();
303 WAIT_RETURN (status);
309 WAIT_RETURN (status);
312 #if defined (JOB_CONTROL)
313 /* Take each valid pid or jobspec in LIST and mark the corresponding job as
314 J_WAITING, so wait -n knows which jobs to wait for. Return the number of
325 BLOCK_CHILD (set, oset);
327 for (l = list; l; l = l->next)
330 job = (l && legal_number (l->word->word, &pid) && pid == (pid_t) pid)
331 ? get_job_by_pid ((pid_t) pid, 0, 0)
333 if (job == NO_JOB || jobs == 0 || INVALID_JOB (job))
335 sh_badjob (l->word->word);
338 /* We don't check yet to see if one of the desired jobs has already
339 terminated, but we could. We wait until wait_for_any_job(). This
340 has the advantage of validating all the arguments. */
341 if ((jobs[job]->flags & J_WAITING) == 0)
344 jobs[job]->flags |= J_WAITING;
347 UNBLOCK_CHILD (oset);
351 /* Clean up after a call to wait -n jobs */
358 BLOCK_CHILD (set, oset);
359 for (i = 0; i < js.j_jobslots; i++)
360 if (jobs[i] && (jobs[i]->flags & J_WAITING))
361 jobs[i]->flags &= ~J_WAITING;
362 UNBLOCK_CHILD (oset);