Skip to content

Commit

Permalink
Fix BUG_KBGPID: $! was not updated under certain conditions
Browse files Browse the repository at this point in the history
The $! special parameter was not set if a background job
(somecommand &) or co-process (somecommand |&) was launched as the
only command within a braces block with an attached redirection,
for example:
	{
		somecommand &
	} >&2
With the bug, $! was unchanged; now it contains the PID of
somecommand.

Ref.: att#1357

src/cmd/ksh93/sh/parse.c: item():
- When processing redirections following a compound command, always
  create a parent node with the TSETIO (I/O redirection) token.
     Before this commit, if the last command was of type TFORK (and
  the last command only tested as TFORK if the bg job or coprocess
  was the only command in a braces block, because the ksh parser
  optimises away the braces in that case), then the parent node was
  created with the TFORK token instead.
     I have no idea what David Korn's intention was with that, but
  this is clearly very wrong. Creating another TFORK node when
  parsing the redirection caused sh_exec() in sh/xec.c to execute
  the redirection in an extra forked, non-background subshell.
  Since redirections are executed before anything else, this
  subshell is what then launched the background job between the
  braces, so $! (a.k.a. shp->bckpid) was updated in that subshell
  only, and never in the main shell. The extra subshell also
  prevented the background job from being noticed by job control
  on interactive shells.
     So, the fix is simply to remove the broken test for TFORK.

src/cmd/ksh93/tests/variables.sh:
- Add regression tests for a bg job and a co-process as the only
  command within a braces block with attached redirection.

(cherry picked from commit ffe5df30e69f7b596941a98498014d8e838861f2)
  • Loading branch information
McDutchie committed Jun 11, 2020
1 parent a935132 commit 1026006
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
10 changes: 10 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ For full details, see the git log at:

Any uppercase BUG_* names are modernish shell bug IDs.

2020-06-04:

- Fix BUG_KBGPID: the $! special parameter was not set if a background job
(somecommand &) or co-process (somecommand |&) was launched as the only
command within a braces block with an attached redirection, for example:
{
somecommand &
} >&2
With the bug, $! was unchanged; now it contains the PID of somecommand.

2020-05-31:

- Fix a bug in autoloading functions. Directories in the path search list
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-05-31"
#define SH_RELEASE "93u+m 2020-06-04"
5 changes: 2 additions & 3 deletions src/cmd/ksh93/sh/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1360,11 +1360,10 @@ static Shnode_t *item(Lex_t *lexp,int flag)
return(t);
}
sh_lex(lexp);
/* redirection(s) following a compound command */
if(io=inout(lexp,io,0))
{
if((tok=t->tre.tretyp&COMMSK) != TFORK)
tok = TSETIO;
t=makeparent(lexp,tok,t);
t=makeparent(lexp,TSETIO,t);
t->tre.treio=io;
}
done:
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,15 @@ set -- $x
: & pid=$!
( : & )
[[ $pid == $! ]] || err_exit '$! value not preserved across subshells'
pid=$!
{ : & } >&2
[[ $pid == $! ]] && err_exit '$! value not updated after bg job in braces+redir'
pid=$!
{ : |& } >&2
[[ $pid == $! ]] && err_exit '$! value not updated after co-process in braces+redir'
unset foo
typeset -A foo
function foo.set
Expand Down

0 comments on commit 1026006

Please sign in to comment.