qemu_guest_hthd.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env bash
  2. # Runs inside the Debian systemd VM (PID 1 = systemd). Exit 0 only on full matrix.
  3. set -euo pipefail
  4. HOST="${HTH_HOST_MNT:-/mnt/hth}"
  5. export PATH="/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  6. log() { echo "HTH_VM: $*"; echo "HTH_VM: $*" >/dev/console; }
  7. emit_file() {
  8. local dest="$1" src="$2"
  9. echo "HTH_GUEST_FILE_BEGIN ${dest}"
  10. cat "${src}"
  11. echo "HTH_GUEST_FILE_END ${dest}"
  12. }
  13. if [[ "$(cat /proc/1/comm)" != systemd ]]; then
  14. log "FAIL PID1=$(cat /proc/1/comm) want systemd"
  15. exit 1
  16. fi
  17. log "PID1=systemd ok"
  18. "${HOST}/deploy/install.sh"
  19. getent group dialout >/dev/null || groupadd --system dialout
  20. systemd-sysusers /usr/lib/sysusers.d/hth.conf
  21. usermod -aG dialout,hth-observe,hth-control hth
  22. systemd-tmpfiles --create /usr/lib/tmpfiles.d/hth.conf
  23. # Mock config with nested sockets matching tmpfiles.
  24. install -d -m 0750 -o hth -g hth /etc/hth /etc/hth/profiles /var/lib/hth
  25. cat >/etc/hth/hthd.yaml <<'EOF'
  26. api_version: hth.api.v1
  27. device_id: prod2u-0
  28. backend: mock
  29. profile: /etc/hth/profiles/prod2u-door-temp/profile.json
  30. runtime_root: /run/hth
  31. sockets:
  32. observe: /run/hth/observe/observe.sock
  33. control: /run/hth/control/control.sock
  34. observe_perm: "0660"
  35. control_perm: "0660"
  36. observe_group: hth-observe
  37. control_group: hth-control
  38. poll:
  39. temperature_ms: 200
  40. motor_status_ms: 80
  41. watchdog:
  42. heartbeat_period_ms: 1000
  43. lease:
  44. min_ttl_ms: 50
  45. max_ttl_ms: 5000
  46. safety:
  47. fan: 10
  48. ptc: 0
  49. door_close_percent: 0
  50. ring_buffer_size: 32
  51. subscriber_buffer_size: 4
  52. grpc:
  53. reflection: false
  54. mock:
  55. seed: 1
  56. startup_complete: true
  57. EOF
  58. chown -R root:hth /etc/hth
  59. chmod 0750 /etc/hth /etc/hth/profiles /etc/hth/profiles/prod2u-door-temp
  60. chmod 0640 /etc/hth/hthd.yaml /etc/hth/profiles/prod2u-door-temp/profile.json
  61. # Service user must not be able to modify config/unit/profile.
  62. if su -s /bin/bash -c 'echo x >/etc/hth/hthd.yaml' hth 2>/dev/null; then
  63. log "FAIL hth user could write /etc/hth/hthd.yaml"
  64. exit 1
  65. fi
  66. if su -s /bin/bash -c 'echo x >/etc/systemd/system/hthd.service' hth 2>/dev/null; then
  67. log "FAIL hth user could write unit"
  68. exit 1
  69. fi
  70. hthd --check-config --config /etc/hth/hthd.yaml
  71. systemctl daemon-reload
  72. if ! systemctl enable --now hthd.service; then
  73. log "hthd.service failed; dumping journal"
  74. journalctl -u hthd -n 80 --no-pager
  75. systemctl status hthd.service --no-pager
  76. exit 1
  77. fi
  78. for i in $(seq 1 50); do
  79. systemctl is-active hthd.service && break
  80. sleep 0.2
  81. done
  82. systemctl is-active hthd.service
  83. test -S /run/hth/observe/observe.sock
  84. test -S /run/hth/control/control.sock
  85. # Type=notify became active only after READY=1. WatchdogSec pets keep it alive.
  86. sleep 8
  87. systemctl is-active hthd.service
  88. WATCHDOG_USEC="$(systemctl show -p WatchdogUSec --value hthd.service)"
  89. log "WatchdogUSec=${WATCHDOG_USEC}"
  90. 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)
  91. "${HTHCTL[@]}" snapshot | tee /tmp/hth-snapshot.json
  92. grep -q prod2u-0 /tmp/hth-snapshot.json
  93. "${HTHCTL[@]}" capabilities | tee /tmp/hth-caps.json
  94. grep -q recommended_rpc_deadline_ms /tmp/hth-caps.json || grep -q recommendedRpcDeadlineMs /tmp/hth-caps.json
  95. "${HTHCTL[@]}" health | tee /tmp/hth-health.txt
  96. # Client group must not unlink/replace sockets.
  97. if ! getent passwd hth-obs-client >/dev/null; then
  98. useradd --system --no-create-home -g hth-observe hth-obs-client
  99. fi
  100. if ! getent passwd hth-ctl-client >/dev/null; then
  101. useradd --system --no-create-home -g hth-control hth-ctl-client
  102. fi
  103. if ! getent passwd hth-nobody >/dev/null; then
  104. useradd --system --no-create-home hth-nobody
  105. fi
  106. if su -s /bin/bash -c 'rm -f /run/hth/observe/observe.sock' hth-obs-client 2>/dev/null; then
  107. log "FAIL observe client unlinked socket"
  108. exit 1
  109. fi
  110. if su -s /bin/bash -c 'rm -f /run/hth/control/control.sock' hth-ctl-client 2>/dev/null; then
  111. log "FAIL control client unlinked socket"
  112. exit 1
  113. fi
  114. 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
  115. log "FAIL untrusted user read observe snapshot"
  116. exit 1
  117. fi
  118. # Observe-group client may snapshot; control-group client must not open observe sock.
  119. 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
  120. log "FAIL observe-group client could not snapshot"
  121. cat /tmp/hth-obs-client-snap.err
  122. exit 1
  123. fi
  124. 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
  125. log "FAIL control-group client opened observe socket"
  126. exit 1
  127. fi
  128. # No TCP listen owned by hthd (inode-matched).
  129. HTHD_PID="$(systemctl show -p MainPID --value hthd.service)"
  130. export HTHD_PID
  131. python3 - <<'PY'
  132. import os, sys
  133. pid = int(os.environ["HTHD_PID"])
  134. owned = set()
  135. fd = f"/proc/{pid}/fd"
  136. for name in os.listdir(fd):
  137. try:
  138. t = os.readlink(f"{fd}/{name}")
  139. except OSError:
  140. continue
  141. if t.startswith("socket:[") and t.endswith("]"):
  142. owned.add(t[8:-1])
  143. for fam in ("tcp", "tcp6"):
  144. path = f"/proc/{pid}/net/{fam}"
  145. try:
  146. lines = open(path).read().splitlines()[1:]
  147. except OSError:
  148. continue
  149. for line in lines:
  150. fields = line.split()
  151. if len(fields) < 10:
  152. continue
  153. if fields[3] != "0A":
  154. continue
  155. if fields[9] in owned:
  156. print("FAIL tcp listen", fam, fields[1], file=sys.stderr)
  157. sys.exit(1)
  158. print("no_tcp_listen_ok")
  159. PY
  160. echo "no_tcp_listen_ok" > /tmp/no-tcp.log
  161. journalctl -u hthd --no-pager | tee /tmp/hth-journal.txt | tee /tmp/journalctl-hthd.log >/dev/null
  162. systemctl show hthd --no-pager | tee /tmp/systemctl-show.log
  163. systemctl status hthd --no-pager | tee /tmp/systemctl-status.log
  164. { 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
  165. { 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
  166. # sd_notify READY=1 is consumed by systemd; it is not required as a journal
  167. # substring. Type=notify + WatchdogUSec already prove the notify path.
  168. { grep -E 'READY=1|WATCHDOG=1|NOTIFY_SOCKET|sd_notify|WATCHDOG|hthd listening' /tmp/hth-journal.txt || true; } | tee /tmp/sdnotify.log
  169. { grep -E 'Watchdog|WATCHDOG' /tmp/systemctl-show.log /tmp/hth-journal.txt || true; } | tee /tmp/watchdog.log
  170. grep -q 'Type=notify' /tmp/systemctl-show.log
  171. grep -q 'WatchdogUSec=30s' /tmp/systemctl-show.log
  172. emit_file journalctl-hthd.log /tmp/journalctl-hthd.log
  173. emit_file systemctl-show.log /tmp/systemctl-show.log
  174. emit_file systemctl-status.log /tmp/systemctl-status.log
  175. emit_file socket-stat.log /tmp/socket-stat.log
  176. emit_file user-group-permission.log /tmp/user-group-permission.log
  177. emit_file sdnotify.log /tmp/sdnotify.log
  178. emit_file watchdog.log /tmp/watchdog.log
  179. emit_file no-tcp.log /tmp/no-tcp.log
  180. systemctl kill -s SIGTERM hthd.service || systemctl stop hthd.service
  181. sleep 1
  182. # TimeoutStopSec=15; process must be gone.
  183. for i in $(seq 1 20); do
  184. systemctl is-active hthd.service >/dev/null 2>&1 || break
  185. sleep 0.5
  186. done
  187. if systemctl is-active hthd.service >/dev/null 2>&1; then
  188. log "FAIL still active after SIGTERM"
  189. exit 1
  190. fi
  191. # sd_notify STOPPING=1 is consumed by systemd and often never appears in the
  192. # unit journal. After SIGTERM, Type=notify stop evidence is the unit leaving
  193. # active plus systemd's SIGTERM / Deactivated lines.
  194. journalctl -u hthd --no-pager | tee /tmp/hth-journal-after-term.txt | tee -a /tmp/hth-journal.txt >/dev/null
  195. if ! grep -qE 'STOPPING|Deactivated successfully|Sent signal SIGTERM' /tmp/hth-journal-after-term.txt; then
  196. log "FAIL no SIGTERM/stop evidence in journal"
  197. cat /tmp/hth-journal-after-term.txt
  198. exit 1
  199. fi
  200. systemctl start hthd.service
  201. sleep 1
  202. systemctl restart hthd.service
  203. sleep 1
  204. systemctl is-active hthd.service
  205. {
  206. echo "after_restart"
  207. systemctl is-active hthd.service
  208. systemctl show hthd -p NRestarts -p MainPID --no-pager
  209. } | tee /tmp/restart.log
  210. journalctl -u hthd --no-pager | tee /tmp/sigterm.log >/dev/null
  211. emit_file restart.log /tmp/restart.log
  212. emit_file sigterm.log /tmp/sigterm.log
  213. "${HOST}/deploy/uninstall.sh"
  214. if [[ -x /usr/local/bin/hthd ]]; then
  215. log "FAIL binary still present after uninstall"
  216. exit 1
  217. fi
  218. {
  219. echo "uninstalled"
  220. command -v hthd || true
  221. test ! -x /usr/local/bin/hthd
  222. } | tee /tmp/install-uninstall.log
  223. emit_file install-uninstall.log /tmp/install-uninstall.log
  224. log "SYSTEMD_VM_MATRIX_PASS"
  225. echo "HTH_SYSTEMD_VM_PASS"
  226. echo "HTH_SYSTEMD_VM_PASS" >/dev/console
  227. exit 0