#!/usr/bin/env bash set -euo pipefail # runit's run script for smallwebwaf, run again whenever smallwebwaf # exits; the wait spaces out the restarts. The state directory and every # file in it are given to the smallwebwaf user, so that a volume mounted # there needs no change of owner; chown -R changes a symbolic link itself, # never what it points to. exec, so that the signal `sv stop` sends # reaches smallwebwaf itself. main() { sleep 1 local dir if dir="$(state_dir)"; then chown -R smallwebwaf:smallwebwaf "${dir:-/var/lib/smallwebwaf}" fi exec chpst -u smallwebwaf:smallwebwaf /usr/local/bin/smallwebwaf } # state_dir prints SWWAF_STATE_DIR as smallwebwaf reads it: the variable, # or what is in the file SWWAF_STATE_DIR_FILE names. It fails when both # are set or the file cannot be read; the state directory then keeps its # owner, and smallwebwaf stops with a message naming the setting. state_dir() { if [[ ! -v SWWAF_STATE_DIR_FILE ]]; then printf '%s\n' "${SWWAF_STATE_DIR:-}" elif [[ ! -v SWWAF_STATE_DIR ]]; then cat -- "$SWWAF_STATE_DIR_FILE" 2>/dev/null else return 1 fi } main "$@"