Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123#!/bin/bashset -euo pipefail
if (( $# == 0 )); then printf 'Usage: %s COMMAND [ARGUMENT ...]\n' "$0" >&2 exit 2filock=${PARCEL_SCREEN_LOCK_PATH:-/tmp/parcel-screen.lock}owner=${PARCEL_SCREEN_OWNER:-reality}acquired=0inherited=0child=# shellcheck disable=SC2329 # Invoked by the exit trap.cleanup() { if [[ -n $child ]]; then kill -TERM "$child" 2>/dev/null || true wait "$child" 2>/dev/null || true fi if (( acquired )); then # A waiter must not observe the directory disappearing during acquisition. exec 9> "$lock.guard" /usr/bin/lockf -s 9 || { printf 'Cannot guard screen lock release: %s\n' "$lock" >&2 return 1 } rm -f "$lock/owner" rmdir "$lock" exec 9>&- fi}trap 'cleanup' EXITtrap 'exit 130' INTtrap 'exit 143' TERM HUPowner_pid() { local recorded=$1 pid [[ $recorded != *$'\n'* && $recorded =~ ^(.+)\ ([1-9][0-9]*)$ ]] || return 1 pid=${BASH_REMATCH[2]} [[ ${#pid} -le 10 ]] && (( pid <= 2147483647 )) || return 1 printf '%s\n' "$pid"}inherits_lock() { local recorded pid ancestor=$PPID parent depth [[ -f $lock/owner && ! -L $lock/owner ]] || return 1 recorded=$(< "$lock/owner") pid=$(owner_pid "$recorded") || return 1 # PID 1 is everyone's ancestor, not a screen-lock wrapper. (( pid > 1 )) && kill -0 "$pid" 2>/dev/null || return 1 for (( depth=0; depth<256 && ancestor>1; depth++ )); do if (( ancestor == pid )); then printf 'Reusing screen lock: %s (ancestor owner: %s)\n' "$lock" "$recorded" >&2 return 0 fi parent=$(ps -p "$ancestor" -o ppid=) || return 2 parent=${parent//[[:space:]]/} [[ $parent =~ ^[1-9][0-9]*$ && ${#parent} -le 10 ]] && (( parent <= 2147483647 )) || return 2 ancestor=$parent done (( ancestor <= 1 )) || return 2 return 1}reclaim_stale_lock() { local recorded pid status=0 [[ -f $lock/owner && ! -L $lock/owner ]] || return 1 recorded=$(< "$lock/owner") pid=$(owner_pid "$recorded") || return 1 kill -0 "$pid" 2>/dev/null && return 1 ps -p "$pid" -o pid= > /dev/null || status=$? (( status == 1 )) || return 1 rm "$lock/owner" && rmdir "$lock" || return 2 printf 'Reclaimed stale screen lock: %s (owner: %s)\n' "$lock" "$recorded" >&2}for (( attempt=0; attempt<=180; attempt++ )); do # Keep one guard inode: unlinking it could split concurrent reclaimers across locks. exec 9> "$lock.guard" status=0 /usr/bin/lockf -s -t 0 9 || status=$? if (( status == 0 )); then if [[ -d $lock && ! -L $lock ]]; then status=0 inherits_lock || status=$? if (( status == 0 )); then inherited=1 elif (( status > 1 )); then printf 'Cannot verify screen lock ancestry: %s\n' "$lock" >&2 exit 1 else status=0 reclaim_stale_lock || status=$? if (( status > 1 )); then printf 'Cannot reclaim stale screen lock: %s\n' "$lock" >&2 exit 1 fi fi fi if (( ! inherited )); then if mkdir "$lock" 2>/dev/null; then acquired=1 printf '%s %s\n' "$owner" "$$" > "$lock/owner" elif [[ ! -d $lock || -L $lock ]]; then printf 'Cannot create screen lock: %s\n' "$lock" >&2 exit 1 fi fi elif (( status != 75 )); then printf 'Cannot guard screen lock: %s (status %s)\n' "$lock" "$status" >&2 exit 1 fi exec 9>&- (( acquired || inherited )) && break if (( attempt == 180 )); then printf 'Screen is still locked after 30 minutes: %s\n' "$lock" >&2 exit 1 fi printf 'Waiting for screen lock: %s\n' "$lock" >&2 sleep 10done"$@" &child=$!status=0wait "$child" || status=$?child=exit "$status"