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
, last_command_exit_signal
;
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
123 #define PROC_SIGNALED 0x10
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 long child_max
= -1L;
135 static void alloc_pid_list
__P((void));
136 static int find_proc_slot
__P((void));
137 static int find_index_by_pid
__P((pid_t
));
138 static int find_status_by_pid
__P((pid_t
));
139 static int process_exit_status
__P((WAIT
));
140 static int find_termsig_by_pid
__P((pid_t
));
141 static int get_termsig
__P((WAIT
));
142 static void set_pid_status
__P((pid_t
, WAIT
));
143 static void set_pid_flags
__P((pid_t
, int));
144 static void unset_pid_flags
__P((pid_t
, int));
145 static int get_pid_flags
__P((pid_t
));
146 static void add_pid
__P((pid_t
, int));
147 static void mark_dead_jobs_as_notified
__P((int));
149 static void get_new_window_size
__P((int));
150 static sighandler sigwinch_sighandler
__P((int));
151 static sighandler wait_sigint_handler
__P((int));
152 static char *j_strsignal
__P((int));
154 #if defined (HAVE_WAITPID)
155 static void reap_zombie_children
__P((void));
158 static void restore_sigint_handler
__P((void));
160 /* Allocate new, or grow existing PID_LIST. */
165 int old
= pid_list_size
;
168 pid_list
= (struct proc_status
*)xrealloc (pid_list
, pid_list_size
* sizeof (struct proc_status
));
170 /* None of the newly allocated slots have process id's yet. */
171 for (i
= old
; i
< pid_list_size
; i
++)
172 pid_list
[i
].pid
= NO_PID
;
175 /* Return the offset within the PID_LIST array of an empty slot. This can
176 create new slots if all of the existing slots are taken. */
182 for (i
= 0; i
< pid_list_size
; i
++)
183 if (pid_list
[i
].pid
== NO_PID
)
186 if (i
== pid_list_size
)
192 /* Return the offset within the PID_LIST array of a slot containing PID,
193 or the value NO_PID if the pid wasn't found. */
195 find_index_by_pid (pid
)
200 for (i
= 0; i
< pid_list_size
; i
++)
201 if (pid_list
[i
].pid
== pid
)
207 /* Return the status of PID as looked up in the PID_LIST array. A
208 return value of PROC_BAD indicates that PID wasn't found. */
210 find_status_by_pid (pid
)
215 i
= find_index_by_pid (pid
);
218 if (pid_list
[i
].flags
& PROC_RUNNING
)
219 return (PROC_STILL_ALIVE
);
220 return (pid_list
[i
].status
);
224 process_exit_status (status
)
227 if (WIFSIGNALED (status
))
228 return (128 + WTERMSIG (status
));
230 return (WEXITSTATUS (status
));
233 /* Return the status of PID as looked up in the PID_LIST array. A
234 return value of PROC_BAD indicates that PID wasn't found. */
236 find_termsig_by_pid (pid
)
241 i
= find_index_by_pid (pid
);
244 if (pid_list
[i
].flags
& PROC_RUNNING
)
246 return (get_termsig (pid_list
[i
].status
));
249 /* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look
250 up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately
251 depending on its flags and exit status. */
256 if (WIFSTOPPED (status
) == 0 && WIFSIGNALED (status
))
257 return (WTERMSIG (status
));
262 /* Give PID the status value STATUS in the PID_LIST array. */
264 set_pid_status (pid
, status
)
270 slot
= find_index_by_pid (pid
);
274 pid_list
[slot
].status
= process_exit_status (status
);
275 pid_list
[slot
].flags
&= ~PROC_RUNNING
;
276 if (WIFSIGNALED (status
))
277 pid_list
[slot
].flags
|= PROC_SIGNALED
;
278 /* If it's not a background process, mark it as notified so it gets
280 if ((pid_list
[slot
].flags
& PROC_ASYNC
) == 0)
281 pid_list
[slot
].flags
|= PROC_NOTIFIED
;
284 /* Give PID the flags FLAGS in the PID_LIST array. */
286 set_pid_flags (pid
, flags
)
292 slot
= find_index_by_pid (pid
);
296 pid_list
[slot
].flags
|= flags
;
299 /* Unset FLAGS for PID in the pid list */
301 unset_pid_flags (pid
, flags
)
307 slot
= find_index_by_pid (pid
);
311 pid_list
[slot
].flags
&= ~flags
;
314 /* Return the flags corresponding to PID in the PID_LIST array. */
321 slot
= find_index_by_pid (pid
);
325 return (pid_list
[slot
].flags
);
335 slot
= find_proc_slot ();
337 pid_list
[slot
].pid
= pid
;
338 pid_list
[slot
].status
= -1;
339 pid_list
[slot
].flags
= PROC_RUNNING
;
341 pid_list
[slot
].flags
|= PROC_ASYNC
;
345 mark_dead_jobs_as_notified (force
)
348 register int i
, ndead
;
350 /* first, count the number of non-running async jobs if FORCE == 0 */
351 for (i
= ndead
= 0; force
== 0 && i
< pid_list_size
; i
++)
353 if (pid_list
[i
].pid
== NO_PID
)
355 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
356 (pid_list
[i
].flags
& PROC_ASYNC
))
361 child_max
= getmaxchild ();
363 child_max
= DEFAULT_CHILD_MAX
;
365 if (force
== 0 && ndead
<= child_max
)
368 /* If FORCE == 0, we just mark as many non-running async jobs as notified
369 to bring us under the CHILD_MAX limit. */
370 for (i
= 0; i
< pid_list_size
; i
++)
372 if (pid_list
[i
].pid
== NO_PID
)
374 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
375 pid_list
[i
].pid
!= last_asynchronous_pid
)
377 pid_list
[i
].flags
|= PROC_NOTIFIED
;
378 if (force
== 0 && (pid_list
[i
].flags
& PROC_ASYNC
) && --ndead
<= child_max
)
384 /* Remove all dead, notified jobs from the pid_list. */
390 #if defined (HAVE_WAITPID)
391 reap_zombie_children ();
394 for (i
= 0; i
< pid_list_size
; i
++)
396 if ((pid_list
[i
].flags
& PROC_RUNNING
) == 0 &&
397 (pid_list
[i
].flags
& PROC_NOTIFIED
))
398 pid_list
[i
].pid
= NO_PID
;
407 mark_dead_jobs_as_notified (0);
408 cleanup_dead_jobs ();
411 /* Initialize the job control mechanism, and set up the tty stuff. */
412 initialize_job_control (force
)
415 shell_tty
= fileno (stderr
);
421 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
422 static SigHandler
*old_winch
= (SigHandler
*)SIG_DFL
;
425 get_new_window_size (from_sig
)
432 if (tty
>= 0 && (ioctl (tty
, TIOCGWINSZ
, &win
) == 0) &&
433 win
.ws_row
> 0 && win
.ws_col
> 0)
436 shell_tty_info
.c_winsize
= win
; /* structure copying */
438 sh_set_lines_and_columns (win
.ws_row
, win
.ws_col
);
439 #if defined (READLINE)
440 rl_set_screen_size (win
.ws_row
, win
.ws_col
);
446 sigwinch_sighandler (sig
)
449 #if defined (MUST_REINSTALL_SIGHANDLERS)
450 set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
451 #endif /* MUST_REINSTALL_SIGHANDLERS */
452 get_new_window_size (1);
456 get_new_window_size (from_sig
)
460 #endif /* TIOCGWINSZ && SIGWINCH */
463 set_sigwinch_handler ()
465 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
466 old_winch
= set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
471 unset_sigwinch_handler ()
473 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
474 set_signal_handler (SIGWINCH
, old_winch
);
478 /* Setup this shell to handle C-C, etc. */
480 initialize_job_signals ()
482 set_signal_handler (SIGINT
, sigint_sighandler
);
483 set_sigwinch_handler ();
485 /* If this is a login shell we don't wish to be disturbed by
488 ignore_tty_job_signals ();
491 #if defined (HAVE_WAITPID)
492 /* Collect the status of all zombie children so that their system
493 resources can be deallocated. */
495 reap_zombie_children ()
497 #if defined (WNOHANG)
501 while ((pid
= waitpid (-1, (int *)&status
, WNOHANG
)) > 0)
502 set_pid_status (pid
, status
);
505 #endif /* WAITPID && WNOHANG */
507 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
508 COMMAND is just for remembering the name of the command; we don't do
509 anything else with it. ASYNC_P says what to do with the tty. If
510 non-zero, then don't give it away. */
512 make_child (command
, async_p
)
517 #if defined (HAVE_WAITPID)
519 #endif /* HAVE_WAITPID */
521 /* Discard saved memory. */
527 #if defined (BUFFERED_INPUT)
528 /* If default_buffered_input is active, we are reading a script. If
529 the command is asynchronous, we have already duplicated /dev/null
530 as fd 0, but have not changed the buffered stream corresponding to
531 the old fd 0. We don't want to sync the stream in this case. */
532 if (default_buffered_input
!= -1 && (!async_p
|| default_buffered_input
> 0))
533 sync_buffered_stream (default_buffered_input
);
534 #endif /* BUFFERED_INPUT */
536 /* Create the child, handle severe errors. */
537 #if defined (HAVE_WAITPID)
539 #endif /* HAVE_WAITPID */
541 if ((pid
= fork ()) < 0)
543 #if defined (HAVE_WAITPID)
544 /* Posix systems with a non-blocking waitpid () system call available
545 get another chance after zombies are reaped. */
546 if (errno
== EAGAIN
&& retry
)
548 reap_zombie_children ();
552 #endif /* HAVE_WAITPID */
556 throw_to_top_level ();
561 #if defined (BUFFERED_INPUT)
562 unset_bash_input (0);
563 #endif /* BUFFERED_INPUT */
565 #if defined (HAVE_POSIX_SIGNALS)
566 /* Restore top-level signal mask. */
567 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
570 /* Ignore INT and QUIT in asynchronous children. */
572 last_asynchronous_pid
= getpid ();
574 default_tty_job_signals ();
583 last_asynchronous_pid
= pid
;
585 add_pid (pid
, async_p
);
591 ignore_tty_job_signals ()
593 #if defined (SIGTSTP)
594 set_signal_handler (SIGTSTP
, SIG_IGN
);
595 set_signal_handler (SIGTTIN
, SIG_IGN
);
596 set_signal_handler (SIGTTOU
, SIG_IGN
);
601 default_tty_job_signals ()
603 #if defined (SIGTSTP)
604 set_signal_handler (SIGTSTP
, SIG_DFL
);
605 set_signal_handler (SIGTTIN
, SIG_DFL
);
606 set_signal_handler (SIGTTOU
, SIG_DFL
);
610 /* Wait for a single pid (PID) and return its exit status. Called by
613 wait_for_single_pid (pid
)
620 pstatus
= find_status_by_pid (pid
);
622 if (pstatus
== PROC_BAD
)
624 internal_error ("wait: pid %ld is not a child of this shell", (long)pid
);
628 if (pstatus
!= PROC_STILL_ALIVE
)
631 last_command_exit_signal
= find_termsig_by_pid (pid
);
635 siginterrupt (SIGINT
, 1);
636 while ((got_pid
= WAITPID (pid
, &status
, 0)) != pid
)
640 if (errno
!= EINTR
&& errno
!= ECHILD
)
642 siginterrupt (SIGINT
, 0);
647 else if (got_pid
> 0)
648 set_pid_status (got_pid
, status
);
653 set_pid_status (got_pid
, status
);
654 set_pid_flags (got_pid
, PROC_NOTIFIED
);
657 siginterrupt (SIGINT
, 0);
660 return (got_pid
> 0 ? process_exit_status (status
) : -1);
663 /* Wait for all of the shell's children to exit. Called by the `wait'
666 wait_for_background_pids ()
671 /* If we aren't using job control, we let the kernel take care of the
672 bookkeeping for us. wait () will return -1 and set errno to ECHILD
673 when there are no more unwaited-for child processes on both
674 4.2 BSD-based and System V-based systems. */
676 siginterrupt (SIGINT
, 1);
678 /* Wait for ECHILD */
679 while ((got_pid
= WAITPID (-1, &status
, 0)) != -1)
680 set_pid_status (got_pid
, status
);
682 if (errno
!= EINTR
&& errno
!= ECHILD
)
684 siginterrupt (SIGINT
, 0);
688 siginterrupt (SIGINT
, 0);
691 mark_dead_jobs_as_notified (1);
692 cleanup_dead_jobs ();
695 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
696 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
697 static SigHandler
*old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
700 restore_sigint_handler ()
702 if (old_sigint_handler
!= INVALID_SIGNAL_HANDLER
)
704 set_signal_handler (SIGINT
, old_sigint_handler
);
705 old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
709 /* Handle SIGINT while we are waiting for children in a script to exit.
710 All interrupts are effectively ignored by the shell, but allowed to
711 kill a running job. */
713 wait_sigint_handler (sig
)
716 SigHandler
*sigint_handler
;
718 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
719 what POSIX.2 says (see builtins/wait.def for more info). */
720 if (this_shell_builtin
&& this_shell_builtin
== wait_builtin
&&
721 signal_is_trapped (SIGINT
) &&
722 ((sigint_handler
= trap_to_sighandler (SIGINT
)) == trap_handler
))
724 last_command_exit_value
= EXECUTION_FAILURE
;
725 restore_sigint_handler ();
726 interrupt_immediately
= 0;
727 trap_handler (SIGINT
); /* set pending_traps[SIGINT] */
728 wait_signal_received
= SIGINT
;
729 longjmp (wait_intr_buf
, 1);
732 if (interrupt_immediately
)
734 last_command_exit_value
= EXECUTION_FAILURE
;
735 restore_sigint_handler ();
740 wait_sigint_received
= 1;
749 static char retcode_name_buffer
[64] = { '\0' };
755 x
= retcode_name_buffer
;
756 sprintf (x
, "Signal %d", s
);
761 /* Wait for pid (one of our children) to terminate. This is called only
762 by the execution code in execute_cmd.c. */
767 int return_val
, pstatus
;
771 pstatus
= find_status_by_pid (pid
);
773 if (pstatus
== PROC_BAD
)
776 if (pstatus
!= PROC_STILL_ALIVE
)
779 last_command_exit_signal
= find_termsig_by_pid (pid
);
783 /* If we are running a script, ignore SIGINT while we're waiting for
784 a child to exit. The loop below does some of this, but not all. */
785 wait_sigint_received
= 0;
786 if (interactive_shell
== 0)
787 old_sigint_handler
= set_signal_handler (SIGINT
, wait_sigint_handler
);
789 while ((got_pid
= WAITPID (-1, &status
, 0)) != pid
) /* XXX was pid now -1 */
791 if (got_pid
< 0 && errno
== ECHILD
)
793 #if !defined (_POSIX_VERSION)
794 status
.w_termsig
= status
.w_retcode
= 0;
797 #endif /* _POSIX_VERSION */
800 else if (got_pid
< 0 && errno
!= EINTR
)
801 programming_error ("wait_for(%ld): %s", (long)pid
, strerror(errno
));
802 else if (got_pid
> 0)
803 set_pid_status (got_pid
, status
);
807 set_pid_status (got_pid
, status
);
809 #if defined (HAVE_WAITPID)
811 reap_zombie_children ();
812 #endif /* HAVE_WAITPID */
814 if (interactive_shell
== 0)
816 SigHandler
*temp_handler
;
818 temp_handler
= old_sigint_handler
;
819 restore_sigint_handler ();
821 /* If the job exited because of SIGINT, make sure the shell acts as if
822 it had received one also. */
823 if (WIFSIGNALED (status
) && (WTERMSIG (status
) == SIGINT
))
826 if (maybe_call_trap_handler (SIGINT
) == 0)
828 if (temp_handler
== SIG_DFL
)
829 termination_unwind_protect (SIGINT
);
830 else if (temp_handler
!= INVALID_SIGNAL_HANDLER
&& temp_handler
!= SIG_IGN
)
831 (*temp_handler
) (SIGINT
);
836 /* Default return value. */
837 /* ``a full 8 bits of status is returned'' */
838 return_val
= process_exit_status (status
);
839 last_command_exit_signal
= get_termsig (status
);
841 #if !defined (DONT_REPORT_SIGPIPE)
842 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
843 (WTERMSIG (status
) != SIGINT
))
845 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
846 (WTERMSIG (status
) != SIGINT
) && (WTERMSIG (status
) != SIGPIPE
))
849 fprintf (stderr
, "%s", j_strsignal (WTERMSIG (status
)));
850 if (WIFCORED (status
))
851 fprintf (stderr
, " (core dumped)");
852 fprintf (stderr
, "\n");
855 if (interactive_shell
&& subshell_environment
== 0)
857 if (WIFSIGNALED (status
) || WIFSTOPPED (status
))
866 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
867 If PID does belong to a job, and the job is stopped, then CONTinue the
868 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
869 then kill the process group associated with PID. */
871 kill_pid (pid
, signal
, group
)
877 result
= group
? killpg (pid
, signal
) : kill (pid
, signal
);
881 static TTYSTRUCT shell_tty_info
;
882 static int got_tty_state
;
884 /* Fill the contents of shell_tty_info with the current tty info. */
892 ttgetattr (tty
, &shell_tty_info
);
894 if (check_window_size
)
895 get_new_window_size (0);
899 /* Make the current tty use the state in shell_tty_info. */
908 if (got_tty_state
== 0)
910 ttsetattr (tty
, &shell_tty_info
);
915 /* Give the terminal to PGRP. */
916 give_terminal_to (pgrp
, force
)
922 /* Stop a pipeline. */
924 stop_pipeline (async
, ignore
)
928 already_making_children
= 0;
935 already_making_children
= 1;
939 stop_making_children ()
941 already_making_children
= 0;
945 get_job_by_pid (pid
, block
)
951 i
= find_index_by_pid (pid
);
952 return ((i
== NO_PID
) ? PROC_BAD
: i
);
955 /* Print descriptive information about the job with leader pid PID. */
960 fprintf (stderr
, "%ld\n", (long) pid
);
964 unfreeze_jobs_list ()