#!/bin/sh # som-lock -- the one lock on the SOM board, shared by som-program-nrf91, # som-recover-nrf91, som-test-nrf91 and the interactive som-console, so that # two of them never drive the same J-Link or console at once. # # som-lock take CMD PID take the lock for PID, which runs CMD: exit 0, or 4 # printing who holds it # som-lock release PID release it, if PID holds it # som-lock show print who holds it: exit 0 held, 1 free # # The lock is one file, $SOM_LOCK (default /tmp/som-board.lock), holding one # line: " ". starttime is field 22 of # /proc//stat, so a pid the kernel has since given to another process is # not taken for the holder ("-" where there is no /proc); epoch is when the lock # was taken. A holder that is gone holds nothing, and its lock is taken over. # # A stop request for the holder is the file $SOM_LOCK.stop holding its pid: the # commands read it between steps, with shell builtins only. A take clears a # request addressed to anyone else. set -u PROG=som-lock LOCK="${SOM_LOCK:-/tmp/som-board.lock}" STOP="$LOCK.stop" usage() { cat </dev/null <"/proc/$1/stat" || [ -n "$s_line" ] || return 1 s_line=${s_line##*) } set -f set -- $s_line set +f [ $# -ge 20 ] || return 1 shift 19 echo "$1" } alive() { # PID STARTTIME case "$1" in ''|*[!0-9]*) return 1 ;; esac if [ "$2" != - ] && [ -r /proc/self/stat ]; then [ "$(starttime "$1")" = "$2" ] else kill -0 "$1" 2>/dev/null fi } # The lock's line into h_line, h_pid, h_st, h_epoch and h_cmd; fails when there # is no lock, or it is empty (a take between its open and its write). read_holder() { h_line="" read -r h_line 2>/dev/null <"$LOCK" || [ -n "$h_line" ] || return 1 set -f set -- $h_line set +f [ $# -ge 4 ] || return 1 h_pid=$1; h_st=$2; h_epoch=$3; shift 3; h_cmd=$* } describe() { h_at=$(date -u -d "@$h_epoch" +%H:%M:%S 2>/dev/null || date -u -r "$h_epoch" +%H:%M:%S 2>/dev/null) || h_at="" echo "$h_cmd (pid $h_pid, started ${h_at:-at $h_epoch} UTC)" } # Remove the lock only while it still holds LINE. A rename is atomic, so of two # takers clearing one stale lock only one gets it; one that got a fresh lock # instead -- a take landed between its read and its rename -- links it back. clear_stale() { # LINE c_tmp=$LOCK.stale.$$ mv -f "$LOCK" "$c_tmp" 2>/dev/null || return 0 c_line="" read -r c_line 2>/dev/null <"$c_tmp" [ "$c_line" = "$1" ] || ln "$c_tmp" "$LOCK" 2>/dev/null rm -f "$c_tmp" } take() { # CMD PID t_cmd=$1; t_pid=$2 case "$t_pid" in ''|*[!0-9]*) echo "$PROG: take wants a pid, got '$t_pid'" >&2; return 2 ;; esac if [ -r /proc/self/stat ]; then t_st=$(starttime "$t_pid") || { echo "$PROG: no process $t_pid" >&2; return 2; } else t_st=- fi t_mine="$t_pid $t_st $(date +%s) $t_cmd" t_n=0; t_empty=0 while [ "$t_n" -lt 10 ]; do t_n=$((t_n + 1)) ( set -C; echo "$t_mine" >"$LOCK" ) 2>/dev/null if read_holder; then t_empty=0 if [ "$h_pid" = "$t_pid" ] && [ "$h_st" = "$t_st" ]; then # A request left for an earlier holder goes; one for this taker, # written before it got here, stays. s_pid="" read -r s_pid _ 2>/dev/null <"$STOP" [ "$s_pid" = "$t_pid" ] || rm -f "$STOP" return 0 fi if alive "$h_pid" "$h_st"; then echo "the board is in use by $(describe)" return 4 fi clear_stale "$h_line" elif [ -e "$LOCK" ]; then # Empty: a take is between its open and its write -- or died there. t_empty=$((t_empty + 1)) if [ "$t_empty" -ge 3 ]; then clear_stale ""; t_empty=0; else sleep 1; fi fi done echo "cannot take the board lock $LOCK" return 1 } release() { # PID read_holder || return 0 [ "$h_pid" = "$1" ] || return 0 rm -f "$LOCK" s_pid="" read -r s_pid _ 2>/dev/null <"$STOP" [ "$s_pid" != "$1" ] || rm -f "$STOP" return 0 } case "${1:-}" in take) [ $# -eq 3 ] || { usage >&2; exit 2; }; take "$2" "$3"; exit $? ;; release) [ $# -eq 2 ] || { usage >&2; exit 2; }; release "$2"; exit $? ;; show) if read_holder && alive "$h_pid" "$h_st"; then describe; exit 0; fi echo "the board is free"; exit 1 ;; -h|--help) usage; exit 0 ;; *) usage >&2; exit 2 ;; esac