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

Fix $PPID for hashbangless script (re: 232b7bff)

I made a mistake in sh.reinit() which caused $PPID to be set to the
new process ID, not the parent process ID. This commit fixes it by
introducing, updating and using sh.current_ppid, so we continue to
minimise context switches due to getpid(2)/getppid(2) system calls.

Thanks to Geoff Clare for the report.
This commit is contained in:
Martijn Dekker 2022-07-27 13:26:34 +02:00
parent 592ce7415a
commit 48f78227a9
6 changed files with 23 additions and 9 deletions

5
NEWS
View file

@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Any uppercase BUG_* names are modernish shell bug IDs.
2022-07-27:
- Fixed a bug introduced on 2022-02-08 where $PPID was incorrect when a script
without a #! path was executed.
2022-07-26:
- Fixed incorrect handling of initial zeros in test/[ and [[ arithmetic

View file

@ -250,6 +250,7 @@ struct Shell_s
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) */
pid_t current_ppid; /* PPID of current ksh process (updates when subshell forks) */
unsigned char sigruntime[2];
Namval_t *bltin_nodes;
Namval_t *bltin_cmds;

View file

@ -22,8 +22,8 @@
#include <releaseflags.h>
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.0-rc.2" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2022-07-26" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_SVER "1.0.0-rc.3" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2022-07-27" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

View file

@ -1276,7 +1276,7 @@ Shell_t *sh_init(register int argc,register char *argv[], Shinit_f userinit)
sh_regress_init();
#endif
sh.current_pid = sh.pid = getpid();
sh.ppid = getppid();
sh.current_ppid = sh.ppid = getppid();
sh.userid=getuid();
sh.euserid=geteuid();
sh.groupid=getgid();
@ -1697,9 +1697,9 @@ int sh_reinit(char *argv[])
sh.inpipe = sh.outpipe = 0;
job_clear();
job.in_critical = 0;
/* update ${.sh.pid}, $$, $PPID */
sh.ppid = sh.current_pid;
sh.current_pid = sh.pid = getpid();
/* update $$, $PPID */
sh.ppid = sh.current_ppid;
sh.pid = sh.current_pid;
/* call user init function, if any */
if(sh.userinit)
(*sh.userinit)(&sh, 1);

View file

@ -2852,6 +2852,7 @@ pid_t _sh_fork(register pid_t parent,int flags,int *jobid)
vmtrace(-1);
#endif
/* This is the child process */
sh.current_ppid = sh.current_pid;
sh.current_pid = getpid(); /* ${.sh.pid} */
sh.outpipepid = ((flags&FPOU)?sh.current_pid:0);
if(sh.trapnote&SH_SIGTERM)

View file

@ -147,9 +147,16 @@ fi
# PPID
exp=$$
got=${ $SHELL -c 'print $PPID'; }
if [[ ${ $SHELL -c 'print $PPID'; } != $$ ]]
then err_exit "PPID variable failed -- expected '$exp', got '$got'"
fi
[[ $got == $$ ]] || err_exit "PPID variable failed in -c script -- expected '$exp', got '$got'"
print 'print $PPID' >ppid.sh
chmod +x ppid.sh
./ppid.sh >|out
got=$(<out)
[[ $got == $$ ]] || err_exit "PPID variable failed in script without #! -- expected '$exp', got '$got'"
print -r "#!$SHELL"$'\nprint $PPID' >|ppid.sh
./ppid.sh >|out
got=$(<out)
[[ $got == $$ ]] || err_exit "PPID variable failed in script with #! -- expected '$exp', got '$got'"
# OLDPWD
old=$PWD
cd /