| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | SOCKET_PATH="${GARCARD_SPRINT04_RUNTIME_SOCKET:-${PWD}/target/garcard-sprint04-runtime.sock}" |
| 5 | LOG_FILE="${GARCARD_SPRINT04_RUNTIME_LOG:-${PWD}/target/garcard-sprint04-runtime.log}" |
| 6 | ACTION_ID="${GARCARD_SPRINT04_RUNTIME_ACTION_ID:-com.mesonbuild.install.run}" |
| 7 | PROMPT_TIMEOUT_SECS="${GARCARD_SPRINT04_RUNTIME_PROMPT_TIMEOUT_SECS:-20}" |
| 8 | |
| 9 | if ! command -v pkcheck >/dev/null 2>&1; then |
| 10 | echo "pkcheck not found; install polkit tools to run runtime validation" |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
| 14 | if command -v garcard >/dev/null 2>&1; then |
| 15 | DAEMON_CMD=(garcard daemon) |
| 16 | else |
| 17 | DAEMON_CMD=(cargo run -q -p garcard -- daemon) |
| 18 | fi |
| 19 | |
| 20 | if command -v garcardctl >/dev/null 2>&1; then |
| 21 | CTL_CMD=(garcardctl) |
| 22 | else |
| 23 | CTL_CMD=(cargo run -q -p garcardctl --) |
| 24 | fi |
| 25 | |
| 26 | DAEMON_PID=0 |
| 27 | PROBE_PID=0 |
| 28 | |
| 29 | run_ctl() { |
| 30 | GARCARD_SOCKET="${SOCKET_PATH}" "${CTL_CMD[@]}" "$@" |
| 31 | } |
| 32 | |
| 33 | wait_for_daemon() { |
| 34 | local tries=120 |
| 35 | while (( tries > 0 )); do |
| 36 | if run_ctl ping >/dev/null 2>&1; then |
| 37 | return 0 |
| 38 | fi |
| 39 | sleep 0.2 |
| 40 | tries=$((tries - 1)) |
| 41 | done |
| 42 | echo "daemon did not become ready in time" |
| 43 | return 1 |
| 44 | } |
| 45 | |
| 46 | start_daemon() { |
| 47 | GARCARD_SOCKET="${SOCKET_PATH}" \ |
| 48 | GARCARD_AGENT_BACKEND=polkit \ |
| 49 | GARCARD_PROMPT_TIMEOUT_SECS="${PROMPT_TIMEOUT_SECS}" \ |
| 50 | RUST_LOG=garcard=debug \ |
| 51 | "${DAEMON_CMD[@]}" >>"${LOG_FILE}" 2>&1 & |
| 52 | DAEMON_PID=$! |
| 53 | wait_for_daemon |
| 54 | } |
| 55 | |
| 56 | stop_daemon_graceful() { |
| 57 | if [[ "${DAEMON_PID}" -gt 0 ]] && kill -0 "${DAEMON_PID}" 2>/dev/null; then |
| 58 | run_ctl quit >/dev/null 2>&1 || true |
| 59 | wait "${DAEMON_PID}" 2>/dev/null || true |
| 60 | fi |
| 61 | DAEMON_PID=0 |
| 62 | } |
| 63 | |
| 64 | stop_daemon_sigterm() { |
| 65 | if [[ "${DAEMON_PID}" -gt 0 ]] && kill -0 "${DAEMON_PID}" 2>/dev/null; then |
| 66 | kill -TERM "${DAEMON_PID}" 2>/dev/null || true |
| 67 | wait "${DAEMON_PID}" 2>/dev/null || true |
| 68 | fi |
| 69 | DAEMON_PID=0 |
| 70 | } |
| 71 | |
| 72 | cleanup() { |
| 73 | stop_daemon_graceful |
| 74 | rm -f "${SOCKET_PATH}" |
| 75 | } |
| 76 | trap cleanup EXIT |
| 77 | |
| 78 | wait_for_active_request() { |
| 79 | local tries=120 |
| 80 | while (( tries > 0 )); do |
| 81 | local summary |
| 82 | summary="$(run_ctl auth-summary 2>/dev/null || true)" |
| 83 | if [[ "${summary}" == *'"active_requests": 1'* ]]; then |
| 84 | return 0 |
| 85 | fi |
| 86 | sleep 0.2 |
| 87 | tries=$((tries - 1)) |
| 88 | done |
| 89 | echo "auth request never became active" |
| 90 | return 1 |
| 91 | } |
| 92 | |
| 93 | run_probe_background() { |
| 94 | local out_file="$1" |
| 95 | local err_file="$2" |
| 96 | pkcheck --allow-user-interaction --process "$$" --action-id "${ACTION_ID}" \ |
| 97 | >"${out_file}" 2>"${err_file}" & |
| 98 | PROBE_PID=$! |
| 99 | } |
| 100 | |
| 101 | wait_probe() { |
| 102 | local probe_pid="$1" |
| 103 | local rc_file="$2" |
| 104 | set +e |
| 105 | wait "${probe_pid}" |
| 106 | local rc=$? |
| 107 | set -e |
| 108 | echo "${rc}" > "${rc_file}" |
| 109 | } |
| 110 | |
| 111 | mkdir -p "$(dirname "${SOCKET_PATH}")" |
| 112 | mkdir -p "$(dirname "${LOG_FILE}")" |
| 113 | rm -f "${SOCKET_PATH}" "${LOG_FILE}" |
| 114 | |
| 115 | echo "Sprint 04 runtime validation" |
| 116 | echo " socket: ${SOCKET_PATH}" |
| 117 | echo " log: ${LOG_FILE}" |
| 118 | echo " action: ${ACTION_ID}" |
| 119 | |
| 120 | echo "[0/3] Reset temporary authorizations" |
| 121 | pkcheck --revoke-temp || true |
| 122 | |
| 123 | echo "[1/3] Active prompt + daemon restart" |
| 124 | start_daemon |
| 125 | probe1_out="${PWD}/target/sprint04-probe-restart.out" |
| 126 | probe1_err="${PWD}/target/sprint04-probe-restart.err" |
| 127 | probe1_rc="${PWD}/target/sprint04-probe-restart.rc" |
| 128 | run_probe_background "${probe1_out}" "${probe1_err}" |
| 129 | wait_for_active_request |
| 130 | run_ctl quit >/dev/null |
| 131 | wait "${DAEMON_PID}" 2>/dev/null || true |
| 132 | DAEMON_PID=0 |
| 133 | wait_probe "${PROBE_PID}" "${probe1_rc}" |
| 134 | start_daemon |
| 135 | run_ctl status >/dev/null |
| 136 | run_ctl auth-summary >/dev/null |
| 137 | stop_daemon_graceful |
| 138 | |
| 139 | echo "[2/3] Active prompt + SIGTERM race" |
| 140 | start_daemon |
| 141 | probe2_out="${PWD}/target/sprint04-probe-sigterm.out" |
| 142 | probe2_err="${PWD}/target/sprint04-probe-sigterm.err" |
| 143 | probe2_rc="${PWD}/target/sprint04-probe-sigterm.rc" |
| 144 | run_probe_background "${probe2_out}" "${probe2_err}" |
| 145 | wait_for_active_request |
| 146 | stop_daemon_sigterm |
| 147 | wait_probe "${PROBE_PID}" "${probe2_rc}" |
| 148 | start_daemon |
| 149 | run_ctl status |
| 150 | run_ctl auth-summary |
| 151 | stop_daemon_graceful |
| 152 | |
| 153 | echo "[3/3] Summary" |
| 154 | echo " restart probe rc=$(cat "${probe1_rc}")" |
| 155 | echo " sigterm probe rc=$(cat "${probe2_rc}")" |
| 156 | echo " restart probe stderr: $(tr '\n' ' ' < "${probe1_err}")" |
| 157 | echo " sigterm probe stderr: $(tr '\n' ' ' < "${probe2_err}")" |
| 158 | echo "Validation complete." |