]>
Commit | Line | Data |
---|---|---|
3185942a | 1 | /* nojobs.c - functions that make children, remember them, and handle their termination. */ |
726f6388 | 2 | |
ccc6cda3 JA |
3 | /* This file works under BSD, System V, minix, and Posix systems. It does |
4 | not implement job control. */ | |
726f6388 | 5 | |
ac50fbac | 6 | /* Copyright (C) 1987-2011 Free Software Foundation, Inc. |
726f6388 JA |
7 | |
8 | This file is part of GNU Bash, the Bourne Again SHell. | |
9 | ||
3185942a JA |
10 | Bash is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation, either version 3 of the License, or | |
13 | (at your option) any later version. | |
726f6388 | 14 | |
3185942a JA |
15 | Bash is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
726f6388 | 19 | |
3185942a JA |
20 | You should have received a copy of the GNU General Public License |
21 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
726f6388 | 23 | |
ccc6cda3 JA |
24 | #include "config.h" |
25 | ||
726f6388 | 26 | #include "bashtypes.h" |
ccc6cda3 JA |
27 | #include "filecntl.h" |
28 | ||
29 | #if defined (HAVE_UNISTD_H) | |
30 | # include <unistd.h> | |
31 | #endif | |
32 | ||
33 | #include <stdio.h> | |
726f6388 | 34 | #include <signal.h> |
726f6388 JA |
35 | #include <errno.h> |
36 | ||
726f6388 JA |
37 | #if defined (BUFFERED_INPUT) |
38 | # include "input.h" | |
39 | #endif | |
40 | ||
bb70624e JA |
41 | /* Need to include this up here for *_TTY_DRIVER definitions. */ |
42 | #include "shtty.h" | |
ccc6cda3 | 43 | |
b80f6443 JA |
44 | #include "bashintl.h" |
45 | ||
bb70624e JA |
46 | #include "shell.h" |
47 | #include "jobs.h" | |
17345e5a | 48 | #include "execute_cmd.h" |
a0c0a00f | 49 | #include "trap.h" |
bb70624e JA |
50 | |
51 | #include "builtins/builtext.h" /* for wait_builtin */ | |
52 | ||
7117c2d2 | 53 | #define DEFAULT_CHILD_MAX 32 |
bb70624e | 54 | |
ccc6cda3 | 55 | #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG) |
726f6388 JA |
56 | # define killpg(pg, sig) kill(-(pg),(sig)) |
57 | #endif /* USG || _POSIX_VERSION */ | |
58 | ||
b80f6443 | 59 | #if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS) |
726f6388 | 60 | # define siginterrupt(sig, code) |
b80f6443 | 61 | #endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */ |
726f6388 | 62 | |
ccc6cda3 | 63 | #if defined (HAVE_WAITPID) |
726f6388 JA |
64 | # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options) |
65 | #else | |
66 | # define WAITPID(pid, statusp, options) wait (statusp) | |
ccc6cda3 | 67 | #endif /* !HAVE_WAITPID */ |
726f6388 | 68 | |
d166f048 JA |
69 | /* Return the fd from which we are actually getting input. */ |
70 | #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr) | |
71 | ||
726f6388 JA |
72 | #if !defined (errno) |
73 | extern int errno; | |
74 | #endif /* !errno */ | |
75 | ||
76 | extern int interactive, interactive_shell, login_shell; | |
77 | extern int subshell_environment; | |
b80f6443 | 78 | extern int last_command_exit_value, last_command_exit_signal; |
bb70624e | 79 | extern int interrupt_immediately; |
f73dda09 | 80 | extern sh_builtin_func_t *this_shell_builtin; |
ccc6cda3 | 81 | #if defined (HAVE_POSIX_SIGNALS) |
726f6388 JA |
82 | extern sigset_t top_level_mask; |
83 | #endif | |
bb70624e | 84 | extern procenv_t wait_intr_buf; |
a0c0a00f | 85 | extern int wait_intr_flag; |
7117c2d2 | 86 | extern int wait_signal_received; |
726f6388 | 87 | |
a0c0a00f CR |
88 | extern void set_original_signal __P((int, SigHandler *)); |
89 | ||
ac50fbac CR |
90 | volatile pid_t last_made_pid = NO_PID; |
91 | volatile pid_t last_asynchronous_pid = NO_PID; | |
726f6388 | 92 | |
a0c0a00f CR |
93 | static int queue_sigchld, waiting_for_child; /* dummy declarations */ |
94 | ||
726f6388 JA |
95 | /* Call this when you start making children. */ |
96 | int already_making_children = 0; | |
97 | ||
d166f048 JA |
98 | /* The controlling tty for this shell. */ |
99 | int shell_tty = -1; | |
100 | ||
ccc6cda3 JA |
101 | /* If this is non-zero, $LINES and $COLUMNS are reset after every process |
102 | exits from get_tty_state(). */ | |
ac50fbac CR |
103 | int check_window_size = CHECKWINSIZE_DEFAULT; |
104 | ||
105 | /* We don't have job control. */ | |
106 | int job_control = 0; | |
ccc6cda3 | 107 | |
a0c0a00f CR |
108 | int running_in_background = 0; /* can't tell without job control */ |
109 | ||
bb70624e JA |
110 | /* STATUS and FLAGS are only valid if pid != NO_PID |
111 | STATUS is only valid if (flags & PROC_RUNNING) == 0 */ | |
726f6388 JA |
112 | struct proc_status { |
113 | pid_t pid; | |
114 | int status; /* Exit status of PID or 128 + fatal signal number */ | |
bb70624e | 115 | int flags; |
726f6388 JA |
116 | }; |
117 | ||
bb70624e JA |
118 | /* Values for proc_status.flags */ |
119 | #define PROC_RUNNING 0x01 | |
120 | #define PROC_NOTIFIED 0x02 | |
121 | #define PROC_ASYNC 0x04 | |
b80f6443 | 122 | #define PROC_SIGNALED 0x10 |
bb70624e | 123 | |
bb70624e JA |
124 | /* Return values from find_status_by_pid */ |
125 | #define PROC_BAD -1 | |
726f6388 JA |
126 | #define PROC_STILL_ALIVE -2 |
127 | ||
f73dda09 JA |
128 | static struct proc_status *pid_list = (struct proc_status *)NULL; |
129 | static int pid_list_size; | |
130 | static int wait_sigint_received; | |
131 | ||
7117c2d2 JA |
132 | static long child_max = -1L; |
133 | ||
f73dda09 | 134 | static void alloc_pid_list __P((void)); |
a0c0a00f | 135 | static int find_proc_slot __P((pid_t)); |
f73dda09 JA |
136 | static int find_index_by_pid __P((pid_t)); |
137 | static int find_status_by_pid __P((pid_t)); | |
138 | static int process_exit_status __P((WAIT)); | |
b80f6443 JA |
139 | static int find_termsig_by_pid __P((pid_t)); |
140 | static int get_termsig __P((WAIT)); | |
f73dda09 JA |
141 | static void set_pid_status __P((pid_t, WAIT)); |
142 | static void set_pid_flags __P((pid_t, int)); | |
143 | static void unset_pid_flags __P((pid_t, int)); | |
b80f6443 | 144 | static int get_pid_flags __P((pid_t)); |
f73dda09 JA |
145 | static void add_pid __P((pid_t, int)); |
146 | static void mark_dead_jobs_as_notified __P((int)); | |
147 | ||
7117c2d2 | 148 | static sighandler wait_sigint_handler __P((int)); |
b80f6443 | 149 | static char *j_strsignal __P((int)); |
f73dda09 JA |
150 | |
151 | #if defined (HAVE_WAITPID) | |
152 | static void reap_zombie_children __P((void)); | |
153 | #endif | |
154 | ||
b80f6443 JA |
155 | #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS) |
156 | static int siginterrupt __P((int, int)); | |
157 | #endif | |
158 | ||
f73dda09 JA |
159 | static void restore_sigint_handler __P((void)); |
160 | ||
726f6388 JA |
161 | /* Allocate new, or grow existing PID_LIST. */ |
162 | static void | |
163 | alloc_pid_list () | |
164 | { | |
165 | register int i; | |
166 | int old = pid_list_size; | |
167 | ||
168 | pid_list_size += 10; | |
f73dda09 | 169 | pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status)); |
726f6388 JA |
170 | |
171 | /* None of the newly allocated slots have process id's yet. */ | |
172 | for (i = old; i < pid_list_size; i++) | |
ccc6cda3 | 173 | pid_list[i].pid = NO_PID; |
726f6388 JA |
174 | } |
175 | ||
176 | /* Return the offset within the PID_LIST array of an empty slot. This can | |
177 | create new slots if all of the existing slots are taken. */ | |
178 | static int | |
a0c0a00f CR |
179 | find_proc_slot (pid) |
180 | pid_t pid; | |
726f6388 JA |
181 | { |
182 | register int i; | |
183 | ||
184 | for (i = 0; i < pid_list_size; i++) | |
a0c0a00f | 185 | if (pid_list[i].pid == NO_PID || pid_list[i].pid == pid) |
726f6388 JA |
186 | return (i); |
187 | ||
188 | if (i == pid_list_size) | |
189 | alloc_pid_list (); | |
190 | ||
191 | return (i); | |
192 | } | |
193 | ||
194 | /* Return the offset within the PID_LIST array of a slot containing PID, | |
195 | or the value NO_PID if the pid wasn't found. */ | |
196 | static int | |
197 | find_index_by_pid (pid) | |
198 | pid_t pid; | |
199 | { | |
200 | register int i; | |
201 | ||
202 | for (i = 0; i < pid_list_size; i++) | |
203 | if (pid_list[i].pid == pid) | |
204 | return (i); | |
205 | ||
206 | return (NO_PID); | |
207 | } | |
208 | ||
209 | /* Return the status of PID as looked up in the PID_LIST array. A | |
210 | return value of PROC_BAD indicates that PID wasn't found. */ | |
211 | static int | |
212 | find_status_by_pid (pid) | |
213 | pid_t pid; | |
214 | { | |
215 | int i; | |
216 | ||
217 | i = find_index_by_pid (pid); | |
218 | if (i == NO_PID) | |
219 | return (PROC_BAD); | |
bb70624e JA |
220 | if (pid_list[i].flags & PROC_RUNNING) |
221 | return (PROC_STILL_ALIVE); | |
726f6388 JA |
222 | return (pid_list[i].status); |
223 | } | |
224 | ||
bb70624e JA |
225 | static int |
226 | process_exit_status (status) | |
227 | WAIT status; | |
228 | { | |
229 | if (WIFSIGNALED (status)) | |
230 | return (128 + WTERMSIG (status)); | |
231 | else | |
232 | return (WEXITSTATUS (status)); | |
233 | } | |
234 | ||
b80f6443 JA |
235 | /* Return the status of PID as looked up in the PID_LIST array. A |
236 | return value of PROC_BAD indicates that PID wasn't found. */ | |
237 | static int | |
238 | find_termsig_by_pid (pid) | |
239 | pid_t pid; | |
240 | { | |
241 | int i; | |
242 | ||
243 | i = find_index_by_pid (pid); | |
244 | if (i == NO_PID) | |
245 | return (0); | |
246 | if (pid_list[i].flags & PROC_RUNNING) | |
247 | return (0); | |
3185942a | 248 | return (get_termsig ((WAIT)pid_list[i].status)); |
b80f6443 JA |
249 | } |
250 | ||
251 | /* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look | |
252 | up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately | |
253 | depending on its flags and exit status. */ | |
254 | static int | |
255 | get_termsig (status) | |
256 | WAIT status; | |
257 | { | |
258 | if (WIFSTOPPED (status) == 0 && WIFSIGNALED (status)) | |
259 | return (WTERMSIG (status)); | |
260 | else | |
261 | return (0); | |
262 | } | |
263 | ||
726f6388 JA |
264 | /* Give PID the status value STATUS in the PID_LIST array. */ |
265 | static void | |
266 | set_pid_status (pid, status) | |
267 | pid_t pid; | |
268 | WAIT status; | |
269 | { | |
270 | int slot; | |
271 | ||
17345e5a JA |
272 | #if defined (COPROCESS_SUPPORT) |
273 | coproc_pidchk (pid, status); | |
274 | #endif | |
275 | ||
726f6388 JA |
276 | slot = find_index_by_pid (pid); |
277 | if (slot == NO_PID) | |
278 | return; | |
279 | ||
bb70624e JA |
280 | pid_list[slot].status = process_exit_status (status); |
281 | pid_list[slot].flags &= ~PROC_RUNNING; | |
b80f6443 JA |
282 | if (WIFSIGNALED (status)) |
283 | pid_list[slot].flags |= PROC_SIGNALED; | |
bb70624e JA |
284 | /* If it's not a background process, mark it as notified so it gets |
285 | cleaned up. */ | |
286 | if ((pid_list[slot].flags & PROC_ASYNC) == 0) | |
287 | pid_list[slot].flags |= PROC_NOTIFIED; | |
288 | } | |
289 | ||
290 | /* Give PID the flags FLAGS in the PID_LIST array. */ | |
291 | static void | |
292 | set_pid_flags (pid, flags) | |
293 | pid_t pid; | |
294 | int flags; | |
295 | { | |
296 | int slot; | |
297 | ||
298 | slot = find_index_by_pid (pid); | |
299 | if (slot == NO_PID) | |
300 | return; | |
301 | ||
302 | pid_list[slot].flags |= flags; | |
303 | } | |
304 | ||
305 | /* Unset FLAGS for PID in the pid list */ | |
306 | static void | |
307 | unset_pid_flags (pid, flags) | |
308 | pid_t pid; | |
309 | int flags; | |
310 | { | |
311 | int slot; | |
312 | ||
313 | slot = find_index_by_pid (pid); | |
314 | if (slot == NO_PID) | |
315 | return; | |
316 | ||
317 | pid_list[slot].flags &= ~flags; | |
726f6388 JA |
318 | } |
319 | ||
b80f6443 JA |
320 | /* Return the flags corresponding to PID in the PID_LIST array. */ |
321 | static int | |
322 | get_pid_flags (pid) | |
323 | pid_t pid; | |
324 | { | |
325 | int slot; | |
326 | ||
327 | slot = find_index_by_pid (pid); | |
328 | if (slot == NO_PID) | |
329 | return 0; | |
330 | ||
331 | return (pid_list[slot].flags); | |
332 | } | |
333 | ||
726f6388 | 334 | static void |
bb70624e | 335 | add_pid (pid, async) |
726f6388 | 336 | pid_t pid; |
bb70624e | 337 | int async; |
726f6388 JA |
338 | { |
339 | int slot; | |
340 | ||
a0c0a00f | 341 | slot = find_proc_slot (pid); |
bb70624e | 342 | |
726f6388 | 343 | pid_list[slot].pid = pid; |
bb70624e JA |
344 | pid_list[slot].status = -1; |
345 | pid_list[slot].flags = PROC_RUNNING; | |
346 | if (async) | |
347 | pid_list[slot].flags |= PROC_ASYNC; | |
348 | } | |
349 | ||
350 | static void | |
351 | mark_dead_jobs_as_notified (force) | |
352 | int force; | |
353 | { | |
354 | register int i, ndead; | |
355 | ||
356 | /* first, count the number of non-running async jobs if FORCE == 0 */ | |
357 | for (i = ndead = 0; force == 0 && i < pid_list_size; i++) | |
358 | { | |
359 | if (pid_list[i].pid == NO_PID) | |
360 | continue; | |
361 | if (((pid_list[i].flags & PROC_RUNNING) == 0) && | |
362 | (pid_list[i].flags & PROC_ASYNC)) | |
28ef6c31 | 363 | ndead++; |
bb70624e JA |
364 | } |
365 | ||
7117c2d2 JA |
366 | if (child_max < 0) |
367 | child_max = getmaxchild (); | |
368 | if (child_max < 0) | |
369 | child_max = DEFAULT_CHILD_MAX; | |
370 | ||
371 | if (force == 0 && ndead <= child_max) | |
bb70624e JA |
372 | return; |
373 | ||
374 | /* If FORCE == 0, we just mark as many non-running async jobs as notified | |
375 | to bring us under the CHILD_MAX limit. */ | |
376 | for (i = 0; i < pid_list_size; i++) | |
377 | { | |
378 | if (pid_list[i].pid == NO_PID) | |
379 | continue; | |
380 | if (((pid_list[i].flags & PROC_RUNNING) == 0) && | |
381 | pid_list[i].pid != last_asynchronous_pid) | |
382 | { | |
383 | pid_list[i].flags |= PROC_NOTIFIED; | |
7117c2d2 | 384 | if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max) |
bb70624e JA |
385 | break; |
386 | } | |
387 | } | |
726f6388 JA |
388 | } |
389 | ||
bb70624e | 390 | /* Remove all dead, notified jobs from the pid_list. */ |
726f6388 JA |
391 | int |
392 | cleanup_dead_jobs () | |
393 | { | |
394 | register int i; | |
395 | ||
ccc6cda3 | 396 | #if defined (HAVE_WAITPID) |
726f6388 JA |
397 | reap_zombie_children (); |
398 | #endif | |
399 | ||
400 | for (i = 0; i < pid_list_size; i++) | |
bb70624e JA |
401 | { |
402 | if ((pid_list[i].flags & PROC_RUNNING) == 0 && | |
403 | (pid_list[i].flags & PROC_NOTIFIED)) | |
404 | pid_list[i].pid = NO_PID; | |
405 | } | |
406 | ||
17345e5a JA |
407 | #if defined (COPROCESS_SUPPORT) |
408 | coproc_reap (); | |
409 | #endif | |
410 | ||
bb70624e JA |
411 | return 0; |
412 | } | |
413 | ||
414 | void | |
415 | reap_dead_jobs () | |
416 | { | |
417 | mark_dead_jobs_as_notified (0); | |
418 | cleanup_dead_jobs (); | |
726f6388 JA |
419 | } |
420 | ||
421 | /* Initialize the job control mechanism, and set up the tty stuff. */ | |
a0c0a00f | 422 | int |
d166f048 JA |
423 | initialize_job_control (force) |
424 | int force; | |
726f6388 | 425 | { |
d166f048 JA |
426 | shell_tty = fileno (stderr); |
427 | ||
428 | if (interactive) | |
429 | get_tty_state (); | |
a0c0a00f | 430 | return 0; |
726f6388 JA |
431 | } |
432 | ||
726f6388 JA |
433 | /* Setup this shell to handle C-C, etc. */ |
434 | void | |
435 | initialize_job_signals () | |
436 | { | |
437 | set_signal_handler (SIGINT, sigint_sighandler); | |
726f6388 JA |
438 | |
439 | /* If this is a login shell we don't wish to be disturbed by | |
440 | stop signals. */ | |
441 | if (login_shell) | |
ccc6cda3 | 442 | ignore_tty_job_signals (); |
726f6388 JA |
443 | } |
444 | ||
ccc6cda3 | 445 | #if defined (HAVE_WAITPID) |
726f6388 JA |
446 | /* Collect the status of all zombie children so that their system |
447 | resources can be deallocated. */ | |
448 | static void | |
449 | reap_zombie_children () | |
450 | { | |
b80f6443 | 451 | # if defined (WNOHANG) |
726f6388 JA |
452 | pid_t pid; |
453 | WAIT status; | |
454 | ||
0628567a | 455 | CHECK_TERMSIG; |
ac50fbac | 456 | CHECK_WAIT_INTR; |
726f6388 JA |
457 | while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0) |
458 | set_pid_status (pid, status); | |
b80f6443 | 459 | # endif /* WNOHANG */ |
0628567a | 460 | CHECK_TERMSIG; |
ac50fbac | 461 | CHECK_WAIT_INTR; |
726f6388 | 462 | } |
b80f6443 JA |
463 | #endif /* WAITPID */ |
464 | ||
465 | #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS) | |
ac50fbac CR |
466 | |
467 | #if !defined (SA_RESTART) | |
468 | # define SA_RESTART 0 | |
469 | #endif | |
470 | ||
b80f6443 JA |
471 | static int |
472 | siginterrupt (sig, flag) | |
473 | int sig, flag; | |
474 | { | |
475 | struct sigaction act; | |
476 | ||
477 | sigaction (sig, (struct sigaction *)NULL, &act); | |
478 | ||
479 | if (flag) | |
480 | act.sa_flags &= ~SA_RESTART; | |
481 | else | |
482 | act.sa_flags |= SA_RESTART; | |
483 | ||
484 | return (sigaction (sig, &act, (struct sigaction *)NULL)); | |
485 | } | |
486 | #endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */ | |
726f6388 JA |
487 | |
488 | /* Fork, handling errors. Returns the pid of the newly made child, or 0. | |
489 | COMMAND is just for remembering the name of the command; we don't do | |
490 | anything else with it. ASYNC_P says what to do with the tty. If | |
491 | non-zero, then don't give it away. */ | |
492 | pid_t | |
493 | make_child (command, async_p) | |
494 | char *command; | |
495 | int async_p; | |
496 | { | |
497 | pid_t pid; | |
3185942a | 498 | int forksleep; |
726f6388 JA |
499 | |
500 | /* Discard saved memory. */ | |
ccc6cda3 | 501 | if (command) |
726f6388 JA |
502 | free (command); |
503 | ||
504 | start_pipeline (); | |
505 | ||
506 | #if defined (BUFFERED_INPUT) | |
507 | /* If default_buffered_input is active, we are reading a script. If | |
508 | the command is asynchronous, we have already duplicated /dev/null | |
509 | as fd 0, but have not changed the buffered stream corresponding to | |
510 | the old fd 0. We don't want to sync the stream in this case. */ | |
cce855bc | 511 | if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0)) |
726f6388 JA |
512 | sync_buffered_stream (default_buffered_input); |
513 | #endif /* BUFFERED_INPUT */ | |
514 | ||
ac50fbac CR |
515 | /* XXX - block SIGTERM here and unblock in child after fork resets the |
516 | set of pending signals? */ | |
517 | RESET_SIGTERM; | |
518 | ||
3185942a JA |
519 | /* Create the child, handle severe errors. Retry on EAGAIN. */ |
520 | forksleep = 1; | |
521 | while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX) | |
726f6388 | 522 | { |
3185942a | 523 | sys_error ("fork: retry"); |
ac50fbac CR |
524 | RESET_SIGTERM; |
525 | ||
ccc6cda3 | 526 | #if defined (HAVE_WAITPID) |
726f6388 JA |
527 | /* Posix systems with a non-blocking waitpid () system call available |
528 | get another chance after zombies are reaped. */ | |
3185942a JA |
529 | reap_zombie_children (); |
530 | if (forksleep > 1 && sleep (forksleep) != 0) | |
531 | break; | |
532 | #else | |
533 | if (sleep (forksleep) != 0) | |
534 | break; | |
ccc6cda3 | 535 | #endif /* HAVE_WAITPID */ |
3185942a JA |
536 | forksleep <<= 1; |
537 | } | |
726f6388 | 538 | |
ac50fbac CR |
539 | if (pid != 0) |
540 | RESET_SIGTERM; | |
541 | ||
3185942a JA |
542 | if (pid < 0) |
543 | { | |
ccc6cda3 | 544 | sys_error ("fork"); |
ac50fbac | 545 | last_command_exit_value = EX_NOEXEC; |
726f6388 JA |
546 | throw_to_top_level (); |
547 | } | |
ccc6cda3 | 548 | |
726f6388 JA |
549 | if (pid == 0) |
550 | { | |
551 | #if defined (BUFFERED_INPUT) | |
cce855bc | 552 | unset_bash_input (0); |
726f6388 JA |
553 | #endif /* BUFFERED_INPUT */ |
554 | ||
a0c0a00f CR |
555 | CLRINTERRUPT; /* XXX - children have their own interrupt state */ |
556 | ||
ccc6cda3 | 557 | #if defined (HAVE_POSIX_SIGNALS) |
726f6388 JA |
558 | /* Restore top-level signal mask. */ |
559 | sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL); | |
560 | #endif | |
561 | ||
3185942a | 562 | #if 0 |
726f6388 JA |
563 | /* Ignore INT and QUIT in asynchronous children. */ |
564 | if (async_p) | |
ccc6cda3 | 565 | last_asynchronous_pid = getpid (); |
3185942a | 566 | #endif |
726f6388 | 567 | |
ccc6cda3 | 568 | default_tty_job_signals (); |
726f6388 JA |
569 | } |
570 | else | |
571 | { | |
572 | /* In the parent. */ | |
573 | ||
574 | last_made_pid = pid; | |
575 | ||
576 | if (async_p) | |
577 | last_asynchronous_pid = pid; | |
578 | ||
bb70624e | 579 | add_pid (pid, async_p); |
726f6388 JA |
580 | } |
581 | return (pid); | |
582 | } | |
583 | ||
ccc6cda3 JA |
584 | void |
585 | ignore_tty_job_signals () | |
586 | { | |
587 | #if defined (SIGTSTP) | |
588 | set_signal_handler (SIGTSTP, SIG_IGN); | |
589 | set_signal_handler (SIGTTIN, SIG_IGN); | |
590 | set_signal_handler (SIGTTOU, SIG_IGN); | |
591 | #endif | |
592 | } | |
593 | ||
594 | void | |
595 | default_tty_job_signals () | |
596 | { | |
597 | #if defined (SIGTSTP) | |
a0c0a00f CR |
598 | if (signal_is_trapped (SIGTSTP) == 0 && signal_is_hard_ignored (SIGTSTP)) |
599 | set_signal_handler (SIGTSTP, SIG_IGN); | |
600 | else | |
601 | set_signal_handler (SIGTSTP, SIG_DFL); | |
602 | if (signal_is_trapped (SIGTTIN) == 0 && signal_is_hard_ignored (SIGTTIN)) | |
603 | set_signal_handler (SIGTTIN, SIG_IGN); | |
604 | else | |
605 | set_signal_handler (SIGTTIN, SIG_DFL); | |
606 | if (signal_is_trapped (SIGTTOU) == 0 && signal_is_hard_ignored (SIGTTOU)) | |
607 | set_signal_handler (SIGTTOU, SIG_IGN); | |
608 | else | |
609 | set_signal_handler (SIGTTOU, SIG_DFL); | |
610 | #endif | |
611 | } | |
612 | ||
613 | /* Called once in a parent process. */ | |
614 | void | |
615 | get_original_tty_job_signals () | |
616 | { | |
617 | static int fetched = 0; | |
618 | ||
619 | if (fetched == 0) | |
620 | { | |
621 | #if defined (SIGTSTP) | |
622 | if (interactive_shell) | |
623 | { | |
624 | set_original_signal (SIGTSTP, SIG_DFL); | |
625 | set_original_signal (SIGTTIN, SIG_DFL); | |
626 | set_original_signal (SIGTTOU, SIG_DFL); | |
627 | } | |
628 | else | |
629 | { | |
630 | get_original_signal (SIGTSTP); | |
631 | get_original_signal (SIGTTIN); | |
632 | get_original_signal (SIGTTOU); | |
633 | } | |
ccc6cda3 | 634 | #endif |
a0c0a00f CR |
635 | fetched = 1; |
636 | } | |
ccc6cda3 JA |
637 | } |
638 | ||
bb70624e JA |
639 | /* Wait for a single pid (PID) and return its exit status. Called by |
640 | the wait builtin. */ | |
f73dda09 | 641 | int |
a0c0a00f | 642 | wait_for_single_pid (pid, flags) |
726f6388 | 643 | pid_t pid; |
a0c0a00f | 644 | int flags; |
726f6388 JA |
645 | { |
646 | pid_t got_pid; | |
647 | WAIT status; | |
a0c0a00f | 648 | int pstatus; |
726f6388 JA |
649 | |
650 | pstatus = find_status_by_pid (pid); | |
651 | ||
652 | if (pstatus == PROC_BAD) | |
ccc6cda3 | 653 | { |
b80f6443 | 654 | internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid); |
ccc6cda3 JA |
655 | return (127); |
656 | } | |
726f6388 JA |
657 | |
658 | if (pstatus != PROC_STILL_ALIVE) | |
b80f6443 JA |
659 | { |
660 | if (pstatus > 128) | |
661 | last_command_exit_signal = find_termsig_by_pid (pid); | |
662 | return (pstatus); | |
663 | } | |
726f6388 JA |
664 | |
665 | siginterrupt (SIGINT, 1); | |
666 | while ((got_pid = WAITPID (pid, &status, 0)) != pid) | |
667 | { | |
0628567a | 668 | CHECK_TERMSIG; |
ac50fbac | 669 | CHECK_WAIT_INTR; |
726f6388 JA |
670 | if (got_pid < 0) |
671 | { | |
672 | if (errno != EINTR && errno != ECHILD) | |
673 | { | |
674 | siginterrupt (SIGINT, 0); | |
ccc6cda3 | 675 | sys_error ("wait"); |
726f6388 JA |
676 | } |
677 | break; | |
678 | } | |
679 | else if (got_pid > 0) | |
28ef6c31 | 680 | set_pid_status (got_pid, status); |
726f6388 JA |
681 | } |
682 | ||
7117c2d2 JA |
683 | if (got_pid > 0) |
684 | { | |
685 | set_pid_status (got_pid, status); | |
686 | set_pid_flags (got_pid, PROC_NOTIFIED); | |
687 | } | |
bb70624e | 688 | |
726f6388 JA |
689 | siginterrupt (SIGINT, 0); |
690 | QUIT; | |
691 | ||
7117c2d2 | 692 | return (got_pid > 0 ? process_exit_status (status) : -1); |
726f6388 JA |
693 | } |
694 | ||
bb70624e JA |
695 | /* Wait for all of the shell's children to exit. Called by the `wait' |
696 | builtin. */ | |
726f6388 JA |
697 | void |
698 | wait_for_background_pids () | |
699 | { | |
700 | pid_t got_pid; | |
701 | WAIT status; | |
702 | ||
703 | /* If we aren't using job control, we let the kernel take care of the | |
ccc6cda3 | 704 | bookkeeping for us. wait () will return -1 and set errno to ECHILD |
726f6388 JA |
705 | when there are no more unwaited-for child processes on both |
706 | 4.2 BSD-based and System V-based systems. */ | |
707 | ||
708 | siginterrupt (SIGINT, 1); | |
709 | ||
710 | /* Wait for ECHILD */ | |
711 | while ((got_pid = WAITPID (-1, &status, 0)) != -1) | |
712 | set_pid_status (got_pid, status); | |
713 | ||
714 | if (errno != EINTR && errno != ECHILD) | |
715 | { | |
716 | siginterrupt (SIGINT, 0); | |
ccc6cda3 | 717 | sys_error("wait"); |
726f6388 JA |
718 | } |
719 | ||
720 | siginterrupt (SIGINT, 0); | |
721 | QUIT; | |
bb70624e JA |
722 | |
723 | mark_dead_jobs_as_notified (1); | |
724 | cleanup_dead_jobs (); | |
725 | } | |
726 | ||
a0c0a00f CR |
727 | void |
728 | wait_sigint_cleanup () | |
729 | { | |
730 | } | |
731 | ||
bb70624e JA |
732 | /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */ |
733 | #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids | |
734 | static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER; | |
735 | ||
736 | static void | |
737 | restore_sigint_handler () | |
738 | { | |
739 | if (old_sigint_handler != INVALID_SIGNAL_HANDLER) | |
740 | { | |
741 | set_signal_handler (SIGINT, old_sigint_handler); | |
742 | old_sigint_handler = INVALID_SIGNAL_HANDLER; | |
743 | } | |
726f6388 JA |
744 | } |
745 | ||
746 | /* Handle SIGINT while we are waiting for children in a script to exit. | |
747 | All interrupts are effectively ignored by the shell, but allowed to | |
748 | kill a running job. */ | |
749 | static sighandler | |
750 | wait_sigint_handler (sig) | |
751 | int sig; | |
752 | { | |
bb70624e JA |
753 | SigHandler *sigint_handler; |
754 | ||
755 | /* If we got a SIGINT while in `wait', and SIGINT is trapped, do | |
756 | what POSIX.2 says (see builtins/wait.def for more info). */ | |
757 | if (this_shell_builtin && this_shell_builtin == wait_builtin && | |
758 | signal_is_trapped (SIGINT) && | |
759 | ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler)) | |
760 | { | |
ac50fbac | 761 | last_command_exit_value = 128+SIGINT; |
bb70624e JA |
762 | restore_sigint_handler (); |
763 | interrupt_immediately = 0; | |
764 | trap_handler (SIGINT); /* set pending_traps[SIGINT] */ | |
7117c2d2 | 765 | wait_signal_received = SIGINT; |
ac50fbac | 766 | SIGRETURN (0); |
bb70624e JA |
767 | } |
768 | ||
28ef6c31 JA |
769 | if (interrupt_immediately) |
770 | { | |
771 | last_command_exit_value = EXECUTION_FAILURE; | |
772 | restore_sigint_handler (); | |
773 | ADDINTERRUPT; | |
774 | QUIT; | |
775 | } | |
726f6388 | 776 | |
bb70624e JA |
777 | wait_sigint_received = 1; |
778 | ||
ccc6cda3 | 779 | SIGRETURN (0); |
726f6388 JA |
780 | } |
781 | ||
b80f6443 JA |
782 | static char * |
783 | j_strsignal (s) | |
784 | int s; | |
785 | { | |
786 | static char retcode_name_buffer[64] = { '\0' }; | |
787 | char *x; | |
788 | ||
789 | x = strsignal (s); | |
790 | if (x == 0) | |
791 | { | |
792 | x = retcode_name_buffer; | |
793 | sprintf (x, "Signal %d", s); | |
794 | } | |
795 | return x; | |
796 | } | |
797 | ||
726f6388 JA |
798 | /* Wait for pid (one of our children) to terminate. This is called only |
799 | by the execution code in execute_cmd.c. */ | |
800 | int | |
801 | wait_for (pid) | |
802 | pid_t pid; | |
803 | { | |
804 | int return_val, pstatus; | |
805 | pid_t got_pid; | |
806 | WAIT status; | |
726f6388 JA |
807 | |
808 | pstatus = find_status_by_pid (pid); | |
809 | ||
810 | if (pstatus == PROC_BAD) | |
811 | return (0); | |
812 | ||
813 | if (pstatus != PROC_STILL_ALIVE) | |
b80f6443 JA |
814 | { |
815 | if (pstatus > 128) | |
816 | last_command_exit_signal = find_termsig_by_pid (pid); | |
817 | return (pstatus); | |
818 | } | |
726f6388 JA |
819 | |
820 | /* If we are running a script, ignore SIGINT while we're waiting for | |
821 | a child to exit. The loop below does some of this, but not all. */ | |
bb70624e JA |
822 | wait_sigint_received = 0; |
823 | if (interactive_shell == 0) | |
726f6388 JA |
824 | old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler); |
825 | ||
826 | while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */ | |
827 | { | |
0628567a | 828 | CHECK_TERMSIG; |
ac50fbac | 829 | CHECK_WAIT_INTR; |
726f6388 JA |
830 | if (got_pid < 0 && errno == ECHILD) |
831 | { | |
832 | #if !defined (_POSIX_VERSION) | |
833 | status.w_termsig = status.w_retcode = 0; | |
834 | #else | |
835 | status = 0; | |
836 | #endif /* _POSIX_VERSION */ | |
837 | break; | |
838 | } | |
839 | else if (got_pid < 0 && errno != EINTR) | |
f73dda09 | 840 | programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno)); |
726f6388 JA |
841 | else if (got_pid > 0) |
842 | set_pid_status (got_pid, status); | |
843 | } | |
844 | ||
7117c2d2 JA |
845 | if (got_pid > 0) |
846 | set_pid_status (got_pid, status); | |
726f6388 | 847 | |
ccc6cda3 | 848 | #if defined (HAVE_WAITPID) |
726f6388 JA |
849 | if (got_pid >= 0) |
850 | reap_zombie_children (); | |
ccc6cda3 | 851 | #endif /* HAVE_WAITPID */ |
726f6388 | 852 | |
ac50fbac CR |
853 | CHECK_TERMSIG; |
854 | CHECK_WAIT_INTR; | |
855 | ||
ccc6cda3 | 856 | if (interactive_shell == 0) |
726f6388 | 857 | { |
bb70624e JA |
858 | SigHandler *temp_handler; |
859 | ||
860 | temp_handler = old_sigint_handler; | |
861 | restore_sigint_handler (); | |
862 | ||
726f6388 JA |
863 | /* If the job exited because of SIGINT, make sure the shell acts as if |
864 | it had received one also. */ | |
865 | if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT)) | |
866 | { | |
bb70624e | 867 | |
726f6388 | 868 | if (maybe_call_trap_handler (SIGINT) == 0) |
bb70624e JA |
869 | { |
870 | if (temp_handler == SIG_DFL) | |
0628567a | 871 | termsig_handler (SIGINT); |
bb70624e JA |
872 | else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN) |
873 | (*temp_handler) (SIGINT); | |
874 | } | |
726f6388 JA |
875 | } |
876 | } | |
ccc6cda3 | 877 | |
726f6388 JA |
878 | /* Default return value. */ |
879 | /* ``a full 8 bits of status is returned'' */ | |
bb70624e | 880 | return_val = process_exit_status (status); |
b80f6443 | 881 | last_command_exit_signal = get_termsig (status); |
ccc6cda3 | 882 | |
ac50fbac CR |
883 | #if defined (DONT_REPORT_SIGPIPE) && defined (DONT_REPORT_SIGTERM) |
884 | # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE && (x) != SIGTERM) | |
885 | #elif !defined (DONT_REPORT_SIGPIPE) && !defined (DONT_REPORT_SIGTERM) | |
886 | # define REPORTSIG(x) ((x) != SIGINT) | |
887 | #elif defined (DONT_REPORT_SIGPIPE) | |
888 | # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE) | |
d166f048 | 889 | #else |
ac50fbac | 890 | # define REPORTSIG(x) ((x) != SIGINT && (x) != SIGTERM) |
d166f048 | 891 | #endif |
ac50fbac CR |
892 | |
893 | if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) && REPORTSIG(WTERMSIG (status))) | |
726f6388 | 894 | { |
b80f6443 | 895 | fprintf (stderr, "%s", j_strsignal (WTERMSIG (status))); |
726f6388 | 896 | if (WIFCORED (status)) |
3185942a | 897 | fprintf (stderr, _(" (core dumped)")); |
726f6388 JA |
898 | fprintf (stderr, "\n"); |
899 | } | |
900 | ||
ccc6cda3 | 901 | if (interactive_shell && subshell_environment == 0) |
726f6388 JA |
902 | { |
903 | if (WIFSIGNALED (status) || WIFSTOPPED (status)) | |
904 | set_tty_state (); | |
905 | else | |
906 | get_tty_state (); | |
907 | } | |
ac50fbac CR |
908 | else if (interactive_shell == 0 && subshell_environment == 0 && check_window_size) |
909 | get_new_window_size (0, (int *)0, (int *)0); | |
726f6388 JA |
910 | |
911 | return (return_val); | |
912 | } | |
913 | ||
95732b49 JA |
914 | /* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero, |
915 | or PID is less than -1, then kill the process group associated with PID. */ | |
726f6388 JA |
916 | int |
917 | kill_pid (pid, signal, group) | |
918 | pid_t pid; | |
919 | int signal, group; | |
920 | { | |
921 | int result; | |
922 | ||
95732b49 JA |
923 | if (pid < -1) |
924 | { | |
925 | pid = -pid; | |
926 | group = 1; | |
927 | } | |
bb70624e | 928 | result = group ? killpg (pid, signal) : kill (pid, signal); |
726f6388 JA |
929 | return (result); |
930 | } | |
931 | ||
bb70624e | 932 | static TTYSTRUCT shell_tty_info; |
ccc6cda3 | 933 | static int got_tty_state; |
726f6388 JA |
934 | |
935 | /* Fill the contents of shell_tty_info with the current tty info. */ | |
a0c0a00f | 936 | int |
726f6388 JA |
937 | get_tty_state () |
938 | { | |
ccc6cda3 JA |
939 | int tty; |
940 | ||
d166f048 | 941 | tty = input_tty (); |
726f6388 JA |
942 | if (tty != -1) |
943 | { | |
bb70624e | 944 | ttgetattr (tty, &shell_tty_info); |
726f6388 | 945 | got_tty_state = 1; |
ccc6cda3 | 946 | if (check_window_size) |
95732b49 | 947 | get_new_window_size (0, (int *)0, (int *)0); |
726f6388 | 948 | } |
a0c0a00f | 949 | return 0; |
726f6388 JA |
950 | } |
951 | ||
952 | /* Make the current tty use the state in shell_tty_info. */ | |
b72432fd | 953 | int |
726f6388 JA |
954 | set_tty_state () |
955 | { | |
ccc6cda3 JA |
956 | int tty; |
957 | ||
d166f048 | 958 | tty = input_tty (); |
726f6388 JA |
959 | if (tty != -1) |
960 | { | |
ccc6cda3 | 961 | if (got_tty_state == 0) |
b72432fd | 962 | return 0; |
bb70624e | 963 | ttsetattr (tty, &shell_tty_info); |
726f6388 | 964 | } |
b72432fd | 965 | return 0; |
726f6388 JA |
966 | } |
967 | ||
968 | /* Give the terminal to PGRP. */ | |
a0c0a00f | 969 | int |
28ef6c31 | 970 | give_terminal_to (pgrp, force) |
726f6388 | 971 | pid_t pgrp; |
28ef6c31 | 972 | int force; |
726f6388 | 973 | { |
a0c0a00f | 974 | return 0; |
726f6388 JA |
975 | } |
976 | ||
977 | /* Stop a pipeline. */ | |
f73dda09 | 978 | int |
726f6388 JA |
979 | stop_pipeline (async, ignore) |
980 | int async; | |
981 | COMMAND *ignore; | |
982 | { | |
983 | already_making_children = 0; | |
f73dda09 | 984 | return 0; |
726f6388 JA |
985 | } |
986 | ||
987 | void | |
988 | start_pipeline () | |
989 | { | |
990 | already_making_children = 1; | |
991 | } | |
992 | ||
f73dda09 JA |
993 | void |
994 | stop_making_children () | |
995 | { | |
996 | already_making_children = 0; | |
997 | } | |
998 | ||
a0c0a00f CR |
999 | /* The name is kind of a misnomer, but it's what the job control code uses. */ |
1000 | void | |
1001 | without_job_control () | |
1002 | { | |
1003 | stop_making_children (); | |
1004 | last_made_pid = NO_PID; /* XXX */ | |
1005 | } | |
1006 | ||
d166f048 JA |
1007 | int |
1008 | get_job_by_pid (pid, block) | |
1009 | pid_t pid; | |
1010 | int block; | |
1011 | { | |
1012 | int i; | |
1013 | ||
1014 | i = find_index_by_pid (pid); | |
1015 | return ((i == NO_PID) ? PROC_BAD : i); | |
1016 | } | |
1017 | ||
726f6388 JA |
1018 | /* Print descriptive information about the job with leader pid PID. */ |
1019 | void | |
1020 | describe_pid (pid) | |
1021 | pid_t pid; | |
1022 | { | |
f73dda09 | 1023 | fprintf (stderr, "%ld\n", (long) pid); |
726f6388 | 1024 | } |
ccc6cda3 | 1025 | |
a0c0a00f | 1026 | int |
495aee44 CR |
1027 | freeze_jobs_list () |
1028 | { | |
a0c0a00f | 1029 | return 0; |
495aee44 CR |
1030 | } |
1031 | ||
ccc6cda3 JA |
1032 | void |
1033 | unfreeze_jobs_list () | |
1034 | { | |
1035 | } | |
bb70624e JA |
1036 | |
1037 | int | |
1038 | count_all_jobs () | |
1039 | { | |
1040 | return 0; | |
1041 | } |