Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: zargs with -P intermittently failing in zsh 5.9 and macOS



> 2022/05/29 12:30, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 
> OK. However, reading through your patch again ... the changed code
> kept track of stopped jobs. Has that never been necessary? Perhaps
> the right thing to do is to continue recording the state on WIFSTOPPED
> but to change to clearing the saved status on WIFCONTINUED?


If I understand correctly, {add,get}bgstatus() exists only for the wait
builtin. The only thing that needs be recored in bgstatus_list is the
exist status (including killed by a signal) of the process. Status of a
running/stopped process is kept elsewhere (jobtab[].procs).

Below is the patch including both B (signals.c) and C (jobs.c).
singnals.s is as the previous patch, but the ternary operator is
replaced by if/else.


diff --git a/Src/jobs.c b/Src/jobs.c
index a91ef787f..a8d6aeded 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -2221,6 +2221,7 @@ addbgstatus(pid_t pid, int status)
 {
     static long child_max;
     Bgstatus bgstatus_entry;
+    LinkNode node;
 
     if (!child_max) {
 #ifdef _SC_CHILD_MAX
@@ -2244,6 +2245,18 @@ addbgstatus(pid_t pid, int status)
 	if (!bgstatus_list)
 	    return;
     }
+    /* See if an entry already exists for the pid */
+    for (node = firstnode(bgstatus_list); node; incnode(node)) {
+	bgstatus_entry = (Bgstatus)getdata(node);
+	if (bgstatus_entry->pid == pid) {
+	    /* In theory this should not happen because addbgstatus() is
+	     * called only once when the process exits or gets killed. */
+	    DPUTS(1, "addbgstatus is called more than once");
+	    bgstatus_entry->status = status;
+	    return;
+	}
+    }
+    /* Add an entry for the pid */
     if (bgstatus_count == child_max) {
 	/* Overflow.  List is in order, remove first */
 	rembgstatus(firstnode(bgstatus_list));
diff --git a/Src/signals.c b/Src/signals.c
index 5c787e2a8..a61368554 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -576,12 +576,10 @@ wait_for_processes(void)
 	 */
 	if (jn && !(jn->stat & (STAT_CURSH|STAT_BUILTIN)) &&
 	    jn - jobtab != thisjob) {
-	    int val = (WIFSIGNALED(status) ?
-		   0200 | WTERMSIG(status) :
-		   (WIFSTOPPED(status) ?
-		    0200 | WEXITSTATUS(status) :
-		    WEXITSTATUS(status)));
-	    addbgstatus(pid, val);
+	    if (WIFEXITED(status))
+		addbgstatus(pid, WEXITSTATUS(status));
+	    else if (WIFSIGNALED(status))
+		addbgstatus(pid, 0200 | WTERMSIG(status));
 	}
 
 	unqueue_signals();






Messages sorted by: Reverse Date, Date, Thread, Author