lock="${1}.lock"
timeout="${2-20}"
if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
- printf 'Impossible to get lock: %s\n' "$lock" >&2
+ debug "Impossible to get lock: $lock"
return 1
fi
while ! mkdir "$lock" 2>&-; do
block="$(cat "$lock/pid" || printf 1)"
if ! { ps -eo pid |grep -qwF "$block"; }; then
- printf 'Overriding stale lock: %s\n' "$lock" >&2
+ debug "Overriding stale lock: $lock"
break
fi
if [ $timeout -le 0 ]; then
- printf 'Timeout while trying to get lock: %s\n' "$lock" >&2
+ debug "Timeout while trying to get lock: $lock"
return 1
fi
timeout=$((timeout - 1))
if [ "$(cat "$lock/pid")" = "$$" ]; then
rm "$lock/pid"
if ! rmdir "$lock"; then
- printf 'Cannot remove tainted lock: %s\n' "$lock" >&2
+ debug "Cannot remove tainted lock: $lock"
printf '%i\n' $$ >"${lock}/pid"
return 1
fi
return 0
else
- printf 'Refusing to release foreign lock: %s\n' "$lock" >&2
+ debug "Refusing to release foreign lock: $lock"
return 1
fi
}