]> git.corax.cc Git - toolbox/commitdiff
include/sem: Allow semaphores to be created in arbitrary locations
authorMatthias Kruk <m@m10k.eu>
Mon, 12 Apr 2021 23:45:07 +0000 (08:45 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 12 Apr 2021 23:45:07 +0000 (08:45 +0900)
The current implementation cannot be used to created semaphores outside
of $TOOLBOX_HOME/sem. This makes it impossible to share semaphores with
scripts that are executed as a different user.
This commit changes the semaphore implementation so that only semaphore
names that don't contain slashes will be created in $TOOLBOX_HOME/sem.

include/sem.sh

index 31df8e9cc524c48dacf3fa44714ec58d2e02377e..4a041343219ab1440c0afe407059136963d782d9 100644 (file)
@@ -21,7 +21,11 @@ _sem_mutexpath() {
 
        sem="$1"
 
-       echo "$__sem_path/$sem.mutex"
+       if [[ "$sem" == *"/"* ]]; then
+               echo "$sem.mutex"
+       else
+               echo "$__sem_path/$sem.mutex"
+       fi
 }
 
 _sem_ownerpath() {
@@ -29,7 +33,11 @@ _sem_ownerpath() {
 
        sem="$1"
 
-       echo "$__sem_path/$sem.owner"
+       if [[ "$sem" == *"/"* ]]; then
+               echo "$sem.owner"
+       else
+               echo "$__sem_path/$sem.owner"
+       fi
 }
 
 _sem_sempath() {
@@ -37,7 +45,11 @@ _sem_sempath() {
 
        sem="$1"
 
-       echo "$__sem_path/$sem"
+       if [[ "$sem" == *"/"* ]]; then
+               echo "$sem"
+       else
+               echo "$__sem_path/$sem"
+       fi
 }
 
 _sem_inc() {
@@ -103,7 +115,7 @@ sem_init() {
                return 1
        fi
 
-       if ! mkdir -p "$__sem_path"; then
+       if ! mkdir -p "${sem%/*}"; then
                return 1
        fi
 
@@ -163,7 +175,7 @@ sem_wait() {
        sem=$(_sem_sempath "$name")
        passed=0
 
-       while (( passed == 0)); do
+       while (( passed == 0 )); do
                mutex_lock "$mutex"
 
                if _sem_dec "$sem"; then