improve of cmpl.
[bush.git] / builtins / wait.def
blobefca4d303d5484a7c47e2d51674fdeaca0fdfcda
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/>.
21 $BUILTIN wait
22 $FUNCTION wait_builtin
23 $DEPENDS_ON JOB_CONTROL
24 $PRODUCES wait.c
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
36 exit status.
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.
46 Exit 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
49 children.
50 $END
52 $BUILTIN wait
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.
62 Exit Status:
63 Returns the status of the last PID; fails if PID is invalid or an invalid
64 option is given.
65 $END
67 #include <config.h>
69 #include "../src/bushtypes.h"
70 #include <signal.h>
72 #if defined (HAVE_UNISTD_H)
73 # include <unistd.h>
74 #endif
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"
85 #include "common.h"
86 #include "bushgetopt.h"
88 extern int wait_signal_received;
90 procenv_t wait_intr_buf;
91 int wait_intr_flag;
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) \
102 do \
104 wait_signal_received = 0; \
105 wait_intr_flag = 0; \
106 return (s);\
108 while (0)
111 wait_builtin (list)
112 WORD_LIST *list;
114 int status, code, opt, nflag, wflags;
115 char *vname;
116 SHELL_VAR *pidvar;
117 struct procstat pstat;
119 USE_VAR(list);
121 nflag = wflags = 0;
122 vname = NULL;
123 pidvar = (SHELL_VAR *)NULL;
124 reset_internal_getopt ();
125 while ((opt = internal_getopt (list, "fnp:")) != -1)
127 switch (opt)
129 #if defined (JOB_CONTROL)
130 case 'n':
131 nflag = 1;
132 break;
133 case 'f':
134 wflags |= JWAIT_FORCE;
135 break;
136 case 'p':
137 vname = list_optarg;
138 break;
139 #endif
140 CASE_HELPOPT;
141 default:
142 builtin_usage ();
143 return (EX_USAGE);
146 list = loptend;
148 /* Sanity-check variable name if -p supplied. */
149 if (vname)
151 #if defined (ARRAY_VARS)
152 int arrayflags;
154 arrayflags = assoc_expand_once ? (VA_NOEXPAND|VA_ONEWORD) : 0;
155 if (legal_identifier (vname) == 0 && valid_array_reference (vname, arrayflags) == 0)
156 #else
157 if (legal_identifier (vname) == 0)
158 #endif
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. */
175 wait_intr_flag = 1;
176 code = setjmp_sigs (wait_intr_buf);
178 if (code)
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);
193 #endif
194 if (opt != -1)
196 last_command_exit_signal = wait_signal_received = opt;
197 status = opt + 128;
198 WAIT_RETURN (status);
201 /* We support jobs or pids.
202 wait <pid-or-job> [pid-or-job ...] */
204 #if defined (JOB_CONTROL)
205 if (nflag)
207 if (list)
209 opt = set_waitlist (list);
210 if (opt == 0)
211 WAIT_RETURN (127);
212 wflags |= JWAIT_WAITING;
215 status = wait_for_any_job (wflags, &pstat);
216 if (status < 0)
217 status = 127;
219 if (vname && status >= 0)
220 bind_var_to_int (vname, pstat.pid);
221 if (list)
222 unset_waitlist ();
223 WAIT_RETURN (status);
225 #endif
227 /* But wait without any arguments means to wait for all of the shell's
228 currently active background processes. */
229 if (list == 0)
231 wait_for_background_pids (&pstat);
232 if (vname)
233 bind_var_to_int (vname, pstat.pid);
234 WAIT_RETURN (EXECUTION_SUCCESS);
237 status = EXECUTION_SUCCESS;
238 while (list)
240 pid_t pid;
241 char *w;
242 intmax_t pid_value;
244 w = list->word->word;
245 if (DIGIT (*w))
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);
251 pstat.pid = pid;
252 pstat.status = status;
254 else
256 sh_badpid (w);
257 pstat.pid = NO_PID;
258 pstat.status = 127;
259 WAIT_RETURN (EXECUTION_FAILURE);
262 #if defined (JOB_CONTROL)
263 else if (*w && *w == '%')
264 /* Must be a job spec. Check it out. */
266 int job;
267 sigset_t set, oset;
269 BLOCK_CHILD (set, oset);
270 job = get_job_spec (list);
272 if (INVALID_JOB (job))
274 if (job != DUP_JOB)
275 sh_badjob (list->word->word);
276 UNBLOCK_CHILD (oset);
277 status = 127; /* As per Posix.2, section 4.70.2 */
278 pstat.pid = NO_PID;
279 pstat.status = status;
280 list = list->next;
281 continue;
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 */
289 else
291 sh_badpid (w);
292 pstat.pid = NO_PID;
293 pstat.status = 127;
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);
306 list = list->next;
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
315 jobs we found. */
316 static int
317 set_waitlist (list)
318 WORD_LIST *list;
320 sigset_t set, oset;
321 int job, r, njob;
322 intmax_t pid;
323 WORD_LIST *l;
325 BLOCK_CHILD (set, oset);
326 njob = 0;
327 for (l = list; l; l = l->next)
329 job = NO_JOB;
330 job = (l && legal_number (l->word->word, &pid) && pid == (pid_t) pid)
331 ? get_job_by_pid ((pid_t) pid, 0, 0)
332 : get_job_spec (l);
333 if (job == NO_JOB || jobs == 0 || INVALID_JOB (job))
335 sh_badjob (l->word->word);
336 continue;
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)
343 njob++;
344 jobs[job]->flags |= J_WAITING;
347 UNBLOCK_CHILD (oset);
348 return (njob);
351 /* Clean up after a call to wait -n jobs */
352 static void
353 unset_waitlist ()
355 int i;
356 sigset_t set, oset;
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);
364 #endif