]> 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 0c9bd751bb80752dfff194812fb1f432fab61bc4..2201a29ac9f927d2b4535d0508fb54d75acf09a6 100644 (file)
--- a/nojobs.c
+++ b/nojobs.c
 #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;
+
+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;
 
@@ -99,6 +98,8 @@ 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 */
 struct proc_status {
@@ -124,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));
@@ -162,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)
@@ -264,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;
@@ -329,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;
@@ -390,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;
     }
 
@@ -410,6 +422,7 @@ reap_dead_jobs ()
 }
 
 /* Initialize the job control mechanism, and set up the tty stuff. */
+int
 initialize_job_control (force)
      int force;
 {
@@ -417,6 +430,7 @@ initialize_job_control (force)
 
   if (interactive)
     get_tty_state ();
+  return 0;
 }
 
 /* Setup this shell to handle C-C, etc. */
@@ -541,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);
@@ -582,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);
 
@@ -675,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;
@@ -879,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;
@@ -891,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. */
@@ -910,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. */
@@ -938,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;
@@ -957,9 +1026,10 @@ describe_pid (pid)
   fprintf (stderr, "%ld\n", (long) pid);
 }
 
-void
+int
 freeze_jobs_list ()
 {
+  return 0;
 }
 
 void
@@ -967,6 +1037,12 @@ unfreeze_jobs_list ()
 {
 }
 
+void
+set_jobs_list_frozen (s)
+     int s;
+{
+}
+
 int
 count_all_jobs ()
 {