Files

23 lines
944 B
Bash

#!/usr/bin/env bash
# The upgrade coordinator holds FD 9 for the complete install/check/backup cycle.
# Child tools may reuse that SAME open file description, never bypass the lock.
acquire_backup_lock() {
[[ $LOCKFILE == /* && ! -L $LOCKFILE ]] || { echo 'Unsafe lock path' >&2; return 1; }
if [[ -e $LOCKFILE ]]; then
[[ -f $LOCKFILE && ! -s $LOCKFILE && $(stat -c %u "$LOCKFILE") == 0 ]] || { echo 'Unsafe existing lock file' >&2; return 1; }
fi
if [[ ${APP_BACKUP_INHERIT_LOCK:-0} == 1 ]]; then
[[ -e /proc/self/fd/9 && $(stat -Lc '%d:%i' /proc/self/fd/9) == "$(stat -Lc '%d:%i' "$LOCKFILE")" ]] || {
echo 'Inherited lock descriptor does not match configured lock' >&2; return 1;
}
else
exec 9>"$LOCKFILE" || return 1
fi
local lock_code
flock -E "${1:-1}" -n 9 || {
lock_code=$?
echo "Another backup/restore/update holds $LOCKFILE, or the lock is unavailable" >&2
return "$lock_code"
}
}