1 /* The thing that makes children, remembers them, and contains wait loops. */
3 /* This file works under BSD, System V, minix, and Posix systems. It does
4 not implement job control. */
6 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
8 This file is part of GNU Bash, the Bourne Again SHell.
10 Bash is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License along
21 with Bash; see the file COPYING. If not, write to the Free Software
22 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
26 #include "bashtypes.h"
29 #if defined (HAVE_UNISTD_H)
37 #if defined (BUFFERED_INPUT)
41 /* Need to include this up here for *_TTY_DRIVER definitions. */
44 #if !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
45 /* For struct winsize on SCO */
46 /* sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
47 # if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
48 # if defined (HAVE_SYS_STREAM_H)
49 # include <sys/stream.h>
51 # include <sys/ptem.h>
52 # endif /* HAVE_SYS_PTEM_H && TIOCGWINSZ && SIGWINCH */
53 #endif /* !STRUCT_WINSIZE_IN_SYS_IOCTL */
58 #include "builtins/builtext.h" /* for wait_builtin */
60 #define DEFAULT_CHILD_MAX 32
62 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
63 # define killpg(pg, sig) kill(-(pg),(sig))
64 #endif /* USG || _POSIX_VERSION */
66 #if !defined (HAVE_SIGINTERRUPT)
67 # define siginterrupt(sig, code)
68 #endif /* !HAVE_SIGINTERRUPT */
70 #if defined (HAVE_WAITPID)
71 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
73 # define WAITPID(pid, statusp, options) wait (statusp)
74 #endif /* !HAVE_WAITPID */
76 /* Return the fd from which we are actually getting input. */
77 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
83 #if defined (READLINE)
84 extern void rl_set_screen_size __P((int, int));
87 extern int interactive, interactive_shell, login_shell;
88 extern int subshell_environment;
89 extern int last_command_exit_value;
90 extern int interrupt_immediately;
91 extern sh_builtin_func_t *this_shell_builtin;
92 #if defined (HAVE_POSIX_SIGNALS)
93 extern sigset_t top_level_mask;
95 extern procenv_t wait_intr_buf;
96 extern int wait_signal_received;
98 pid_t last_made_pid = NO_PID;
99 pid_t last_asynchronous_pid = NO_PID;
101 /* Call this when you start making children. */
102 int already_making_children = 0;
104 /* The controlling tty for this shell. */
107 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
108 exits from get_tty_state(). */
109 int check_window_size;
111 /* STATUS and FLAGS are only valid if pid != NO_PID
112 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
115 int status; /* Exit status of PID or 128 + fatal signal number */
119 /* Values for proc_status.flags */
120 #define PROC_RUNNING 0x01
121 #define PROC_NOTIFIED 0x02
122 #define PROC_ASYNC 0x04
124 /* Return values from find_status_by_pid */
126 #define PROC_STILL_ALIVE -2
128 static struct proc_status *pid_list = (struct proc_status *)NULL;
129 static int pid_list_size;
130 static int wait_sigint_received;
132 static long child_max = -1L;
134 static void alloc_pid_list __P((void));
135 static int find_proc_slot __P((void));
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));
139 static void set_pid_status __P((pid_t, WAIT));
140 static void set_pid_flags __P((pid_t, int));
141 static void unset_pid_flags __P((pid_t, int));
142 static void add_pid __P((pid_t, int));
143 static void mark_dead_jobs_as_notified __P((int));
145 static void get_new_window_size __P((int));
146 static sighandler sigwinch_sighandler __P((int));
147 static sighandler wait_sigint_handler __P((int));
149 #if defined (HAVE_WAITPID)
150 static void reap_zombie_children __P((void));
153 static void restore_sigint_handler __P((void));
155 /* Allocate new, or grow existing PID_LIST. */
160 int old = pid_list_size;
163 pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
165 /* None of the newly allocated slots have process id's yet. */
166 for (i = old; i < pid_list_size; i++)
167 pid_list[i].pid = NO_PID;
170 /* Return the offset within the PID_LIST array of an empty slot. This can
171 create new slots if all of the existing slots are taken. */
177 for (i = 0; i < pid_list_size; i++)
178 if (pid_list[i].pid == NO_PID)
181 if (i == pid_list_size)
187 /* Return the offset within the PID_LIST array of a slot containing PID,
188 or the value NO_PID if the pid wasn't found. */
190 find_index_by_pid (pid)
195 for (i = 0; i < pid_list_size; i++)
196 if (pid_list[i].pid == pid)
202 /* Return the status of PID as looked up in the PID_LIST array. A
203 return value of PROC_BAD indicates that PID wasn't found. */
205 find_status_by_pid (pid)
210 i = find_index_by_pid (pid);
213 if (pid_list[i].flags & PROC_RUNNING)
214 return (PROC_STILL_ALIVE);
215 return (pid_list[i].status);
219 process_exit_status (status)
222 if (WIFSIGNALED (status))
223 return (128 + WTERMSIG (status));
225 return (WEXITSTATUS (status));
228 /* Give PID the status value STATUS in the PID_LIST array. */
230 set_pid_status (pid, status)
236 slot = find_index_by_pid (pid);
240 pid_list[slot].status = process_exit_status (status);
241 pid_list[slot].flags &= ~PROC_RUNNING;
242 /* If it's not a background process, mark it as notified so it gets
244 if ((pid_list[slot].flags & PROC_ASYNC) == 0)
245 pid_list[slot].flags |= PROC_NOTIFIED;
248 /* Give PID the flags FLAGS in the PID_LIST array. */
250 set_pid_flags (pid, flags)
256 slot = find_index_by_pid (pid);
260 pid_list[slot].flags |= flags;
263 /* Unset FLAGS for PID in the pid list */
265 unset_pid_flags (pid, flags)
271 slot = find_index_by_pid (pid);
275 pid_list[slot].flags &= ~flags;
285 slot = find_proc_slot ();
287 pid_list[slot].pid = pid;
288 pid_list[slot].status = -1;
289 pid_list[slot].flags = PROC_RUNNING;
291 pid_list[slot].flags |= PROC_ASYNC;
295 mark_dead_jobs_as_notified (force)
298 register int i, ndead;
300 /* first, count the number of non-running async jobs if FORCE == 0 */
301 for (i = ndead = 0; force == 0 && i < pid_list_size; i++)
303 if (pid_list[i].pid == NO_PID)
305 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
306 (pid_list[i].flags & PROC_ASYNC))
311 child_max = getmaxchild ();
313 child_max = DEFAULT_CHILD_MAX;
315 if (force == 0 && ndead <= child_max)
318 /* If FORCE == 0, we just mark as many non-running async jobs as notified
319 to bring us under the CHILD_MAX limit. */
320 for (i = 0; i < pid_list_size; i++)
322 if (pid_list[i].pid == NO_PID)
324 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
325 pid_list[i].pid != last_asynchronous_pid)
327 pid_list[i].flags |= PROC_NOTIFIED;
328 if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max)
334 /* Remove all dead, notified jobs from the pid_list. */
340 #if defined (HAVE_WAITPID)
341 reap_zombie_children ();
344 for (i = 0; i < pid_list_size; i++)
346 if ((pid_list[i].flags & PROC_RUNNING) == 0 &&
347 (pid_list[i].flags & PROC_NOTIFIED))
348 pid_list[i].pid = NO_PID;
357 mark_dead_jobs_as_notified (0);
358 cleanup_dead_jobs ();
361 /* Initialize the job control mechanism, and set up the tty stuff. */
362 initialize_job_control (force)
365 shell_tty = fileno (stderr);
371 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
372 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
375 get_new_window_size (from_sig)
382 if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
383 win.ws_row > 0 && win.ws_col > 0)
386 shell_tty_info.c_winsize = win; /* structure copying */
388 sh_set_lines_and_columns (win.ws_row, win.ws_col);
389 #if defined (READLINE)
390 rl_set_screen_size (win.ws_row, win.ws_col);
396 sigwinch_sighandler (sig)
399 #if defined (MUST_REINSTALL_SIGHANDLERS)
400 set_signal_handler (SIGWINCH, sigwinch_sighandler);
401 #endif /* MUST_REINSTALL_SIGHANDLERS */
402 get_new_window_size (1);
406 get_new_window_size (from_sig)
410 #endif /* TIOCGWINSZ && SIGWINCH */
413 set_sigwinch_handler ()
415 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
416 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
421 unset_sigwinch_handler ()
423 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
424 set_signal_handler (SIGWINCH, old_winch);
428 /* Setup this shell to handle C-C, etc. */
430 initialize_job_signals ()
432 set_signal_handler (SIGINT, sigint_sighandler);
433 set_sigwinch_handler ();
435 /* If this is a login shell we don't wish to be disturbed by
438 ignore_tty_job_signals ();
441 #if defined (HAVE_WAITPID)
442 /* Collect the status of all zombie children so that their system
443 resources can be deallocated. */
445 reap_zombie_children ()
447 #if defined (WNOHANG)
451 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
452 set_pid_status (pid, status);
455 #endif /* WAITPID && WNOHANG */
457 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
458 COMMAND is just for remembering the name of the command; we don't do
459 anything else with it. ASYNC_P says what to do with the tty. If
460 non-zero, then don't give it away. */
462 make_child (command, async_p)
467 #if defined (HAVE_WAITPID)
469 #endif /* HAVE_WAITPID */
471 /* Discard saved memory. */
477 #if defined (BUFFERED_INPUT)
478 /* If default_buffered_input is active, we are reading a script. If
479 the command is asynchronous, we have already duplicated /dev/null
480 as fd 0, but have not changed the buffered stream corresponding to
481 the old fd 0. We don't want to sync the stream in this case. */
482 if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
483 sync_buffered_stream (default_buffered_input);
484 #endif /* BUFFERED_INPUT */
486 /* Create the child, handle severe errors. */
487 #if defined (HAVE_WAITPID)
489 #endif /* HAVE_WAITPID */
491 if ((pid = fork ()) < 0)
493 #if defined (HAVE_WAITPID)
494 /* Posix systems with a non-blocking waitpid () system call available
495 get another chance after zombies are reaped. */
496 if (errno == EAGAIN && retry)
498 reap_zombie_children ();
502 #endif /* HAVE_WAITPID */
506 throw_to_top_level ();
511 #if defined (BUFFERED_INPUT)
512 unset_bash_input (0);
513 #endif /* BUFFERED_INPUT */
515 #if defined (HAVE_POSIX_SIGNALS)
516 /* Restore top-level signal mask. */
517 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
520 /* Ignore INT and QUIT in asynchronous children. */
522 last_asynchronous_pid = getpid ();
524 default_tty_job_signals ();
533 last_asynchronous_pid = pid;
535 add_pid (pid, async_p);
541 ignore_tty_job_signals ()
543 #if defined (SIGTSTP)
544 set_signal_handler (SIGTSTP, SIG_IGN);
545 set_signal_handler (SIGTTIN, SIG_IGN);
546 set_signal_handler (SIGTTOU, SIG_IGN);
551 default_tty_job_signals ()
553 #if defined (SIGTSTP)
554 set_signal_handler (SIGTSTP, SIG_DFL);
555 set_signal_handler (SIGTTIN, SIG_DFL);
556 set_signal_handler (SIGTTOU, SIG_DFL);
560 /* Wait for a single pid (PID) and return its exit status. Called by
563 wait_for_single_pid (pid)
570 pstatus = find_status_by_pid (pid);
572 if (pstatus == PROC_BAD)
574 internal_error ("wait: pid %ld is not a child of this shell", (long)pid);
578 if (pstatus != PROC_STILL_ALIVE)
581 siginterrupt (SIGINT, 1);
582 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
586 if (errno != EINTR && errno != ECHILD)
588 siginterrupt (SIGINT, 0);
593 else if (got_pid > 0)
594 set_pid_status (got_pid, status);
599 set_pid_status (got_pid, status);
600 set_pid_flags (got_pid, PROC_NOTIFIED);
603 siginterrupt (SIGINT, 0);
606 return (got_pid > 0 ? process_exit_status (status) : -1);
609 /* Wait for all of the shell's children to exit. Called by the `wait'
612 wait_for_background_pids ()
617 /* If we aren't using job control, we let the kernel take care of the
618 bookkeeping for us. wait () will return -1 and set errno to ECHILD
619 when there are no more unwaited-for child processes on both
620 4.2 BSD-based and System V-based systems. */
622 siginterrupt (SIGINT, 1);
624 /* Wait for ECHILD */
625 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
626 set_pid_status (got_pid, status);
628 if (errno != EINTR && errno != ECHILD)
630 siginterrupt (SIGINT, 0);
634 siginterrupt (SIGINT, 0);
637 mark_dead_jobs_as_notified (1);
638 cleanup_dead_jobs ();
641 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
642 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
643 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
646 restore_sigint_handler ()
648 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
650 set_signal_handler (SIGINT, old_sigint_handler);
651 old_sigint_handler = INVALID_SIGNAL_HANDLER;
655 /* Handle SIGINT while we are waiting for children in a script to exit.
656 All interrupts are effectively ignored by the shell, but allowed to
657 kill a running job. */
659 wait_sigint_handler (sig)
662 SigHandler *sigint_handler;
664 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
665 what POSIX.2 says (see builtins/wait.def for more info). */
666 if (this_shell_builtin && this_shell_builtin == wait_builtin &&
667 signal_is_trapped (SIGINT) &&
668 ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
670 last_command_exit_value = EXECUTION_FAILURE;
671 restore_sigint_handler ();
672 interrupt_immediately = 0;
673 trap_handler (SIGINT); /* set pending_traps[SIGINT] */
674 wait_signal_received = SIGINT;
675 longjmp (wait_intr_buf, 1);
678 if (interrupt_immediately)
680 last_command_exit_value = EXECUTION_FAILURE;
681 restore_sigint_handler ();
686 wait_sigint_received = 1;
691 /* Wait for pid (one of our children) to terminate. This is called only
692 by the execution code in execute_cmd.c. */
697 int return_val, pstatus;
701 pstatus = find_status_by_pid (pid);
703 if (pstatus == PROC_BAD)
706 if (pstatus != PROC_STILL_ALIVE)
709 /* If we are running a script, ignore SIGINT while we're waiting for
710 a child to exit. The loop below does some of this, but not all. */
711 wait_sigint_received = 0;
712 if (interactive_shell == 0)
713 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
715 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
717 if (got_pid < 0 && errno == ECHILD)
719 #if !defined (_POSIX_VERSION)
720 status.w_termsig = status.w_retcode = 0;
723 #endif /* _POSIX_VERSION */
726 else if (got_pid < 0 && errno != EINTR)
727 programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
728 else if (got_pid > 0)
729 set_pid_status (got_pid, status);
733 set_pid_status (got_pid, status);
735 #if defined (HAVE_WAITPID)
737 reap_zombie_children ();
738 #endif /* HAVE_WAITPID */
740 if (interactive_shell == 0)
742 SigHandler *temp_handler;
744 temp_handler = old_sigint_handler;
745 restore_sigint_handler ();
747 /* If the job exited because of SIGINT, make sure the shell acts as if
748 it had received one also. */
749 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
752 if (maybe_call_trap_handler (SIGINT) == 0)
754 if (temp_handler == SIG_DFL)
755 termination_unwind_protect (SIGINT);
756 else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
757 (*temp_handler) (SIGINT);
762 /* Default return value. */
763 /* ``a full 8 bits of status is returned'' */
764 return_val = process_exit_status (status);
766 #if !defined (DONT_REPORT_SIGPIPE)
767 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
768 (WTERMSIG (status) != SIGINT))
770 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
771 (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
774 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
775 if (WIFCORED (status))
776 fprintf (stderr, " (core dumped)");
777 fprintf (stderr, "\n");
780 if (interactive_shell && subshell_environment == 0)
782 if (WIFSIGNALED (status) || WIFSTOPPED (status))
791 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
792 If PID does belong to a job, and the job is stopped, then CONTinue the
793 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
794 then kill the process group associated with PID. */
796 kill_pid (pid, signal, group)
802 result = group ? killpg (pid, signal) : kill (pid, signal);
806 static TTYSTRUCT shell_tty_info;
807 static int got_tty_state;
809 /* Fill the contents of shell_tty_info with the current tty info. */
817 ttgetattr (tty, &shell_tty_info);
819 if (check_window_size)
820 get_new_window_size (0);
824 /* Make the current tty use the state in shell_tty_info. */
833 if (got_tty_state == 0)
835 ttsetattr (tty, &shell_tty_info);
840 /* Give the terminal to PGRP. */
841 give_terminal_to (pgrp, force)
847 /* Stop a pipeline. */
849 stop_pipeline (async, ignore)
853 already_making_children = 0;
860 already_making_children = 1;
864 stop_making_children ()
866 already_making_children = 0;
870 get_job_by_pid (pid, block)
876 i = find_index_by_pid (pid);
877 return ((i == NO_PID) ? PROC_BAD : i);
880 /* Print descriptive information about the job with leader pid PID. */
885 fprintf (stderr, "%ld\n", (long) pid);
889 unfreeze_jobs_list ()