From 66c37202fde05424433f25d758e15ab9fcd5158c Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sat, 10 Apr 2021 17:56:11 +0100 Subject: [PATCH] SHOPT_SPAWN: rm unused job control code (re: f207cd57, 41ebb55a) Since f207cd57, sh_ntfork() is never called if job.jobcontrol is set (i.e. if job control is active on an interactive shell), so the code that is only run if job.jobcontrol is set should be removed. src/cmd/ksh93/sh/xec.c: - Remove spawnveg() define that is unused as of 7b0e0776. - sh_exec(): Simplify SHOPT_SPAWN preprocessor logic. As sh_fork() never returns a negative value, only run the parent<0 check after running sh_ntfork() -- that check already didn't happen when compiling ksh with SHOPT_SPAWN disabled. - sh_ntfork(): Remove signal and terminal handling (with race condition) that was only run with job.jobcontrol set. --- src/cmd/ksh93/sh/xec.c | 62 ++++++++++-------------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index fcb43e26c..5e85c68a8 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -57,9 +57,6 @@ #if _lib_nice extern int nice(int); #endif /* _lib_nice */ -#if !_lib_spawnveg -# define spawnveg(a,b,c,d) spawnve(a,b,c) -#endif /* !_lib_spawnveg */ #if SHOPT_SPAWN static pid_t sh_ntfork(Shell_t*,const Shnode_t*,char*[],int*,int); #endif /* SHOPT_SPAWN */ @@ -1624,22 +1621,20 @@ int sh_exec(register const Shnode_t *t, int flags) #endif #if SHOPT_SPAWN if(com && !job.jobcontrol) - parent = sh_ntfork(shp,t,com,&jobid,ntflag); - else - parent = sh_fork(shp,type,&jobid); - if(parent<0) { - /* prevent a file descriptor leak from a 'not found' error */ - if(shp->topfd > topfd) - sh_iorestore(shp,topfd,0); - - if(shp->comsub==1 && usepipe && unpipe) - sh_iounpipe(shp); - break; + parent = sh_ntfork(shp,t,com,&jobid,ntflag); + if(parent<0) + { + if(shp->topfd > topfd) + sh_iorestore(shp,topfd,0); /* prevent FD leak from 'not found' */ + if(shp->comsub==1 && usepipe && unpipe) + sh_iounpipe(shp); + break; + } } -#else - parent = sh_fork(shp,type,&jobid); + else #endif /* SHOPT_SPAWN */ + parent = sh_fork(shp,type,&jobid); } if(job.parent=parent) /* This is the parent branch of fork @@ -1735,7 +1730,7 @@ int sh_exec(register const Shnode_t *t, int flags) } sh_offstate(SH_INTERACTIVE); /* pipe in or out */ -#ifdef _lib_nice +#if _lib_nice if((type&FAMP) && sh_isoption(SH_BGNICE)) nice(4); #endif /* _lib_nice */ @@ -3500,7 +3495,7 @@ static void sigreset(Shell_t *shp,int mode) /* * A combined fork/exec for systems with slow fork(). - * Note: Incompatible with job control. + * Incompatible with job control on interactive shells (job.jobcontrol). */ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,int flag) { @@ -3509,7 +3504,7 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in static int savejobid; struct checkpt *buffp = (struct checkpt*)stkalloc(shp->stk,sizeof(struct checkpt)); int otype=0, jmpval,jobfork=0; - volatile int jobwasset=0, scope=0, sigwasset=0; + volatile int scope=0, sigwasset=0; char **arge, *path; volatile pid_t grp = 0; Pathcomp_t *pp; @@ -3577,17 +3572,8 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in } arge = sh_envgen(); shp->exitval = 0; -#ifdef SIGTSTP - if(job.jobcontrol) - { - signal(SIGTTIN,SIG_DFL); - signal(SIGTTOU,SIG_DFL); - signal(SIGTSTP,SIG_DFL); - jobwasset++; - } -#endif /* SIGTSTP */ #ifdef JOBS - if(sh_isstate(SH_MONITOR) && (job.jobcontrol || (otype&FAMP))) + if(sh_isstate(SH_MONITOR) && (otype&FAMP)) { if((otype&FAMP) || job.curpgid==0) grp = 1; @@ -3643,17 +3629,6 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in sh_popcontext(shp,buffp); if(buffp->olist) free_list(buffp->olist); -#ifdef SIGTSTP - if(jobwasset) - { - signal(SIGTTIN,SIG_IGN); - signal(SIGTTOU,SIG_IGN); - if(sh_isstate(SH_INTERACTIVE)) - signal(SIGTSTP,SIG_IGN); - else - signal(SIGTSTP,SIG_DFL); - } -#endif /* SIGTSTP */ if(sigwasset) sigreset(shp,1); /* restore ignored signals */ if(scope) @@ -3673,13 +3648,6 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in #ifdef JOBS if(grp==1) job.curpgid = spawnpid; -# ifdef SIGTSTP - if(job.jobcontrol && grp>0 && !(otype&FAMP)) - { - while(tcsetpgrp(job.fd,job.curpgid)<0 && job.curpgid!=spawnpid) - job.curpgid = spawnpid; - } -# endif /* SIGTSTP */ #endif /* JOBS */ savejobid = *jobid; if(otype)