diff --git a/src/cmd/ksh93/edit/history.c b/src/cmd/ksh93/edit/history.c index 3b115acec837..24d9ffb074c4 100644 --- a/src/cmd/ksh93/edit/history.c +++ b/src/cmd/ksh93/edit/history.c @@ -363,7 +363,7 @@ int sh_histinit(void *sh_context) if(hist_clean(fd) && hist_start>1 && hsize > HIST_MAX) { #ifdef DEBUG - sfprintf(sfstderr,"%d: hist_trim hsize=%d\n",getpid(),hsize); + sfprintf(sfstderr,"%d: hist_trim hsize=%d\n",shgd->current_pid,hsize); sfsync(sfstderr); #endif /* DEBUG */ hp = hist_trim(hp,(int)hp->histind-maxlines); diff --git a/src/cmd/ksh93/include/defs.h b/src/cmd/ksh93/include/defs.h index 92e437fd87be..fe833506cdb9 100644 --- a/src/cmd/ksh93/include/defs.h +++ b/src/cmd/ksh93/include/defs.h @@ -134,9 +134,9 @@ struct shared uid_t euserid; gid_t groupid; gid_t egroupid; - pid_t pid; - pid_t ppid; - pid_t current_pid; /* pid of the current ksh instance */ + pid_t pid; /* $$, the main shell's PID (invariable) */ + pid_t ppid; /* $PPID, the main shell's parent's PID */ + pid_t current_pid; /* ${.sh.pid}, PID of current ksh process (updates when subshell forks) */ unsigned char sigruntime[2]; Namval_t *bltin_nodes; Namval_t *bltin_cmds; diff --git a/src/cmd/ksh93/sh/fault.c b/src/cmd/ksh93/sh/fault.c index 6af0b003a51b..786a5cf662a1 100644 --- a/src/cmd/ksh93/sh/fault.c +++ b/src/cmd/ksh93/sh/fault.c @@ -660,7 +660,7 @@ void sh_done(void *ptr, register int sig) } signal(sig,SIG_DFL); sigrelease(sig); - kill(getpid(),sig); + kill(shgd->current_pid,sig); pause(); } #if SHOPT_KIA diff --git a/src/cmd/ksh93/sh/init.c b/src/cmd/ksh93/sh/init.c index 639baf017c7b..90bf2681decc 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -1574,6 +1574,9 @@ int sh_reinit(char *argv[]) shp->inpipe = shp->outpipe = 0; job_clear(); job.in_critical = 0; + /* update ${.sh.pid}, $$, $PPID */ + shgd->current_pid = shgd->pid = getpid(); + shgd->ppid = getppid(); return(1); } diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c index 4083864d95cb..ef9e880eca93 100644 --- a/src/cmd/ksh93/sh/io.c +++ b/src/cmd/ksh93/sh/io.c @@ -1058,7 +1058,7 @@ static char *io_usename(char *name, int *perm, int fno, int mode) stakseek(0); } stakputc('.'); - sfprintf(stkstd,"%<#d_%d{;.tmp",getpid(),fno); + sfprintf(stkstd,"%<#d_%d{;.tmp",shgd->current_pid,fno); tname = stakfreeze(1); switch(mode) { @@ -1410,7 +1410,7 @@ int sh_redirect(Shell_t *shp,struct ionod *iop, int flag) } if(!np) { - if(flag==0 || tname || (flag==1 && fn==1 && (shp->fdstatus[fn]&IONOSEEK) && shp->outpipepid && shp->outpipepid==getpid())) + if(flag==0 || tname || (flag==1 && fn==1 && (shp->fdstatus[fn]&IONOSEEK) && shp->outpipepid == shgd->current_pid)) { if(fd==fn) { @@ -2170,7 +2170,7 @@ static void sftrack(Sfio_t* sp, int flag, void* data) #ifdef DEBUG if(flag==SF_READ || flag==SF_WRITE) { - char *z = fmtbase((long)getpid(),0,0); + char *z = fmtbase((long)shgd->current_pid,0,0); write(ERRIO,z,strlen(z)); write(ERRIO,": ",2); write(ERRIO,"attempt to ",11); diff --git a/src/cmd/ksh93/sh/jobs.c b/src/cmd/ksh93/sh/jobs.c index 9e2123f976b8..71ab74e0f2af 100644 --- a/src/cmd/ksh93/sh/jobs.c +++ b/src/cmd/ksh93/sh/jobs.c @@ -276,7 +276,7 @@ int job_reap(register int sig) abort(); } #ifdef DEBUG - if(sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d signal=%d\n",__LINE__,getpid(),job.in_critical,sig) <=0) + if(sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d signal=%d\n",__LINE__,shgd->current_pid,job.in_critical,sig) <=0) write(2,"waitsafe\n",9); sfsync(sfstderr); #endif /* DEBUG */ @@ -325,7 +325,7 @@ int job_reap(register int sig) if(!(pw=job_bypid(pid))) { #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d unknown job pid=%d pw=%x\n",__LINE__,getpid(),job.in_critical,pid,pw); + sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d unknown job pid=%d pw=%x\n",__LINE__,shgd->current_pid,job.in_critical,pid,pw); #endif /* DEBUG */ if (WIFCONTINUED(wstat) && wcontinued) continue; @@ -355,7 +355,7 @@ int job_reap(register int sig) pw->p_flag |= (P_NOTIFY|P_SIGNALLED|P_STOPPED); pw->p_exit = WSTOPSIG(wstat); if(pw->p_pgrp && pw->p_pgrp==job.curpgid && sh_isstate(SH_STOPOK)) - kill(getpid(),pw->p_exit); + kill(shgd->current_pid,pw->p_exit); if(px) { /* move to top of job list */ @@ -393,7 +393,7 @@ int job_reap(register int sig) { pw->p_flag &= ~P_NOTIFY; sh_offstate(SH_STOPOK); - kill(getpid(),SIGINT); + kill(shgd->current_pid,SIGINT); sh_onstate(SH_STOPOK); } } @@ -430,7 +430,7 @@ int job_reap(register int sig) jp->exitval |= SH_EXITSIG; } #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d job %d with pid %d flags=%o complete with status=%x exit=%d\n",__LINE__,getpid(),job.in_critical,pw->p_job,pid,pw->p_flag,wstat,pw->p_exit); + sfprintf(sfstderr,"ksh: job line %4d: reap pid=%d critical=%d job %d with pid %d flags=%o complete with status=%x exit=%d\n",__LINE__,shgd->current_pid,job.in_critical,pw->p_job,pid,pw->p_flag,wstat,pw->p_exit); sfsync(sfstderr); #endif /* DEBUG*/ /* only top-level process in job should have notify set */ @@ -645,7 +645,7 @@ int job_close(Shell_t* shp) return(0); else if(!possible && (!sh_isstate(SH_MONITOR) || sh_isstate(SH_FORKED))) return(0); - else if(getpid() != job.mypid) + else if(shgd->current_pid != job.mypid) return(0); job_lock(); if(!tty_check(0)) @@ -1238,7 +1238,7 @@ int job_post(Shell_t *shp,pid_t pid, pid_t join) pw->p_fgrp = 0; pw->p_pgrp = pw->p_fgrp; #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: post pid=%d critical=%d job=%d pid=%d pgid=%d savesig=%d join=%d\n",__LINE__,getpid(),job.in_critical,pw->p_job, + sfprintf(sfstderr,"ksh: job line %4d: post pid=%d critical=%d job=%d pid=%d pgid=%d savesig=%d join=%d\n",__LINE__,shgd->current_pid,job.in_critical,pw->p_job, pw->p_pid,pw->p_pgrp,job.savesig,join); sfsync(sfstderr); #endif /* DEBUG */ @@ -1390,9 +1390,9 @@ int job_wait(register pid_t pid) } pwfg = pw; #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: wait pid=%d critical=%d job=%d pid=%d\n",__LINE__,getpid(),job.in_critical,jobid,pid); + sfprintf(sfstderr,"ksh: job line %4d: wait pid=%d critical=%d job=%d pid=%d\n",__LINE__,shgd->current_pid,job.in_critical,jobid,pid); if(pw) - sfprintf(sfstderr,"ksh: job line %4d: wait pid=%d critical=%d flags=%o\n",__LINE__,getpid(),job.in_critical,pw->p_flag); + sfprintf(sfstderr,"ksh: job line %4d: wait pid=%d critical=%d flags=%o\n",__LINE__,shgd->current_pid,job.in_critical,pw->p_flag); #endif /* DEBUG*/ errno = 0; if(shp->coutpipe>=0 && lastpid && shp->cpid==lastpid) @@ -1498,12 +1498,12 @@ int job_wait(register pid_t pid) job_reset(pw); /* propagate keyboard interrupts to parent */ if((pw->p_flag&P_SIGNALLED) && pw->p_exit==SIGINT && !(shp->sigflag[SIGINT]&SH_SIGOFF)) - kill(getpid(),SIGINT); + kill(shgd->current_pid,SIGINT); #ifdef SIGTSTP else if((pw->p_flag&P_STOPPED) && pw->p_exit==SIGTSTP) { job.parent = 0; - kill(getpid(),SIGTSTP); + kill(shgd->current_pid,SIGTSTP); } #endif /* SIGTSTP */ } @@ -1647,7 +1647,7 @@ static struct process *job_unpost(register struct process *pwtop,int notify) register struct process *pw; /* make sure all processes are done */ #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: drop pid=%d critical=%d pid=%d env=%u\n",__LINE__,getpid(),job.in_critical,pwtop->p_pid,pwtop->p_env); + sfprintf(sfstderr,"ksh: job line %4d: drop pid=%d critical=%d pid=%d env=%u\n",__LINE__,shgd->current_pid,job.in_critical,pwtop->p_pid,pwtop->p_env); sfsync(sfstderr); #endif /* DEBUG */ pwtop = pw = job_byjid((int)pwtop->p_job); @@ -1686,7 +1686,7 @@ static struct process *job_unpost(register struct process *pwtop,int notify) } pwtop->p_pid = 0; #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: free pid=%d critical=%d job=%d\n",__LINE__,getpid(),job.in_critical,pwtop->p_job); + sfprintf(sfstderr,"ksh: job line %4d: free pid=%d critical=%d job=%d\n",__LINE__,shgd->current_pid,job.in_critical,pwtop->p_job); sfsync(sfstderr); #endif /* DEBUG */ job_free((int)pwtop->p_job); @@ -1889,7 +1889,7 @@ int sh_waitsafe(void) void job_fork(pid_t parent) { #ifdef DEBUG - sfprintf(sfstderr,"ksh: job line %4d: fork pid=%d critical=%d parent=%d\n",__LINE__,getpid(),job.in_critical,parent); + sfprintf(sfstderr,"ksh: job line %4d: fork pid=%d critical=%d parent=%d\n",__LINE__,shgd->current_pid,job.in_critical,parent); #endif /* DEBUG */ switch (parent) { diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index d5baf5d0a983..67184e1b7450 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -306,7 +306,7 @@ int sh_lex(Lex_t *lp) if(flag&ARG_QUOTED) quoted = "quoted:"; } - sfprintf(sfstderr,"%d: line %d: %o:%s%s%s%s %s\n",getpid(),shp->inlineno,tok,quoted, + sfprintf(sfstderr,"%d: line %d: %o:%s%s%s%s %s\n",shgd->current_pid,shp->inlineno,tok,quoted, macro, split, expand, fmttoken(lp,tok,tokstr)); return(tok); } diff --git a/src/cmd/ksh93/sh/main.c b/src/cmd/ksh93/sh/main.c index 0a2d1e67f62d..54e54d2b2f1f 100644 --- a/src/cmd/ksh93/sh/main.c +++ b/src/cmd/ksh93/sh/main.c @@ -146,8 +146,6 @@ int sh_main(int ac, char *av[], Shinit_f userinit) { /* begin script execution here */ sh_reinit((char**)0); - shp->gd->pid = getpid(); - shp->gd->ppid = getppid(); } shp->fn_depth = shp->dot_depth = 0; command = error_info.id; @@ -518,7 +516,7 @@ static void exfile(register Shell_t *shp, register Sfio_t *iop,register int fno) { buff.mode = SH_JMPERREXIT; #ifdef DEBUG - errormsg(SH_DICT,ERROR_warn(0),"%d: mode changed to JMP_EXIT",getpid()); + errormsg(SH_DICT,ERROR_warn(0),"%d: mode changed to JMP_EXIT",shgd->current_pid); #endif } } diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index 032871479015..d3ede39bce2d 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -1077,7 +1077,7 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env #if _lib_readlink /* save original pathname */ stakseek(PATH_OFFSET); - pidsize = sfprintf(stkstd,"*%d*",spawn?getpid():getppid()); + pidsize = sfprintf(stkstd,"*%d*",spawn?shgd->current_pid:getppid()); stakputs(opath); opath = stakfreeze(1)+PATH_OFFSET+pidsize; /* only use tracked alias if we're not searching default path */ @@ -1304,7 +1304,7 @@ static void exscript(Shell_t *shp,register char *path,register char *argv[],char } if((euserid=geteuid()) != shp->gd->userid) { - strncpy(name+9,fmtbase((long)getpid(),10,0),sizeof(name)-10); + strncpy(name+9,fmtbase((long)shgd->current_pid,10,0),sizeof(name)-10); /* create a suid open file with owner equal effective uid */ if((n=open(name,O_CREAT|O_TRUNC|O_WRONLY,S_ISUID|S_IXUSR)) < 0) goto fail; diff --git a/src/cmd/ksh93/sh/subshell.c b/src/cmd/ksh93/sh/subshell.c index ef3750fe365b..e639556bdbc0 100644 --- a/src/cmd/ksh93/sh/subshell.c +++ b/src/cmd/ksh93/sh/subshell.c @@ -791,7 +791,7 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub) sp->prev->sig = sp->sig; else { - kill(getpid(),sp->sig); + kill(shgd->current_pid,sp->sig); sh_chktrap(shp); } } @@ -800,7 +800,7 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub) nsig = shp->savesig; shp->savesig = 0; if(nsig>0) - kill(getpid(),nsig); + kill(shgd->current_pid,nsig); if(sp->subpid) { job_wait(sp->subpid); @@ -816,7 +816,7 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub) { int sig = shp->exitval&SH_EXITMASK; if(sig==SIGINT || sig== SIGQUIT) - kill(getpid(),sig); + kill(shgd->current_pid,sig); } if(duped) { @@ -825,9 +825,9 @@ Sfio_t *sh_subshell(Shell_t *shp,Shnode_t *t, volatile int flags, int comsub) errormsg(SH_DICT,ERROR_system(1),e_redirect); } if(shp->ignsig) - kill(getpid(),shp->ignsig); + kill(shgd->current_pid,shp->ignsig); if(jmpval==SH_JMPSUB && shp->lastsig) - kill(getpid(),shp->lastsig); + kill(shgd->current_pid,shp->lastsig); if(jmpval && shp->toomany) siglongjmp(*shp->jmplist,jmpval); return(iop); diff --git a/src/cmd/ksh93/sh/suid_exec.c b/src/cmd/ksh93/sh/suid_exec.c index 4974ef9d2b4f..19812c83ee63 100644 --- a/src/cmd/ksh93/sh/suid_exec.c +++ b/src/cmd/ksh93/sh/suid_exec.c @@ -482,7 +482,7 @@ static void setids(int mode,uid_t owner,gid_t group) static void maketemp(char *template) { register char *cp = template; - register pid_t n = getpid(); + register pid_t n = shgd->current_pid; /* skip to end of string */ while(*++cp); /* convert process id to string */ diff --git a/src/cmd/ksh93/sh/timers.c b/src/cmd/ksh93/sh/timers.c index 10dc05d94f5e..d0c5a4ee41a8 100644 --- a/src/cmd/ksh93/sh/timers.c +++ b/src/cmd/ksh93/sh/timers.c @@ -94,7 +94,7 @@ static void sigalrm(int sig) if(time_state&SIGALRM_CALL) time_state &= ~SIGALRM_CALL; else if(alarm(0)) - kill(getpid(),SIGALRM|SH_TRAP); + kill(shgd->current_pid,SIGALRM|SH_TRAP); if(time_state) { if(time_state&IN_ADDTIMEOUT) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index d8fd5220df13..8d7c5a74a867 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -1636,7 +1636,7 @@ int sh_exec(register const Shnode_t *t, int flags) { shp->trapnote &= ~SH_SIGIGNORE; if(shp->exitval == (SH_EXITSIG|SIGINT)) - kill(getpid(),SIGINT); + kill(shgd->current_pid,SIGINT); } } if(type&FAMP) @@ -2887,8 +2887,9 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid) #if !_std_malloc vmtrace(-1); #endif - shp->outpipepid = ((flags&FPOU)?getpid():0); /* This is the child process */ + shgd->current_pid = getpid(); /* ${.sh.pid} */ + shp->outpipepid = ((flags&FPOU)?shgd->current_pid:0); if(shp->trapnote&SH_SIGTERM) sh_exit(SH_EXITSIG|SIGTERM); shp->gd->nforks=0; @@ -2898,7 +2899,7 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid) sh_offstate(SH_MONITOR); if(sh_isstate(SH_MONITOR)) { - parent = getpid(); + parent = shgd->current_pid; if(postid==0) job.curpgid = parent; while(setpgid(0,job.curpgid)<0 && job.curpgid!=parent) @@ -2925,9 +2926,6 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid) sh_onstate(SH_NOLOG); if (shp->fn_reset) shp->fn_depth = shp->fn_reset = 0; - /* Set ${.sh.pid} to the child process ID */ - shp->gd->current_pid = getpid(); - SH_PIDNOD->nvalue.lp = &shp->gd->current_pid; #if SHOPT_ACCT sh_accsusp(); #endif /* SHOPT_ACCT */ @@ -2944,7 +2942,7 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid) sig = shp->savesig; shp->savesig = 0; if(sig>0) - kill(getpid(),sig); + kill(shgd->current_pid,sig); sh_sigcheck(shp); usepipe=0; return(0); @@ -2965,7 +2963,7 @@ pid_t sh_fork(Shell_t *shp,int flags, int *jobid) sig = shp->savesig; shp->savesig = 0; if(sig>0) - kill(getpid(),sig); + kill(shgd->current_pid,sig); job_fork(parent); return(parent); } @@ -3177,7 +3175,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg) if(jmpval) r=shp->exitval; if(r>SH_EXITSIG && ((r&SH_EXITMASK)==SIGINT || ((r&SH_EXITMASK)==SIGQUIT))) - kill(getpid(),r&SH_EXITMASK); + kill(shgd->current_pid,r&SH_EXITMASK); if(jmpval > SH_JMPFUN) { sh_chktrap(shp);