#!/bin/bash
########################################################################
# cet_lock
#
# Filesystem-based locking using hard links, which are atomic (yes, even
# on NFS).
#
####################################
# Notes
#
# * If successfully obtained, the lock will persist for the duration of
#   the specified command. If there is no command, the lock will be
#   release immediately once obtained. To perform a test, use
#   e.g. "sleep 10."
#
# * The combination of "resource" (-r) and "lockdir" (-d) must identify
#   the resource uniquely such that the same combination will be used
#   for every lock operation for that resource, regardless of the node
#   requesting that lock, or the specific mount point of "lockdir."
#
# * If simultaneous locks are requested for multiple resources, specify
#   those resources with multiple -r options *in the same order
#   everywhere you wish to use any of those resources*. The caller is
#   responsible for this.
#
# * "lockdir" must:
#
#   * support hard links by non-privileged users.
#
#   * be accessible to all users of the resource to be locked.
#
# * A resource-specific lockdir following the rules should prefix the
#   resource, separated by a single colon. If a resource descriptor
#   contains a colon, then add a leading colon to avoid ambiguity.
#
# * sleep time and timeout (if specified) must be in whole numbers of
#   seconds.
########################################################################

libexec="$(cd "${BASH_SOURCE%/*}" && pwd -P)"
. "$libexec/message_functions.sh" || \
  { echo "ERROR: could not find essential functions in $libexec/message_functions.sh" \
         1>&2; exit 1; }

prog="${0##*/}"
fill="${prog//?/}"

stat_cmd=(stat)
if [[ "$OSTYPE" == *darwin* ]]; then
  stat_cmd+=(-f '%l')
else
  stat_cmd+=(-c '%h')
fi

lock_files=()
lock_links=()

cet_lock() {
  local OPT OPTIND OPTARG \
        lockdir="$(pwd -P)/.locks" \
        locker="$prog" \
        resource_default="<unknown>" \
        resources=() \
        sleep_time=5 \
        timeout_i=30

  while getopts :d:l:Rr:s:t:v OPT; do
    case $OPT in
      d) lockdir="$OPTARG";;
      l) locker="$OPTARG";;
      R) (( _cet_redirected = 1 ));;
      r) resources+=("$OPTARG");;
      s) (( sleep_time = $OPTARG ));;
      t) (( timeout_i = $OPTARG ));;
      v) (( _cet_verbose = 1 ));;
      *)
        cat <<EOF
usage: $prog [-d <lockdir>] [-l <locker> ] [-Rv] [-r <resource>]...
       $fill [-s <sleep_time>] [-r <timeout>] [--]
       $fill [<cmd> [<arg>...]]
EOF
        exit 2
    esac
  done
  shift $(( OPTIND - 1 ))
  OPTIND=1
  (( ${#resources[@]} )) || resources=("$resource_default")
  mkdir -p "$lockdir" || \
    fatal_error "unable to make specified locks directory $lockdir"
  local resource= lockdir_local=
  for resource in ${resources[*]:+"${resources[@]}"}; do
    if ! [ "${resource#*:}" = "${resource}" ]; then
      lockdir_local="${resource%%:*}"
      resource="${resource#*:}"
      if [ -z "$lockdir_local" ]; then
        lockdir_local="$lockdir"
      else
        mkdir -p "${lockdir_local}" || \
          fatal_error "unable to make specified locks directory $lockdir for resource $resource"
      fi
    else
      lockdir_local="$lockdir"
    fi
    local timeout=$timeout_i
    local resource_stem=($(sha256sum <<<"$resource" 2>/dev/null))
    # If the lock directory starts with a `.', the files don't have to.
    [[ "${lockdir_local##*/}" == .* ]] || resource_stem=".$resource_stem"
    local lock_file="$(mktemp -p "$lockdir_local" -t $resource_stem.XXXXXXXXXX.lock || fatal_error "unable to create lock file for $resource")"
    lock_files+=("$lock_file")
    trap "rm -f \${lock_links[*]:+\"\${lock_links[@]}\"} \${lock_files[*]:+\"\${lock_files[@]}\"}; unset lock_files lock_links" EXIT
    cat > "$lock_file" <<EOF
Locked resource: $resource
Locker ID: ${locker}
Locker PID: $$
Actor ID: ${LOGNAME:-${USER:-<unknown>}}@${HOSTNAME:-$(hostname -f 2>/dev/null || echo localhost)}
EOF
    local lock_link="${lock_file%/*}/$resource_stem.lock"
    local actor_id= locker_id= locker_pid= locked_resource= dummy= lock_taken=
    local last_locker= last_lock_taken= our_request="$(date +%s.%N)"
    echo "Lock requested: $our_request" >> "$lock_file"
    while ! { ln "$lock_file" "$lock_link" || \
                (( $("${stat_cmd[@]}" "$lock_file") == 2 )); } >/dev/null 2>&1; do
      IFS=$'\n' read -r -d ''  \
         locked_resource \
         locker_id \
         locker_pid \
         actor_id \
         dummy \
         lock_taken\
         <"$lock_link" >/dev/null 2>&1
      [ "${locked_resource#*: }" = "$resource" ] || \
        warning "requested resource \"$resource\" and locked resource \"${locked_resource#*: }\" do not match"
      if ! { [ "$locker_id:$locker_pid" = "$last_locker" ] && \
               [ "$lock_taken" = "$last_lock_taken" ]; }; then
        notify "resource \"$resource\" requested at $our_request currently locked by ${actor_id#*: }
        (via ${locker_id#*: } PID ${locker_pid#*: }) since ${lock_taken#*: }"
        last_lock_taken="$lock_taken"
        last_locker="$locker_id:$locker_pid"
      fi
      (( timeout -= sleep_time ))
      if (( timeout > 0 )); then
        sleep $sleep_time
      else
        fatal_error "install lock timeout (${timeout_i}s) exceeded:
             lock for requested resource \"$resource\" still held by PID ${locker_pid#*: } - execute

               rm -f \"$lock_file\" \\
                     \"$lock_link\"

             if known stale"
      fi
    done
    echo "Lock taken: $(date +%s.%N)" >> "$lock_file"
    lock_links+=("$lock_link")
  done
  verbose "${*:+executing command }utilizing locked resource(s)${resources[*]:+ ${resources[@]}}${*:+$'\n'$@}"
  # Execute the provided command.
  [ -z "$*" ] || ${*:+"$@"}
}

cet_lock ${*:+"$@"}
