1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

rm redundant getpid(2) syscalls (re: 9de65210)

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.
This commit is contained in:
Martijn Dekker 2020-09-23 04:19:02 +02:00
parent ce68e1be37
commit 843b546c1a
13 changed files with 43 additions and 44 deletions

View file

@ -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;