- while ! mkdir "$lock" 2>&-; do
- block="$(cat "$lock/pid" || printf 1)"
- if ! { ps -eo pid |grep -qwF "$block"; }; then
- debug "Overriding stale lock: $lock"
- break
- fi
- if [ $timeout -le 0 ]; then
- debug "Timeout while trying to get lock: $lock"
- return 1
+ while [ $timeout -gt 0 ]; do
+ printf '%i\n' $$ >>"${lock}"
+ read block <"$lock"
+ if [ "$block" = $$ ]; then
+ return 0
+ elif ! { ps -eo pid |grep -qwF "$block"; }; then
+ debug "Trying to override stale lock: $lock"
+ if LOCK "$lock" 1; then
+ rm -- "$lock"
+ RELEASE "$lock"
+ fi
+ else
+ timeout=$((timeout - 1))
+ [ $timeout -gt 0 ] && sleep 1