summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <p.stephenson@samsung.com>2022-03-30 09:28:43 +0100
committerPeter Stephenson <p.stephenson@samsung.com>2022-03-30 09:28:43 +0100
commit98e46340867028808e71e7f3373881cb7e5b6764 (patch)
treece804301a1890959e620dabf3e87da5a63503f80
parentf11227f78d8d477048194df5b93dfe315d4b1588 (diff)
downloadzsh-98e46340867028808e71e7f3373881cb7e5b6764.tar.gz
zsh-98e46340867028808e71e7f3373881cb7e5b6764.tar.xz
zsh-98e46340867028808e71e7f3373881cb7e5b6764.zip
49906 (Bart), 49911: Fixes to querying jobs in subshell.
Don't attempt to query invalid job off end of table, resulting in
crashes from $jobtstates.

If background task started in subshell, look at tatsks within subshell
instead of main shell.  Document and add test.
-rw-r--r--ChangeLog8
-rw-r--r--Doc/Zsh/builtins.yo18
-rw-r--r--Doc/Zsh/mod_parameter.yo6
-rw-r--r--Src/exec.c1
-rw-r--r--Src/jobs.c20
-rw-r--r--Test/W03jobparameters.ztst28
6 files changed, 81 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c52119fa8..e6f73e30e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-03-30  Peter Stephenson  <p.stephenson@samsung.com>
+
+	* 49906 (Bart), 49911: Doc/Zsh/builtins.yo,
+	Doc/Zsh/mod_parameter.yo, Src/exec.c, Src/jobs.c,
+	Test/W03jobparameters.ztst: Fix querying jobs in subshell.
+	Don't uery invalid job at end, if background job started in
+	subshell query jobs in subshell instead of main shell.
+
 2022-03-30  Mikael Magnusson  <mikachu@gmail.com>
 
 	* 49893: Src/Zle/comp.h, Src/Zle/compcore.c: Fix comments for
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 5649e00d4..1d74f0c17 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -1114,6 +1114,24 @@ The tt(-Z) option replaces the shell's argument and environment space with
 the given string, truncated if necessary to fit.  This will normally be
 visible in tt(ps) (manref(ps)(1)) listings.  This feature is typically
 used by daemons, to indicate their state.
+
+Full job control is only available in the top-level interactive shell,
+not in commands run in the left hand side of pipelines or within
+the tt(LPAR())var(...)tt(RPAR()) construct.  However, a snapshot
+of the job state at that point is taken, so it is still possible
+to use the tt(jobs) builtin, or any parameter providing job information.
+This gives information about the state of jobs at the point the subshell
+was created.  If background processes are created within the subshell,
+then instead information about those processes is provided.
+
+For example,
+
+example(sleep 10 &    # Job in background
+LPAR()             # Shell forks
+jobs          # Shows information about "sleep 10 &"
+sleep 5 &     # Process in background (no job control)
+jobs          # Shows information about "sleep 5 &"
+RPAR())
 )
 findex(kill)
 cindex(killing jobs)
diff --git a/Doc/Zsh/mod_parameter.yo b/Doc/Zsh/mod_parameter.yo
index 2e3011e44..f3bcd7957 100644
--- a/Doc/Zsh/mod_parameter.yo
+++ b/Doc/Zsh/mod_parameter.yo
@@ -165,6 +165,8 @@ The keys of the associative arrays are usually valid job numbers,
 and these are the values output with, for example, tt(${(k)jobdirs}).
 Non-numeric job references may be used when looking up a value;
 for example, tt(${jobdirs[%+]}) refers to the current job.
+
+See the tt(jobs) builtin for how job information is provided in a subshell.
 )
 vindex(jobtexts)
 item(tt(jobtexts))(
@@ -173,6 +175,8 @@ that were used to start the jobs.
 
 Handling of the keys of the associative array is as described for
 tt(jobdirs) above.
+
+See the tt(jobs) builtin for how job information is provided in a subshell.
 )
 vindex(jobstates)
 item(tt(jobstates))(
@@ -189,6 +193,8 @@ the var(state) describes the state of that process.
 
 Handling of the keys of the associative array is as described for
 tt(jobdirs) above.
+
+See the tt(jobs) builtin for how job information is provided in a subshell.
 )
 vindex(nameddirs)
 item(tt(nameddirs))(
diff --git a/Src/exec.c b/Src/exec.c
index 70cbfc97f..27d49e005 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1689,6 +1689,7 @@ execpline(Estate state, wordcode slcode, int how, int last1)
     execpline2(state, code, how, opipe[0], ipipe[1], last1);
     pline_level--;
     if (how & Z_ASYNC) {
+	clearoldjobtab();
 	lastwj = newjob;
 
         if (thisjob == list_pipe_job)
diff --git a/Src/jobs.c b/Src/jobs.c
index 18e43f03c..af0a1233d 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -1718,8 +1718,15 @@ clearjobtab(int monitor)
 	/* Don't report any job we're part of */
 	if (thisjob != -1 && thisjob < oldmaxjob)
 	    memset(oldjobtab+thisjob, 0, sizeof(struct job));
+
+	/* oldmaxjob is now the size of the table, but outside
+	 * this function, it's used as a job number, which must
+	 * be the largest index available in the table.
+	 */
+	--oldmaxjob;
     }
 
+
     memset(jobtab, 0, jobtabsize * sizeof(struct job)); /* zero out table */
     maxjob = 0;
 
@@ -1733,6 +1740,18 @@ clearjobtab(int monitor)
     thisjob = initjob();
 }
 
+/* In a subshell, decide we want our own job table after all. */
+
+/**/
+mod_export void
+clearoldjobtab(void)
+{
+    if (oldjobtab)
+	free(oldjobtab);
+    oldjobtab = NULL;
+    oldmaxjob = 0;
+}
+
 static int initnewjob(int i)
 {
     jobtab[i].stat = STAT_INUSE;
@@ -2449,6 +2468,7 @@ bin_fg(char *name, char **argv, Options ops, int func)
 	case BIN_BG:
 	case BIN_WAIT:
 	    if (func == BIN_BG) {
+		clearoldjobtab();
 		jobtab[job].stat |= STAT_NOSTTY;
 		jobtab[job].stat &= ~STAT_CURSH;
 	    }
diff --git a/Test/W03jobparameters.ztst b/Test/W03jobparameters.ztst
index af889c6d5..a6f7a09b1 100644
--- a/Test/W03jobparameters.ztst
+++ b/Test/W03jobparameters.ztst
@@ -48,3 +48,31 @@
 *>running:+:*=running
 *>running:+:*=running
 *>zsh:*SIGHUPed*
+
+# $jobstates refers to a job started in the main shell unless
+# one has been started in the subshell.  In the latter case,
+# the subshell has no job control so the job is not marked as current.
+  zpty_start
+  zpty_input "MODULE_PATH=${(q)MODULE_PATH}"
+  zpty_input 'sleep 3 &'
+  zpty_input '(print main; print $jobstates; sleep 2& print sub; print $jobstates)'
+  zpty_input 'jobs -s'
+  zpty_stop
+0:$jobstate shows one job started in main shell or one started in subshell
+*>\[1] [0-9]##
+>main
+*>running:+:*=running
+>sub
+*>running::*=running
+*>zsh:*SIGHUPed*
+
+# output from zpty removes empty lines
+  zpty_start
+  zpty_input "MODULE_PATH=${(q)MODULE_PATH}"
+  zpty_input '(print main; print $jobstates; sleep 2& print sub; print $jobstates)'
+  zpty_input 'jobs -s'
+  zpty_stop
+0:$jobstate shows no job started in main shell but one started in subshell
+>main
+>sub
+*>running::*=running