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 #if !defined (CHILD_MAX)
64 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
65 # define killpg(pg, sig) kill(-(pg),(sig))
66 #endif /* USG || _POSIX_VERSION */
68 #if !defined (HAVE_SIGINTERRUPT)
69 # define siginterrupt(sig, code)
70 #endif /* !HAVE_SIGINTERRUPT */
72 #if defined (HAVE_WAITPID)
73 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
75 # define WAITPID(pid, statusp, options) wait (statusp)
76 #endif /* !HAVE_WAITPID */
78 /* Return the fd from which we are actually getting input. */
79 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
85 #if defined (READLINE)
86 extern void rl_set_screen_size __P((int, int));
89 extern int interactive, interactive_shell, login_shell;
90 extern int subshell_environment;
91 extern int last_command_exit_value;
92 extern int interrupt_immediately;
93 extern sh_builtin_func_t *this_shell_builtin;
94 #if defined (HAVE_POSIX_SIGNALS)
95 extern sigset_t top_level_mask;
97 extern procenv_t wait_intr_buf;
99 pid_t last_made_pid = NO_PID;
100 pid_t last_asynchronous_pid = NO_PID;
102 /* Call this when you start making children. */
103 int already_making_children = 0;
105 /* The controlling tty for this shell. */
108 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
109 exits from get_tty_state(). */
110 int check_window_size;
112 /* STATUS and FLAGS are only valid if pid != NO_PID
113 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
116 int status; /* Exit status of PID or 128 + fatal signal number */
120 /* Values for proc_status.flags */
121 #define PROC_RUNNING 0x01
122 #define PROC_NOTIFIED 0x02
123 #define PROC_ASYNC 0x04
125 /* Return values from find_status_by_pid */
127 #define PROC_STILL_ALIVE -2
129 static struct proc_status *pid_list = (struct proc_status *)NULL;
130 static int pid_list_size;
131 static int wait_sigint_received;
133 static void alloc_pid_list __P((void));
134 static int find_proc_slot __P((void));
135 static int find_index_by_pid __P((pid_t));
136 static int find_status_by_pid __P((pid_t));
137 static int process_exit_status __P((WAIT));
138 static void set_pid_status __P((pid_t, WAIT));
139 static void set_pid_flags __P((pid_t, int));
140 static void unset_pid_flags __P((pid_t, int));
141 static void add_pid __P((pid_t, int));
142 static void mark_dead_jobs_as_notified __P((int));
144 static void get_new_window_size __P((int));
145 static sighandler sigwinch_sighandler __P((int));
146 static sighandler wait_signal_handler __P((int));
148 #if defined (HAVE_WAITPID)
149 static void reap_zombie_children __P((void));
152 static void restore_sigint_handler __P((void));
154 /* Allocate new, or grow existing PID_LIST. */
159 int old = pid_list_size;
162 pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
164 /* None of the newly allocated slots have process id's yet. */
165 for (i = old; i < pid_list_size; i++)
166 pid_list[i].pid = NO_PID;
169 /* Return the offset within the PID_LIST array of an empty slot. This can
170 create new slots if all of the existing slots are taken. */
176 for (i = 0; i < pid_list_size; i++)
177 if (pid_list[i].pid == NO_PID)
180 if (i == pid_list_size)
186 /* Return the offset within the PID_LIST array of a slot containing PID,
187 or the value NO_PID if the pid wasn't found. */
189 find_index_by_pid (pid)
194 for (i = 0; i < pid_list_size; i++)
195 if (pid_list[i].pid == pid)
201 /* Return the status of PID as looked up in the PID_LIST array. A
202 return value of PROC_BAD indicates that PID wasn't found. */
204 find_status_by_pid (pid)
209 i = find_index_by_pid (pid);
212 if (pid_list[i].flags & PROC_RUNNING)
213 return (PROC_STILL_ALIVE);
214 return (pid_list[i].status);
218 process_exit_status (status)
221 if (WIFSIGNALED (status))
222 return (128 + WTERMSIG (status));
224 return (WEXITSTATUS (status));
227 /* Give PID the status value STATUS in the PID_LIST array. */
229 set_pid_status (pid, status)
235 slot = find_index_by_pid (pid);
239 pid_list[slot].status = process_exit_status (status);
240 pid_list[slot].flags &= ~PROC_RUNNING;
241 /* If it's not a background process, mark it as notified so it gets
243 if ((pid_list[slot].flags & PROC_ASYNC) == 0)
244 pid_list[slot].flags |= PROC_NOTIFIED;
247 /* Give PID the flags FLAGS in the PID_LIST array. */
249 set_pid_flags (pid, flags)
255 slot = find_index_by_pid (pid);
259 pid_list[slot].flags |= flags;
262 /* Unset FLAGS for PID in the pid list */
264 unset_pid_flags (pid, flags)
270 slot = find_index_by_pid (pid);
274 pid_list[slot].flags &= ~flags;
284 slot = find_proc_slot ();
286 pid_list[slot].pid = pid;
287 pid_list[slot].status = -1;
288 pid_list[slot].flags = PROC_RUNNING;
290 pid_list[slot].flags |= PROC_ASYNC;
294 mark_dead_jobs_as_notified (force)
297 register int i, ndead;
299 /* first, count the number of non-running async jobs if FORCE == 0 */
300 for (i = ndead = 0; force == 0 && i < pid_list_size; i++)
302 if (pid_list[i].pid == NO_PID)
304 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
305 (pid_list[i].flags & PROC_ASYNC))
309 if (force == 0 && ndead <= CHILD_MAX)
312 /* If FORCE == 0, we just mark as many non-running async jobs as notified
313 to bring us under the CHILD_MAX limit. */
314 for (i = 0; i < pid_list_size; i++)
316 if (pid_list[i].pid == NO_PID)
318 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
319 pid_list[i].pid != last_asynchronous_pid)
321 pid_list[i].flags |= PROC_NOTIFIED;
322 if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= CHILD_MAX)
328 /* Remove all dead, notified jobs from the pid_list. */
334 #if defined (HAVE_WAITPID)
335 reap_zombie_children ();
338 for (i = 0; i < pid_list_size; i++)
340 if ((pid_list[i].flags & PROC_RUNNING) == 0 &&
341 (pid_list[i].flags & PROC_NOTIFIED))
342 pid_list[i].pid = NO_PID;
351 mark_dead_jobs_as_notified (0);
352 cleanup_dead_jobs ();
355 /* Initialize the job control mechanism, and set up the tty stuff. */
356 initialize_job_control (force)
359 shell_tty = fileno (stderr);
365 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
366 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
369 get_new_window_size (from_sig)
376 if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
377 win.ws_row > 0 && win.ws_col > 0)
380 shell_tty_info.c_winsize = win; /* structure copying */
382 sh_set_lines_and_columns (win.ws_row, win.ws_col);
383 #if defined (READLINE)
384 rl_set_screen_size (win.ws_row, win.ws_col);
390 sigwinch_sighandler (sig)
393 #if defined (MUST_REINSTALL_SIGHANDLERS)
394 set_signal_handler (SIGWINCH, sigwinch_sighandler);
395 #endif /* MUST_REINSTALL_SIGHANDLERS */
396 get_new_window_size (1);
400 get_new_window_size (from_sig)
404 #endif /* TIOCGWINSZ && SIGWINCH */
407 set_sigwinch_handler ()
409 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
410 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
415 unset_sigwinch_handler ()
417 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
418 set_signal_handler (SIGWINCH, old_winch);
422 /* Setup this shell to handle C-C, etc. */
424 initialize_job_signals ()
426 set_signal_handler (SIGINT, sigint_sighandler);
427 set_sigwinch_handler ();
429 /* If this is a login shell we don't wish to be disturbed by
432 ignore_tty_job_signals ();
435 #if defined (HAVE_WAITPID)
436 /* Collect the status of all zombie children so that their system
437 resources can be deallocated. */
439 reap_zombie_children ()
441 #if defined (WNOHANG)
445 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
446 set_pid_status (pid, status);
449 #endif /* WAITPID && WNOHANG */
451 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
452 COMMAND is just for remembering the name of the command; we don't do
453 anything else with it. ASYNC_P says what to do with the tty. If
454 non-zero, then don't give it away. */
456 make_child (command, async_p)
461 #if defined (HAVE_WAITPID)
463 #endif /* HAVE_WAITPID */
465 /* Discard saved memory. */
471 #if defined (BUFFERED_INPUT)
472 /* If default_buffered_input is active, we are reading a script. If
473 the command is asynchronous, we have already duplicated /dev/null
474 as fd 0, but have not changed the buffered stream corresponding to
475 the old fd 0. We don't want to sync the stream in this case. */
476 if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
477 sync_buffered_stream (default_buffered_input);
478 #endif /* BUFFERED_INPUT */
480 /* Create the child, handle severe errors. */
481 #if defined (HAVE_WAITPID)
483 #endif /* HAVE_WAITPID */
485 if ((pid = fork ()) < 0)
487 #if defined (HAVE_WAITPID)
488 /* Posix systems with a non-blocking waitpid () system call available
489 get another chance after zombies are reaped. */
490 if (errno == EAGAIN && retry)
492 reap_zombie_children ();
496 #endif /* HAVE_WAITPID */
500 throw_to_top_level ();
505 #if defined (BUFFERED_INPUT)
506 unset_bash_input (0);
507 #endif /* BUFFERED_INPUT */
509 #if defined (HAVE_POSIX_SIGNALS)
510 /* Restore top-level signal mask. */
511 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
514 /* Ignore INT and QUIT in asynchronous children. */
516 last_asynchronous_pid = getpid ();
518 default_tty_job_signals ();
527 last_asynchronous_pid = pid;
529 add_pid (pid, async_p);
535 ignore_tty_job_signals ()
537 #if defined (SIGTSTP)
538 set_signal_handler (SIGTSTP, SIG_IGN);
539 set_signal_handler (SIGTTIN, SIG_IGN);
540 set_signal_handler (SIGTTOU, SIG_IGN);
545 default_tty_job_signals ()
547 #if defined (SIGTSTP)
548 set_signal_handler (SIGTSTP, SIG_DFL);
549 set_signal_handler (SIGTTIN, SIG_DFL);
550 set_signal_handler (SIGTTOU, SIG_DFL);
554 /* Wait for a single pid (PID) and return its exit status. Called by
557 wait_for_single_pid (pid)
564 pstatus = find_status_by_pid (pid);
566 if (pstatus == PROC_BAD)
568 internal_error ("wait: pid %ld is not a child of this shell", (long)pid);
572 if (pstatus != PROC_STILL_ALIVE)
575 siginterrupt (SIGINT, 1);
576 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
580 if (errno != EINTR && errno != ECHILD)
582 siginterrupt (SIGINT, 0);
587 else if (got_pid > 0)
588 set_pid_status (got_pid, status);
591 set_pid_status (got_pid, status);
592 set_pid_flags (got_pid, PROC_NOTIFIED);
594 siginterrupt (SIGINT, 0);
597 return (process_exit_status (status));
600 /* Wait for all of the shell's children to exit. Called by the `wait'
603 wait_for_background_pids ()
608 /* If we aren't using job control, we let the kernel take care of the
609 bookkeeping for us. wait () will return -1 and set errno to ECHILD
610 when there are no more unwaited-for child processes on both
611 4.2 BSD-based and System V-based systems. */
613 siginterrupt (SIGINT, 1);
615 /* Wait for ECHILD */
616 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
617 set_pid_status (got_pid, status);
619 if (errno != EINTR && errno != ECHILD)
621 siginterrupt (SIGINT, 0);
625 siginterrupt (SIGINT, 0);
628 mark_dead_jobs_as_notified (1);
629 cleanup_dead_jobs ();
632 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
633 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
634 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
637 restore_sigint_handler ()
639 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
641 set_signal_handler (SIGINT, old_sigint_handler);
642 old_sigint_handler = INVALID_SIGNAL_HANDLER;
646 /* Handle SIGINT while we are waiting for children in a script to exit.
647 All interrupts are effectively ignored by the shell, but allowed to
648 kill a running job. */
650 wait_sigint_handler (sig)
653 SigHandler *sigint_handler;
655 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
656 what POSIX.2 says (see builtins/wait.def for more info). */
657 if (this_shell_builtin && this_shell_builtin == wait_builtin &&
658 signal_is_trapped (SIGINT) &&
659 ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
661 last_command_exit_value = EXECUTION_FAILURE;
662 restore_sigint_handler ();
663 interrupt_immediately = 0;
664 trap_handler (SIGINT); /* set pending_traps[SIGINT] */
665 longjmp (wait_intr_buf, 1);
668 if (interrupt_immediately)
670 last_command_exit_value = EXECUTION_FAILURE;
671 restore_sigint_handler ();
676 wait_sigint_received = 1;
681 /* Wait for pid (one of our children) to terminate. This is called only
682 by the execution code in execute_cmd.c. */
687 int return_val, pstatus;
691 pstatus = find_status_by_pid (pid);
693 if (pstatus == PROC_BAD)
696 if (pstatus != PROC_STILL_ALIVE)
699 /* If we are running a script, ignore SIGINT while we're waiting for
700 a child to exit. The loop below does some of this, but not all. */
701 wait_sigint_received = 0;
702 if (interactive_shell == 0)
703 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
705 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
707 if (got_pid < 0 && errno == ECHILD)
709 #if !defined (_POSIX_VERSION)
710 status.w_termsig = status.w_retcode = 0;
713 #endif /* _POSIX_VERSION */
716 else if (got_pid < 0 && errno != EINTR)
717 programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
718 else if (got_pid > 0)
719 set_pid_status (got_pid, status);
722 set_pid_status (got_pid, status);
724 #if defined (HAVE_WAITPID)
726 reap_zombie_children ();
727 #endif /* HAVE_WAITPID */
729 if (interactive_shell == 0)
731 SigHandler *temp_handler;
733 temp_handler = old_sigint_handler;
734 restore_sigint_handler ();
736 /* If the job exited because of SIGINT, make sure the shell acts as if
737 it had received one also. */
738 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
741 if (maybe_call_trap_handler (SIGINT) == 0)
743 if (temp_handler == SIG_DFL)
744 termination_unwind_protect (SIGINT);
745 else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
746 (*temp_handler) (SIGINT);
751 /* Default return value. */
752 /* ``a full 8 bits of status is returned'' */
753 return_val = process_exit_status (status);
755 #if !defined (DONT_REPORT_SIGPIPE)
756 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
757 (WTERMSIG (status) != SIGINT))
759 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
760 (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
763 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
764 if (WIFCORED (status))
765 fprintf (stderr, " (core dumped)");
766 fprintf (stderr, "\n");
769 if (interactive_shell && subshell_environment == 0)
771 if (WIFSIGNALED (status) || WIFSTOPPED (status))
780 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
781 If PID does belong to a job, and the job is stopped, then CONTinue the
782 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
783 then kill the process group associated with PID. */
785 kill_pid (pid, signal, group)
791 result = group ? killpg (pid, signal) : kill (pid, signal);
795 static TTYSTRUCT shell_tty_info;
796 static int got_tty_state;
798 /* Fill the contents of shell_tty_info with the current tty info. */
806 ttgetattr (tty, &shell_tty_info);
808 if (check_window_size)
809 get_new_window_size (0);
813 /* Make the current tty use the state in shell_tty_info. */
822 if (got_tty_state == 0)
824 ttsetattr (tty, &shell_tty_info);
829 /* Give the terminal to PGRP. */
830 give_terminal_to (pgrp, force)
836 /* Stop a pipeline. */
838 stop_pipeline (async, ignore)
842 already_making_children = 0;
849 already_making_children = 1;
853 stop_making_children ()
855 already_making_children = 0;
859 get_job_by_pid (pid, block)
865 i = find_index_by_pid (pid);
866 return ((i == NO_PID) ? PROC_BAD : i);
869 /* Print descriptive information about the job with leader pid PID. */
874 fprintf (stderr, "%ld\n", (long) pid);
878 unfreeze_jobs_list ()