From 4be7e535508bf446c2bedb2971aa0def2a365050 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sat, 18 Jun 2022 19:17:44 +0100 Subject: [PATCH] sh_subshell(): remove nofork flag and some dead code Part of the *subshell_data/*sp struct: 92: int nofork; In subshell.c, sh_subshell(), before entering a virtual subshell: 628: if(!(sp->nofork = sh_state(SH_NOFORK))) 629: sh_onstate(SH_NOFORK); ...and upon restoring when leaving the subshell: 696: if(!sp->nofork) 697: sh_offstate(SH_NOFORK); This code is clearly nonsense, because 'sh_state(SH_NOFORK)' is a static expression that always has the value 1. This can be seen by looking at the definitions in defs.h: 194: #define sh_state(x) ( 1<<(x)) ...and in shell.h: 68: #define SH_NOFORK 0 /* set when fork not necessary */ So, sh_state(SH_NOFORK) == 1<<(0) == 1, meaning sp->nofork is always 0, meaning the statements conditional on it are never executed. We can get rid of all of this; it's dead code. In subshell.c on line 628, perhaps sh_isstate(SH_NOFORK) was meant instead of sh_state(SH_NOFORK) -- but that state flag is supposed to signal that forking is not necessary when running an external command, that we can potentially 'exec' it directly if it's the last command in the subshell -- which makes no sense at all when being in a virtual subshell which does not have its own process. This dead code was added in version 2008-09-21 ksh93t. The subsequent 'flags |= sh_state(SH_NOFORK)' (subshell.c line 630) probably makes more sense, since this sets execflg in sh_exec(), which is also used for optimising redirections (avoids bothering to store previous state if it's the last command in the subshell). --- src/cmd/ksh93/sh/subshell.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/cmd/ksh93/sh/subshell.c b/src/cmd/ksh93/sh/subshell.c index 0b31429cd..05dbdf2cd 100644 --- a/src/cmd/ksh93/sh/subshell.c +++ b/src/cmd/ksh93/sh/subshell.c @@ -89,7 +89,6 @@ static struct subshell pid_t cpid; int coutpipe; int cpipe; - int nofork; int subdup; char subshare; char comsub; @@ -625,8 +624,6 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub) sfswap(iop,sfstdout); sfset(sfstdout,SF_READ,0); sh.fdstatus[1] = IOWRITE; - if(!(sp->nofork = sh_state(SH_NOFORK))) - sh_onstate(SH_NOFORK); flags |= sh_state(SH_NOFORK); } else if(sp->prev) @@ -693,8 +690,6 @@ Sfio_t *sh_subshell(Shnode_t *t, volatile int flags, int comsub) sigrelease(SIGTSTP); #endif /* re-enable job control */ - if(!sp->nofork) - sh_offstate(SH_NOFORK); job.jobcontrol = sp->jobcontrol; if(sp->monitor) sh_onstate(SH_MONITOR);