From: Michael Tremer Date: Fri, 23 Sep 2016 19:09:52 +0000 (+0200) Subject: Allow using locks with a name instead of path X-Git-Tag: 009~278 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f5ba5f6b41879a1642b24361dec03084fa92069c;p=network.git Allow using locks with a name instead of path This makes it easier to create a temporary lock in code without constructing the path of the lock file first. Signed-off-by: Michael Tremer --- diff --git a/src/functions/functions.constants b/src/functions/functions.constants index e7df05c1..271dbe57 100644 --- a/src/functions/functions.constants +++ b/src/functions/functions.constants @@ -24,6 +24,7 @@ TEXTDOMAIN="network" LOG_DIR=/var/log/network RUN_DIR=/run/network +LOCK_DIR=/var/lock RED_RUN=${RUN_DIR}/red PPP_SECRETS=/etc/ppp/secrets diff --git a/src/functions/functions.lock b/src/functions/functions.lock index b09b0b36..fac86bdd 100644 --- a/src/functions/functions.lock +++ b/src/functions/functions.lock @@ -19,15 +19,27 @@ # # ############################################################################### +__lock_path() { + local name=${1} + + if [ "${name:0:1}" = "/" ]; then + echo "${name}" + else + echo "${LOCK_DIR}/network-${name}" + fi +} + lock_acquire() { - local lockfile="${1}" - assert isset lockfile + local name=${1} + assert isset name + + local lockfile=$(__lock_path ${name}) # timeout value in seconds local timeout=120 timeout=$(( ${timeout} * 4 )) - log DEBUG "Acquiring lock '${lockfile}'" + log DEBUG "Acquiring lock '${name}'" local free="false" while [ ${timeout} -gt 0 ]; do @@ -40,7 +52,7 @@ lock_acquire() { sleep 0.25 done - assert ${free} "Could not acquire lock '${lockfile}'" + assert ${free} "Could not acquire lock '${name}'" # Write out pid to the lockfile and make sure that # nobody else can access it. @@ -49,9 +61,12 @@ lock_acquire() { } lock_release() { - local lockfile="${1}" + local name=${1} + assert isset name + + local lockfile=$(__lock_path ${name}) - log DEBUG "Releasing lock '${lockfile}'" + log DEBUG "Releasing lock '${name}'" # Remove the lockfile (okay if it does not exist). rm -f ${lockfile}