]> git.corax.cc Git - toolbox/commitdiff
include/mutex: Prevent children from unlocking their parents' mutexes
authorMatthias Kruk <m@m10k.eu>
Tue, 20 Apr 2021 23:28:34 +0000 (08:28 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 20 Apr 2021 23:28:34 +0000 (08:28 +0900)
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

index 01f67ac06d1029b59d24ebe5a74ae2c2c92125f8..a72be4a998d61019f9b12c0cc7ed0a2571e0d4db 100644 (file)
@@ -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