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 && (!async_p || default_buffered_input > 0))
359 sync_buffered_stream (default_buffered_input);
360 #endif /* BUFFERED_INPUT */
362 /* Create the child, handle severe errors. */
363 #if defined (HAVE_WAITPID)
365 #endif /* HAVE_WAITPID */
367 if ((pid = fork ()) < 0)
369 #if defined (HAVE_WAITPID)
370 /* Posix systems with a non-blocking waitpid () system call available
371 get another chance after zombies are reaped. */
372 if (errno == EAGAIN && retry)
374 reap_zombie_children ();
378 #endif /* HAVE_WAITPID */
382 throw_to_top_level ();
387 #if defined (BUFFERED_INPUT)
388 unset_bash_input (0);
389 #endif /* BUFFERED_INPUT */
391 #if defined (HAVE_POSIX_SIGNALS)
392 /* Restore top-level signal mask. */
393 sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
396 /* Ignore INT and QUIT in asynchronous children. */
398 last_asynchronous_pid = getpid ();
400 default_tty_job_signals ();
409 last_asynchronous_pid = pid;
417 ignore_tty_job_signals ()
419 #if defined (SIGTSTP)
420 set_signal_handler (SIGTSTP, SIG_IGN);
421 set_signal_handler (SIGTTIN, SIG_IGN);
422 set_signal_handler (SIGTTOU, SIG_IGN);
427 default_tty_job_signals ()
429 #if defined (SIGTSTP)
430 set_signal_handler (SIGTSTP, SIG_DFL);
431 set_signal_handler (SIGTTIN, SIG_DFL);
432 set_signal_handler (SIGTTOU, SIG_DFL);
436 /* Wait for a single pid (PID) and return its exit status. */
437 wait_for_single_pid (pid)
444 pstatus = find_status_by_pid (pid);
446 if (pstatus == PROC_BAD)
448 internal_error ("wait: pid %d is not a child of this shell", pid);
452 if (pstatus != PROC_STILL_ALIVE)
455 siginterrupt (SIGINT, 1);
456 while ((got_pid = WAITPID (pid, &status, 0)) != pid)
460 if (errno != EINTR && errno != ECHILD)
462 siginterrupt (SIGINT, 0);
467 else if (got_pid > 0)
468 set_pid_status (got_pid, status);
471 set_pid_status (got_pid, status);
472 siginterrupt (SIGINT, 0);
475 if (WIFSIGNALED (status))
476 return (128 + WTERMSIG (status));
478 return (WEXITSTATUS (status));
481 /* Wait for all of the shell's children to exit. */
483 wait_for_background_pids ()
488 /* If we aren't using job control, we let the kernel take care of the
489 bookkeeping for us. wait () will return -1 and set errno to ECHILD
490 when there are no more unwaited-for child processes on both
491 4.2 BSD-based and System V-based systems. */
493 siginterrupt (SIGINT, 1);
495 /* Wait for ECHILD */
496 while ((got_pid = WAITPID (-1, &status, 0)) != -1)
497 set_pid_status (got_pid, status);
499 if (errno != EINTR && errno != ECHILD)
501 siginterrupt (SIGINT, 0);
505 siginterrupt (SIGINT, 0);
509 /* Handle SIGINT while we are waiting for children in a script to exit.
510 All interrupts are effectively ignored by the shell, but allowed to
511 kill a running job. */
513 wait_sigint_handler (sig)
517 /* Run a trap handler if one has been defined. */
518 maybe_call_trap_handler (sig);
524 /* Wait for pid (one of our children) to terminate. This is called only
525 by the execution code in execute_cmd.c. */
530 int return_val, pstatus;
533 SigHandler *old_sigint_handler;
535 pstatus = find_status_by_pid (pid);
537 if (pstatus == PROC_BAD)
540 if (pstatus != PROC_STILL_ALIVE)
543 /* If we are running a script, ignore SIGINT while we're waiting for
544 a child to exit. The loop below does some of this, but not all. */
545 if (!interactive_shell)
546 old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
548 while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
550 if (got_pid < 0 && errno == ECHILD)
552 #if !defined (_POSIX_VERSION)
553 status.w_termsig = status.w_retcode = 0;
556 #endif /* _POSIX_VERSION */
559 else if (got_pid < 0 && errno != EINTR)
560 programming_error ("wait_for(%d): %s", pid, strerror(errno));
561 else if (got_pid > 0)
562 set_pid_status (got_pid, status);
565 set_pid_status (got_pid, status);
567 #if defined (HAVE_WAITPID)
569 reap_zombie_children ();
570 #endif /* HAVE_WAITPID */
572 if (interactive_shell == 0)
574 set_signal_handler (SIGINT, old_sigint_handler);
575 /* If the job exited because of SIGINT, make sure the shell acts as if
576 it had received one also. */
577 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
579 if (maybe_call_trap_handler (SIGINT) == 0)
580 (*old_sigint_handler) (SIGINT);
584 /* Default return value. */
585 /* ``a full 8 bits of status is returned'' */
586 if (WIFSIGNALED (status))
587 return_val = 128 + WTERMSIG (status);
589 return_val = WEXITSTATUS (status);
591 #if !defined (DONT_REPORT_SIGPIPE)
592 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
593 (WTERMSIG (status) != SIGINT))
595 if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
596 (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
599 fprintf (stderr, "%s", strsignal (WTERMSIG (status)));
600 if (WIFCORED (status))
601 fprintf (stderr, " (core dumped)");
602 fprintf (stderr, "\n");
605 if (interactive_shell && subshell_environment == 0)
607 if (WIFSIGNALED (status) || WIFSTOPPED (status))
616 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
617 If PID does belong to a job, and the job is stopped, then CONTinue the
618 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
619 then kill the process group associated with PID. */
621 kill_pid (pid, signal, group)
628 result = killpg (pid, signal);
630 result = kill (pid, signal);
635 #if defined (TERMIOS_TTY_DRIVER)
636 static struct termios shell_tty_info;
637 #else /* !TERMIOS_TTY_DRIVER */
638 # if defined (TERMIO_TTY_DRIVER)
639 static struct termio shell_tty_info;
641 static struct sgttyb shell_tty_info;
642 # endif /* !TERMIO_TTY_DRIVER */
643 #endif /* !TERMIOS_TTY_DRIVER */
645 static int got_tty_state;
647 /* Fill the contents of shell_tty_info with the current tty info. */
655 #if defined (TERMIOS_TTY_DRIVER)
656 tcgetattr (tty, &shell_tty_info);
658 # if defined (TERMIO_TTY_DRIVER)
659 ioctl (tty, TCGETA, &shell_tty_info);
661 ioctl (tty, TIOCGETP, &shell_tty_info);
665 if (check_window_size)
666 get_new_window_size (0);
670 /* Make the current tty use the state in shell_tty_info. */
679 if (got_tty_state == 0)
682 #if defined (TERMIOS_TTY_DRIVER)
683 tcsetattr (tty, TCSADRAIN, &shell_tty_info);
685 # if defined (TERMIO_TTY_DRIVER)
686 ioctl (tty, TCSETAW, &shell_tty_info); /* Wait for output, no flush */
688 ioctl (tty, TIOCSETN, &shell_tty_info);
695 /* Give the terminal to PGRP. */
696 give_terminal_to (pgrp)
701 /* Stop a pipeline. */
702 stop_pipeline (async, ignore)
706 already_making_children = 0;
712 already_making_children = 1;
716 get_job_by_pid (pid, block)
722 i = find_index_by_pid (pid);
723 return ((i == NO_PID) ? PROC_BAD : i);
726 /* Print descriptive information about the job with leader pid PID. */
731 fprintf (stderr, "%d\n", (int) pid);
735 unfreeze_jobs_list ()