]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.lock
Refactor network-hotplug-rename
[people/ms/network.git] / src / functions / functions.lock
index d9c3acfc11075576e9ca1b124238712686859dcf..8f7efc431c3dbaf41541ac04e4cc03fc727f8a78 100644 (file)
@@ -29,6 +29,38 @@ __lock_path() {
        fi
 }
 
+lock() {
+       local lock="${1}"
+       shift
+
+       local timeout="60"
+
+       # Make partent directory
+       make_parent_directory "${lock}"
+
+       (
+               log DEBUG "Trying to acquire lock ${lock}"
+
+               # Try to acquire lock on fd 9
+               if ! flock -w "${timeout}" 9; then
+                       log ERROR "Failed to acquire lock ${lock} after ${timeout} seconds"
+                       exit ${EXIT_ERROR}
+               fi
+
+               log DEBUG "Acquired lock ${lock}"
+
+               # Remember return code
+               ret=${EXIT_OK}
+
+               # Run command
+               "$@" || ret=$?
+
+               log DEBUG "Released lock ${lock}"
+
+               exit ${ret}
+       ) 9>${lock} || exit $?
+}
+
 lock_exists() {
        local name=${1}
        assert isset name