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-2011 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
volatile pid_t last_made_pid = NO_PID;
87
volatile 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 = CHECKWINSIZE_DEFAULT;
99
/* We don't have job control. */
102
/* STATUS and FLAGS are only valid if pid != NO_PID
103
STATUS is only valid if (flags & PROC_RUNNING) == 0 */
106
int status; /* Exit status of PID or 128 + fatal signal number */
110
/* Values for proc_status.flags */
111
#define PROC_RUNNING 0x01
112
#define PROC_NOTIFIED 0x02
113
#define PROC_ASYNC 0x04
114
#define PROC_SIGNALED 0x10
116
/* Return values from find_status_by_pid */
118
#define PROC_STILL_ALIVE -2
120
static struct proc_status *pid_list = (struct proc_status *)NULL;
121
static int pid_list_size;
122
static int wait_sigint_received;
124
static long child_max = -1L;
126
static void alloc_pid_list __P((void));
127
static int find_proc_slot __P((void));
128
static int find_index_by_pid __P((pid_t));
129
static int find_status_by_pid __P((pid_t));
130
static int process_exit_status __P((WAIT));
131
static int find_termsig_by_pid __P((pid_t));
132
static int get_termsig __P((WAIT));
133
static void set_pid_status __P((pid_t, WAIT));
134
static void set_pid_flags __P((pid_t, int));
135
static void unset_pid_flags __P((pid_t, int));
136
static int get_pid_flags __P((pid_t));
137
static void add_pid __P((pid_t, int));
138
static void mark_dead_jobs_as_notified __P((int));
140
static sighandler wait_sigint_handler __P((int));
141
static char *j_strsignal __P((int));
143
#if defined (HAVE_WAITPID)
144
static void reap_zombie_children __P((void));
147
#if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
148
static int siginterrupt __P((int, int));
151
static void restore_sigint_handler __P((void));
153
/* Allocate new, or grow existing PID_LIST. */
158
int old = pid_list_size;
161
pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));
163
/* None of the newly allocated slots have process id's yet. */
164
for (i = old; i < pid_list_size; i++)
165
pid_list[i].pid = NO_PID;
168
/* Return the offset within the PID_LIST array of an empty slot. This can
169
create new slots if all of the existing slots are taken. */
175
for (i = 0; i < pid_list_size; i++)
176
if (pid_list[i].pid == NO_PID)
179
if (i == pid_list_size)
185
/* Return the offset within the PID_LIST array of a slot containing PID,
186
or the value NO_PID if the pid wasn't found. */
188
find_index_by_pid (pid)
193
for (i = 0; i < pid_list_size; i++)
194
if (pid_list[i].pid == pid)
200
/* Return the status of PID as looked up in the PID_LIST array. A
201
return value of PROC_BAD indicates that PID wasn't found. */
203
find_status_by_pid (pid)
208
i = find_index_by_pid (pid);
211
if (pid_list[i].flags & PROC_RUNNING)
212
return (PROC_STILL_ALIVE);
213
return (pid_list[i].status);
217
process_exit_status (status)
220
if (WIFSIGNALED (status))
221
return (128 + WTERMSIG (status));
223
return (WEXITSTATUS (status));
226
/* Return the status of PID as looked up in the PID_LIST array. A
227
return value of PROC_BAD indicates that PID wasn't found. */
229
find_termsig_by_pid (pid)
234
i = find_index_by_pid (pid);
237
if (pid_list[i].flags & PROC_RUNNING)
239
return (get_termsig ((WAIT)pid_list[i].status));
242
/* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS. If STATUS is -1, look
243
up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately
244
depending on its flags and exit status. */
249
if (WIFSTOPPED (status) == 0 && WIFSIGNALED (status))
250
return (WTERMSIG (status));
255
/* Give PID the status value STATUS in the PID_LIST array. */
257
set_pid_status (pid, status)
263
#if defined (COPROCESS_SUPPORT)
264
coproc_pidchk (pid, status);
267
slot = find_index_by_pid (pid);
271
pid_list[slot].status = process_exit_status (status);
272
pid_list[slot].flags &= ~PROC_RUNNING;
273
if (WIFSIGNALED (status))
274
pid_list[slot].flags |= PROC_SIGNALED;
275
/* If it's not a background process, mark it as notified so it gets
277
if ((pid_list[slot].flags & PROC_ASYNC) == 0)
278
pid_list[slot].flags |= PROC_NOTIFIED;
281
/* Give PID the flags FLAGS in the PID_LIST array. */
283
set_pid_flags (pid, flags)
289
slot = find_index_by_pid (pid);
293
pid_list[slot].flags |= flags;
296
/* Unset FLAGS for PID in the pid list */
298
unset_pid_flags (pid, flags)
304
slot = find_index_by_pid (pid);
308
pid_list[slot].flags &= ~flags;
311
/* Return the flags corresponding to PID in the PID_LIST array. */
318
slot = find_index_by_pid (pid);
322
return (pid_list[slot].flags);
332
slot = find_proc_slot ();
334
pid_list[slot].pid = pid;
335
pid_list[slot].status = -1;
336
pid_list[slot].flags = PROC_RUNNING;
338
pid_list[slot].flags |= PROC_ASYNC;
342
mark_dead_jobs_as_notified (force)
345
register int i, ndead;
347
/* first, count the number of non-running async jobs if FORCE == 0 */
348
for (i = ndead = 0; force == 0 && i < pid_list_size; i++)
350
if (pid_list[i].pid == NO_PID)
352
if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
353
(pid_list[i].flags & PROC_ASYNC))
358
child_max = getmaxchild ();
360
child_max = DEFAULT_CHILD_MAX;
362
if (force == 0 && ndead <= child_max)
365
/* If FORCE == 0, we just mark as many non-running async jobs as notified
366
to bring us under the CHILD_MAX limit. */
367
for (i = 0; i < pid_list_size; i++)
369
if (pid_list[i].pid == NO_PID)
371
if (((pid_list[i].flags & PROC_RUNNING) == 0) &&
372
pid_list[i].pid != last_asynchronous_pid)
374
pid_list[i].flags |= PROC_NOTIFIED;
375
if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max)
381
/* Remove all dead, notified jobs from the pid_list. */
387
#if defined (HAVE_WAITPID)
388
reap_zombie_children ();
391
for (i = 0; i < pid_list_size; i++)
393
if ((pid_list[i].flags & PROC_RUNNING) == 0 &&
394
(pid_list[i].flags & PROC_NOTIFIED))
395
pid_list[i].pid = NO_PID;
398
#if defined (COPROCESS_SUPPORT)
408
mark_dead_jobs_as_notified (0);
409
cleanup_dead_jobs ();
412
/* Initialize the job control mechanism, and set up the tty stuff. */
413
initialize_job_control (force)
416
shell_tty = fileno (stderr);
422
/* Setup this shell to handle C-C, etc. */
424
initialize_job_signals ()
426
set_signal_handler (SIGINT, sigint_sighandler);
428
/* If this is a login shell we don't wish to be disturbed by
431
ignore_tty_job_signals ();
434
#if defined (HAVE_WAITPID)
435
/* Collect the status of all zombie children so that their system
436
resources can be deallocated. */
438
reap_zombie_children ()
440
# if defined (WNOHANG)
446
while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
447
set_pid_status (pid, status);
448
# endif /* WNOHANG */
454
#if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
456
#if !defined (SA_RESTART)
457
# define SA_RESTART 0
461
siginterrupt (sig, flag)
464
struct sigaction act;
466
sigaction (sig, (struct sigaction *)NULL, &act);
469
act.sa_flags &= ~SA_RESTART;
471
act.sa_flags |= SA_RESTART;
473
return (sigaction (sig, &act, (struct sigaction *)NULL));
475
#endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS */
477
/* Fork, handling errors. Returns the pid of the newly made child, or 0.
478
COMMAND is just for remembering the name of the command; we don't do
479
anything else with it. ASYNC_P says what to do with the tty. If
480
non-zero, then don't give it away. */
482
make_child (command, async_p)
489
/* Discard saved memory. */
495
#if defined (BUFFERED_INPUT)
496
/* If default_buffered_input is active, we are reading a script. If
497
the command is asynchronous, we have already duplicated /dev/null
498
as fd 0, but have not changed the buffered stream corresponding to
499
the old fd 0. We don't want to sync the stream in this case. */
500
if (default_buffered_input != -1 && (!async_p || default_buffered_input > 0))
501
sync_buffered_stream (default_buffered_input);
502
#endif /* BUFFERED_INPUT */
504
/* XXX - block SIGTERM here and unblock in child after fork resets the
505
set of pending signals? */
508
/* Create the child, handle severe errors. Retry on EAGAIN. */
510
while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX)
512
sys_error ("fork: retry");
515
#if defined (HAVE_WAITPID)
516
/* Posix systems with a non-blocking waitpid () system call available
517
get another chance after zombies are reaped. */
518
reap_zombie_children ();
519
if (forksleep > 1 && sleep (forksleep) != 0)
522
if (sleep (forksleep) != 0)
524
#endif /* HAVE_WAITPID */
534
last_command_exit_value = EX_NOEXEC;
535
throw_to_top_level ();
540
#if defined (BUFFERED_INPUT)
541
unset_bash_input (0);
542
#endif /* BUFFERED_INPUT */
544
#if defined (HAVE_POSIX_SIGNALS)
545
/* Restore top-level signal mask. */
546
sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
550
/* Ignore INT and QUIT in asynchronous children. */
552
last_asynchronous_pid = getpid ();
555
default_tty_job_signals ();
564
last_asynchronous_pid = pid;
566
add_pid (pid, async_p);
572
ignore_tty_job_signals ()
574
#if defined (SIGTSTP)
575
set_signal_handler (SIGTSTP, SIG_IGN);
576
set_signal_handler (SIGTTIN, SIG_IGN);
577
set_signal_handler (SIGTTOU, SIG_IGN);
582
default_tty_job_signals ()
584
#if defined (SIGTSTP)
585
set_signal_handler (SIGTSTP, SIG_DFL);
586
set_signal_handler (SIGTTIN, SIG_DFL);
587
set_signal_handler (SIGTTOU, SIG_DFL);
591
/* Wait for a single pid (PID) and return its exit status. Called by
594
wait_for_single_pid (pid)
601
pstatus = find_status_by_pid (pid);
603
if (pstatus == PROC_BAD)
605
internal_error (_("wait: pid %ld is not a child of this shell"), (long)pid);
609
if (pstatus != PROC_STILL_ALIVE)
612
last_command_exit_signal = find_termsig_by_pid (pid);
616
siginterrupt (SIGINT, 1);
617
while ((got_pid = WAITPID (pid, &status, 0)) != pid)
623
if (errno != EINTR && errno != ECHILD)
625
siginterrupt (SIGINT, 0);
630
else if (got_pid > 0)
631
set_pid_status (got_pid, status);
636
set_pid_status (got_pid, status);
637
set_pid_flags (got_pid, PROC_NOTIFIED);
640
siginterrupt (SIGINT, 0);
643
return (got_pid > 0 ? process_exit_status (status) : -1);
646
/* Wait for all of the shell's children to exit. Called by the `wait'
649
wait_for_background_pids ()
654
/* If we aren't using job control, we let the kernel take care of the
655
bookkeeping for us. wait () will return -1 and set errno to ECHILD
656
when there are no more unwaited-for child processes on both
657
4.2 BSD-based and System V-based systems. */
659
siginterrupt (SIGINT, 1);
661
/* Wait for ECHILD */
662
while ((got_pid = WAITPID (-1, &status, 0)) != -1)
663
set_pid_status (got_pid, status);
665
if (errno != EINTR && errno != ECHILD)
667
siginterrupt (SIGINT, 0);
671
siginterrupt (SIGINT, 0);
674
mark_dead_jobs_as_notified (1);
675
cleanup_dead_jobs ();
678
/* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
679
#define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
680
static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
683
restore_sigint_handler ()
685
if (old_sigint_handler != INVALID_SIGNAL_HANDLER)
687
set_signal_handler (SIGINT, old_sigint_handler);
688
old_sigint_handler = INVALID_SIGNAL_HANDLER;
692
/* Handle SIGINT while we are waiting for children in a script to exit.
693
All interrupts are effectively ignored by the shell, but allowed to
694
kill a running job. */
696
wait_sigint_handler (sig)
699
SigHandler *sigint_handler;
701
/* If we got a SIGINT while in `wait', and SIGINT is trapped, do
702
what POSIX.2 says (see builtins/wait.def for more info). */
703
if (this_shell_builtin && this_shell_builtin == wait_builtin &&
704
signal_is_trapped (SIGINT) &&
705
((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
707
last_command_exit_value = 128+SIGINT;
708
restore_sigint_handler ();
709
interrupt_immediately = 0;
710
trap_handler (SIGINT); /* set pending_traps[SIGINT] */
711
wait_signal_received = SIGINT;
715
if (interrupt_immediately)
717
last_command_exit_value = EXECUTION_FAILURE;
718
restore_sigint_handler ();
723
wait_sigint_received = 1;
732
static char retcode_name_buffer[64] = { '\0' };
738
x = retcode_name_buffer;
739
sprintf (x, "Signal %d", s);
744
/* Wait for pid (one of our children) to terminate. This is called only
745
by the execution code in execute_cmd.c. */
750
int return_val, pstatus;
754
pstatus = find_status_by_pid (pid);
756
if (pstatus == PROC_BAD)
759
if (pstatus != PROC_STILL_ALIVE)
762
last_command_exit_signal = find_termsig_by_pid (pid);
766
/* If we are running a script, ignore SIGINT while we're waiting for
767
a child to exit. The loop below does some of this, but not all. */
768
wait_sigint_received = 0;
769
if (interactive_shell == 0)
770
old_sigint_handler = set_signal_handler (SIGINT, wait_sigint_handler);
772
while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
776
if (got_pid < 0 && errno == ECHILD)
778
#if !defined (_POSIX_VERSION)
779
status.w_termsig = status.w_retcode = 0;
782
#endif /* _POSIX_VERSION */
785
else if (got_pid < 0 && errno != EINTR)
786
programming_error ("wait_for(%ld): %s", (long)pid, strerror(errno));
787
else if (got_pid > 0)
788
set_pid_status (got_pid, status);
792
set_pid_status (got_pid, status);
794
#if defined (HAVE_WAITPID)
796
reap_zombie_children ();
797
#endif /* HAVE_WAITPID */
802
if (interactive_shell == 0)
804
SigHandler *temp_handler;
806
temp_handler = old_sigint_handler;
807
restore_sigint_handler ();
809
/* If the job exited because of SIGINT, make sure the shell acts as if
810
it had received one also. */
811
if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGINT))
814
if (maybe_call_trap_handler (SIGINT) == 0)
816
if (temp_handler == SIG_DFL)
817
termsig_handler (SIGINT);
818
else if (temp_handler != INVALID_SIGNAL_HANDLER && temp_handler != SIG_IGN)
819
(*temp_handler) (SIGINT);
824
/* Default return value. */
825
/* ``a full 8 bits of status is returned'' */
826
return_val = process_exit_status (status);
827
last_command_exit_signal = get_termsig (status);
829
#if defined (DONT_REPORT_SIGPIPE) && defined (DONT_REPORT_SIGTERM)
830
# define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE && (x) != SIGTERM)
831
#elif !defined (DONT_REPORT_SIGPIPE) && !defined (DONT_REPORT_SIGTERM)
832
# define REPORTSIG(x) ((x) != SIGINT)
833
#elif defined (DONT_REPORT_SIGPIPE)
834
# define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE)
836
# define REPORTSIG(x) ((x) != SIGINT && (x) != SIGTERM)
839
if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) && REPORTSIG(WTERMSIG (status)))
841
fprintf (stderr, "%s", j_strsignal (WTERMSIG (status)));
842
if (WIFCORED (status))
843
fprintf (stderr, _(" (core dumped)"));
844
fprintf (stderr, "\n");
847
if (interactive_shell && subshell_environment == 0)
849
if (WIFSIGNALED (status) || WIFSTOPPED (status))
854
else if (interactive_shell == 0 && subshell_environment == 0 && check_window_size)
855
get_new_window_size (0, (int *)0, (int *)0);
860
/* Send PID SIGNAL. Returns -1 on failure, 0 on success. If GROUP is non-zero,
861
or PID is less than -1, then kill the process group associated with PID. */
863
kill_pid (pid, signal, group)
874
result = group ? killpg (pid, signal) : kill (pid, signal);
878
static TTYSTRUCT shell_tty_info;
879
static int got_tty_state;
881
/* Fill the contents of shell_tty_info with the current tty info. */
889
ttgetattr (tty, &shell_tty_info);
891
if (check_window_size)
892
get_new_window_size (0, (int *)0, (int *)0);
896
/* Make the current tty use the state in shell_tty_info. */
905
if (got_tty_state == 0)
907
ttsetattr (tty, &shell_tty_info);
912
/* Give the terminal to PGRP. */
913
give_terminal_to (pgrp, force)
919
/* Stop a pipeline. */
921
stop_pipeline (async, ignore)
925
already_making_children = 0;
932
already_making_children = 1;
936
stop_making_children ()
938
already_making_children = 0;
942
get_job_by_pid (pid, block)
948
i = find_index_by_pid (pid);
949
return ((i == NO_PID) ? PROC_BAD : i);
952
/* Print descriptive information about the job with leader pid PID. */
957
fprintf (stderr, "%ld\n", (long) pid);
966
unfreeze_jobs_list ()