From: Matthias Kruk Date: Tue, 20 Apr 2021 23:28:34 +0000 (+0900) Subject: include/mutex: Prevent children from unlocking their parents' mutexes X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=97f5763dcd6c4669ff9d53af5df2ad7893aec9f5;p=toolbox include/mutex: Prevent children from unlocking their parents' mutexes The mutex_trylock() and mutex_unlock() functions use $$ to determine the current process's PID. However, this variable always reflects the PID of the original shell. This commit changes the functions to use $BASHPID to determine the PID of the current shell. --- diff --git a/include/mutex.sh b/include/mutex.sh index 01f67ac..a72be4a 100644 --- a/include/mutex.sh +++ b/include/mutex.sh @@ -14,7 +14,7 @@ mutex_trylock() { lock="$1" - if ! ln -s "$$" "$lock" &> /dev/null; then + if ! ln -s "$BASHPID" "$lock" &> /dev/null; then return 1 fi @@ -45,7 +45,7 @@ mutex_unlock() { return 1 fi - if [ "$owner" -ne "$$" ]; then + if [ "$owner" -ne "$BASHPID" ]; then return 2 fi