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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "bashtypes.h"
29 #if defined (HAVE_UNISTD_H)
47 #if defined (BUFFERED_INPUT)
51 #if defined (TERMIOS_TTY_DRIVER)
54 # if defined (TERMIO_TTY_DRIVER)
58 # endif /* !TERMIO_TTY_DRIVER */
59 #endif /* TERMIOS_TTY_DRIVER */
61 #if !defined (STRUCT_WINSIZE_IN_SYS_IOCTL)
62 /* For struct winsize on SCO */
63 /* sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
64 # if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
65 # if defined (HAVE_SYS_STREAM_H)
66 # include <sys/stream.h>
68 # include <sys/ptem.h>
69 # endif /* HAVE_SYS_PTEM_H && TIOCGWINSZ && SIGWINCH */
70 #endif /* !STRUCT_WINSIZE_IN_SYS_IOCTL */
72 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
73 # define killpg(pg, sig) kill(-(pg),(sig))
74 #endif /* USG || _POSIX_VERSION */
76 #if !defined (HAVE_SIGINTERRUPT)
77 # define siginterrupt(sig, code)
80 #if defined (HAVE_WAITPID)
81 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
83 # define WAITPID(pid, statusp, options) wait (statusp)
84 #endif /* !HAVE_WAITPID */
86 /* Return the fd from which we are actually getting input. */
87 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
93 #if defined (READLINE)
94 extern void _rl_set_screen_size ();
97 extern int interactive, interactive_shell, login_shell;
98 extern int subshell_environment;
99 extern int last_command_exit_value;
100 #if defined (HAVE_POSIX_SIGNALS)
101 extern sigset_t top_level_mask;
104 pid_t last_made_pid = NO_PID;
105 pid_t last_asynchronous_pid = NO_PID;
107 /* Call this when you start making children. */
108 int already_making_children = 0;
110 /* The controlling tty for this shell. */
113 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
114 exits from get_tty_state(). */
115 int check_window_size;
117 #if defined (HAVE_WAITPID)
118 static void reap_zombie_children ();
123 int status; /* Exit status of PID or 128 + fatal signal number */
126 static struct proc_status *pid_list = (struct proc_status *)NULL;
127 static int pid_list_size;
130 #define PROC_STILL_ALIVE -2
132 /* Allocate new, or grow existing PID_LIST. */
137 int old = pid_list_size;
140 pid_list = (struct proc_status *)
141 xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
143 /* None of the newly allocated slots have process id's yet. */
144 for (i = old; i < pid_list_size; i++)
145 pid_list[i].pid = NO_PID;
148 /* Return the offset within the PID_LIST array of an empty slot. This can
149 create new slots if all of the existing slots are taken. */
155 for (i = 0; i < pid_list_size; i++)
156 if (pid_list[i].pid == NO_PID)
159 if (i == pid_list_size)
165 /* Return the offset within the PID_LIST array of a slot containing PID,
166 or the value NO_PID if the pid wasn't found. */
168 find_index_by_pid (pid)
173 for (i = 0; i < pid_list_size; i++)
174 if (pid_list[i].pid == pid)
180 /* Return the status of PID as looked up in the PID_LIST array. A
181 return value of PROC_BAD indicates that PID wasn't found. */
183 find_status_by_pid (pid)
188 i = find_index_by_pid (pid);
191 return (pid_list[i].status);
194 /* Give PID the status value STATUS in the PID_LIST array. */
196 set_pid_status (pid, status)
202 slot = find_index_by_pid (pid);
206 if (WIFSIGNALED (status))
207 pid_list[slot].status = 128 + WTERMSIG (status);
209 pid_list[slot].status = WEXITSTATUS (status);
218 slot = find_proc_slot ();
219 pid_list[slot].pid = pid;
220 pid_list[slot].status = PROC_STILL_ALIVE;
228 #if defined (HAVE_WAITPID)
229 reap_zombie_children ();
232 for (i = 0; i < pid_list_size; i++)
233 if (pid_list[i].status != PROC_STILL_ALIVE)
234 pid_list[i].pid = NO_PID;
237 /* Initialize the job control mechanism, and set up the tty stuff. */
238 initialize_job_control (force)
241 shell_tty = fileno (stderr);
247 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
248 static SigHandler *old_winch = (SigHandler *)SIG_DFL;
251 get_new_window_size (from_sig)
258 if (tty >= 0 && (ioctl (tty, TIOCGWINSZ, &win) == 0) &&
259 win.ws_row > 0 && win.ws_col > 0)
262 shell_tty_info.c_winsize = win; /* structure copying */
264 set_lines_and_columns (win.ws_row, win.ws_col);
265 #if defined (READLINE)
266 _rl_set_screen_size (win.ws_row, win.ws_col);
272 sigwinch_sighandler (sig)
275 #if defined (MUST_REINSTALL_SIGHANDLERS)
276 set_signal_handler (SIGWINCH, sigwinch_sighandler);
277 #endif /* MUST_REINSTALL_SIGHANDLERS */
278 get_new_window_size (1);
282 get_new_window_size (from_sig)
286 #endif /* TIOCGWINSZ && SIGWINCH */
289 set_sigwinch_handler ()
291 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
292 old_winch = set_signal_handler (SIGWINCH, sigwinch_sighandler);
297 unset_sigwinch_handler ()
299 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
300 set_signal_handler (SIGWINCH, old_winch);
304 /* Setup this shell to handle C-C, etc. */
306 initialize_job_signals ()
308 set_signal_handler (SIGINT, sigint_sighandler);
309 set_sigwinch_handler ();
311 /* If this is a login shell we don't wish to be disturbed by
314 ignore_tty_job_signals ();
317 #if defined (HAVE_WAITPID)
318 /* Collect the status of all zombie children so that their system
319 resources can be deallocated. */
321 reap_zombie_children ()
323 #if defined (WNOHANG)
327 while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
328 set_pid_status (pid, status);
331 #endif /* WAITPID && WNOHANG */
333 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
334 COMMAND is just for remembering the name of the command; we don't do
335 anything else with it. ASYNC_P says what to do with the tty. If
336 non-zero, then don't give it away. */
338 make_child (command, async_p)
343 #if defined (HAVE_WAITPID)
345 #endif /* HAVE_WAITPID */
347 /* Discard saved memory. */
353 #if defined (BUFFERED_INPUT)
354 /* If default_buffered_input is active, we are reading a script. If
355 the command is asynchronous, we have already duplicated /dev/null
356 as fd 0, but have not changed the buffered stream corresponding to
357 the old fd 0. We don't want to sync the stream in this case. */
358 if (default_buffered_input != -1 &&
359 (!async_p || default_buffered_input > 0))
360 sync_buffered_stream (default_buffered_input);
361 #endif /* BUFFERED_INPUT */
363 /* Create the child, handle severe errors. */
364 #if defined (HAVE_WAITPID)
366 #endif /* HAVE_WAITPID */
368 if ((pid = fork ()) < 0)
370 #if defined (HAVE_WAITPID)
371 /* Posix systems with a non-blocking waitpid () system call available
372 get another chance after zombies are reaped. */
373 if (errno == EAGAIN && retry)
375 reap_zombie_children ();
379 #endif /* HAVE_WAITPID */
383 throw_to_top_level ();
388 #if defined (BUFFERED_INPUT)
389 if (default_buffered_input > 0)
391 close_buffered_fd (default_buffered_input);
392 default_buffered_input = bash_input.location.buffered_fd = -1;
394 #endif /* BUFFERED_INPUT */
396 #if defined (HAVE_POSIX_SIGNALS)
397 /* Restore top-level signal mask. */
398 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
401 /* Ignore INT and QUIT in asynchronous children. */
403 last_asynchronous_pid = getpid ();
405 default_tty_job_signals ();
414 last_asynchronous_pid = pid;
422 ignore_tty_job_signals ()
424 #if defined (SIGTSTP)
425 set_signal_handler (SIGTSTP, SIG_IGN);
426 set_signal_handler (SIGTTIN, SIG_IGN);
427 set_signal_handler (SIGTTOU, SIG_IGN);
432 default_tty_job_signals ()
434 #if defined (SIGTSTP)
435 set_signal_handler (SIGTSTP, SIG_DFL);
436 set_signal_handler (SIGTTIN, SIG_DFL);
437 set_signal_handler (SIGTTOU, SIG_DFL);
441 /* Wait for a single pid (PID) and return its exit status. */
442 wait_for_single_pid (pid)
449 pstatus = find_status_by_pid (pid);
451 if (pstatus == PROC_BAD)
453 internal_error ("wait: pid %d is not a child of this shell", pid);
457 if (pstatus != PROC_STILL_ALIVE)
460 siginterrupt (SIGINT, 1);
461 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
465 if (errno != EINTR && errno != ECHILD)
467 siginterrupt (SIGINT, 0);
472 else if (got_pid > 0)
473 set_pid_status (got_pid, status);
476 set_pid_status (got_pid, status);
477 siginterrupt (SIGINT, 0);
480 if (WIFSIGNALED (status))
481 return (128 + WTERMSIG (status));
483 return (WEXITSTATUS (status));
486 /* Wait for all of the shell's children to exit. */
488 wait_for_background_pids ()
493 /* If we aren't using job control, we let the kernel take care of the
494 bookkeeping for us. wait () will return -1 and set errno to ECHILD
495 when there are no more unwaited-for child processes on both
496 4.2 BSD-based and System V-based systems. */
498 siginterrupt (SIGINT, 1);
500 /* Wait for ECHILD */
501 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
502 set_pid_status (got_pid, status);
504 if (errno != EINTR && errno != ECHILD)
506 siginterrupt (SIGINT, 0);
510 siginterrupt (SIGINT, 0);
514 /* Handle SIGINT while we are waiting for children in a script to exit.
515 All interrupts are effectively ignored by the shell, but allowed to
516 kill a running job. */
518 wait_sigint_handler (sig)
522 /* Run a trap handler if one has been defined. */
523 maybe_call_trap_handler (sig);
529 /* Wait for pid (one of our children) to terminate. This is called only
530 by the execution code in execute_cmd.c. */
535 int return_val, pstatus;
538 SigHandler *old_sigint_handler;
540 pstatus = find_status_by_pid (pid);
542 if (pstatus == PROC_BAD)
545 if (pstatus != PROC_STILL_ALIVE)
548 /* If we are running a script, ignore SIGINT while we're waiting for
549 a child to exit. The loop below does some of this, but not all. */
550 if (!interactive_shell)
551 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
553 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
555 if (got_pid < 0 && errno == ECHILD)
557 #if !defined (_POSIX_VERSION)
558 status.w_termsig = status.w_retcode = 0;
561 #endif /* _POSIX_VERSION */
564 else if (got_pid < 0 && errno != EINTR)
565 programming_error ("wait_for(%d): %s", pid, strerror(errno));
566 else if (got_pid > 0)
567 set_pid_status (got_pid, status);
570 set_pid_status (got_pid, status);
572 #if defined (HAVE_WAITPID)
574 reap_zombie_children ();
575 #endif /* HAVE_WAITPID */
577 if (interactive_shell == 0)
579 set_signal_handler (SIGINT, old_sigint_handler);
580 /* If the job exited because of SIGINT, make sure the shell acts as if
581 it had received one also. */
582 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
584 if (maybe_call_trap_handler (SIGINT) == 0)
585 (*old_sigint_handler) (SIGINT);
589 /* Default return value. */
590 /* ``a full 8 bits of status is returned'' */
591 if (WIFSIGNALED (status))
592 return_val = 128 + WTERMSIG (status);
594 return_val = WEXITSTATUS (status);
596 #if !defined (DONT_REPORT_SIGPIPE)
597 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
598 (WTERMSIG (status) != SIGINT))
600 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
601 (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
604 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
605 if (WIFCORED (status))
606 fprintf (stderr, " (core dumped)");
607 fprintf (stderr, "\n");
610 if (interactive_shell && subshell_environment == 0)
612 if (WIFSIGNALED (status) || WIFSTOPPED (status))
621 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
622 If PID does belong to a job, and the job is stopped, then CONTinue the
623 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
624 then kill the process group associated with PID. */
626 kill_pid (pid, signal, group)
633 result = killpg (pid, signal);
635 result = kill (pid, signal);
640 #if defined (TERMIOS_TTY_DRIVER)
641 static struct termios shell_tty_info;
642 #else /* !TERMIOS_TTY_DRIVER */
643 # if defined (TERMIO_TTY_DRIVER)
644 static struct termio shell_tty_info;
646 static struct sgttyb shell_tty_info;
647 # endif /* !TERMIO_TTY_DRIVER */
648 #endif /* !TERMIOS_TTY_DRIVER */
650 static int got_tty_state;
652 /* Fill the contents of shell_tty_info with the current tty info. */
660 #if defined (TERMIOS_TTY_DRIVER)
661 tcgetattr (tty, &shell_tty_info);
663 # if defined (TERMIO_TTY_DRIVER)
664 ioctl (tty, TCGETA, &shell_tty_info);
666 ioctl (tty, TIOCGETP, &shell_tty_info);
670 if (check_window_size)
671 get_new_window_size (0);
675 /* Make the current tty use the state in shell_tty_info. */
683 if (got_tty_state == 0)
686 #if defined (TERMIOS_TTY_DRIVER)
687 tcsetattr (tty, TCSADRAIN, &shell_tty_info);
689 # if defined (TERMIO_TTY_DRIVER)
690 ioctl (tty, TCSETAW, &shell_tty_info); /* Wait for output, no flush */
692 ioctl (tty, TIOCSETN, &shell_tty_info);
698 /* Give the terminal to PGRP. */
699 give_terminal_to (pgrp)
704 /* Stop a pipeline. */
705 stop_pipeline (async, ignore)
709 already_making_children = 0;
715 already_making_children = 1;
719 get_job_by_pid (pid, block)
725 i = find_index_by_pid (pid);
726 return ((i == NO_PID) ? PROC_BAD : i);
729 /* Print descriptive information about the job with leader pid PID. */
734 fprintf (stderr, "%d\n", (int) pid);
738 unfreeze_jobs_list ()