Skip to content

Commit

Permalink
rm redundant getpid(2) syscalls (re: 9de6521)
Browse files Browse the repository at this point in the history
Now that we have ${.sh.pid} a.k.a. shgd->current_pid, which is
updated using getpid() whenever forking a new process, there is no
need for anything else to ever call getpid(); we can use the stored
value instead. There were a lot of these syscalls kicking around,
some of them in performance-sensitive places.

The following lists only changes *other* than changing getpid() to
shgd->currentpid.

src/cmd/ksh93/include/defs.h:
- Comments: clarify what shgd->{pid,ppid,current_pid} are for.

src/cmd/ksh93/sh/main.c,
src/cmd/ksh93/sh/init.c:
- On reinit for a new script, update shgd->{pid,ppid,current_pid}
  in the sh_reinit() function itself instead of calling sh_reinit()
  from sh_main() and then updating those immediately after that
  call. It just makes more sense this way. Nothing else ever calls
  sh_reinit() so there are no side effects.

src/cmd/ksh93/sh/xec.c: _sh_fork():
- Update shgd->current_pid in the child early, so that the rest of
  the function can use it instead of calling getpid() again.
- Remove reassignment of SH_PIDNOD->nvalue.lp value pointer to
  shgd->current_pid (which makes ${.sh.pid} work in the shell).
  It's constant and was already set on init.
  • Loading branch information
McDutchie committed Sep 23, 2020
1 parent ce68e1b commit 843b546
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/edit/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/ksh93/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/ksh93/sh/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 14 additions & 14 deletions src/cmd/ksh93/sh/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/cmd/ksh93/sh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/sh/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/ksh93/sh/subshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/suid_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions src/cmd/ksh93/sh/xec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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 */
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 843b546

Please sign in to comment.