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