1 /* nojobs.c - functions that make children, remember them, and handle their termination. */
3 /* This file works under BSD, System V, minix, and Posix systems. It does
4 not implement job control. */
6 /* Copyright (C) 1987-2009 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
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
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. */
48 #include "execute_cmd.h"
50 #include "builtins/builtext.h" /* for wait_builtin */
52 #define DEFAULT_CHILD_MAX 32
54 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
55 # define killpg(pg, sig) kill(-(pg),(sig))
56 #endif /* USG || _POSIX_VERSION */
58 #if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS)
59 # define siginterrupt(sig, code)
60 #endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */
62 #if defined (HAVE_WAITPID)
63 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
65 # define WAITPID(pid, statusp, options) wait (statusp)
66 #endif /* !HAVE_WAITPID */
68 /* Return the fd from which we are actually getting input. */
69 #define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)
75 extern int interactive
, interactive_shell
, login_shell
;
76 extern int subshell_environment
;
77 extern int last_command_exit_value
, last_command_exit_signal
;
78 extern int interrupt_immediately
;
79 extern sh_builtin_func_t
*this_shell_builtin
;
80 #if defined (HAVE_POSIX_SIGNALS)
81 extern sigset_t top_level_mask
;
83 extern procenv_t wait_intr_buf
;
84 extern int wait_signal_received
;
86 pid_t last_made_pid
= NO_PID
;
87 pid_t last_asynchronous_pid
= NO_PID
;
89 /* Call this when you start making children. */
90 int already_making_children
= 0;
92 /* The controlling tty for this shell. */
95 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
96 exits from get_tty_state(). */
97 int check_window_size
;
99 /* STATUS and FLAGS are only valid if pid != NO_PID
100 STATUS is only valid if (flags & PROC_RUNNING) == 0 */
103 int status
; /* Exit status of PID or 128 + fatal signal number */
107 /* Values for proc_status.flags */
108 #define PROC_RUNNING 0x01
109 #define PROC_NOTIFIED 0x02
110 #define PROC_ASYNC 0x04
111 #define PROC_SIGNALED 0x10
113 /* Return values from find_status_by_pid */
115 #define PROC_STILL_ALIVE -2
117 static struct proc_status
*pid_list
= (struct proc_status
*)NULL
;
118 static int pid_list_size
;
119 static int wait_sigint_received
;
121 static long child_max
= -1L;
123 static void alloc_pid_list
__P((void));
124 static int find_proc_slot
__P((void));
125 static int find_index_by_pid
__P((pid_t
));
126 static int find_status_by_pid
__P((pid_t
));
127 static int process_exit_status
__P((WAIT
));
128 static int find_termsig_by_pid
__P((pid_t
));
129 static int get_termsig
__P((WAIT
));
130 static void set_pid_status
__P((pid_t
, WAIT
));
131 static void set_pid_flags
__P((pid_t
, int));
132 static void unset_pid_flags
__P((pid_t
, int));
133 static int get_pid_flags
__P((pid_t
));
134 static void add_pid
__P((pid_t
, int));
135 static void mark_dead_jobs_as_notified
__P((int));
137 static sighandler wait_sigint_handler
__P((int));
138 static char *j_strsignal
__P((int));
140 #if defined (HAVE_WAITPID)
141 static void reap_zombie_children
__P((void));
144 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
145 static int siginterrupt
__P((int, int));
148 static void restore_sigint_handler
__P((void));
150 /* Allocate new, or grow existing PID_LIST. */
155 int old
= pid_list_size
;
158 pid_list
= (struct proc_status
*)xrealloc (pid_list
, pid_list_size
* sizeof (struct proc_status
));
160 /* None of the newly allocated slots have process id's yet. */
161 for (i
= old
; i
< pid_list_size
; i
++)
162 pid_list
[i
].pid
= NO_PID
;
165 /* Return the offset within the PID_LIST array of an empty slot. This can
166 create new slots if all of the existing slots are taken. */
172 for (i
= 0; i
< pid_list_size
; i
++)
173 if (pid_list
[i
].pid
== NO_PID
)
176 if (i
== pid_list_size
)
182 /* Return the offset within the PID_LIST array of a slot containing PID,
183 or the value NO_PID if the pid wasn't found. */
185 find_index_by_pid (pid
)
190 for (i
= 0; i
< pid_list_size
; i
++)
191 if (pid_list
[i
].pid
== pid
)
197 /* Return the status of PID as looked up in the PID_LIST array. A
198 return value of PROC_BAD indicates that PID wasn't found. */
200 find_status_by_pid (pid
)
205 i
= find_index_by_pid (pid
);
208 if (pid_list
[i
].flags
& PROC_RUNNING
)
209 return (PROC_STILL_ALIVE
);
210 return (pid_list
[i
].status
);
214 process_exit_status (status
)
217 if (WIFSIGNALED (status
))
218 return (128 + WTERMSIG (status
));
220 return (WEXITSTATUS (status
));
223 /* Return the status of PID as looked up in the PID_LIST array. A
224 return value of PROC_BAD indicates that PID wasn't found. */
226 find_termsig_by_pid (pid
)
231 i
= find_index_by_pid (pid
);
234 if (pid_list
[i
].flags
& PROC_RUNNING
)
236 return (get_termsig ((WAIT
)pid_list
[i
].status
));
239 /* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look
240 up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately
241 depending on its flags and exit status. */
246 if (WIFSTOPPED (status
) == 0 && WIFSIGNALED (status
))
247 return (WTERMSIG (status
));
252 /* Give PID the status value STATUS in the PID_LIST array. */
254 set_pid_status (pid
, status
)
260 #if defined (COPROCESS_SUPPORT)
261 coproc_pidchk (pid
, status
);
264 slot
= find_index_by_pid (pid
);
268 pid_list
[slot
].status
= process_exit_status (status
);
269 pid_list
[slot
].flags
&= ~PROC_RUNNING
;
270 if (WIFSIGNALED (status
))
271 pid_list
[slot
].flags
|= PROC_SIGNALED
;
272 /* If it's not a background process, mark it as notified so it gets
274 if ((pid_list
[slot
].flags
& PROC_ASYNC
) == 0)
275 pid_list
[slot
].flags
|= PROC_NOTIFIED
;
278 /* Give PID the flags FLAGS in the PID_LIST array. */
280 set_pid_flags (pid
, flags
)
286 slot
= find_index_by_pid (pid
);
290 pid_list
[slot
].flags
|= flags
;
293 /* Unset FLAGS for PID in the pid list */
295 unset_pid_flags (pid
, flags
)
301 slot
= find_index_by_pid (pid
);
305 pid_list
[slot
].flags
&= ~flags
;
308 /* Return the flags corresponding to PID in the PID_LIST array. */
315 slot
= find_index_by_pid (pid
);
319 return (pid_list
[slot
].flags
);
329 slot
= find_proc_slot ();
331 pid_list
[slot
].pid
= pid
;
332 pid_list
[slot
].status
= -1;
333 pid_list
[slot
].flags
= PROC_RUNNING
;
335 pid_list
[slot
].flags
|= PROC_ASYNC
;
339 mark_dead_jobs_as_notified (force
)
342 register int i
, ndead
;
344 /* first, count the number of non-running async jobs if FORCE == 0 */
345 for (i
= ndead
= 0; force
== 0 && i
< pid_list_size
; i
++)
347 if (pid_list
[i
].pid
== NO_PID
)
349 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
350 (pid_list
[i
].flags
& PROC_ASYNC
))
355 child_max
= getmaxchild ();
357 child_max
= DEFAULT_CHILD_MAX
;
359 if (force
== 0 && ndead
<= child_max
)
362 /* If FORCE == 0, we just mark as many non-running async jobs as notified
363 to bring us under the CHILD_MAX limit. */
364 for (i
= 0; i
< pid_list_size
; i
++)
366 if (pid_list
[i
].pid
== NO_PID
)
368 if (((pid_list
[i
].flags
& PROC_RUNNING
) == 0) &&
369 pid_list
[i
].pid
!= last_asynchronous_pid
)
371 pid_list
[i
].flags
|= PROC_NOTIFIED
;
372 if (force
== 0 && (pid_list
[i
].flags
& PROC_ASYNC
) && --ndead
<= child_max
)
378 /* Remove all dead, notified jobs from the pid_list. */
384 #if defined (HAVE_WAITPID)
385 reap_zombie_children ();
388 for (i
= 0; i
< pid_list_size
; i
++)
390 if ((pid_list
[i
].flags
& PROC_RUNNING
) == 0 &&
391 (pid_list
[i
].flags
& PROC_NOTIFIED
))
392 pid_list
[i
].pid
= NO_PID
;
395 #if defined (COPROCESS_SUPPORT)
405 mark_dead_jobs_as_notified (0);
406 cleanup_dead_jobs ();
409 /* Initialize the job control mechanism, and set up the tty stuff. */
410 initialize_job_control (force
)
413 shell_tty
= fileno (stderr
);
419 /* Setup this shell to handle C-C, etc. */
421 initialize_job_signals ()
423 set_signal_handler (SIGINT
, sigint_sighandler
);
425 /* If this is a login shell we don't wish to be disturbed by
428 ignore_tty_job_signals ();
431 #if defined (HAVE_WAITPID)
432 /* Collect the status of all zombie children so that their system
433 resources can be deallocated. */
435 reap_zombie_children ()
437 # if defined (WNOHANG)
442 while ((pid
= waitpid (-1, (int *)&status
, WNOHANG
)) > 0)
443 set_pid_status (pid
, status
);
444 # endif /* WNOHANG */
449 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
451 siginterrupt (sig
, flag
)
454 struct sigaction act
;
456 sigaction (sig
, (struct sigaction
*)NULL
, &act
);
459 act
.sa_flags
&= ~SA_RESTART
;
461 act
.sa_flags
|= SA_RESTART
;
463 return (sigaction (sig
, &act
, (struct sigaction
*)NULL
));
465 #endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */
467 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
468 COMMAND is just for remembering the name of the command; we don't do
469 anything else with it. ASYNC_P says what to do with the tty. If
470 non-zero, then don't give it away. */
472 make_child (command
, async_p
)
479 /* Discard saved memory. */
485 #if defined (BUFFERED_INPUT)
486 /* If default_buffered_input is active, we are reading a script. If
487 the command is asynchronous, we have already duplicated /dev/null
488 as fd 0, but have not changed the buffered stream corresponding to
489 the old fd 0. We don't want to sync the stream in this case. */
490 if (default_buffered_input
!= -1 && (!async_p
|| default_buffered_input
> 0))
491 sync_buffered_stream (default_buffered_input
);
492 #endif /* BUFFERED_INPUT */
494 /* Create the child, handle severe errors. Retry on EAGAIN. */
496 while ((pid
= fork ()) < 0 && errno
== EAGAIN
&& forksleep
< FORKSLEEP_MAX
)
498 sys_error ("fork: retry");
499 #if defined (HAVE_WAITPID)
500 /* Posix systems with a non-blocking waitpid () system call available
501 get another chance after zombies are reaped. */
502 reap_zombie_children ();
503 if (forksleep
> 1 && sleep (forksleep
) != 0)
506 if (sleep (forksleep
) != 0)
508 #endif /* HAVE_WAITPID */
515 throw_to_top_level ();
520 #if defined (BUFFERED_INPUT)
521 unset_bash_input (0);
522 #endif /* BUFFERED_INPUT */
524 #if defined (HAVE_POSIX_SIGNALS)
525 /* Restore top-level signal mask. */
526 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
530 /* Ignore INT and QUIT in asynchronous children. */
532 last_asynchronous_pid
= getpid ();
535 default_tty_job_signals ();
544 last_asynchronous_pid
= pid
;
546 add_pid (pid
, async_p
);
552 ignore_tty_job_signals ()
554 #if defined (SIGTSTP)
555 set_signal_handler (SIGTSTP
, SIG_IGN
);
556 set_signal_handler (SIGTTIN
, SIG_IGN
);
557 set_signal_handler (SIGTTOU
, SIG_IGN
);
562 default_tty_job_signals ()
564 #if defined (SIGTSTP)
565 set_signal_handler (SIGTSTP
, SIG_DFL
);
566 set_signal_handler (SIGTTIN
, SIG_DFL
);
567 set_signal_handler (SIGTTOU
, SIG_DFL
);
571 /* Wait for a single pid (PID) and return its exit status. Called by
574 wait_for_single_pid (pid
)
581 pstatus
= find_status_by_pid (pid
);
583 if (pstatus
== PROC_BAD
)
585 internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid
);
589 if (pstatus
!= PROC_STILL_ALIVE
)
592 last_command_exit_signal
= find_termsig_by_pid (pid
);
596 siginterrupt (SIGINT
, 1);
597 while ((got_pid
= WAITPID (pid
, &status
, 0)) != pid
)
602 if (errno
!= EINTR
&& errno
!= ECHILD
)
604 siginterrupt (SIGINT
, 0);
609 else if (got_pid
> 0)
610 set_pid_status (got_pid
, status
);
615 set_pid_status (got_pid
, status
);
616 set_pid_flags (got_pid
, PROC_NOTIFIED
);
619 siginterrupt (SIGINT
, 0);
622 return (got_pid
> 0 ? process_exit_status (status
) : -1);
625 /* Wait for all of the shell's children to exit. Called by the `wait'
628 wait_for_background_pids ()
633 /* If we aren't using job control, we let the kernel take care of the
634 bookkeeping for us. wait () will return -1 and set errno to ECHILD
635 when there are no more unwaited-for child processes on both
636 4.2 BSD-based and System V-based systems. */
638 siginterrupt (SIGINT
, 1);
640 /* Wait for ECHILD */
641 while ((got_pid
= WAITPID (-1, &status
, 0)) != -1)
642 set_pid_status (got_pid
, status
);
644 if (errno
!= EINTR
&& errno
!= ECHILD
)
646 siginterrupt (SIGINT
, 0);
650 siginterrupt (SIGINT
, 0);
653 mark_dead_jobs_as_notified (1);
654 cleanup_dead_jobs ();
657 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
658 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
659 static SigHandler
*old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
662 restore_sigint_handler ()
664 if (old_sigint_handler
!= INVALID_SIGNAL_HANDLER
)
666 set_signal_handler (SIGINT
, old_sigint_handler
);
667 old_sigint_handler
= INVALID_SIGNAL_HANDLER
;
671 /* Handle SIGINT while we are waiting for children in a script to exit.
672 All interrupts are effectively ignored by the shell, but allowed to
673 kill a running job. */
675 wait_sigint_handler (sig
)
678 SigHandler
*sigint_handler
;
680 /* If we got a SIGINT while in `wait', and SIGINT is trapped, do
681 what POSIX.2 says (see builtins/wait.def for more info). */
682 if (this_shell_builtin
&& this_shell_builtin
== wait_builtin
&&
683 signal_is_trapped (SIGINT
) &&
684 ((sigint_handler
= trap_to_sighandler (SIGINT
)) == trap_handler
))
686 last_command_exit_value
= EXECUTION_FAILURE
;
687 restore_sigint_handler ();
688 interrupt_immediately
= 0;
689 trap_handler (SIGINT
); /* set pending_traps[SIGINT] */
690 wait_signal_received
= SIGINT
;
691 longjmp (wait_intr_buf
, 1);
694 if (interrupt_immediately
)
696 last_command_exit_value
= EXECUTION_FAILURE
;
697 restore_sigint_handler ();
702 wait_sigint_received
= 1;
711 static char retcode_name_buffer
[64] = { '\0' };
717 x
= retcode_name_buffer
;
718 sprintf (x
, "Signal %d", s
);
723 /* Wait for pid (one of our children) to terminate. This is called only
724 by the execution code in execute_cmd.c. */
729 int return_val
, pstatus
;
733 pstatus
= find_status_by_pid (pid
);
735 if (pstatus
== PROC_BAD
)
738 if (pstatus
!= PROC_STILL_ALIVE
)
741 last_command_exit_signal
= find_termsig_by_pid (pid
);
745 /* If we are running a script, ignore SIGINT while we're waiting for
746 a child to exit. The loop below does some of this, but not all. */
747 wait_sigint_received
= 0;
748 if (interactive_shell
== 0)
749 old_sigint_handler
= set_signal_handler (SIGINT
, wait_sigint_handler
);
751 while ((got_pid
= WAITPID (-1, &status
, 0)) != pid
) /* XXX was pid now -1 */
754 if (got_pid
< 0 && errno
== ECHILD
)
756 #if !defined (_POSIX_VERSION)
757 status
.w_termsig
= status
.w_retcode
= 0;
760 #endif /* _POSIX_VERSION */
763 else if (got_pid
< 0 && errno
!= EINTR
)
764 programming_error ("wait_for(%ld): %s", (long)pid
, strerror(errno
));
765 else if (got_pid
> 0)
766 set_pid_status (got_pid
, status
);
770 set_pid_status (got_pid
, status
);
772 #if defined (HAVE_WAITPID)
774 reap_zombie_children ();
775 #endif /* HAVE_WAITPID */
777 if (interactive_shell
== 0)
779 SigHandler
*temp_handler
;
781 temp_handler
= old_sigint_handler
;
782 restore_sigint_handler ();
784 /* If the job exited because of SIGINT, make sure the shell acts as if
785 it had received one also. */
786 if (WIFSIGNALED (status
) && (WTERMSIG (status
) == SIGINT
))
789 if (maybe_call_trap_handler (SIGINT
) == 0)
791 if (temp_handler
== SIG_DFL
)
792 termsig_handler (SIGINT
);
793 else if (temp_handler
!= INVALID_SIGNAL_HANDLER
&& temp_handler
!= SIG_IGN
)
794 (*temp_handler
) (SIGINT
);
799 /* Default return value. */
800 /* ``a full 8 bits of status is returned'' */
801 return_val
= process_exit_status (status
);
802 last_command_exit_signal
= get_termsig (status
);
804 #if !defined (DONT_REPORT_SIGPIPE)
805 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
806 (WTERMSIG (status
) != SIGINT
))
808 if ((WIFSTOPPED (status
) == 0) && WIFSIGNALED (status
) &&
809 (WTERMSIG (status
) != SIGINT
) && (WTERMSIG (status
) != SIGPIPE
))
812 fprintf (stderr
, "%s", j_strsignal (WTERMSIG (status
)));
813 if (WIFCORED (status
))
814 fprintf (stderr
, _(" (core dumped)"));
815 fprintf (stderr
, "\n");
818 if (interactive_shell
&& subshell_environment
== 0)
820 if (WIFSIGNALED (status
) || WIFSTOPPED (status
))
829 /* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero,
830 or PID is less than -1, then kill the process group associated with PID. */
832 kill_pid (pid
, signal
, group
)
843 result
= group
? killpg (pid
, signal
) : kill (pid
, signal
);
847 static TTYSTRUCT shell_tty_info
;
848 static int got_tty_state
;
850 /* Fill the contents of shell_tty_info with the current tty info. */
858 ttgetattr (tty
, &shell_tty_info
);
860 if (check_window_size
)
861 get_new_window_size (0, (int *)0, (int *)0);
865 /* Make the current tty use the state in shell_tty_info. */
874 if (got_tty_state
== 0)
876 ttsetattr (tty
, &shell_tty_info
);
881 /* Give the terminal to PGRP. */
882 give_terminal_to (pgrp
, force
)
888 /* Stop a pipeline. */
890 stop_pipeline (async
, ignore
)
894 already_making_children
= 0;
901 already_making_children
= 1;
905 stop_making_children ()
907 already_making_children
= 0;
911 get_job_by_pid (pid
, block
)
917 i
= find_index_by_pid (pid
);
918 return ((i
== NO_PID
) ? PROC_BAD
: i
);
921 /* Print descriptive information about the job with leader pid PID. */
926 fprintf (stderr
, "%ld\n", (long) pid
);
930 unfreeze_jobs_list ()