]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - nojobs.c
Bash-5.0 patch 4: the wait builtin without arguments only waits for known children...
[thirdparty/bash.git] / nojobs.c
index b73e3d6d1c1d7470a33128002a286526803c3246..2201a29ac9f927d2b4535d0508fb54d75acf09a6 100644 (file)
--- a/nojobs.c
+++ b/nojobs.c
@@ -3,7 +3,7 @@
 /* This file works under BSD, System V, minix, and Posix systems.  It does
    not implement job control. */
 
-/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2011 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
 #include "shell.h"
 #include "jobs.h"
 #include "execute_cmd.h"
+#include "trap.h"
 
 #include "builtins/builtext.h" /* for wait_builtin */
+#include "builtins/common.h"
 
 #define DEFAULT_CHILD_MAX 32
 
 extern int errno;
 #endif /* !errno */
 
-extern int interactive, interactive_shell, login_shell;
-extern int subshell_environment;
-extern int last_command_exit_value, last_command_exit_signal;
-extern int interrupt_immediately;
-extern sh_builtin_func_t *this_shell_builtin;
 #if defined (HAVE_POSIX_SIGNALS)
 extern sigset_t top_level_mask;
 #endif
-extern procenv_t wait_intr_buf;
-extern int wait_signal_received;
 
-pid_t last_made_pid = NO_PID;
-pid_t last_asynchronous_pid = NO_PID;
+extern void set_original_signal __P((int, SigHandler *));
+
+volatile pid_t last_made_pid = NO_PID;
+volatile pid_t last_asynchronous_pid = NO_PID;
+
+static int queue_sigchld, waiting_for_child;   /* dummy declarations */
 
 /* Call this when you start making children. */
 int already_making_children = 0;
@@ -94,7 +93,12 @@ int shell_tty = -1;
 
 /* If this is non-zero, $LINES and $COLUMNS are reset after every process
    exits from get_tty_state(). */
-int check_window_size;
+int check_window_size = CHECKWINSIZE_DEFAULT;
+
+/* We don't have job control. */
+int job_control = 0;
+
+int running_in_background = 0; /* can't tell without job control */
 
 /* STATUS and FLAGS are only valid if pid != NO_PID
    STATUS is only valid if (flags & PROC_RUNNING) == 0 */
@@ -121,7 +125,7 @@ static int wait_sigint_received;
 static long child_max = -1L;
 
 static void alloc_pid_list __P((void));
-static int find_proc_slot __P((void));
+static int find_proc_slot __P((pid_t));
 static int find_index_by_pid __P((pid_t));
 static int find_status_by_pid __P((pid_t));
 static int process_exit_status __P((WAIT));
@@ -159,18 +163,22 @@ alloc_pid_list ()
 
   /* None of the newly allocated slots have process id's yet. */
   for (i = old; i < pid_list_size; i++)
-    pid_list[i].pid = NO_PID;
+    {
+      pid_list[i].pid = NO_PID;
+      pid_list[i].status = pid_list[i].flags = 0;
+    }
 }
 
 /* Return the offset within the PID_LIST array of an empty slot.  This can
    create new slots if all of the existing slots are taken. */
 static int
-find_proc_slot ()
+find_proc_slot (pid)
+     pid_t pid;
 {
   register int i;
 
   for (i = 0; i < pid_list_size; i++)
-    if (pid_list[i].pid == NO_PID)
+    if (pid_list[i].pid == NO_PID || pid_list[i].pid == pid)
       return (i);
 
   if (i == pid_list_size)
@@ -261,6 +269,12 @@ set_pid_status (pid, status)
   coproc_pidchk (pid, status);
 #endif
 
+#if defined (PROCESS_SUBSTITUTION)
+  if ((slot = find_procsub_child (pid)) >= 0)
+    set_procsub_status (slot, pid, WSTATUS (status));
+    /* XXX - also saving in list below */
+#endif
+
   slot = find_index_by_pid (pid);
   if (slot == NO_PID)
     return;
@@ -326,7 +340,7 @@ add_pid (pid, async)
 {
   int slot;
 
-  slot = find_proc_slot ();
+  slot = find_proc_slot (pid);
 
   pid_list[slot].pid = pid;
   pid_list[slot].status = -1;
@@ -387,8 +401,9 @@ cleanup_dead_jobs ()
 
   for (i = 0; i < pid_list_size; i++)
     {
-      if ((pid_list[i].flags & PROC_RUNNING) == 0 &&
-         (pid_list[i].flags & PROC_NOTIFIED))
+      if (pid_list[i].pid != NO_PID &&
+           (pid_list[i].flags & PROC_RUNNING) == 0 &&
+           (pid_list[i].flags & PROC_NOTIFIED))
        pid_list[i].pid = NO_PID;
     }
 
@@ -407,6 +422,7 @@ reap_dead_jobs ()
 }
 
 /* Initialize the job control mechanism, and set up the tty stuff. */
+int
 initialize_job_control (force)
      int force;
 {
@@ -414,6 +430,7 @@ initialize_job_control (force)
 
   if (interactive)
     get_tty_state ();
+  return 0;
 }
 
 /* Setup this shell to handle C-C, etc. */
@@ -439,14 +456,21 @@ reap_zombie_children ()
   WAIT status;
 
   CHECK_TERMSIG;
+  CHECK_WAIT_INTR;
   while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)
     set_pid_status (pid, status);
 #  endif /* WNOHANG */
   CHECK_TERMSIG;
+  CHECK_WAIT_INTR;
 }
 #endif /* WAITPID */
 
 #if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)
+
+#if !defined (SA_RESTART)
+#  define SA_RESTART 0
+#endif
+
 static int
 siginterrupt (sig, flag)
      int sig, flag;
@@ -491,11 +515,17 @@ make_child (command, async_p)
     sync_buffered_stream (default_buffered_input);
 #endif /* BUFFERED_INPUT */
 
+  /* XXX - block SIGTERM here and unblock in child after fork resets the
+     set of pending signals? */
+  RESET_SIGTERM;
+
   /* Create the child, handle severe errors.  Retry on EAGAIN. */
   forksleep = 1;
   while ((pid = fork ()) < 0 && errno == EAGAIN && forksleep < FORKSLEEP_MAX)
     {
       sys_error ("fork: retry");
+      RESET_SIGTERM;
+
 #if defined (HAVE_WAITPID)
       /* Posix systems with a non-blocking waitpid () system call available
         get another chance after zombies are reaped. */
@@ -509,9 +539,13 @@ make_child (command, async_p)
       forksleep <<= 1;
     }
 
+  if (pid != 0)
+    RESET_SIGTERM;
+
   if (pid < 0)
     {
       sys_error ("fork");
+      last_command_exit_value = EX_NOEXEC;
       throw_to_top_level ();
     }
 
@@ -521,6 +555,8 @@ make_child (command, async_p)
       unset_bash_input (0);
 #endif /* BUFFERED_INPUT */
 
+      CLRINTERRUPT;    /* XXX - children have their own interrupt state */
+
 #if defined (HAVE_POSIX_SIGNALS)
       /* Restore top-level signal mask. */
       sigprocmask (SIG_SETMASK, &top_level_mask, (sigset_t *)NULL);
@@ -562,21 +598,57 @@ void
 default_tty_job_signals ()
 {
 #if defined (SIGTSTP)
-  set_signal_handler (SIGTSTP, SIG_DFL);
-  set_signal_handler (SIGTTIN, SIG_DFL);
-  set_signal_handler (SIGTTOU, SIG_DFL);
+  if (signal_is_trapped (SIGTSTP) == 0 && signal_is_hard_ignored (SIGTSTP))
+    set_signal_handler (SIGTSTP, SIG_IGN);
+  else
+    set_signal_handler (SIGTSTP, SIG_DFL);
+  if (signal_is_trapped (SIGTTIN) == 0 && signal_is_hard_ignored (SIGTTIN))
+    set_signal_handler (SIGTTIN, SIG_IGN);
+  else
+    set_signal_handler (SIGTTIN, SIG_DFL);
+  if (signal_is_trapped (SIGTTOU) == 0 && signal_is_hard_ignored (SIGTTOU))
+    set_signal_handler (SIGTTOU, SIG_IGN);
+  else
+    set_signal_handler (SIGTTOU, SIG_DFL);
+#endif
+}
+
+/* Called once in a parent process. */
+void
+get_original_tty_job_signals ()
+{
+  static int fetched = 0;
+
+  if (fetched == 0)
+    {
+#if defined (SIGTSTP)
+      if (interactive_shell)
+       {
+         set_original_signal (SIGTSTP, SIG_DFL);
+         set_original_signal (SIGTTIN, SIG_DFL);
+         set_original_signal (SIGTTOU, SIG_DFL);
+       }
+      else
+       {
+         get_original_signal (SIGTSTP);
+         get_original_signal (SIGTTIN);
+         get_original_signal (SIGTTOU);
+       }
 #endif
+      fetched = 1;
+    }
 }
 
 /* Wait for a single pid (PID) and return its exit status.  Called by
    the wait builtin. */
 int
-wait_for_single_pid (pid)
+wait_for_single_pid (pid, flags)
      pid_t pid;
+     int flags;
 {
   pid_t got_pid;
   WAIT status;
-  int pstatus, flags;
+  int pstatus;
 
   pstatus = find_status_by_pid (pid);
 
@@ -597,6 +669,7 @@ wait_for_single_pid (pid)
   while ((got_pid = WAITPID (pid, &status, 0)) != pid)
     {
       CHECK_TERMSIG;
+      CHECK_WAIT_INTR;
       if (got_pid < 0)
        {
          if (errno != EINTR && errno != ECHILD)
@@ -654,6 +727,11 @@ wait_for_background_pids ()
   cleanup_dead_jobs ();
 }
 
+void
+wait_sigint_cleanup ()
+{
+}
+
 /* Make OLD_SIGINT_HANDLER the SIGINT signal handler. */
 #define INVALID_SIGNAL_HANDLER (SigHandler *)wait_for_background_pids
 static SigHandler *old_sigint_handler = INVALID_SIGNAL_HANDLER;
@@ -683,12 +761,12 @@ wait_sigint_handler (sig)
       signal_is_trapped (SIGINT) &&
       ((sigint_handler = trap_to_sighandler (SIGINT)) == trap_handler))
     {
-      last_command_exit_value = EXECUTION_FAILURE;
+      last_command_exit_value = 128+SIGINT;
       restore_sigint_handler ();
       interrupt_immediately = 0;
       trap_handler (SIGINT);   /* set pending_traps[SIGINT] */
       wait_signal_received = SIGINT;
-      longjmp (wait_intr_buf, 1);
+      SIGRETURN (0);
     }
 
   if (interrupt_immediately)
@@ -751,6 +829,7 @@ wait_for (pid)
   while ((got_pid = WAITPID (-1, &status, 0)) != pid) /* XXX was pid now -1 */
     {
       CHECK_TERMSIG;
+      CHECK_WAIT_INTR;
       if (got_pid < 0 && errno == ECHILD)
        {
 #if !defined (_POSIX_VERSION)
@@ -774,6 +853,9 @@ wait_for (pid)
     reap_zombie_children ();
 #endif /* HAVE_WAITPID */
 
+  CHECK_TERMSIG;
+  CHECK_WAIT_INTR;
+
   if (interactive_shell == 0)
     {
       SigHandler *temp_handler;
@@ -801,13 +883,17 @@ wait_for (pid)
   return_val = process_exit_status (status);
   last_command_exit_signal = get_termsig (status);
 
-#if !defined (DONT_REPORT_SIGPIPE)
-  if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
-       (WTERMSIG (status) != SIGINT))
+#if defined (DONT_REPORT_SIGPIPE) && defined (DONT_REPORT_SIGTERM)
+#  define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE && (x) != SIGTERM)
+#elif !defined (DONT_REPORT_SIGPIPE) && !defined (DONT_REPORT_SIGTERM)
+#  define REPORTSIG(x) ((x) != SIGINT)
+#elif defined (DONT_REPORT_SIGPIPE)
+#  define REPORTSIG(x) ((x) != SIGINT && (x) != SIGPIPE)
 #else
-  if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) &&
-       (WTERMSIG (status) != SIGINT) && (WTERMSIG (status) != SIGPIPE))
+#  define REPORTSIG(x) ((x) != SIGINT && (x) != SIGTERM)
 #endif
+
+  if ((WIFSTOPPED (status) == 0) && WIFSIGNALED (status) && REPORTSIG(WTERMSIG (status)))
     {
       fprintf (stderr, "%s", j_strsignal (WTERMSIG (status)));
       if (WIFCORED (status))
@@ -822,6 +908,8 @@ wait_for (pid)
       else
        get_tty_state ();
     }
+  else if (interactive_shell == 0 && subshell_environment == 0 && check_window_size)
+    get_new_window_size (0, (int *)0, (int *)0);
 
   return (return_val);
 }
@@ -848,6 +936,7 @@ static TTYSTRUCT shell_tty_info;
 static int got_tty_state;
 
 /* Fill the contents of shell_tty_info with the current tty info. */
+int
 get_tty_state ()
 {
   int tty;
@@ -860,6 +949,7 @@ get_tty_state ()
       if (check_window_size)
        get_new_window_size (0, (int *)0, (int *)0);
     }
+  return 0;
 }
 
 /* Make the current tty use the state in shell_tty_info. */
@@ -879,10 +969,12 @@ set_tty_state ()
 }
 
 /* Give the terminal to PGRP.  */
+int
 give_terminal_to (pgrp, force)
      pid_t pgrp;
      int force;
 {
+  return 0;
 }
 
 /* Stop a pipeline. */
@@ -907,6 +999,14 @@ stop_making_children ()
   already_making_children = 0;
 }
 
+/* The name is kind of a misnomer, but it's what the job control code uses. */
+void
+without_job_control ()
+{
+  stop_making_children ();
+  last_made_pid = NO_PID;      /* XXX */
+}
+
 int
 get_job_by_pid (pid, block)
      pid_t pid;
@@ -926,11 +1026,23 @@ describe_pid (pid)
   fprintf (stderr, "%ld\n", (long) pid);
 }
 
+int
+freeze_jobs_list ()
+{
+  return 0;
+}
+
 void
 unfreeze_jobs_list ()
 {
 }
 
+void
+set_jobs_list_frozen (s)
+     int s;
+{
+}
+
 int
 count_all_jobs ()
 {