#!/bin/sh set -eu SERVICE=${BONGO_TEST_SERVICE:-bongo.service} PACKAGE=${BONGO_TEST_PACKAGE:-mail-mta/bongo} RUNTIME_DIR=${BONGO_TEST_RUNTIME_DIR:-/run/bongo} STATE_DIR=${BONGO_TEST_STATE_DIR:-/var/lib/bongo} MAILDROP_ROOT=${BONGO_TEST_MAILDROP_ROOT:-/var/spool/bongo} MAILDROP_DIR=${BONGO_TEST_MAILDROP_DIR:-$MAILDROP_ROOT/maildrop} TMPFILES_NAME=${BONGO_TEST_TMPFILES_NAME:-bongo.conf} if [ "${BONGO_ALLOW_LIVE_RUNTIME_TEST:-}" != 1 ]; then echo "set BONGO_ALLOW_LIVE_RUNTIME_TEST=1 for the disposable host" >&2 exit 2 fi case "$RUNTIME_DIR" in /run/bongo) ;; *) echo "refusing to remove unexpected runtime directory $RUNTIME_DIR" >&2 exit 2 ;; esac systemctl_cmd() { sudo -n /usr/bin/systemctl "$@" } runtime_pid_ready() { size=$(sudo -n /usr/bin/stat -c '%s' \ "$RUNTIME_DIR/bongomanager.pid" 2>/dev/null || true) [ -n "$size" ] && [ "$size" -gt 0 ] 2>/dev/null } restore_service() { systemctl_cmd reset-failed "$SERVICE" >/dev/null 2>&1 || true systemctl_cmd start "$SERVICE" >/dev/null 2>&1 || true } trap restore_service EXIT HUP INT TERM package_paths=$(qlist -e "$PACKAGE") if printf '%s\n' "$package_paths" | grep -Eq '^(/run|/var/run)(/|$)|/bongomanager\.pid$|/[[:alnum:]_-]+\.pid$'; then echo "$PACKAGE contains a runtime directory or PID file" >&2 printf '%s\n' "$package_paths" | grep -E '^(/run|/var/run)(/|$)|/bongomanager\.pid$|/[[:alnum:]_-]+\.pid$' >&2 exit 1 fi echo "PASS package image contains no runtime directory or PID file" tmpfiles_path=/usr/lib/tmpfiles.d/$TMPFILES_NAME if [ ! -f "$tmpfiles_path" ] || ! grep -Eq "^d[[:space:]]+$RUNTIME_DIR[[:space:]]+0750[[:space:]]+bongo[[:space:]]+bongo" "$tmpfiles_path"; then echo "$tmpfiles_path does not declare $RUNTIME_DIR correctly" >&2 exit 1 fi echo "PASS installed tmpfiles declaration owns $RUNTIME_DIR" systemctl_cmd stop "$SERVICE" remaining=30 while [ "$remaining" -gt 0 ]; do if ! /usr/bin/pgrep -x bongo-manager >/dev/null 2>&1; then break fi sleep 1 remaining=$((remaining - 1)) done if /usr/bin/pgrep -x bongo-manager >/dev/null 2>&1; then echo "$SERVICE did not stop cleanly" >&2 exit 1 fi sudo -n /usr/bin/rm -rf -- "$RUNTIME_DIR" if [ -e "$RUNTIME_DIR" ]; then echo "could not remove $RUNTIME_DIR before recreation test" >&2 exit 1 fi sudo -n /usr/bin/systemd-tmpfiles --create "$TMPFILES_NAME" assert_path() { path=$1 expected_mode=$2 expected_owner=$3 actual=$(sudo -n /usr/bin/stat -c '%a %U:%G' "$path") if [ "$actual" != "$expected_mode $expected_owner" ]; then echo "$path has $actual, expected $expected_mode $expected_owner" >&2 exit 1 fi } assert_path "$RUNTIME_DIR" 750 bongo:bongo assert_path "$STATE_DIR" 750 bongo:bongo assert_path "$STATE_DIR/dbf" 750 bongo:bongo assert_path "$STATE_DIR/dbf/cookies" 700 bongo:bongo assert_path "$MAILDROP_ROOT" 710 bongo:bongopostdrop assert_path "$MAILDROP_DIR" 1730 bongo:bongopostdrop assert_path /etc/bongo/ssl.d 750 root:bongo echo "PASS tmpfiles recreated volatile and persistent directory ownership" systemctl_cmd start "$SERVICE" remaining=30 while [ "$remaining" -gt 0 ]; do if systemctl_cmd is-active "$SERVICE" >/dev/null 2>&1; then manager=$(systemctl_cmd show "$SERVICE" -p MainPID --value) if [ -n "$manager" ] && [ "$manager" -gt 1 ] 2>/dev/null && runtime_pid_ready; then break fi fi sleep 1 remaining=$((remaining - 1)) done if ! systemctl_cmd is-active "$SERVICE" >/dev/null 2>&1 || ! runtime_pid_ready; then echo "$SERVICE did not create its runtime PID file after recreation" >&2 exit 1 fi trap - EXIT HUP INT TERM echo "PASS manager PID $manager uses recreated $RUNTIME_DIR" echo "PASS runtime path lifecycle; service remains active"