From 97f5763dcd6c4669ff9d53af5df2ad7893aec9f5 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Wed, 21 Apr 2021 08:28:34 +0900 Subject: [PATCH] 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. --- include/mutex.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 -- 2.47.3