| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #!/usr/bin/env bash
- # Runs inside the Debian systemd VM (PID 1 = systemd). Exit 0 only on full matrix.
- set -euo pipefail
- HOST="${HTH_HOST_MNT:-/mnt/hth}"
- export PATH="/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- log() { echo "HTH_VM: $*"; echo "HTH_VM: $*" >/dev/console; }
- emit_file() {
- local dest="$1" src="$2"
- echo "HTH_GUEST_FILE_BEGIN ${dest}"
- cat "${src}"
- echo "HTH_GUEST_FILE_END ${dest}"
- }
- if [[ "$(cat /proc/1/comm)" != systemd ]]; then
- log "FAIL PID1=$(cat /proc/1/comm) want systemd"
- exit 1
- fi
- log "PID1=systemd ok"
- "${HOST}/deploy/install.sh"
- getent group dialout >/dev/null || groupadd --system dialout
- systemd-sysusers /usr/lib/sysusers.d/hth.conf
- usermod -aG dialout,hth-observe,hth-control hth
- systemd-tmpfiles --create /usr/lib/tmpfiles.d/hth.conf
- # Mock config with nested sockets matching tmpfiles.
- install -d -m 0750 -o hth -g hth /etc/hth /etc/hth/profiles /var/lib/hth
- cat >/etc/hth/hthd.yaml <<'EOF'
- api_version: hth.api.v1
- device_id: prod2u-0
- backend: mock
- profile: /etc/hth/profiles/prod2u-door-temp/profile.json
- runtime_root: /run/hth
- sockets:
- observe: /run/hth/observe/observe.sock
- control: /run/hth/control/control.sock
- observe_perm: "0660"
- control_perm: "0660"
- observe_group: hth-observe
- control_group: hth-control
- poll:
- temperature_ms: 200
- motor_status_ms: 80
- watchdog:
- heartbeat_period_ms: 1000
- lease:
- min_ttl_ms: 50
- max_ttl_ms: 5000
- safety:
- fan: 10
- ptc: 0
- door_close_percent: 0
- ring_buffer_size: 32
- subscriber_buffer_size: 4
- grpc:
- reflection: false
- mock:
- seed: 1
- startup_complete: true
- EOF
- chown -R root:hth /etc/hth
- chmod 0750 /etc/hth /etc/hth/profiles /etc/hth/profiles/prod2u-door-temp
- chmod 0640 /etc/hth/hthd.yaml /etc/hth/profiles/prod2u-door-temp/profile.json
- # Service user must not be able to modify config/unit/profile.
- if su -s /bin/bash -c 'echo x >/etc/hth/hthd.yaml' hth 2>/dev/null; then
- log "FAIL hth user could write /etc/hth/hthd.yaml"
- exit 1
- fi
- if su -s /bin/bash -c 'echo x >/etc/systemd/system/hthd.service' hth 2>/dev/null; then
- log "FAIL hth user could write unit"
- exit 1
- fi
- hthd --check-config --config /etc/hth/hthd.yaml
- systemctl daemon-reload
- if ! systemctl enable --now hthd.service; then
- log "hthd.service failed; dumping journal"
- journalctl -u hthd -n 80 --no-pager
- systemctl status hthd.service --no-pager
- exit 1
- fi
- for i in $(seq 1 50); do
- systemctl is-active hthd.service && break
- sleep 0.2
- done
- systemctl is-active hthd.service
- test -S /run/hth/observe/observe.sock
- test -S /run/hth/control/control.sock
- # Type=notify became active only after READY=1. WatchdogSec pets keep it alive.
- sleep 8
- systemctl is-active hthd.service
- WATCHDOG_USEC="$(systemctl show -p WatchdogUSec --value hthd.service)"
- log "WatchdogUSec=${WATCHDOG_USEC}"
- HTHCTL=(/usr/local/bin/hthctl --observe-sock /run/hth/observe/observe.sock --control-sock /run/hth/control/control.sock --i-understand-this-is-real-hardware --timeout 8s)
- "${HTHCTL[@]}" snapshot | tee /tmp/hth-snapshot.json
- grep -q prod2u-0 /tmp/hth-snapshot.json
- "${HTHCTL[@]}" capabilities | tee /tmp/hth-caps.json
- grep -q recommended_rpc_deadline_ms /tmp/hth-caps.json || grep -q recommendedRpcDeadlineMs /tmp/hth-caps.json
- "${HTHCTL[@]}" health | tee /tmp/hth-health.txt
- # Client group must not unlink/replace sockets.
- if ! getent passwd hth-obs-client >/dev/null; then
- useradd --system --no-create-home -g hth-observe hth-obs-client
- fi
- if ! getent passwd hth-ctl-client >/dev/null; then
- useradd --system --no-create-home -g hth-control hth-ctl-client
- fi
- if ! getent passwd hth-nobody >/dev/null; then
- useradd --system --no-create-home hth-nobody
- fi
- if su -s /bin/bash -c 'rm -f /run/hth/observe/observe.sock' hth-obs-client 2>/dev/null; then
- log "FAIL observe client unlinked socket"
- exit 1
- fi
- if su -s /bin/bash -c 'rm -f /run/hth/control/control.sock' hth-ctl-client 2>/dev/null; then
- log "FAIL control client unlinked socket"
- exit 1
- fi
- if su -s /bin/bash -c '/usr/local/bin/hthctl --observe-sock /run/hth/observe/observe.sock --i-understand-this-is-real-hardware --timeout 8s snapshot' hth-nobody >/tmp/hth-nobody-snap.txt 2>/dev/null; then
- log "FAIL untrusted user read observe snapshot"
- exit 1
- fi
- # Observe-group client may snapshot; control-group client must not open observe sock.
- if ! su -s /bin/bash -c '/usr/local/bin/hthctl --observe-sock /run/hth/observe/observe.sock --i-understand-this-is-real-hardware --timeout 8s snapshot' hth-obs-client >/tmp/hth-obs-client-snap.txt 2>/tmp/hth-obs-client-snap.err; then
- log "FAIL observe-group client could not snapshot"
- cat /tmp/hth-obs-client-snap.err
- exit 1
- fi
- if su -s /bin/bash -c '/usr/local/bin/hthctl --observe-sock /run/hth/observe/observe.sock --i-understand-this-is-real-hardware --timeout 8s snapshot' hth-ctl-client >/tmp/hth-ctl-obs.txt 2>/dev/null; then
- log "FAIL control-group client opened observe socket"
- exit 1
- fi
- # No TCP listen owned by hthd (inode-matched).
- HTHD_PID="$(systemctl show -p MainPID --value hthd.service)"
- export HTHD_PID
- python3 - <<'PY'
- import os, sys
- pid = int(os.environ["HTHD_PID"])
- owned = set()
- fd = f"/proc/{pid}/fd"
- for name in os.listdir(fd):
- try:
- t = os.readlink(f"{fd}/{name}")
- except OSError:
- continue
- if t.startswith("socket:[") and t.endswith("]"):
- owned.add(t[8:-1])
- for fam in ("tcp", "tcp6"):
- path = f"/proc/{pid}/net/{fam}"
- try:
- lines = open(path).read().splitlines()[1:]
- except OSError:
- continue
- for line in lines:
- fields = line.split()
- if len(fields) < 10:
- continue
- if fields[3] != "0A":
- continue
- if fields[9] in owned:
- print("FAIL tcp listen", fam, fields[1], file=sys.stderr)
- sys.exit(1)
- print("no_tcp_listen_ok")
- PY
- echo "no_tcp_listen_ok" > /tmp/no-tcp.log
- journalctl -u hthd --no-pager | tee /tmp/hth-journal.txt | tee /tmp/journalctl-hthd.log >/dev/null
- systemctl show hthd --no-pager | tee /tmp/systemctl-show.log
- systemctl status hthd --no-pager | tee /tmp/systemctl-status.log
- { stat /run/hth/observe/observe.sock; stat /run/hth/control/control.sock; ls -l /run/hth/observe /run/hth/control; } | tee /tmp/socket-stat.log
- { getent passwd hth hth-obs-client hth-ctl-client hth-nobody; getent group hth hth-observe hth-control dialout; ls -ld /run/hth /run/hth/observe /run/hth/control; } | tee /tmp/user-group-permission.log
- # sd_notify READY=1 is consumed by systemd; it is not required as a journal
- # substring. Type=notify + WatchdogUSec already prove the notify path.
- { grep -E 'READY=1|WATCHDOG=1|NOTIFY_SOCKET|sd_notify|WATCHDOG|hthd listening' /tmp/hth-journal.txt || true; } | tee /tmp/sdnotify.log
- { grep -E 'Watchdog|WATCHDOG' /tmp/systemctl-show.log /tmp/hth-journal.txt || true; } | tee /tmp/watchdog.log
- grep -q 'Type=notify' /tmp/systemctl-show.log
- grep -q 'WatchdogUSec=30s' /tmp/systemctl-show.log
- emit_file journalctl-hthd.log /tmp/journalctl-hthd.log
- emit_file systemctl-show.log /tmp/systemctl-show.log
- emit_file systemctl-status.log /tmp/systemctl-status.log
- emit_file socket-stat.log /tmp/socket-stat.log
- emit_file user-group-permission.log /tmp/user-group-permission.log
- emit_file sdnotify.log /tmp/sdnotify.log
- emit_file watchdog.log /tmp/watchdog.log
- emit_file no-tcp.log /tmp/no-tcp.log
- systemctl kill -s SIGTERM hthd.service || systemctl stop hthd.service
- sleep 1
- # TimeoutStopSec=15; process must be gone.
- for i in $(seq 1 20); do
- systemctl is-active hthd.service >/dev/null 2>&1 || break
- sleep 0.5
- done
- if systemctl is-active hthd.service >/dev/null 2>&1; then
- log "FAIL still active after SIGTERM"
- exit 1
- fi
- # sd_notify STOPPING=1 is consumed by systemd and often never appears in the
- # unit journal. After SIGTERM, Type=notify stop evidence is the unit leaving
- # active plus systemd's SIGTERM / Deactivated lines.
- journalctl -u hthd --no-pager | tee /tmp/hth-journal-after-term.txt | tee -a /tmp/hth-journal.txt >/dev/null
- if ! grep -qE 'STOPPING|Deactivated successfully|Sent signal SIGTERM' /tmp/hth-journal-after-term.txt; then
- log "FAIL no SIGTERM/stop evidence in journal"
- cat /tmp/hth-journal-after-term.txt
- exit 1
- fi
- systemctl start hthd.service
- sleep 1
- systemctl restart hthd.service
- sleep 1
- systemctl is-active hthd.service
- {
- echo "after_restart"
- systemctl is-active hthd.service
- systemctl show hthd -p NRestarts -p MainPID --no-pager
- } | tee /tmp/restart.log
- journalctl -u hthd --no-pager | tee /tmp/sigterm.log >/dev/null
- emit_file restart.log /tmp/restart.log
- emit_file sigterm.log /tmp/sigterm.log
- "${HOST}/deploy/uninstall.sh"
- if [[ -x /usr/local/bin/hthd ]]; then
- log "FAIL binary still present after uninstall"
- exit 1
- fi
- {
- echo "uninstalled"
- command -v hthd || true
- test ! -x /usr/local/bin/hthd
- } | tee /tmp/install-uninstall.log
- emit_file install-uninstall.log /tmp/install-uninstall.log
- log "SYSTEMD_VM_MATRIX_PASS"
- echo "HTH_SYSTEMD_VM_PASS"
- echo "HTH_SYSTEMD_VM_PASS" >/dev/console
- exit 0
|