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 /* For struct winsize on SCO */
62 /* sys/ptem.h has winsize but needs mblk_t from sys/stream.h */
63 #if defined (HAVE_SYS_PTEM_H) && defined (TIOCGWINSZ) && defined (SIGWINCH)
64 # if defined (HAVE_SYS_STREAM_H)
65 # include <sys/stream.h>
67 # include <sys/ptem.h>
70 #if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)
71 # define killpg(pg, sig) kill(-(pg),(sig))
72 #endif /* USG || _POSIX_VERSION */
74 #if !defined (HAVE_SIGINTERRUPT)
75 # define siginterrupt(sig, code)
78 #if defined (HAVE_WAITPID)
79 # define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)
81 # define WAITPID(pid, statusp, options) wait (statusp)
82 #endif /* !HAVE_WAITPID */
88 extern int interactive
, interactive_shell
, login_shell
;
89 extern int subshell_environment
;
90 extern int last_command_exit_value
;
91 #if defined (HAVE_POSIX_SIGNALS)
92 extern sigset_t top_level_mask
;
95 pid_t last_made_pid
= NO_PID
;
96 pid_t last_asynchronous_pid
= NO_PID
;
98 /* Call this when you start making children. */
99 int already_making_children
= 0;
101 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
102 exits from get_tty_state(). */
103 int check_window_size
;
105 #if defined (HAVE_WAITPID)
106 static void reap_zombie_children ();
111 int status
; /* Exit status of PID or 128 + fatal signal number */
114 static struct proc_status
*pid_list
= (struct proc_status
*)NULL
;
115 static int pid_list_size
;
118 #define PROC_STILL_ALIVE -2
120 /* Allocate new, or grow existing PID_LIST. */
125 int old
= pid_list_size
;
128 pid_list
= (struct proc_status
*)
129 xrealloc (pid_list
, pid_list_size
* sizeof (struct proc_status
));
131 /* None of the newly allocated slots have process id's yet. */
132 for (i
= old
; i
< pid_list_size
; i
++)
133 pid_list
[i
].pid
= NO_PID
;
136 /* Return the offset within the PID_LIST array of an empty slot. This can
137 create new slots if all of the existing slots are taken. */
143 for (i
= 0; i
< pid_list_size
; i
++)
144 if (pid_list
[i
].pid
== NO_PID
)
147 if (i
== pid_list_size
)
153 /* Return the offset within the PID_LIST array of a slot containing PID,
154 or the value NO_PID if the pid wasn't found. */
156 find_index_by_pid (pid
)
161 for (i
= 0; i
< pid_list_size
; i
++)
162 if (pid_list
[i
].pid
== pid
)
168 /* Return the status of PID as looked up in the PID_LIST array. A
169 return value of PROC_BAD indicates that PID wasn't found. */
171 find_status_by_pid (pid
)
176 i
= find_index_by_pid (pid
);
179 return (pid_list
[i
].status
);
182 /* Give PID the status value STATUS in the PID_LIST array. */
184 set_pid_status (pid
, status
)
190 slot
= find_index_by_pid (pid
);
194 if (WIFSIGNALED (status
))
195 pid_list
[slot
].status
= 128 + WTERMSIG (status
);
197 pid_list
[slot
].status
= WEXITSTATUS (status
);
206 slot
= find_proc_slot ();
207 pid_list
[slot
].pid
= pid
;
208 pid_list
[slot
].status
= PROC_STILL_ALIVE
;
216 #if defined (HAVE_WAITPID)
217 reap_zombie_children ();
220 for (i
= 0; i
< pid_list_size
; i
++)
221 if (pid_list
[i
].status
!= PROC_STILL_ALIVE
)
222 pid_list
[i
].pid
= NO_PID
;
225 /* Initialize the job control mechanism, and set up the tty stuff. */
231 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
232 static SigHandler
*old_winch
= (SigHandler
*)SIG_DFL
;
235 get_new_window_size (from_sig
)
241 tty
= open ("/dev/tty", O_RDONLY
);
242 if (tty
>= 0 && (ioctl (tty
, TIOCGWINSZ
, &win
) == 0) &&
243 win
.ws_row
> 0 && win
.ws_col
> 0)
246 shell_tty_info
.c_winsize
= win
; /* structure copying */
248 set_lines_and_columns (win
.ws_row
, win
.ws_col
);
249 #if defined (READLINE)
250 _rl_set_screen_size (win
.ws_row
, win
.ws_col
);
257 sigwinch_sighandler (sig
)
260 #if defined (MUST_REINSTALL_SIGHANDLERS)
261 set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
262 #endif /* MUST_REINSTALL_SIGHANDLERS */
263 get_new_window_size (1);
267 get_new_window_size (from_sig
)
271 #endif /* TIOCGWINSZ && SIGWINCH */
274 set_sigwinch_handler ()
276 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
277 old_winch
= set_signal_handler (SIGWINCH
, sigwinch_sighandler
);
282 unset_sigwinch_handler ()
284 #if defined (TIOCGWINSZ) && defined (SIGWINCH)
285 set_signal_handler (SIGWINCH
, old_winch
);
289 /* Setup this shell to handle C-C, etc. */
291 initialize_job_signals ()
293 set_signal_handler (SIGINT
, sigint_sighandler
);
294 set_sigwinch_handler ();
296 /* If this is a login shell we don't wish to be disturbed by
299 ignore_tty_job_signals ();
302 #if defined (HAVE_WAITPID)
303 /* Collect the status of all zombie children so that their system
304 resources can be deallocated. */
306 reap_zombie_children ()
308 #if defined (WNOHANG)
312 while ((pid
= waitpid (-1, (int *)&status
, WNOHANG
)) > 0)
313 set_pid_status (pid
, status
);
316 #endif /* WAITPID && WNOHANG */
318 /* Fork, handling errors. Returns the pid of the newly made child, or 0.
319 COMMAND is just for remembering the name of the command; we don't do
320 anything else with it. ASYNC_P says what to do with the tty. If
321 non-zero, then don't give it away. */
323 make_child (command
, async_p
)
328 #if defined (HAVE_WAITPID)
330 #endif /* HAVE_WAITPID */
332 /* Discard saved memory. */
338 #if defined (BUFFERED_INPUT)
339 /* If default_buffered_input is active, we are reading a script. If
340 the command is asynchronous, we have already duplicated /dev/null
341 as fd 0, but have not changed the buffered stream corresponding to
342 the old fd 0. We don't want to sync the stream in this case. */
343 if (default_buffered_input
!= -1 &&
344 (!async_p
|| default_buffered_input
> 0))
345 sync_buffered_stream (default_buffered_input
);
346 #endif /* BUFFERED_INPUT */
348 /* Create the child, handle severe errors. */
349 #if defined (HAVE_WAITPID)
351 #endif /* HAVE_WAITPID */
353 if ((pid
= fork ()) < 0)
355 #if defined (HAVE_WAITPID)
356 /* Posix systems with a non-blocking waitpid () system call available
357 get another chance after zombies are reaped. */
358 if (errno
== EAGAIN
&& retry
)
360 reap_zombie_children ();
364 #endif /* HAVE_WAITPID */
368 throw_to_top_level ();
373 #if defined (BUFFERED_INPUT)
374 if (default_buffered_input
> 0)
376 close_buffered_fd (default_buffered_input
);
377 default_buffered_input
= bash_input
.location
.buffered_fd
= -1;
379 #endif /* BUFFERED_INPUT */
381 #if defined (HAVE_POSIX_SIGNALS)
382 /* Restore top-level signal mask. */
383 sigprocmask (SIG_SETMASK
, &top_level_mask
, (sigset_t
*)NULL
);
386 /* Ignore INT and QUIT in asynchronous children. */
388 last_asynchronous_pid
= getpid ();
390 default_tty_job_signals ();
399 last_asynchronous_pid
= pid
;
407 ignore_tty_job_signals ()
409 #if defined (SIGTSTP)
410 set_signal_handler (SIGTSTP
, SIG_IGN
);
411 set_signal_handler (SIGTTIN
, SIG_IGN
);
412 set_signal_handler (SIGTTOU
, SIG_IGN
);
417 default_tty_job_signals ()
419 #if defined (SIGTSTP)
420 set_signal_handler (SIGTSTP
, SIG_DFL
);
421 set_signal_handler (SIGTTIN
, SIG_DFL
);
422 set_signal_handler (SIGTTOU
, SIG_DFL
);
426 /* Wait for a single pid (PID) and return its exit status. */
427 wait_for_single_pid (pid
)
434 pstatus
= find_status_by_pid (pid
);
436 if (pstatus
== PROC_BAD
)
438 internal_error ("wait: pid %d is not a child of this shell", pid
);
442 if (pstatus
!= PROC_STILL_ALIVE
)
445 siginterrupt (SIGINT
, 1);
446 while ((got_pid
= WAITPID (pid
, &status
, 0)) != pid
)
450 if (errno
!= EINTR
&& errno
!= ECHILD
)
452 siginterrupt (SIGINT
, 0);
457 else if (got_pid
> 0)
458 set_pid_status (got_pid
, status
);
461 set_pid_status (got_pid
, status
);
462 siginterrupt (SIGINT
, 0);
465 if (WIFSIGNALED (status
))
466 return (128 + WTERMSIG (status
));
468 return (WEXITSTATUS (status
));
471 /* Wait for all of the shell's children to exit. */
473 wait_for_background_pids ()
478 /* If we aren't using job control, we let the kernel take care of the
479 bookkeeping for us. wait () will return -1 and set errno to ECHILD
480 when there are no more unwaited-for child processes on both
481 4.2 BSD-based and System V-based systems. */
483 siginterrupt (SIGINT
, 1);
485 /* Wait for ECHILD */
486 while ((got_pid
= WAITPID (-1, &status
, 0)) != -1)
487 set_pid_status (got_pid
, status
);
489 if (errno
!= EINTR
&& errno
!= ECHILD
)
491 siginterrupt (SIGINT
, 0);
495 siginterrupt (SIGINT
, 0);
499 /* Handle SIGINT while we are waiting for children in a script to exit.
500 All interrupts are effectively ignored by the shell, but allowed to
501 kill a running job. */
503 wait_sigint_handler (sig
)
507 /* Run a trap handler if one has been defined. */
508 maybe_call_trap_handler (sig
);
514 /* Wait for pid (one of our children) to terminate. This is called only
515 by the execution code in execute_cmd.c. */
520 int return_val
, pstatus
;
523 SigHandler
*old_sigint_handler
;
525 pstatus
= find_status_by_pid (pid
);
527 if (pstatus
== PROC_BAD
)
530 if (pstatus
!= PROC_STILL_ALIVE
)
533 /* If we are running a script, ignore SIGINT while we're waiting for
534 a child to exit. The loop below does some of this, but not all. */
535 if (!interactive_shell
)
536 old_sigint_handler
= set_signal_handler (SIGINT
, wait_sigint_handler
);
538 while ((got_pid
= WAITPID (-1, &status
, 0)) != pid
) /* XXX was pid now -1 */
540 if (got_pid
< 0 && errno
== ECHILD
)
542 #if !defined (_POSIX_VERSION)
543 status
.w_termsig
= status
.w_retcode
= 0;
546 #endif /* _POSIX_VERSION */
549 else if (got_pid
< 0 && errno
!= EINTR
)
550 programming_error ("wait_for(%d): %s", pid
, strerror(errno
));
551 else if (got_pid
> 0)
552 set_pid_status (got_pid
, status
);
555 set_pid_status (got_pid
, status
);
557 #if defined (HAVE_WAITPID)
559 reap_zombie_children ();
560 #endif /* HAVE_WAITPID */
562 if (interactive_shell
== 0)
564 set_signal_handler (SIGINT
, old_sigint_handler
);
565 /* If the job exited because of SIGINT, make sure the shell acts as if
566 it had received one also. */
567 if (WIFSIGNALED (status
) && (WTERMSIG (status
) == SIGINT
))
569 if (maybe_call_trap_handler (SIGINT
) == 0)
570 (*old_sigint_handler
) (SIGINT
);
574 /* Default return value. */
575 /* ``a full 8 bits of status is returned'' */
576 if (WIFSIGNALED (status
))
577 return_val
= 128 + WTERMSIG (status
);
579 return_val
= WEXITSTATUS (status
);
581 if (!WIFSTOPPED (status
) && WIFSIGNALED (status
) &&
582 (WTERMSIG (status
) != SIGINT
))
584 fprintf (stderr
, "%s", strsignal (WTERMSIG (status
)));
585 if (WIFCORED (status
))
586 fprintf (stderr
, " (core dumped)");
587 fprintf (stderr
, "\n");
590 if (interactive_shell
&& subshell_environment
== 0)
592 if (WIFSIGNALED (status
) || WIFSTOPPED (status
))
601 /* Give PID SIGNAL. This determines what job the pid belongs to (if any).
602 If PID does belong to a job, and the job is stopped, then CONTinue the
603 job after giving it SIGNAL. Returns -1 on failure. If GROUP is non-null,
604 then kill the process group associated with PID. */
606 kill_pid (pid
, signal
, group
)
613 result
= killpg (pid
, signal
);
615 result
= kill (pid
, signal
);
620 #if defined (TERMIOS_TTY_DRIVER)
621 static struct termios shell_tty_info
;
622 #else /* !TERMIOS_TTY_DRIVER */
623 # if defined (TERMIO_TTY_DRIVER)
624 static struct termio shell_tty_info
;
626 static struct sgttyb shell_tty_info
;
627 # endif /* !TERMIO_TTY_DRIVER */
628 #endif /* !TERMIOS_TTY_DRIVER */
630 static int got_tty_state
;
632 /* Fill the contents of shell_tty_info with the current tty info. */
637 tty
= open ("/dev/tty", O_RDONLY
);
640 #if defined (TERMIOS_TTY_DRIVER)
641 tcgetattr (tty
, &shell_tty_info
);
643 # if defined (TERMIO_TTY_DRIVER)
644 ioctl (tty
, TCGETA
, &shell_tty_info
);
646 ioctl (tty
, TIOCGETP
, &shell_tty_info
);
651 if (check_window_size
)
652 get_new_window_size (0);
656 /* Make the current tty use the state in shell_tty_info. */
661 tty
= open ("/dev/tty", O_RDONLY
);
664 if (got_tty_state
== 0)
669 #if defined (TERMIOS_TTY_DRIVER)
670 tcsetattr (tty
, TCSADRAIN
, &shell_tty_info
);
672 # if defined (TERMIO_TTY_DRIVER)
673 ioctl (tty
, TCSETAW
, &shell_tty_info
); /* Wait for output, no flush */
675 ioctl (tty
, TIOCSETN
, &shell_tty_info
);
682 /* Give the terminal to PGRP. */
683 give_terminal_to (pgrp
)
688 /* Stop a pipeline. */
689 stop_pipeline (async
, ignore
)
693 already_making_children
= 0;
699 already_making_children
= 1;
702 /* Print descriptive information about the job with leader pid PID. */
707 fprintf (stderr
, "%d\n", (int) pid
);
711 unfreeze_jobs_list ()