X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Fnetwork.git;a=blobdiff_plain;f=src%2Ffunctions%2Ffunctions.lock;h=8f7efc431c3dbaf41541ac04e4cc03fc727f8a78;hp=d9c3acfc11075576e9ca1b124238712686859dcf;hb=aa96e2b952a25eae763ecd6337fa26c224d10512;hpb=39aae67414deb3edb8d2fbf909ef472ad04c9b6e diff --git a/src/functions/functions.lock b/src/functions/functions.lock index d9c3acfc..8f7efc43 100644 --- a/src/functions/functions.lock +++ b/src/functions/functions.lock @@ -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