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-2006 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. */
49 #include "builtins/builtext.h" /* for wait_builtin */
51 #define DEFAULT_CHILD_MAX 32
53 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
54 # define killpg(pg, sig) kill(-(pg),(sig))
55 #endif /* USG || _POSIX_VERSION */
57 #if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS)
58 # define siginterrupt(sig, code)
59 #endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */
61 #if defined (HAVE_WAITPID)
62 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
64 # define WAITPID(pid, statusp, options) wait (statusp)
65 #endif /* !HAVE_WAITPID */
67 /* Return the fd from which we are actually getting input. */
68 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
74 extern int interactive
, interactive_shell
, login_shell
;
75 extern int subshell_environment
;
76 extern int last_command_exit_value
, last_command_exit_signal
;
77 extern int interrupt_immediately
;
78 extern sh_builtin_func_t
*this_shell_builtin
;
79 #if defined (HAVE_POSIX_SIGNALS)
80 extern sigset_t top_level_mask
;
82 extern procenv_t wait_intr_buf
;
83 extern int wait_signal_received
;
85 pid_t last_made_pid
= NO_PID
;
86 pid_t last_asynchronous_pid
= NO_PID
;
88 /* Call this when you start making children. */
89 int already_making_children
= 0;
91 /* The controlling tty for this shell. */
94 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
95 exits from get_tty_state(). */
96 int check_window_size
;
98 /* STATUS and FLAGS are only valid if pid != NO_PID
99 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
102 int status
; /* Exit status of PID or 128 + fatal signal number */
106 /* Values for proc_status.flags */
107 #define PROC_RUNNING 0x01
108 #define PROC_NOTIFIED 0x02
109 #define PROC_ASYNC 0x04
110 #define PROC_SIGNALED 0x10
112 /* Return values from find_status_by_pid */
114 #define PROC_STILL_ALIVE -2
116 static struct proc_status
*pid_list
= (struct proc_status
*)NULL
;
117 static int pid_list_size
;
118 static int wait_sigint_received
;
120 static long child_max
= -1L;
122 static void alloc_pid_list
__P((void));
123 static int find_proc_slot
__P((void));
124 static int find_index_by_pid
__P((pid_t
));
125 static int find_status_by_pid
__P((pid_t
));
126 static int process_exit_status
__P((WAIT
));
127 static int find_termsig_by_pid
__P((pid_t
));
128 static int get_termsig
__P((WAIT
));
129 static void set_pid_status
__P((pid_t
, WAIT
));
130 static void set_pid_flags
__P((pid_t
, int));
131 static void unset_pid_flags
__P((pid_t
, int));
132 static int get_pid_flags
__P((pid_t
));
133 static void add_pid
__P((pid_t
, int));
134 static void mark_dead_jobs_as_notified
__P((int));
136 static sighandler wait_sigint_handler
__P((int));
137 static char *j_strsignal
__P((int));
139 #if defined (HAVE_WAITPID)
140 static void reap_zombie_children
__P((void));
143 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
144 static int siginterrupt
__P((int, int));
147 static void restore_sigint_handler
__P((void));
149 /* Allocate new, or grow existing PID_LIST. */
154 int old
= pid_list_size
;
157 pid_list
= (struct proc_status
*)xrealloc (pid_list
, pid_list_size
* sizeof (struct proc_status
));
159 /* None of the newly allocated slots have process id's yet. */
160 for (i
= old
; i
< pid_list_size
; i
++)
161 pid_list
[i
].pid
= NO_PID
;
164 /* Return the offset within the PID_LIST array of an empty slot. This can
165 create new slots if all of the existing slots are taken. */
171 for (i
= 0; i
< pid_list_size
; i
++)
172 if (pid_list
[i
].pid
== NO_PID
)
175 if (i
== pid_list_size
)
181 /* Return the offset within the PID_LIST array of a slot containing PID,
182 or the value NO_PID if the pid wasn't found. */
184 find_index_by_pid (pid
)
189 for (i
= 0; i
< pid_list_size
; i
++)
190 if (pid_list
[i
].pid
== pid
)
196 /* Return the status of PID as looked up in the PID_LIST array. A
197 return value of PROC_BAD indicates that PID wasn't found. */
199 find_status_by_pid (pid
)
204 i
= find_index_by_pid (pid
);
207 if (pid_list
[i
].flags
& PROC_RUNNING
)
208 return (PROC_STILL_ALIVE
);
209 return (pid_list
[i
].status
);
213 process_exit_status (status
)
216 if (WIFSIGNALED (status
))
217 return (128 + WTERMSIG (status
));
219 return (WEXITSTATUS (status
));
222 /* Return the status of PID as looked up in the PID_LIST array. A
223 return value of PROC_BAD indicates that PID wasn't found. */
225 find_termsig_by_pid (pid
)
230 i
= find_index_by_pid (pid
);
233 if (pid_list
[i
].flags
& PROC_RUNNING
)
235 return (get_termsig (pid_list
[i
].status
));
238 /* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look
239 up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately
240 depending on its flags and exit status. */
245 if (WIFSTOPPED (status
) == 0 && WIFSIGNALED (status
))
246 return (WTERMSIG (status
));
251 /* Give PID the status value STATUS in the PID_LIST array. */
253 set_pid_status (pid
, status
)
259 slot
= find_index_by_pid (pid
);
263 pid_list
[slot
].status
= process_exit_status (status
);
264 pid_list
[slot
].flags
&= ~PROC_RUNNING
;
265 if (WIFSIGNALED (status
))
266 pid_list
[slot
].flags
|= PROC_SIGNALED
;
267 /* If it's not a background process, mark it as notified so it gets
269 if ((pid_list
[slot
].flags
& PROC_ASYNC
) == 0)
270 pid_list
[slot
].flags
|= PROC_NOTIFIED
;
273 /* Give PID the flags FLAGS in the PID_LIST array. */
275 set_pid_flags (pid
, flags
)
281 slot
= find_index_by_pid (pid
);
285 pid_list
[slot
].flags
|= flags
;
288 /* Unset FLAGS for PID in the pid list */
290 unset_pid_flags (pid
, flags
)
296 slot
= find_index_by_pid (pid
);
300 pid_list
[slot
].flags
&= ~flags
;
303 /* Return the flags corresponding to PID in the PID_LIST array. */
310 slot
= find_index_by_pid (pid
);
314 return (pid_list
[slot
].flags
);
324 slot
= find_proc_slot ();
326 pid_list
[slot
].pid
= pid
;
327 pid_list
[slot
].status
= -1;
328 pid_list
[slot
].flags
= PROC_RUNNING
;
330 pid_list
[slot
].flags
|= PROC_ASYNC
;
334 mark_dead_jobs_as_notified (force
)
337 register int i
, ndead
;
339 /* first, count the number of non-running async jobs if FORCE == 0 */
340 for (i
= ndead
= 0; force
== 0 && i
< pid_list_size
; i
++)
342 if (pid_list
[i
].pid
== NO_PID
)
344 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
345 (pid_list
[i
].flags
& PROC_ASYNC
))
350 child_max
= getmaxchild ();
352 child_max
= DEFAULT_CHILD_MAX
;
354 if (force
== 0 && ndead
<= child_max
)
357 /* If FORCE == 0, we just mark as many non-running async jobs as notified
358 to bring us under the CHILD_MAX limit. */
359 for (i
= 0; i
< pid_list_size
; i
++)
361 if (pid_list
[i
].pid
== NO_PID
)
363 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
364 pid_list
[i
].pid
!= last_asynchronous_pid
)
366 pid_list
[i
].flags
|= PROC_NOTIFIED
;
367 if (force
== 0 && (pid_list
[i
].flags
& PROC_ASYNC
) && --ndead
<= child_max
)
373 /* Remove all dead, notified jobs from the pid_list. */
379 #if defined (HAVE_WAITPID)
380 reap_zombie_children ();
383 for (i
= 0; i
< pid_list_size
; i
++)
385 if ((pid_list
[i
].flags
& PROC_RUNNING
) == 0 &&
386 (pid_list
[i
].flags
& PROC_NOTIFIED
))
387 pid_list
[i
].pid
= NO_PID
;
396 mark_dead_jobs_as_notified (0);
397 cleanup_dead_jobs ();
400 /* Initialize the job control mechanism, and set up the tty stuff. */
401 initialize_job_control (force
)
404 shell_tty
= fileno (stderr
);
410 /* Setup this shell to handle C-C, etc. */
412 initialize_job_signals ()
414 set_signal_handler (SIGINT
, sigint_sighandler
);
416 /* If this is a login shell we don't wish to be disturbed by
419 ignore_tty_job_signals ();
422 #if defined (HAVE_WAITPID)
423 /* Collect the status of all zombie children so that their system
424 resources can be deallocated. */
426 reap_zombie_children ()
428 # if defined (WNOHANG)
433 while ((pid
= waitpid (-1, (int *)&status
, WNOHANG
)) > 0)
434 set_pid_status (pid
, status
);
435 # endif /* WNOHANG */
440 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
442 siginterrupt (sig
, flag
)
445 struct sigaction act
;
447 sigaction (sig
, (struct sigaction
*)NULL
, &act
);
450 act
.sa_flags
&= ~SA_RESTART
;
452 act
.sa_flags
|= SA_RESTART
;
454 return (sigaction (sig
, &act
, (struct sigaction
*)NULL
));
456 #endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */
458 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
459 COMMAND is just for remembering the name of the command; we don't do
460 anything else with it. ASYNC_P says what to do with the tty. If
461 non-zero, then don't give it away. */
463 make_child (command
, async_p
)
468 #if defined (HAVE_WAITPID)
470 #endif /* HAVE_WAITPID */
472 /* Discard saved memory. */
478 #if defined (BUFFERED_INPUT)
479 /* If default_buffered_input is active, we are reading a script. If
480 the command is asynchronous, we have already duplicated /dev/null
481 as fd 0, but have not changed the buffered stream corresponding to
482 the old fd 0. We don't want to sync the stream in this case. */
483 if (default_buffered_input
!= -1 && (!async_p
|| default_buffered_input
> 0))
484 sync_buffered_stream (default_buffered_input
);
485 #endif /* BUFFERED_INPUT */
487 /* Create the child, handle severe errors. */
488 #if defined (HAVE_WAITPID)
490 #endif /* HAVE_WAITPID */
492 if ((pid
= fork ()) < 0)
494 #if defined (HAVE_WAITPID)
495 /* Posix systems with a non-blocking waitpid () system call available
496 get another chance after zombies are reaped. */
497 if (errno
== EAGAIN
&& retry
)
499 reap_zombie_children ();
503 #endif /* HAVE_WAITPID */
507 throw_to_top_level ();
512 #if defined (BUFFERED_INPUT)
513 unset_bash_input (0);
514 #endif /* BUFFERED_INPUT */
516 #if defined (HAVE_POSIX_SIGNALS)
517 /* Restore top-level signal mask. */
518 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
521 /* Ignore INT and QUIT in asynchronous children. */
523 last_asynchronous_pid
= getpid ();
525 default_tty_job_signals ();
534 last_asynchronous_pid
= pid
;
536 add_pid (pid
, async_p
);
542 ignore_tty_job_signals ()
544 #if defined (SIGTSTP)
545 set_signal_handler (SIGTSTP
, SIG_IGN
);
546 set_signal_handler (SIGTTIN
, SIG_IGN
);
547 set_signal_handler (SIGTTOU
, SIG_IGN
);
552 default_tty_job_signals ()
554 #if defined (SIGTSTP)
555 set_signal_handler (SIGTSTP
, SIG_DFL
);
556 set_signal_handler (SIGTTIN
, SIG_DFL
);
557 set_signal_handler (SIGTTOU
, SIG_DFL
);
561 /* Wait for a single pid (PID) and return its exit status. Called by
564 wait_for_single_pid (pid
)
571 pstatus
= find_status_by_pid (pid
);
573 if (pstatus
== PROC_BAD
)
575 internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid
);
579 if (pstatus
!= PROC_STILL_ALIVE
)
582 last_command_exit_signal
= find_termsig_by_pid (pid
);
586 siginterrupt (SIGINT
, 1);
587 while ((got_pid
= WAITPID (pid
, &status
, 0)) != pid
)
592 if (errno
!= EINTR
&& errno
!= ECHILD
)
594 siginterrupt (SIGINT
, 0);
599 else if (got_pid
> 0)
600 set_pid_status (got_pid
, status
);
605 set_pid_status (got_pid
, status
);
606 set_pid_flags (got_pid
, PROC_NOTIFIED
);
609 siginterrupt (SIGINT
, 0);
612 return (got_pid
> 0 ? process_exit_status (status
) : -1);
615 /* Wait for all of the shell's children to exit. Called by the `wait'
618 wait_for_background_pids ()
623 /* If we aren't using job control, we let the kernel take care of the
624 bookkeeping for us. wait () will return -1 and set errno to ECHILD
625 when there are no more unwaited-for child processes on both
626 4.2 BSD-based and System V-based systems. */
628 siginterrupt (SIGINT
, 1);
630 /* Wait for ECHILD */
631 while ((got_pid
= WAITPID (-1, &status
, 0)) != -1)
632 set_pid_status (got_pid
, status
);
634 if (errno
!= EINTR
&& errno
!= ECHILD
)
636 siginterrupt (SIGINT
, 0);
640 siginterrupt (SIGINT
, 0);
643 mark_dead_jobs_as_notified (1);
644 cleanup_dead_jobs ();
647 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
648 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
649 static SigHandler
*old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
652 restore_sigint_handler ()
654 if (old_sigint_handler
!= INVALID_SIGNAL_HANDLER
)
656 set_signal_handler (SIGINT
, old_sigint_handler
);
657 old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
661 /* Handle SIGINT while we are waiting for children in a script to exit.
662 All interrupts are effectively ignored by the shell, but allowed to
663 kill a running job. */
665 wait_sigint_handler (sig
)
668 SigHandler
*sigint_handler
;
670 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
671 what POSIX.2 says (see builtins/wait.def for more info). */
672 if (this_shell_builtin
&& this_shell_builtin
== wait_builtin
&&
673 signal_is_trapped (SIGINT
) &&
674 ((sigint_handler
= trap_to_sighandler (SIGINT
)) == trap_handler
))
676 last_command_exit_value
= EXECUTION_FAILURE
;
677 restore_sigint_handler ();
678 interrupt_immediately
= 0;
679 trap_handler (SIGINT
); /* set pending_traps[SIGINT] */
680 wait_signal_received
= SIGINT
;
681 longjmp (wait_intr_buf
, 1);
684 if (interrupt_immediately
)
686 last_command_exit_value
= EXECUTION_FAILURE
;
687 restore_sigint_handler ();
692 wait_sigint_received
= 1;
701 static char retcode_name_buffer
[64] = { '\0' };
707 x
= retcode_name_buffer
;
708 sprintf (x
, "Signal %d", s
);
713 /* Wait for pid (one of our children) to terminate. This is called only
714 by the execution code in execute_cmd.c. */
719 int return_val
, pstatus
;
723 pstatus
= find_status_by_pid (pid
);
725 if (pstatus
== PROC_BAD
)
728 if (pstatus
!= PROC_STILL_ALIVE
)
731 last_command_exit_signal
= find_termsig_by_pid (pid
);
735 /* If we are running a script, ignore SIGINT while we're waiting for
736 a child to exit. The loop below does some of this, but not all. */
737 wait_sigint_received
= 0;
738 if (interactive_shell
== 0)
739 old_sigint_handler
= set_signal_handler (SIGINT
, wait_sigint_handler
);
741 while ((got_pid
= WAITPID (-1, &status
, 0)) != pid
) /* XXX was pid now -1 */
744 if (got_pid
< 0 && errno
== ECHILD
)
746 #if !defined (_POSIX_VERSION)
747 status
.w_termsig
= status
.w_retcode
= 0;
750 #endif /* _POSIX_VERSION */
753 else if (got_pid
< 0 && errno
!= EINTR
)
754 programming_error ("wait_for(%ld): %s", (long)pid
, strerror(errno
));
755 else if (got_pid
> 0)
756 set_pid_status (got_pid
, status
);
760 set_pid_status (got_pid
, status
);
762 #if defined (HAVE_WAITPID)
764 reap_zombie_children ();
765 #endif /* HAVE_WAITPID */
767 if (interactive_shell
== 0)
769 SigHandler
*temp_handler
;
771 temp_handler
= old_sigint_handler
;
772 restore_sigint_handler ();
774 /* If the job exited because of SIGINT, make sure the shell acts as if
775 it had received one also. */
776 if (WIFSIGNALED (status
) && (WTERMSIG (status
) == SIGINT
))
779 if (maybe_call_trap_handler (SIGINT
) == 0)
781 if (temp_handler
== SIG_DFL
)
782 termsig_handler (SIGINT
);
783 else if (temp_handler
!= INVALID_SIGNAL_HANDLER
&& temp_handler
!= SIG_IGN
)
784 (*temp_handler
) (SIGINT
);
789 /* Default return value. */
790 /* ``a full 8 bits of status is returned'' */
791 return_val
= process_exit_status (status
);
792 last_command_exit_signal
= get_termsig (status
);
794 #if !defined (DONT_REPORT_SIGPIPE)
795 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
796 (WTERMSIG (status
) != SIGINT
))
798 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
799 (WTERMSIG (status
) != SIGINT
) && (WTERMSIG (status
) != SIGPIPE
))
802 fprintf (stderr
, "%s", j_strsignal (WTERMSIG (status
)));
803 if (WIFCORED (status
))
804 fprintf (stderr
, " (core dumped)");
805 fprintf (stderr
, "\n");
808 if (interactive_shell
&& subshell_environment
== 0)
810 if (WIFSIGNALED (status
) || WIFSTOPPED (status
))
819 /* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero,
820 or PID is less than -1, then kill the process group associated with PID. */
822 kill_pid (pid
, signal
, group
)
833 result
= group
? killpg (pid
, signal
) : kill (pid
, signal
);
837 static TTYSTRUCT shell_tty_info
;
838 static int got_tty_state
;
840 /* Fill the contents of shell_tty_info with the current tty info. */
848 ttgetattr (tty
, &shell_tty_info
);
850 if (check_window_size
)
851 get_new_window_size (0, (int *)0, (int *)0);
855 /* Make the current tty use the state in shell_tty_info. */
864 if (got_tty_state
== 0)
866 ttsetattr (tty
, &shell_tty_info
);
871 /* Give the terminal to PGRP. */
872 give_terminal_to (pgrp
, force
)
878 /* Stop a pipeline. */
880 stop_pipeline (async
, ignore
)
884 already_making_children
= 0;
891 already_making_children
= 1;
895 stop_making_children ()
897 already_making_children
= 0;
901 get_job_by_pid (pid
, block
)
907 i
= find_index_by_pid (pid
);
908 return ((i
== NO_PID
) ? PROC_BAD
: i
);
911 /* Print descriptive information about the job with leader pid PID. */
916 fprintf (stderr
, "%ld\n", (long) pid
);
920 unfreeze_jobs_list ()