]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/wait.def
bash-4.4 beta release
[thirdparty/bash.git] / builtins / wait.def
1 This file is wait.def, from which is created wait.c.
2 It implements the builtin "wait" in Bash.
3
4 Copyright (C) 1987-2015 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash 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.
12
13 Bash 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.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20
21 $BUILTIN wait
22 $FUNCTION wait_builtin
23 $DEPENDS_ON JOB_CONTROL
24 $PRODUCES wait.c
25 $SHORT_DOC wait [-n] [id ...]
26 Wait for job completion and return exit status.
27
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 a job specification, waits for all processes
32 in that job's pipeline.
33
34 If the -n option is supplied, waits for the next job to terminate and
35 returns its exit status.
36
37 Exit Status:
38 Returns the status of the last ID; fails if ID is invalid or an invalid
39 option is given.
40 $END
41
42 $BUILTIN wait
43 $FUNCTION wait_builtin
44 $DEPENDS_ON !JOB_CONTROL
45 $SHORT_DOC wait [pid ...]
46 Wait for process completion and return exit status.
47
48 Waits for each process specified by a PID and reports its termination status.
49 If PID is not given, waits for all currently active child processes,
50 and the return status is zero. PID must be a process ID.
51
52 Exit Status:
53 Returns the status of the last PID; fails if PID is invalid or an invalid
54 option is given.
55 $END
56
57 #include <config.h>
58
59 #include "../bashtypes.h"
60 #include <signal.h>
61
62 #if defined (HAVE_UNISTD_H)
63 # include <unistd.h>
64 #endif
65
66 #include <chartypes.h>
67
68 #include "../bashansi.h"
69
70 #include "../shell.h"
71 #include "../jobs.h"
72 #include "common.h"
73 #include "bashgetopt.h"
74
75 extern int wait_signal_received;
76 extern int last_command_exit_signal;
77
78 procenv_t wait_intr_buf;
79 int wait_intr_flag;
80
81 /* Wait for the pid in LIST to stop or die. If no arguments are given, then
82 wait for all of the active background processes of the shell and return
83 0. If a list of pids or job specs are given, return the exit status of
84 the last one waited for. */
85
86 #define WAIT_RETURN(s) \
87 do \
88 { \
89 interrupt_immediately = old_interrupt_immediately;\
90 wait_signal_received = 0; \
91 wait_intr_flag = 0; \
92 return (s);\
93 } \
94 while (0)
95
96 int
97 wait_builtin (list)
98 WORD_LIST *list;
99 {
100 int status, code, opt, nflag;
101 volatile int old_interrupt_immediately;
102
103 USE_VAR(list);
104
105 nflag = 0;
106 reset_internal_getopt ();
107 while ((opt = internal_getopt (list, "n")) != -1)
108 {
109 switch (opt)
110 {
111 #if defined (JOB_CONTROL)
112 case 'n':
113 nflag = 1;
114 break;
115 #endif
116 default:
117 builtin_usage ();
118 return (EX_USAGE);
119 }
120 }
121 list = loptend;
122
123 old_interrupt_immediately = interrupt_immediately;
124 #if 0
125 interrupt_immediately++;
126 #endif
127
128 /* POSIX.2 says: When the shell is waiting (by means of the wait utility)
129 for asynchronous commands to complete, the reception of a signal for
130 which a trap has been set shall cause the wait utility to return
131 immediately with an exit status greater than 128, after which the trap
132 associated with the signal shall be taken.
133
134 We handle SIGINT here; it's the only one that needs to be treated
135 specially (I think), since it's handled specially in {no,}jobs.c. */
136 wait_intr_flag = 1;
137 code = setjmp_sigs (wait_intr_buf);
138
139 if (code)
140 {
141 last_command_exit_signal = wait_signal_received;
142 status = 128 + wait_signal_received;
143 wait_sigint_cleanup ();
144 WAIT_RETURN (status);
145 }
146
147 /* We support jobs or pids.
148 wait <pid-or-job> [pid-or-job ...] */
149
150 #if defined (JOB_CONTROL)
151 if (nflag)
152 {
153 status = wait_for_any_job ();
154 if (status < 0)
155 status = 127;
156 WAIT_RETURN (status);
157 }
158 #endif
159
160 /* But wait without any arguments means to wait for all of the shell's
161 currently active background processes. */
162 if (list == 0)
163 {
164 wait_for_background_pids ();
165 WAIT_RETURN (EXECUTION_SUCCESS);
166 }
167
168 status = EXECUTION_SUCCESS;
169 while (list)
170 {
171 pid_t pid;
172 char *w;
173 intmax_t pid_value;
174
175 w = list->word->word;
176 if (DIGIT (*w))
177 {
178 if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
179 {
180 pid = (pid_t)pid_value;
181 status = wait_for_single_pid (pid);
182 }
183 else
184 {
185 sh_badpid (w);
186 WAIT_RETURN (EXECUTION_FAILURE);
187 }
188 }
189 #if defined (JOB_CONTROL)
190 else if (*w && *w == '%')
191 /* Must be a job spec. Check it out. */
192 {
193 int job;
194 sigset_t set, oset;
195
196 BLOCK_CHILD (set, oset);
197 job = get_job_spec (list);
198
199 if (INVALID_JOB (job))
200 {
201 if (job != DUP_JOB)
202 sh_badjob (list->word->word);
203 UNBLOCK_CHILD (oset);
204 status = 127; /* As per Posix.2, section 4.70.2 */
205 list = list->next;
206 continue;
207 }
208
209 /* Job spec used. Wait for the last pid in the pipeline. */
210 UNBLOCK_CHILD (oset);
211 status = wait_for_job (job);
212 }
213 #endif /* JOB_CONTROL */
214 else
215 {
216 sh_badpid (w);
217 status = EXECUTION_FAILURE;
218 }
219 list = list->next;
220 }
221
222 WAIT_RETURN (status);
223 }