From 91c6ff7fd7dc0e71673ec079b83007df2eb38932 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Tue, 13 Apr 2021 08:45:07 +0900 Subject: [PATCH] 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. --- include/sem.sh | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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 -- 2.47.3