From: Matthias Kruk Date: Mon, 12 Apr 2021 23:45:07 +0000 (+0900) Subject: include/sem: Allow semaphores to be created in arbitrary locations X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=91c6ff7fd7dc0e71673ec079b83007df2eb38932;p=toolbox include/sem: Allow semaphores to be created in arbitrary locations 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. --- diff --git a/include/sem.sh b/include/sem.sh index 31df8e9..4a04134 100644 --- a/include/sem.sh +++ b/include/sem.sh @@ -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