Patch-ID: bash32-017
[bash.git] / builtins / wait.def
bloba30959528fe96ae8e151ce0fa2c1cf983831bf45
1 This file is wait.def, from which is created wait.c.
2 It implements the builtin "wait" in Bash.
4 Copyright (C) 1987-2005 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
22 $BUILTIN wait
23 $FUNCTION wait_builtin
24 $DEPENDS_ON JOB_CONTROL
25 $PRODUCES wait.c
26 $SHORT_DOC wait [n]
27 Wait for the specified process and report its termination status. If
28 N is not given, all currently active child processes are waited for,
29 and the return code is zero. N may be a process ID or a job
30 specification; if a job spec is given, all processes in the job's
31 pipeline are waited for.
32 $END
34 $BUILTIN wait
35 $FUNCTION wait_builtin
36 $DEPENDS_ON !JOB_CONTROL
37 $SHORT_DOC wait [n]
38 Wait for the specified process and report its termination status. If
39 N is not given, all currently active child processes are waited for,
40 and the return code is zero. N is a process ID; if it is not given,
41 all child processes of the shell are waited for.
42 $END
44 #include <config.h>
46 #include "../bashtypes.h"
47 #include <signal.h>
49 #if defined (HAVE_UNISTD_H)
50 # include <unistd.h>
51 #endif
53 #include <chartypes.h>
55 #include "../bashansi.h"
57 #include "../shell.h"
58 #include "../jobs.h"
59 #include "common.h"
60 #include "bashgetopt.h"
62 extern int wait_signal_received;
64 procenv_t wait_intr_buf;
66 /* Wait for the pid in LIST to stop or die. If no arguments are given, then
67 wait for all of the active background processes of the shell and return
68 0. If a list of pids or job specs are given, return the exit status of
69 the last one waited for. */
71 #define WAIT_RETURN(s) \
72 do \
73 { \
74 interrupt_immediately = old_interrupt_immediately;\
75 return (s);\
76 } \
77 while (0)
79 int
80 wait_builtin (list)
81 WORD_LIST *list;
83 int status, code;
84 volatile int old_interrupt_immediately;
86 USE_VAR(list);
88 if (no_options (list))
89 return (EX_USAGE);
90 list = loptend;
92 old_interrupt_immediately = interrupt_immediately;
93 interrupt_immediately++;
95 /* POSIX.2 says: When the shell is waiting (by means of the wait utility)
96 for asynchronous commands to complete, the reception of a signal for
97 which a trap has been set shall cause the wait utility to return
98 immediately with an exit status greater than 128, after which the trap
99 associated with the signal shall be taken.
101 We handle SIGINT here; it's the only one that needs to be treated
102 specially (I think), since it's handled specially in {no,}jobs.c. */
103 code = setjmp (wait_intr_buf);
104 if (code)
106 status = 128 + wait_signal_received;
107 WAIT_RETURN (status);
110 /* We support jobs or pids.
111 wait <pid-or-job> [pid-or-job ...] */
113 /* But wait without any arguments means to wait for all of the shell's
114 currently active background processes. */
115 if (list == 0)
117 wait_for_background_pids ();
118 WAIT_RETURN (EXECUTION_SUCCESS);
121 status = EXECUTION_SUCCESS;
122 while (list)
124 pid_t pid;
125 char *w;
126 intmax_t pid_value;
128 w = list->word->word;
129 if (DIGIT (*w))
131 if (legal_number (w, &pid_value) && pid_value == (pid_t)pid_value)
133 pid = (pid_t)pid_value;
134 status = wait_for_single_pid (pid);
136 else
138 sh_badpid (w);
139 WAIT_RETURN (EXECUTION_FAILURE);
142 #if defined (JOB_CONTROL)
143 else if (*w && *w == '%')
144 /* Must be a job spec. Check it out. */
146 int job;
147 sigset_t set, oset;
149 BLOCK_CHILD (set, oset);
150 job = get_job_spec (list);
152 if (INVALID_JOB (job))
154 if (job != DUP_JOB)
155 sh_badjob (list->word->word);
156 UNBLOCK_CHILD (oset);
157 status = 127; /* As per Posix.2, section 4.70.2 */
158 list = list->next;
159 continue;
162 /* Job spec used. Wait for the last pid in the pipeline. */
163 UNBLOCK_CHILD (oset);
164 status = wait_for_job (job);
166 #endif /* JOB_CONTROL */
167 else
169 sh_badpid (w);
170 status = EXECUTION_FAILURE;
172 list = list->next;
175 WAIT_RETURN (status);