]> git.ipfire.org Git - people/stevee/network.git/commitdiff
Allow using locks with a name instead of path
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 23 Sep 2016 19:09:52 +0000 (21:09 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 23 Sep 2016 19:09:52 +0000 (21:09 +0200)
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 <michael.tremer@ipfire.org>
src/functions/functions.constants
src/functions/functions.lock

index e7df05c1b1624a3e07e9df2858a27f14d09b8644..271dbe57f7bd0ae0fc3b1b296d4f114b9368e3ca 100644 (file)
@@ -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
index b09b0b3623ecb7f4569605675fff1eb6fc04a974..fac86bddc336a6d1dd13d0e95f8c6ad683ccbaad 100644 (file)
 #                                                                             #
 ###############################################################################
 
+__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}