mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
Add ${.sh.pid} as an alternative to $BASHPID (#109)
This variable is like Bash's $BASHPID, but in virtual subshells it will retain its previous value as virtual subshells don't fork. Both $BASHPID and ${.sh.pid} are different from $$ as the latter is only set to the parent shell's process ID (i.e. it isn't set to the process ID of the current subshell). src/cmd/ksh93/include/defs.h: - Add 'current_pid' for storing the current process ID at a valid memory address. - Change 'ppid' from 'int32_t' to 'pid_t', as the return value from 'getppid' is of the 'pid_t' data type. src/cmd/ksh93/data/variables.c, src/cmd/ksh93/include/variables.h, src/cmd/ksh93/sh/init.c, src/cmd/ksh93/sh/xec.c: - Add the ${.sh.pid} variable as an alternative to $BASHPID. The process ID is stored in a struct before ${.sh.pid} is set as environment variables are pointers that must point to a valid memory address. ${.sh.pid} is updated by the _sh_fork() function, which is called when ksh forks a new process with sh_fork() or sh_ntfork(). src/cmd/ksh93/tests/variables.sh: - Add ${.sh.pid} to the list of special variables and add three regression tests for ${.sh.pid}. src/cmd/ksh93/tests/subshell.sh: - Update the PATH forking regression test to use ${.sh.pid} and remove the TODO note.
This commit is contained in:
parent
f9fdbfc9e9
commit
9de65210c6
9 changed files with 45 additions and 7 deletions
|
@ -747,14 +747,13 @@ v=$($SHELL $testvars) && [[ "$v" == "a= b= c=0" ]] || err_exit 'variables set in
|
|||
|
||||
# ======
|
||||
# Setting PATH in virtual subshell should trigger a fork; restoring PATH after leaving virtual subshell should not.
|
||||
# TODO: it would be really nice to have a ${.sh.pid} for this sort of test (like $BASHPID on bash)...
|
||||
SHELL=$SHELL "$SHELL" -c '
|
||||
(
|
||||
"$SHELL" -c "echo DEBUG \$PPID"
|
||||
echo "DEBUG ${.sh.pid}"
|
||||
readonly PATH
|
||||
"$SHELL" -c "echo DEBUG \$PPID"
|
||||
echo "DEBUG ${.sh.pid}"
|
||||
)
|
||||
"$SHELL" -c "echo DEBUG \$PPID"
|
||||
echo "DEBUG ${.sh.pid}"
|
||||
: extra command to disable "-c" exec optimization
|
||||
' | awk '/^DEBUG/ { pid[NR] = $2; } END { exit !(pid[1] == pid[2] && pid[2] == pid[3]); }' \
|
||||
|| err_exit "setting PATH to readonly in subshell triggers an erroneous fork"
|
||||
|
|
|
@ -915,6 +915,7 @@ set -- \
|
|||
".sh.stats" \
|
||||
".sh.math" \
|
||||
".sh.pool" \
|
||||
".sh.pid" \
|
||||
"SHLVL" \
|
||||
"CSWIDTH"
|
||||
|
||||
|
@ -991,5 +992,20 @@ $SHELL -c '
|
|||
e=$?
|
||||
((e == 1)) || err_exit "Failure in making one or more special variables readonly in a subshell (exit status $e)"
|
||||
|
||||
# ======
|
||||
# ${.sh.pid} should be the forked subshell's PID
|
||||
(
|
||||
ulimit -t unlimited
|
||||
[[ ${.sh.pid} == $$ ]]
|
||||
) && err_exit "\${.sh.pid} is the same as \$$ (both are $$)"
|
||||
|
||||
# ${.sh.pid} should be the PID of the running job
|
||||
echo ${.sh.pid} > "$tmp/jobpid" &
|
||||
wait
|
||||
[[ $(cat "$tmp/jobpid") == ${.sh.pid} ]] && err_exit "\${.sh.pid} is not set to a job's PID (expected $!, got $(cat "$tmp/jobpid"))"
|
||||
|
||||
# ${.sh.pid} should be the same as $$ in the parent shell
|
||||
[[ $$ == ${.sh.pid} ]] || err_exit "\${.sh.pid} and \$$ differ in the parent shell (expected $$, got ${.sh.pid})"
|
||||
|
||||
# ======
|
||||
exit $((Errors<125?Errors:125))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue