Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/cmd/ksh93/sh/subshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,10 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub)
sfswap(iop,sfstdout);
sfset(sfstdout,SFIO_READ,0);
sh.fdstatus[1] = IOWRITE;
flags |= sh_state(SH_NOFORK);
}
else if(sp->prev)
{
sp->pipe = sp->prev->pipe;
flags &= ~sh_state(SH_NOFORK);
}
flags |= sh_state(SH_NOFORK);
if(sh.savesig < 0)
{
sh.savesig = 0;
Expand Down
32 changes: 32 additions & 0 deletions src/cmd/ksh93/tests/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,15 @@ esac
# ======
# Test exec optimization of last command in script or subshell

(
ulimit -t unlimited 2>/dev/null # fork subshell
print "${.sh.pid:-$("$SHELL" -c 'echo "$PPID"')}" # fallback for pre-93u+m ksh without ${.sh.pid}
"$SHELL" -c 'print "$$"'
) >out
pid1= pid2=
{ read pid1 && read pid2; } <out && let "pid1 == pid2" \
|| err_exit "last command in forked subshell not exec-optimized ($pid1 != $pid2)"

got=$(
ulimit -t unlimited 2>/dev/null # fork subshell
print "${.sh.pid:-$("$SHELL" -c 'echo "$PPID"')}" # fallback for pre-93u+m ksh without ${.sh.pid}
Expand All @@ -996,6 +1005,29 @@ pid1= pid2=
{ read pid1 && read pid2; } <<<$got && let "pid1 == pid2" \
|| err_exit "last command in forked comsub not exec-optimized ($pid1 != $pid2)"

# https://github.com/ksh93/ksh/issues/507
mkdir "$tmp/subshell-optimize"
cat <<'EOF1' >"$tmp/subshell-optimize/A"
cat <<'EOF2' >B
( echo B1 ) | cat
( echo B2 ) | cat
EOF2

cat <<'EOF2' >C
( echo C1 ) | cat
( echo C2 ) | cat
EOF2

(
. ./B
. ./C
) | cat
EOF1
exp=$'B1\nB2\nC1\nC2'
got=$(cd "$tmp/subshell-optimize"; "$SHELL" "$tmp/subshell-optimize/A")
[[ $exp == $got ]] || err_exit "last command exec optimization in virtual subshells is broken" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

cat >script <<\EOF
echo $$
sh -c 'echo $$'
Expand Down