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 ();
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 Function *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 #if defined (HAVE_WAITPID)
113 static void reap_zombie_children ();
116 static int wait_sigint_received;
118 /* STATUS and FLAGS are only valid if pid != NO_PID
119 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
122 int status; /* Exit status of PID or 128 + fatal signal number */
126 /* Values for proc_status.flags */
127 #define PROC_RUNNING 0x01
128 #define PROC_NOTIFIED 0x02
129 #define PROC_ASYNC 0x04
131 static struct proc_status *pid_list = (struct proc_status *)NULL;
132 static int pid_list_size;
134 /* Return values from find_status_by_pid */
136 #define PROC_STILL_ALIVE -2
138 /* Allocate new, or grow existing PID_LIST. */
143 int old = pid_list_size;
146 pid_list = (struct proc_status *)
147 xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
149 /* None of the newly allocated slots have process id's yet. */
150 for (i = old; i < pid_list_size; i++)
151 pid_list[i].pid = NO_PID;
154 /* Return the offset within the PID_LIST array of an empty slot. This can
155 create new slots if all of the existing slots are taken. */
161 for (i = 0; i < pid_list_size; i++)
162 if (pid_list[i].pid == NO_PID)
165 if (i == pid_list_size)
171 /* Return the offset within the PID_LIST array of a slot containing PID,
172 or the value NO_PID if the pid wasn't found. */
174 find_index_by_pid (pid)
179 for (i = 0; i < pid_list_size; i++)
180 if (pid_list[i].pid == pid)
186 /* Return the status of PID as looked up in the PID_LIST array. A
187 return value of PROC_BAD indicates that PID wasn't found. */
189 find_status_by_pid (pid)
194 i = find_index_by_pid (pid);
197 if (pid_list[i].flags & PROC_RUNNING)
198 return (PROC_STILL_ALIVE);
199 return (pid_list[i].status);
203 process_exit_status (status)
206 if (WIFSIGNALED (status))
207 return (128 + WTERMSIG (status));
209 return (WEXITSTATUS (status));
212 /* Give PID the status value STATUS in the PID_LIST array. */
214 set_pid_status (pid, status)
220 slot = find_index_by_pid (pid);
224 pid_list[slot].status = process_exit_status (status);
225 pid_list[slot].flags &= ~PROC_RUNNING;
226 /* If it's not a background process, mark it as notified so it gets
228 if ((pid_list[slot].flags & PROC_ASYNC) == 0)
229 pid_list[slot].flags |= PROC_NOTIFIED;
232 /* Give PID the flags FLAGS in the PID_LIST array. */
234 set_pid_flags (pid, flags)
240 slot = find_index_by_pid (pid);
244 pid_list[slot].flags |= flags;
247 /* Unset FLAGS for PID in the pid list */
249 unset_pid_flags (pid, flags)
255 slot = find_index_by_pid (pid);
259 pid_list[slot].flags &= ~flags;
269 slot = find_proc_slot ();
271 pid_list[slot].pid = pid;
272 pid_list[slot].status = -1;
273 pid_list[slot].flags = PROC_RUNNING;
275 pid_list[slot].flags |= PROC_ASYNC;
279 mark_dead_jobs_as_notified (force)
282 register int i, ndead;
284 /* first, count the number of non-running async jobs if FORCE == 0 */
285 for (i = ndead = 0; force == 0 && i < pid_list_size; i++)
287 if (pid_list[i].pid == NO_PID)
289 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
290 (pid_list[i].flags & PROC_ASYNC))
294 if (force == 0 && ndead <= CHILD_MAX)
297 /* If FORCE == 0, we just mark as many non-running async jobs as notified
298 to bring us under the CHILD_MAX limit. */
299 for (i = 0; i < pid_list_size; i++)
301 if (pid_list[i].pid == NO_PID)
303 if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
304 pid_list[i].pid != last_asynchronous_pid)
306 pid_list[i].flags |= PROC_NOTIFIED;
307 if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= CHILD_MAX)
313 /* Remove all dead, notified jobs from the pid_list. */
319 #if defined (HAVE_WAITPID)
320 reap_zombie_children ();
323 for (i = 0; i < pid_list_size; i++)
325 if ((pid_list[i].flags & PROC_RUNNING) == 0 &&
326 (pid_list[i].flags & PROC_NOTIFIED))
327 pid_list[i].pid = NO_PID;
336 mark_dead_jobs_as_notified (0);
337 cleanup_dead_jobs ();
340 /* Initialize the job control mechanism, and set up the tty stuff. */
341 initialize_job_control (force)
344 shell_tty = fileno (stderr);
350 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
351 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
354 get_new_window_size (from_sig)
361 if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
362 win.ws_row > 0 && win.ws_col > 0)
365 shell_tty_info.c_winsize = win; /* structure copying */
367 set_lines_and_columns (win.ws_row, win.ws_col);
368 #if defined (READLINE)
369 _rl_set_screen_size (win.ws_row, win.ws_col);
375 sigwinch_sighandler (sig)
378 #if defined (MUST_REINSTALL_SIGHANDLERS)
379 set_signal_handler (SIGWINCH, sigwinch_sighandler);
380 #endif /* MUST_REINSTALL_SIGHANDLERS */
381 get_new_window_size (1);
385 get_new_window_size (from_sig)
389 #endif /* TIOCGWINSZ && SIGWINCH */
392 set_sigwinch_handler ()
394 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
395 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
400 unset_sigwinch_handler ()
402 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
403 set_signal_handler (SIGWINCH, old_winch);
407 /* Setup this shell to handle C-C, etc. */
409 initialize_job_signals ()
411 set_signal_handler (SIGINT, sigint_sighandler);
412 set_sigwinch_handler ();
414 /* If this is a login shell we don't wish to be disturbed by
417 ignore_tty_job_signals ();
420 #if defined (HAVE_WAITPID)
421 /* Collect the status of all zombie children so that their system
422 resources can be deallocated. */
424 reap_zombie_children ()
426 #if defined (WNOHANG)
430 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
431 set_pid_status (pid, status);
434 #endif /* WAITPID && WNOHANG */
436 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
437 COMMAND is just for remembering the name of the command; we don't do
438 anything else with it. ASYNC_P says what to do with the tty. If
439 non-zero, then don't give it away. */
441 make_child (command, async_p)
446 #if defined (HAVE_WAITPID)
448 #endif /* HAVE_WAITPID */
450 /* Discard saved memory. */
456 #if defined (BUFFERED_INPUT)
457 /* If default_buffered_input is active, we are reading a script. If
458 the command is asynchronous, we have already duplicated /dev/null
459 as fd 0, but have not changed the buffered stream corresponding to
460 the old fd 0. We don't want to sync the stream in this case. */
461 if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
462 sync_buffered_stream (default_buffered_input);
463 #endif /* BUFFERED_INPUT */
465 /* Create the child, handle severe errors. */
466 #if defined (HAVE_WAITPID)
468 #endif /* HAVE_WAITPID */
470 if ((pid = fork ()) < 0)
472 #if defined (HAVE_WAITPID)
473 /* Posix systems with a non-blocking waitpid () system call available
474 get another chance after zombies are reaped. */
475 if (errno == EAGAIN && retry)
477 reap_zombie_children ();
481 #endif /* HAVE_WAITPID */
485 throw_to_top_level ();
490 #if defined (BUFFERED_INPUT)
491 unset_bash_input (0);
492 #endif /* BUFFERED_INPUT */
494 #if defined (HAVE_POSIX_SIGNALS)
495 /* Restore top-level signal mask. */
496 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
499 /* Ignore INT and QUIT in asynchronous children. */
501 last_asynchronous_pid = getpid ();
503 default_tty_job_signals ();
512 last_asynchronous_pid = pid;
514 add_pid (pid, async_p);
520 ignore_tty_job_signals ()
522 #if defined (SIGTSTP)
523 set_signal_handler (SIGTSTP, SIG_IGN);
524 set_signal_handler (SIGTTIN, SIG_IGN);
525 set_signal_handler (SIGTTOU, SIG_IGN);
530 default_tty_job_signals ()
532 #if defined (SIGTSTP)
533 set_signal_handler (SIGTSTP, SIG_DFL);
534 set_signal_handler (SIGTTIN, SIG_DFL);
535 set_signal_handler (SIGTTOU, SIG_DFL);
539 /* Wait for a single pid (PID) and return its exit status. Called by
541 wait_for_single_pid (pid)
548 pstatus = find_status_by_pid (pid);
550 if (pstatus == PROC_BAD)
552 internal_error ("wait: pid %d is not a child of this shell", pid);
556 if (pstatus != PROC_STILL_ALIVE)
559 siginterrupt (SIGINT, 1);
560 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
564 if (errno != EINTR && errno != ECHILD)
566 siginterrupt (SIGINT, 0);
571 else if (got_pid > 0)
572 set_pid_status (got_pid, status);
575 set_pid_status (got_pid, status);
576 set_pid_flags (got_pid, PROC_NOTIFIED);
578 siginterrupt (SIGINT, 0);
581 return (process_exit_status (status));
584 /* Wait for all of the shell's children to exit. Called by the `wait'
587 wait_for_background_pids ()
592 /* If we aren't using job control, we let the kernel take care of the
593 bookkeeping for us. wait () will return -1 and set errno to ECHILD
594 when there are no more unwaited-for child processes on both
595 4.2 BSD-based and System V-based systems. */
597 siginterrupt (SIGINT, 1);
599 /* Wait for ECHILD */
600 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
601 set_pid_status (got_pid, status);
603 if (errno != EINTR && errno != ECHILD)
605 siginterrupt (SIGINT, 0);
609 siginterrupt (SIGINT, 0);
612 mark_dead_jobs_as_notified (1);
613 cleanup_dead_jobs ();
616 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
617 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
618 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
621 restore_sigint_handler ()
623 if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
625 set_signal_handler (SIGINT, old_sigint_handler);
626 old_sigint_handler = INVALID_SIGNAL_HANDLER;
630 /* Handle SIGINT while we are waiting for children in a script to exit.
631 All interrupts are effectively ignored by the shell, but allowed to
632 kill a running job. */
634 wait_sigint_handler (sig)
637 SigHandler *sigint_handler;
639 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
640 what POSIX.2 says (see builtins/wait.def for more info). */
641 if (this_shell_builtin && this_shell_builtin == wait_builtin &&
642 signal_is_trapped (SIGINT) &&
643 ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
645 last_command_exit_value = EXECUTION_FAILURE;
646 restore_sigint_handler ();
647 interrupt_immediately = 0;
648 trap_handler (SIGINT); /* set pending_traps[SIGINT] */
649 longjmp (wait_intr_buf, 1);
653 /* Run a trap handler if one has been defined. */
654 maybe_call_trap_handler (sig);
657 wait_sigint_received = 1;
662 /* Wait for pid (one of our children) to terminate. This is called only
663 by the execution code in execute_cmd.c. */
668 int return_val, pstatus;
672 pstatus = find_status_by_pid (pid);
674 if (pstatus == PROC_BAD)
677 if (pstatus != PROC_STILL_ALIVE)
680 /* If we are running a script, ignore SIGINT while we're waiting for
681 a child to exit. The loop below does some of this, but not all. */
682 wait_sigint_received = 0;
683 if (interactive_shell == 0)
684 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
686 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
688 if (got_pid < 0 && errno == ECHILD)
690 #if !defined (_POSIX_VERSION)
691 status.w_termsig = status.w_retcode = 0;
694 #endif /* _POSIX_VERSION */
697 else if (got_pid < 0 && errno != EINTR)
698 programming_error ("wait_for(%d): %s", pid, strerror(errno));
699 else if (got_pid > 0)
700 set_pid_status (got_pid, status);
703 set_pid_status (got_pid, status);
705 #if defined (HAVE_WAITPID)
707 reap_zombie_children ();
708 #endif /* HAVE_WAITPID */
710 if (interactive_shell == 0)
712 SigHandler *temp_handler;
714 temp_handler = old_sigint_handler;
715 restore_sigint_handler ();
717 /* If the job exited because of SIGINT, make sure the shell acts as if
718 it had received one also. */
719 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
722 if (maybe_call_trap_handler (SIGINT) == 0)
724 if (temp_handler == SIG_DFL)
725 termination_unwind_protect (SIGINT);
726 else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
727 (*temp_handler) (SIGINT);
732 /* Default return value. */
733 /* ``a full 8 bits of status is returned'' */
734 return_val = process_exit_status (status);
736 #if !defined (DONT_REPORT_SIGPIPE)
737 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
738 (WTERMSIG (status) != SIGINT))
740 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
741 (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
744 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
745 if (WIFCORED (status))
746 fprintf (stderr, " (core dumped)");
747 fprintf (stderr, "\n");
750 if (interactive_shell && subshell_environment == 0)
752 if (WIFSIGNALED (status) || WIFSTOPPED (status))
761 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
762 If PID does belong to a job, and the job is stopped, then CONTinue the
763 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
764 then kill the process group associated with PID. */
766 kill_pid (pid, signal, group)
772 result = group ? killpg (pid, signal) : kill (pid, signal);
776 static TTYSTRUCT shell_tty_info;
777 static int got_tty_state;
779 /* Fill the contents of shell_tty_info with the current tty info. */
787 ttgetattr (tty, &shell_tty_info);
789 if (check_window_size)
790 get_new_window_size (0);
794 /* Make the current tty use the state in shell_tty_info. */
803 if (got_tty_state == 0)
805 ttsetattr (tty, &shell_tty_info);
810 /* Give the terminal to PGRP. */
811 give_terminal_to (pgrp)
816 /* Stop a pipeline. */
817 stop_pipeline (async, ignore)
821 already_making_children = 0;
827 already_making_children = 1;
831 get_job_by_pid (pid, block)
837 i = find_index_by_pid (pid);
838 return ((i == NO_PID) ? PROC_BAD : i);
841 /* Print descriptive information about the job with leader pid PID. */
846 fprintf (stderr, "%d\n", (int) pid);
850 unfreeze_jobs_list ()