1 /* The thing that makes children, remembers them, and contains wait loops. */
3 /* This file works under BSD, System V, minix, and Posix systems. */
5 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
9 Bash is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 1, or (at your option) any later
14 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with Bash; see the file COPYING. If not, write to the Free Software
21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "bashtypes.h"
37 #if defined (BUFFERED_INPUT)
41 #if !defined (USG) && !defined (_POSIX_VERSION)
44 # if defined (_POSIX_VERSION)
49 # include <sys/ttold.h>
51 # endif /* !POSIX_VERSION */
52 #endif /* USG && _POSIX_VERSION */
54 #if !defined (SIGABRT)
55 # define SIGABRT SIGIOT
58 #if defined (USG) || defined (_POSIX_VERSION)
59 # define killpg(pg, sig) kill(-(pg),(sig))
60 #endif /* USG || _POSIX_VERSION */
63 # define siginterrupt(sig, code)
66 #if defined (_POSIX_VERSION)
67 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
69 # define WAITPID(pid, statusp, options) wait (statusp)
70 #endif /* !_POSIX_VERSION */
76 extern int interactive, interactive_shell, login_shell;
77 extern int subshell_environment;
78 extern int last_command_exit_value;
79 #if defined (_POSIX_VERSION)
80 extern sigset_t top_level_mask;
83 pid_t last_made_pid = NO_PID;
84 pid_t last_asynchronous_pid = NO_PID;
86 /* Call this when you start making children. */
87 int already_making_children = 0;
89 #if defined (_POSIX_VERSION)
90 static void reap_zombie_children ();
95 int status; /* Exit status of PID or 128 + fatal signal number */
98 static struct proc_status *pid_list = (struct proc_status *)NULL;
99 static int pid_list_size = 0;
102 #define PROC_STILL_ALIVE -2
104 /* Allocate new, or grow existing PID_LIST. */
109 int old = pid_list_size;
112 pid_list = (struct proc_status *)
113 xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
115 /* None of the newly allocated slots have process id's yet. */
116 for (i = old; i < pid_list_size; i++)
117 pid_list[i].pid = NO_PID;
120 /* Return the offset within the PID_LIST array of an empty slot. This can
121 create new slots if all of the existing slots are taken. */
127 for (i = 0; i < pid_list_size; i++)
128 if (pid_list[i].pid == NO_PID)
131 if (i == pid_list_size)
137 /* Return the offset within the PID_LIST array of a slot containing PID,
138 or the value NO_PID if the pid wasn't found. */
140 find_index_by_pid (pid)
145 for (i = 0; i < pid_list_size; i++)
146 if (pid_list[i].pid == pid)
152 /* Return the status of PID as looked up in the PID_LIST array. A
153 return value of PROC_BAD indicates that PID wasn't found. */
155 find_status_by_pid (pid)
160 i = find_index_by_pid (pid);
163 return (pid_list[i].status);
166 /* Give PID the status value STATUS in the PID_LIST array. */
168 set_pid_status (pid, status)
174 slot = find_index_by_pid (pid);
178 if (WIFSIGNALED (status))
179 pid_list[slot].status = 128 + WTERMSIG (status);
181 pid_list[slot].status = WEXITSTATUS (status);
190 slot = find_proc_slot ();
191 pid_list[slot].pid = pid;
192 pid_list[slot].status = PROC_STILL_ALIVE;
200 #if defined (_POSIX_VERSION)
201 reap_zombie_children ();
204 for (i = 0; i < pid_list_size; i++)
205 if (pid_list[i].status != PROC_STILL_ALIVE)
206 pid_list[i].pid = NO_PID;
209 /* Initialize the job control mechanism, and set up the tty stuff. */
215 #if !defined (READLINE) && defined (TIOCGWINSZ) && defined (SIGWINCH)
216 static SigHandler *old_winch;
219 sigwinch_sighandler (sig)
224 #if defined (USG) && !defined (_POSIX_VERSION)
225 set_signal_handler (SIGWINCH, sigwinch_sighandler);
226 #endif /* USG && !_POSIX_VERSION */
227 if ((ioctl (0, TIOCGWINSZ, &win) == 0) &&
228 win.ws_row > 0 && win.ws_col > 0)
229 set_lines_and_columns (win.ws_row, win.ws_col);
231 #endif /* !READLINE && TIOCGWINSZ && SIGWINCH */
233 /* Setup this shell to handle C-C, etc. */
235 initialize_job_signals ()
237 set_signal_handler (SIGINT, sigint_sighandler);
238 #if !defined (READLINE) && defined (TIOCGWINSZ) && defined (SIGWINCH)
239 set_signal_handler (SIGWINCH, sigwinch_sighandler);
240 #endif /* !READLINE && TIOCGWINSZ && SIGWINCH */
242 /* If this is a login shell we don't wish to be disturbed by
246 #if defined (SIGTSTP)
247 set_signal_handler (SIGTSTP, SIG_IGN);
248 set_signal_handler (SIGTTOU, SIG_IGN);
249 set_signal_handler (SIGTTIN, SIG_IGN);
254 #if defined (_POSIX_VERSION)
255 /* Collect the status of all zombie children so that their system
256 resources can be deallocated. */
258 reap_zombie_children ()
260 #if defined (WNOHANG)
264 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
265 set_pid_status (pid, status);
268 #endif /* _POSIX_VERSION */
270 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
271 COMMAND is just for remembering the name of the command; we don't do
272 anything else with it. ASYNC_P says what to do with the tty. If
273 non-zero, then don't give it away. */
275 make_child (command, async_p)
280 #if defined (_POSIX_VERSION)
282 #endif /* _POSIX_VERSION */
284 /* Discard saved memory. */
290 #if defined (BUFFERED_INPUT)
291 /* If default_buffered_input is active, we are reading a script. If
292 the command is asynchronous, we have already duplicated /dev/null
293 as fd 0, but have not changed the buffered stream corresponding to
294 the old fd 0. We don't want to sync the stream in this case. */
295 if (default_buffered_input != -1 &&
296 (!async_p || default_buffered_input > 0))
297 sync_buffered_stream (default_buffered_input);
298 #endif /* BUFFERED_INPUT */
300 /* Create the child, handle severe errors. */
301 #if defined (_POSIX_VERSION)
303 #endif /* _POSIX_VERSION */
305 if ((pid = fork ()) < 0)
307 #if defined (_POSIX_VERSION)
308 /* Posix systems with a non-blocking waitpid () system call available
309 get another chance after zombies are reaped. */
310 if (errno == EAGAIN && retry)
312 reap_zombie_children ();
316 #endif /* _POSIX_VERSION */
318 internal_error ("fork: %s", strerror (errno));
320 throw_to_top_level ();
325 #if defined (BUFFERED_INPUT)
326 if (default_buffered_input > 0)
328 close_buffered_fd (default_buffered_input);
329 default_buffered_input = bash_input.location.buffered_fd = -1;
331 #endif /* BUFFERED_INPUT */
333 #if defined (_POSIX_VERSION)
334 /* Restore top-level signal mask. */
335 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
338 /* Ignore INT and QUIT in asynchronous children. */
342 /* This now done by setup_async_signals (). */
343 set_signal_handler (SIGINT, SIG_IGN);
344 set_signal_handler (SIGQUIT, SIG_IGN);
346 last_asynchronous_pid = getpid ();
349 #if defined (SIGTSTP)
350 set_signal_handler (SIGTSTP, SIG_DFL);
351 set_signal_handler (SIGTTIN, SIG_DFL);
352 set_signal_handler (SIGTTOU, SIG_DFL);
362 last_asynchronous_pid = pid;
369 /* Wait for a single pid (PID) and return its exit status. */
370 wait_for_single_pid (pid)
377 pstatus = find_status_by_pid (pid);
379 if (pstatus == PROC_BAD)
382 if (pstatus != PROC_STILL_ALIVE)
385 siginterrupt (SIGINT, 1);
386 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
390 if (errno != EINTR && errno != ECHILD)
392 siginterrupt (SIGINT, 0);
397 else if (got_pid > 0)
398 set_pid_status (got_pid, status);
401 set_pid_status (got_pid, status);
402 siginterrupt (SIGINT, 0);
405 if (WIFSIGNALED (status))
406 return (128 + WTERMSIG (status));
408 return (WEXITSTATUS (status));
411 /* Wait for all of the shell's children to exit. */
413 wait_for_background_pids ()
418 /* If we aren't using job control, we let the kernel take care of the
419 bookkeeping for us. wait () will return -1 and set errno to ECHILD
420 when there are no more unwaited-for child processes on both
421 4.2 BSD-based and System V-based systems. */
423 siginterrupt (SIGINT, 1);
425 /* Wait for ECHILD */
426 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
427 set_pid_status (got_pid, status);
429 if (errno != EINTR && errno != ECHILD)
431 siginterrupt (SIGINT, 0);
435 siginterrupt (SIGINT, 0);
439 /* Handle SIGINT while we are waiting for children in a script to exit.
440 All interrupts are effectively ignored by the shell, but allowed to
441 kill a running job. */
443 wait_sigint_handler (sig)
447 /* Run a trap handler if one has been defined. */
448 maybe_call_trap_handler (sig);
451 #if !defined (VOID_SIGHANDLER)
453 #endif /* !VOID_SIGHANDLER */
456 /* Wait for pid (one of our children) to terminate. This is called only
457 by the execution code in execute_cmd.c. */
462 int return_val, pstatus;
465 SigHandler *old_sigint_handler;
467 pstatus = find_status_by_pid (pid);
469 if (pstatus == PROC_BAD)
472 if (pstatus != PROC_STILL_ALIVE)
475 /* If we are running a script, ignore SIGINT while we're waiting for
476 a child to exit. The loop below does some of this, but not all. */
477 if (!interactive_shell)
478 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
480 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
482 if (got_pid < 0 && errno == ECHILD)
484 #if !defined (_POSIX_VERSION)
485 status.w_termsig = status.w_retcode = 0;
488 #endif /* _POSIX_VERSION */
491 else if (got_pid < 0 && errno != EINTR)
492 programming_error ("got errno %d while waiting for %d", errno, pid);
493 else if (got_pid > 0)
494 set_pid_status (got_pid, status);
497 set_pid_status (got_pid, status);
499 #if defined (_POSIX_VERSION)
501 reap_zombie_children ();
502 #endif /* _POSIX_VERSION */
504 if (!interactive_shell)
506 set_signal_handler (SIGINT, old_sigint_handler);
507 /* If the job exited because of SIGINT, make sure the shell acts as if
508 it had received one also. */
509 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
511 if (maybe_call_trap_handler (SIGINT) == 0)
512 (*old_sigint_handler) (SIGINT);
516 /* Default return value. */
517 /* ``a full 8 bits of status is returned'' */
518 if (WIFSIGNALED (status))
519 return_val = 128 + WTERMSIG (status);
521 return_val = WEXITSTATUS (status);
523 if (!WIFSTOPPED (status) && WIFSIGNALED (status) &&
524 (WTERMSIG (status) != SIGINT))
526 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
527 if (WIFCORED (status))
528 fprintf (stderr, " (core dumped)");
529 fprintf (stderr, "\n");
532 if (interactive_shell && !subshell_environment)
534 if (WIFSIGNALED (status) || WIFSTOPPED (status))
543 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
544 If PID does belong to a job, and the job is stopped, then CONTinue the
545 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
546 then kill the process group associated with PID. */
548 kill_pid (pid, signal, group)
555 result = killpg (pid, signal);
557 result = kill (pid, signal);
562 #if defined (_POSIX_VERSION)
563 static struct termios shell_tty_info;
566 static struct termio shell_tty_info;
568 static struct sgttyb shell_tty_info;
570 #endif /* _POSIX_VERSION */
572 static int got_tty_state = 0;
574 /* Fill the contents of shell_tty_info with the current tty info. */
577 int tty = open ("/dev/tty", O_RDONLY);
580 #if defined (_POSIX_VERSION)
581 tcgetattr (tty, &shell_tty_info);
584 ioctl (tty, TCGETA, &shell_tty_info);
586 ioctl (tty, TIOCGETP, &shell_tty_info);
594 /* Make the current tty use the state in shell_tty_info. */
597 int tty = open ("/dev/tty", O_RDONLY);
605 #if defined (_POSIX_VERSION)
606 tcsetattr (tty, TCSADRAIN, &shell_tty_info);
609 ioctl (tty, TCSETAW, &shell_tty_info); /* Wait for output, no flush */
611 ioctl (tty, TIOCSETN, &shell_tty_info);
618 /* Give the terminal to PGRP. */
619 give_terminal_to (pgrp)
624 /* Stop a pipeline. */
625 stop_pipeline (async, ignore)
629 already_making_children = 0;
635 already_making_children = 1;
638 /* Print descriptive information about the job with leader pid PID. */
643 fprintf (stderr, "%d\n", (int) pid);