704a93
From 223ac53797d33b0473323efc0d5a44d1dceaf746 Mon Sep 17 00:00:00 2001
704a93
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
704a93
Date: Sun, 26 Oct 2014 17:47:42 +0000
704a93
Subject: [PATCH 1/2] 33531 with additions: retain status of exited background
704a93
 jobs.
704a93
704a93
Add linked list of unwaited-for background jobs.
704a93
Truncate at value of _SC_CHILD_MAX discarding oldest.
704a93
Remove old lastpid_status mechanism for latest exited process only.
704a93
Slightly tighten safety of permanently allocated linked lists so
704a93
that this doesn't compromise signal handling.
704a93
704a93
Upstream-commit: b4f7ccecd93ca9e64c3c3c774fdaefae83d7204a
704a93
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
704a93
---
704a93
 Doc/Zsh/builtins.yo |  16 ++++++
704a93
 Doc/Zsh/options.yo  |   8 +--
704a93
 Doc/zshoptions.1    |   8 +--
704a93
 Src/exec.c          |   2 -
704a93
 Src/init.c          |   1 -
704a93
 Src/jobs.c          | 138 ++++++++++++++++++++++++++++++++++++++++++++--------
704a93
 Src/linklist.c      |   4 ++
704a93
 Src/signals.c       |  14 +++---
704a93
 8 files changed, 152 insertions(+), 39 deletions(-)
704a93
704a93
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
704a93
index 46f40cc..edc335e 100644
704a93
--- a/Doc/Zsh/builtins.yo
704a93
+++ b/Doc/Zsh/builtins.yo
704a93
@@ -1905,6 +1905,22 @@ then all currently active child processes are waited for.
704a93
 Each var(job) can be either a job specification or the process ID
704a93
 of a job in the job table.
704a93
 The exit status from this command is that of the job waited for.
704a93
+
704a93
+It is possible to wait for recent processes (specified by process ID,
704a93
+not by job) that were running in the background even if the process has
704a93
+exited.  Typically the process ID will be recorded by capturing the
704a93
+value of the variable tt($!) immediately after the process has been
704a93
+started.  There is a limit on the number of process IDs remembered by
704a93
+the shell; this is given by the value of the system configuration
704a93
+parameter tt(CHILD_MAX).  When this limit is reached, older process IDs
704a93
+are discarded, least recently started processes first.
704a93
+
704a93
+Note there is no protection against the process ID wrapping, i.e. if the
704a93
+wait is not executed soon enough there is a chance the process waited
704a93
+for is the wrong one.  A conflict implies both process IDs have been
704a93
+generated by the shell, as other processes are not recorded, and that
704a93
+the user is potentially interested in both, so this problem is intrinsic
704a93
+to process IDs.
704a93
 )
704a93
 findex(whence)
704a93
 item(tt(whence) [ tt(-vcwfpams) ] var(name) ...)(
704a93
diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
704a93
index 068a253..452b258 100644
704a93
--- a/Doc/Zsh/options.yo
704a93
+++ b/Doc/Zsh/options.yo
704a93
@@ -1401,10 +1401,10 @@ shell is saved for output within a subshell (for example, within a
704a93
 pipeline).  When the option is set, the output of tt(jobs) is empty
704a93
 until a job is started within the subshell.
704a93
 
704a93
-When the option is set, it becomes possible to use the tt(wait) builtin to
704a93
-wait for the last job started in the background (as given by tt($!)) even
704a93
-if that job has already exited.  This works even if the option is turned
704a93
-on temporarily around the use of the tt(wait) builtin.
704a93
+In previous versions of the shell, it was necessary to enable
704a93
+tt(POSIX_JOBS) in order for the builtin command tt(wait) to return the
704a93
+status of background jobs that had already exited.  This is no longer
704a93
+the case.
704a93
 )
704a93
 enditem()
704a93
 
704a93
diff --git a/Doc/zshoptions.1 b/Doc/zshoptions.1
704a93
index dc8f2f8..17f9e97 100644
704a93
--- a/Doc/zshoptions.1
704a93
+++ b/Doc/zshoptions.1
704a93
@@ -867,10 +867,10 @@ shell is saved for output within a subshell (for example, within a
704a93
 pipeline)\&.  When the option is set, the output of \fBjobs\fP is empty
704a93
 until a job is started within the subshell\&.
704a93
 .PP
704a93
-When the option is set, it becomes possible to use the \fBwait\fP builtin to
704a93
-wait for the last job started in the background (as given by \fB$!\fP) even
704a93
-if that job has already exited\&.  This works even if the option is turned
704a93
-on temporarily around the use of the \fBwait\fP builtin\&.
704a93
+In previous versions of the shell, it was necessary to enable
704a93
+\fBPOSIX_JOBS\fP in order for the builtin command \fBwait\fP to return the
704a93
+status of background jobs that had already exited\&.  This is no longer
704a93
+the case\&.
704a93
 .RE
704a93
 .RE
704a93
 .PP
704a93
diff --git a/Src/exec.c b/Src/exec.c
704a93
index d0fadd6..a9c4688 100644
704a93
--- a/Src/exec.c
704a93
+++ b/Src/exec.c
704a93
@@ -2852,8 +2852,6 @@ execcmd(Estate state, int input, int output, int how, int last1)
704a93
 #endif
704a93
 	    if (how & Z_ASYNC) {
704a93
 		lastpid = (zlong) pid;
704a93
-		/* indicate it's possible to set status for lastpid */
704a93
-		lastpid_status = -2L;
704a93
 	    } else if (!jobtab[thisjob].stty_in_env && varspc) {
704a93
 		/* search for STTY=... */
704a93
 		Wordcode p = varspc;
704a93
diff --git a/Src/init.c b/Src/init.c
704a93
index c26d887..6666f98 100644
704a93
--- a/Src/init.c
704a93
+++ b/Src/init.c
704a93
@@ -1018,7 +1018,6 @@ setupvals(void)
704a93
     bufstack = znewlinklist();
704a93
     hsubl = hsubr = NULL;
704a93
     lastpid = 0;
704a93
-    lastpid_status = -1L;
704a93
 
704a93
     get_usage();
704a93
 
704a93
diff --git a/Src/jobs.c b/Src/jobs.c
704a93
index bd95afb..18bb648 100644
704a93
--- a/Src/jobs.c
704a93
+++ b/Src/jobs.c
704a93
@@ -104,15 +104,6 @@ int prev_errflag, prev_breaks, errbrk_saved;
704a93
 /**/
704a93
 int numpipestats, pipestats[MAX_PIPESTATS];
704a93
 
704a93
-/*
704a93
- * The status associated with the process lastpid.
704a93
- * -1 if not set and no associated lastpid
704a93
- * -2 if lastpid is set and status isn't yet
704a93
- * else the value returned by wait().
704a93
- */
704a93
-/**/
704a93
-long lastpid_status;
704a93
-
704a93
 /* Diff two timevals for elapsed-time computations */
704a93
 
704a93
 /**/
704a93
@@ -1210,14 +1201,6 @@ addproc(pid_t pid, char *text, int aux, struct timeval *bgtime)
704a93
 {
704a93
     Process pn, *pnlist;
704a93
 
704a93
-    if (pid == lastpid && lastpid_status != -2L) {
704a93
-	/*
704a93
-	 * The status for the previous lastpid is invalid.
704a93
-	 * Presumably process numbers have wrapped.
704a93
-	 */
704a93
-	lastpid_status = -1L;
704a93
-    }
704a93
-
704a93
     DPUTS(thisjob == -1, "No valid job in addproc.");
704a93
     pn = (Process) zshcalloc(sizeof *pn);
704a93
     pn->pid = pid;
704a93
@@ -1826,6 +1809,122 @@ maybeshrinkjobtab(void)
704a93
     unqueue_signals();
704a93
 }
704a93
 
704a93
+/*
704a93
+ * Definitions for the background process stuff recorded below.
704a93
+ * This would be more efficient as a hash, but
704a93
+ * - that's quite heavyweight for something not needed very often
704a93
+ * - we need some kind of ordering as POSIX allows us to limit
704a93
+ *   the size of the list to the value of _SC_CHILD_MAX and clearly
704a93
+ *   we want to clear the oldest first
704a93
+ * - cases with a long list of background jobs where the user doesn't
704a93
+ *   wait for a large number, and then does wait for one (the only
704a93
+ *   inefficient case) are rare
704a93
+ * - in the context of waiting for an external process, looping
704a93
+ *   over a list isn't so very inefficient.
704a93
+ * Enough excuses already.
704a93
+ */
704a93
+
704a93
+/* Data in the link list, a key (process ID) / value (exit status) pair. */
704a93
+struct bgstatus {
704a93
+    pid_t pid;
704a93
+    int status;
704a93
+};
704a93
+typedef struct bgstatus *Bgstatus;
704a93
+/* The list of those entries */
704a93
+LinkList bgstatus_list;
704a93
+/* Count of entries.  Reaches value of _SC_CHILD_MAX and stops. */
704a93
+long bgstatus_count;
704a93
+
704a93
+/*
704a93
+ * Remove and free a bgstatus entry.
704a93
+ */
704a93
+static void rembgstatus(LinkNode node)
704a93
+{
704a93
+    zfree(remnode(bgstatus_list, node), sizeof(struct bgstatus));
704a93
+    bgstatus_count--;
704a93
+}
704a93
+
704a93
+/*
704a93
+ * Record the status of a background process that exited so we
704a93
+ * can execute the builtin wait for it.
704a93
+ *
704a93
+ * We can't execute the wait builtin for something that exited in the
704a93
+ * foreground as it's not visible to the user, so don't bother recording.
704a93
+ */
704a93
+
704a93
+/**/
704a93
+void
704a93
+addbgstatus(pid_t pid, int status)
704a93
+{
704a93
+    static long child_max;
704a93
+    Bgstatus bgstatus_entry;
704a93
+
704a93
+    if (!child_max) {
704a93
+#ifdef _SC_CHILD_MAX
704a93
+	child_max = sysconf(_SC_CHILD_MAX);
704a93
+	if (!child_max) /* paranoia */
704a93
+#endif
704a93
+	{
704a93
+	    /* Be inventive */
704a93
+	    child_max = 1024L;
704a93
+	}
704a93
+    }
704a93
+
704a93
+    if (!bgstatus_list) {
704a93
+	bgstatus_list = znewlinklist();
704a93
+	/*
704a93
+	 * We're not always robust about memory failures, but
704a93
+	 * this is pretty deep in the shell basics to be failing owing
704a93
+	 * to memory, and a failure to wait is reported loudly, so test
704a93
+	 * and fail silently here.
704a93
+	 */
704a93
+	if (!bgstatus_list)
704a93
+	    return;
704a93
+    }
704a93
+    if (bgstatus_count == child_max) {
704a93
+	/* Overflow.  List is in order, remove first */
704a93
+	rembgstatus(firstnode(bgstatus_list));
704a93
+    }
704a93
+    bgstatus_entry = (Bgstatus)zalloc(sizeof(*bgstatus_entry));
704a93
+    if (!bgstatus_entry) {
704a93
+	/* See note above */
704a93
+	return;
704a93
+    }
704a93
+    bgstatus_entry->pid = pid;
704a93
+    bgstatus_entry->status = status;
704a93
+    if (!zaddlinknode(bgstatus_list, bgstatus_entry)) {
704a93
+	zfree(bgstatus_entry, sizeof(*bgstatus_entry));
704a93
+	return;
704a93
+    }
704a93
+    bgstatus_count++;
704a93
+}
704a93
+
704a93
+/*
704a93
+ * See if pid has a recorded exit status.
704a93
+ * Note we make no guarantee that the PIDs haven't wrapped, so this
704a93
+ * may not be the right process.
704a93
+ *
704a93
+ * This is only used by wait, which must only work on each
704a93
+ * pid once, so we need to remove the entry if we find it.
704a93
+ */
704a93
+
704a93
+static int getbgstatus(pid_t pid)
704a93
+{
704a93
+    LinkNode node;
704a93
+    Bgstatus bgstatus_entry;
704a93
+
704a93
+    if (!bgstatus_list)
704a93
+	return -1;
704a93
+    for (node = firstnode(bgstatus_list); node; incnode(node)) {
704a93
+	bgstatus_entry = (Bgstatus)getdata(node);
704a93
+	if (bgstatus_entry->pid == pid) {
704a93
+	    int status = bgstatus_entry->status;
704a93
+	    rembgstatus(node);
704a93
+	    return status;
704a93
+	}
704a93
+    }
704a93
+    return -1;
704a93
+}
704a93
 
704a93
 /* bg, disown, fg, jobs, wait: most of the job control commands are     *
704a93
  * here.  They all take the same type of argument.  Exception: wait can *
704a93
@@ -1971,10 +2070,7 @@ bin_fg(char *name, char **argv, Options ops, int func)
704a93
 		}
704a93
 		if (retval == 0)
704a93
 		    retval = lastval2;
704a93
-	    } else if (isset(POSIXJOBS) &&
704a93
-		       pid == lastpid && lastpid_status >= 0L) {
704a93
-		retval = (int)lastpid_status;
704a93
-	    } else {
704a93
+	    } else if ((retval = getbgstatus(pid)) < 0) {
704a93
 		zwarnnam(name, "pid %d is not a child of this shell", pid);
704a93
 		/* presumably lastval2 doesn't tell us a heck of a lot? */
704a93
 		retval = 1;
704a93
diff --git a/Src/linklist.c b/Src/linklist.c
704a93
index 1e364fb..3aa8125 100644
704a93
--- a/Src/linklist.c
704a93
+++ b/Src/linklist.c
704a93
@@ -118,6 +118,8 @@ znewlinklist(void)
704a93
     LinkList list;
704a93
 
704a93
     list = (LinkList) zalloc(sizeof *list);
704a93
+    if (!list)
704a93
+	return NULL;
704a93
     list->list.first = NULL;
704a93
     list->list.last = &list->node;
704a93
     list->list.flags = 0;
704a93
@@ -152,6 +154,8 @@ zinsertlinknode(LinkList list, LinkNode node, void *dat)
704a93
 
704a93
     tmp = node->next;
704a93
     node->next = new = (LinkNode) zalloc(sizeof *tmp);
704a93
+    if (!new)
704a93
+	return NULL;
704a93
     new->prev = node;
704a93
     new->dat = dat;
704a93
     new->next = tmp;
704a93
diff --git a/Src/signals.c b/Src/signals.c
704a93
index 2df69f9..e728505 100644
704a93
--- a/Src/signals.c
704a93
+++ b/Src/signals.c
704a93
@@ -520,14 +520,14 @@ wait_for_processes(void)
704a93
 	    get_usage();
704a93
 	}
704a93
 	/*
704a93
-	 * Remember the status associated with $!, so we can
704a93
-	 * wait for it even if it's exited.  This value is
704a93
-	 * only used if we can't find the PID in the job table,
704a93
-	 * so it doesn't matter that the value we save here isn't
704a93
-	 * useful until the process has exited.
704a93
+	 * Accumulate a list of older jobs.  We only do this for
704a93
+	 * background jobs, which is something in the job table
704a93
+	 * that's not marked as in the current shell or as shell builtin
704a93
+	 * and is not equal to the current foreground job.
704a93
 	 */
704a93
-	if (pn != NULL && pid == lastpid && lastpid_status != -1L)
704a93
-	    lastpid_status = lastval2;
704a93
+	if (jn && !(jn->stat & (STAT_CURSH|STAT_BUILTIN)) &&
704a93
+	    jn - jobtab != thisjob)
704a93
+	    addbgstatus(pid, (int)lastval2);
704a93
     }
704a93
 }
704a93
 
704a93
-- 
704a93
2.1.0
704a93
704a93
704a93
From 2d59469450ba80b69449dc2777f0fc0673e0fbd6 Mon Sep 17 00:00:00 2001
704a93
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
704a93
Date: Sun, 26 Oct 2014 19:04:47 +0000
704a93
Subject: [PATCH 2/2] 33542: test logic for waiting for already exited
704a93
 processes
704a93
704a93
Upstream-commit: 9a551ca85999ff329714fd2cca138ce2f7d3c3d9
704a93
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
704a93
---
704a93
 Test/A05execution.ztst | 29 +++++++++++++++++++++++++++--
704a93
 1 file changed, 27 insertions(+), 2 deletions(-)
704a93
704a93
diff --git a/Test/A05execution.ztst b/Test/A05execution.ztst
704a93
index ca97f4f..589815f 100644
704a93
--- a/Test/A05execution.ztst
704a93
+++ b/Test/A05execution.ztst
704a93
@@ -178,3 +178,28 @@
704a93
   kill $!
704a93
 0:Status reset by starting a backgrounded command
704a93
 >0
704a93
+
704a93
+# This tests that we record the status of processes that have already exited
704a93
+# for when we wait for them.
704a93
+#
704a93
+# Actually, we don't guarantee here that the jobs have already exited, but
704a93
+# the order of the waits means it's highly likely we do need to recall a
704a93
+# previous status, barring accidents which shouldn't happen very often.  In
704a93
+# other words, we rely on the test working repeatedly rather than just
704a93
+# once.  The monitor option is irrelevant to the logic, so we'll make
704a93
+# our job easier by turning it off.
704a93
+  unsetopt monitor
704a93
+  (exit 1) &
704a93
+  one=$!
704a93
+  (exit 2) &
704a93
+  two=$!
704a93
+  (exit 3) &
704a93
+  three=$!
704a93
+  wait $three
704a93
+  print $?
704a93
+  wait $two
704a93
+  print $?
704a93
+  wait $one
704a93
+1:The status of recently exited background jobs is recorded
704a93
+>3
704a93
+>2
704a93
-- 
704a93
2.1.0
704a93