diff --git a/It-screenClient b/It-screenClient deleted file mode 160000 index 21ba434..0000000 --- a/It-screenClient +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 21ba43446b7f7eaca5c777af275ac63f481e8e03 diff --git a/stage_itscreen/itscreen b/stage_itscreen/itscreen index b5e78c7..5739cdb 100644 Binary files a/stage_itscreen/itscreen and b/stage_itscreen/itscreen differ diff --git a/stage_itscreen/stage_itscreen_install.sh b/stage_itscreen/stage_itscreen_install.sh index 1443d3f..4629eb8 100755 --- a/stage_itscreen/stage_itscreen_install.sh +++ b/stage_itscreen/stage_itscreen_install.sh @@ -13,7 +13,9 @@ ITS_SESSION_SCRIPT="/usr/local/bin/itscreen-session" ITS_SESSION_LAUNCH="/usr/local/bin/itscreen-session-launch" ITS_X11_LAUNCHER="/usr/local/bin/itscreen-x11-launch" ITS_X11_CLEANUP="/usr/local/bin/itscreen-x11-cleanup" +ITS_RECOVERY_SCRIPT="/usr/local/bin/itscreen-boot-recovery" ITS_SERVICE="itscreen-x11.service" +ITS_RECOVERY_SERVICE="itscreen-x11-recovery.service" LIGHTDM_SERVICE="lightdm.service" DISPLAY_MANAGER_SERVICE="display-manager.service" NM_WAIT_SERVICE="NetworkManager-wait-online.service" @@ -22,6 +24,7 @@ LIGHTDM_AUTOLOGIN_DROPIN="/etc/lightdm/lightdm.conf.d/90-iomatex-autologin.conf" XSESSION_HOOK="/etc/X11/Xsession.d/99-iomatex-allow-itscreen" XWRAPPER_CONFIG="/etc/X11/Xwrapper.config" ITS_UNIT_FILE="/etc/systemd/system/${ITS_SERVICE}" +ITS_RECOVERY_UNIT_FILE="/etc/systemd/system/${ITS_RECOVERY_SERVICE}" ITS_RUN_DIR="/run/itscreen" LOCK_DIR="/var/lib/itscreen" LOCK_FILE="${LOCK_DIR}/itscreen.lock" @@ -31,7 +34,7 @@ DEVICE_ID_FILE="/userdata/device-id" DEVICE_ID_RE='^g-box-[a-f0-9]{8}$' step_n=0 -TOTAL_STEPS=13 +TOTAL_STEPS=15 log() { local line="[$(date -u +'%F %T UTC')] $*" @@ -635,6 +638,215 @@ exec /usr/bin/xinit /usr/local/bin/itscreen-session -- \ EOF } +install_recovery_assets() { + step "Install recovery helpers" + + write_file "$ITS_RECOVERY_SCRIPT" 0755 root root <<'EOF' +#!/usr/bin/env bash +set -u +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + +RECOVERY_GRACE_SECONDS="${RECOVERY_GRACE_SECONDS:-60}" +RECOVERY_POLL_SECONDS=10 +ITS_SERVICE="itscreen-x11.service" +LIGHTDM_SERVICE="lightdm.service" +STATE_DIR="/var/lib/itscreen" +LOG_DIR="${STATE_DIR}/logs" +FLAG_FILE="${STATE_DIR}/recovery.flag" +RECOVERY_LOG="${LOG_DIR}/recovery.log" +RECOVERY_JOURNAL="${LOG_DIR}/recovery-journal.log" +RECOVERY_XORG="${LOG_DIR}/recovery-xorg.log" +XORG_LOG="/var/log/Xorg.0.log" + +timestamp_utc() { + date -u +'%F %T UTC' +} + +log() { + local line="[$(timestamp_utc)] $*" + echo "$line" + echo "$line" >>"$RECOVERY_LOG" 2>/dev/null || true +} + +service_active_state() { + systemctl is-active "$1" 2>/dev/null || true +} + +service_enabled_state() { + systemctl is-enabled "$1" 2>/dev/null || true +} + +have_drm_status() { + compgen -G '/sys/class/drm/*/status' >/dev/null 2>&1 +} + +hdmi_status_summary() { + local summary="" + if have_drm_status; then + summary="$( + for status_file in /sys/class/drm/*/status; do + [ -e "$status_file" ] || continue + printf '%s=%s;' "$(basename "$(dirname "$status_file")")" "$(tr -d '\n' <"$status_file" 2>/dev/null || true)" + done + )" + summary="${summary%;}" + fi + printf '%s\n' "${summary:-unavailable}" +} + +appears_alive() { + if [ "$(service_active_state "$ITS_SERVICE")" = "active" ]; then + return 0 + fi + if pgrep -x itscreen >/dev/null 2>&1; then + return 0 + fi + if pgrep -x Xorg >/dev/null 2>&1; then + return 0 + fi + if [ -e /tmp/.X11-unix/X0 ]; then + return 0 + fi + return 1 +} + +append_section() { + local title="$1" + shift + { + echo "### ${title}" + "$@" + echo + } >>"$RECOVERY_LOG" 2>&1 || true +} + +append_shell_section() { + local title="$1" + local script="$2" + { + echo "### ${title}" + bash -lc "$script" + echo + } >>"$RECOVERY_LOG" 2>&1 || true +} + +save_diagnostics() { + mkdir -p "$STATE_DIR" "$LOG_DIR" >/dev/null 2>&1 || true + : >>"$RECOVERY_LOG" 2>/dev/null || true + + { + echo "==== recovery invocation $(timestamp_utc) ====" + echo "hostname=$(hostname 2>/dev/null || true)" + echo "hdmi_status=$(hdmi_status_summary)" + echo + } >>"$RECOVERY_LOG" 2>&1 || true + + append_section "date" date + append_section "uname -a" uname -a + append_section "hostname" hostname + append_section "systemctl status itscreen-x11.service --no-pager -l" systemctl status itscreen-x11.service --no-pager -l + append_section "systemctl status lightdm.service --no-pager -l" systemctl status lightdm.service --no-pager -l + append_shell_section "ps aux | grep -E 'Xorg|xinit|openbox|itscreen'" "ps aux | grep -E 'Xorg|xinit|openbox|itscreen' || true" + append_shell_section "pgrep -a itscreen" "pgrep -a itscreen || true" + append_shell_section "ls -la /tmp/.X11-unix /run/itscreen /var/lib/itscreen/logs" "ls -la /tmp/.X11-unix /run/itscreen /var/lib/itscreen/logs || true" + append_shell_section "cat /sys/class/drm/*/status" "if compgen -G '/sys/class/drm/*/status' >/dev/null 2>&1; then cat /sys/class/drm/*/status; else echo unavailable; fi" + + if [ -f "$XORG_LOG" ]; then + tail -n 200 "$XORG_LOG" >"$RECOVERY_XORG" 2>&1 || true + else + printf 'Xorg log not available: %s\n' "$XORG_LOG" >"$RECOVERY_XORG" 2>&1 || true + fi + append_shell_section "tail -n 200 /var/log/Xorg.0.log" "if [ -f '$XORG_LOG' ]; then tail -n 200 '$XORG_LOG'; else echo unavailable; fi" + + { + journalctl -u itscreen-x11.service -b -n 200 --no-pager || true + echo + journalctl -u itscreen-x11-recovery.service -b -n 200 --no-pager || true + } >"$RECOVERY_JOURNAL" 2>&1 || true +} + +write_flag() { + local reason="$1" + local action="$2" + local its_active lightdm_active its_enabled lightdm_enabled + its_active="$(service_active_state "$ITS_SERVICE")" + its_enabled="$(service_enabled_state "$ITS_SERVICE")" + lightdm_active="$(service_active_state "$LIGHTDM_SERVICE")" + lightdm_enabled="$(service_enabled_state "$LIGHTDM_SERVICE")" + + mkdir -p "$STATE_DIR" "$LOG_DIR" >/dev/null 2>&1 || true + cat >"$FLAG_FILE" </dev/null 2>&1 || true + : >>"$RECOVERY_LOG" 2>/dev/null || true + + log "Recovery invoked for ${ITS_SERVICE}; grace=${RECOVERY_GRACE_SECONDS}s poll=${RECOVERY_POLL_SECONDS}s" + + while [ "$elapsed" -lt "$RECOVERY_GRACE_SECONDS" ]; do + if appears_alive; then + reason="startup-delay-or-headless-but-alive" + action="skipped-lightdm-recovery" + log "itscreen-x11 appears alive; skipping LightDM recovery" + save_diagnostics + write_flag "$reason" "$action" + exit 0 + fi + log "itscreen-x11 still not alive after ${elapsed}s; waiting ${RECOVERY_POLL_SECONDS}s before re-check" + sleep "$RECOVERY_POLL_SECONDS" + elapsed=$((elapsed + RECOVERY_POLL_SECONDS)) + done + + if appears_alive; then + reason="became-alive-after-grace" + action="skipped-lightdm-recovery" + log "itscreen-x11 appears alive; skipping LightDM recovery" + save_diagnostics + write_flag "$reason" "$action" + exit 0 + fi + + log "itscreen-x11 still not healthy after grace period; collecting diagnostics and starting LightDM fallback" + save_diagnostics + systemctl stop "$ITS_SERVICE" >/dev/null 2>&1 || true + systemctl reset-failed "$ITS_SERVICE" >/dev/null 2>&1 || true + systemctl start "$LIGHTDM_SERVICE" >/dev/null 2>&1 || true + save_diagnostics + write_flag "$reason" "$action" +} + +main "$@" +EOF + + write_file "$ITS_RECOVERY_UNIT_FILE" 0644 root root <<'EOF' +[Unit] +Description=ITSCREEN boot recovery fallback + +[Service] +Type=oneshot +User=root +ExecStart=/usr/local/bin/itscreen-boot-recovery +StandardOutput=journal+console +StandardError=journal+console +EOF +} + install_systemd_unit() { step "Systemd service (dedicated X11 session)" write_file "$ITS_UNIT_FILE" 0644 root root <<'EOF' @@ -646,6 +858,7 @@ Conflicts=lightdm.service display-manager.service Before=lightdm.service display-manager.service StartLimitIntervalSec=300 StartLimitBurst=3 +OnFailure=itscreen-x11-recovery.service [Service] Type=simple @@ -695,6 +908,117 @@ enable_managed_services() { restart_service_safe "$ITS_SERVICE" } +binary_architecture_report() { + local target="$1" + local machine="$2" + local description="" + + if command -v file >/dev/null 2>&1; then + description="$(file -b "$target" 2>/dev/null || true)" + fi + + if [[ -z "$description" ]]; then + printf 'unknown|unable to inspect binary architecture\n' + return 1 + fi + + case "$machine" in + aarch64|arm64) + if printf '%s\n' "$description" | grep -qiE 'aarch64|arm64'; then + printf 'match|%s\n' "$description" + return 0 + fi + ;; + x86_64|amd64) + if printf '%s\n' "$description" | grep -qiE 'x86-64|x86_64|amd64'; then + printf 'match|%s\n' "$description" + return 0 + fi + ;; + armv7l|armv7*) + if printf '%s\n' "$description" | grep -qiE 'ARM(,| )'; then + printf 'match|%s\n' "$description" + return 0 + fi + ;; + esac + + printf 'mismatch|%s\n' "$description" + return 1 +} + +post_install_self_test() { + local missing_path + local status_output="" + local journal_output="" + local service_active="no" + local arch_machine="" + local arch_report="" + local arch_status="" + local arch_detail="" + + step "Post-install self-test" + + if [[ ! -f "$ITS_BINARY" ]]; then + die "Required binary missing: ${ITS_BINARY}" + fi + if [[ ! -x "$ITS_BINARY" ]]; then + die "Binary is not executable: ${ITS_BINARY}" + fi + if [[ ! -f "$ITS_UNIT_FILE" ]]; then + die "Service file missing: ${ITS_UNIT_FILE}" + fi + + for missing_path in \ + "$ITS_WRAPPER" \ + "$ITS_SESSION_SCRIPT" \ + "$ITS_SESSION_LAUNCH" \ + "$ITS_X11_LAUNCHER" \ + "$ITS_X11_CLEANUP" \ + "$ITS_RECOVERY_SCRIPT"; do + if [[ ! -x "$missing_path" ]]; then + die "Required launcher script missing or not executable: ${missing_path}" + fi + done + + log "Self-test: waiting up to 30s for ${ITS_SERVICE} to become active" + for _ in $(seq 1 30); do + if systemctl is-active --quiet "$ITS_SERVICE"; then + service_active="yes" + break + fi + sleep 1 + done + + arch_machine="$(uname -m 2>/dev/null || true)" + arch_report="$(binary_architecture_report "$ITS_BINARY" "$arch_machine" || true)" + arch_status="${arch_report%%|*}" + arch_detail="${arch_report#*|}" + + log "Self-test: binary exists and is executable: ${ITS_BINARY}" + log "Self-test: service file present: ${ITS_UNIT_FILE}" + log "Self-test: runtime helper scripts present" + log "Self-test: uname -m=${arch_machine:-unknown}" + log "Self-test: binary architecture check=${arch_status:-unknown} detail=${arch_detail:-unknown}" + if [[ "${arch_status:-unknown}" == "mismatch" ]]; then + warn "Self-test: binary architecture does not match uname -m (${arch_machine:-unknown})" + elif [[ "${arch_status:-unknown}" == "unknown" ]]; then + warn "Self-test: unable to confirm binary architecture" + fi + + if [[ "$service_active" == "yes" ]]; then + log "Self-test: ${ITS_SERVICE} is active within grace window" + else + warn "Self-test: ${ITS_SERVICE} is not active after 30s; HDMI/headless startup may still be in progress" + status_output="$(systemctl status "$ITS_SERVICE" --no-pager -l 2>&1 || true)" + journal_output="$(journalctl -u "$ITS_SERVICE" -b -n 100 --no-pager 2>&1 || true)" + log "Self-test diagnostics: systemctl status ${ITS_SERVICE}" + printf '%s\n' "$status_output" >>"$LOG" 2>/dev/null || true + log "Self-test diagnostics: journalctl -u ${ITS_SERVICE} -b -n 100" + printf '%s\n' "$journal_output" >>"$LOG" 2>/dev/null || true + fi +} + print_summary() { echo echo "Stage_3 ready (dedicated X11 appliance mode)." @@ -715,7 +1039,9 @@ print_summary() { echo " ${ITS_SESSION_LAUNCH}" echo " ${ITS_X11_LAUNCHER}" echo " ${ITS_X11_CLEANUP}" + echo " ${ITS_RECOVERY_SCRIPT}" echo " ${ITS_UNIT_FILE}" + echo " ${ITS_RECOVERY_UNIT_FILE}" echo "Services after install:" echo " ${LIGHTDM_SERVICE}: disabled" echo " ${ITS_SERVICE}: enabled" @@ -732,6 +1058,8 @@ print_summary() { echo "Return to appliance mode:" echo " systemctl stop ${LIGHTDM_SERVICE}" echo " systemctl start ${ITS_SERVICE}" + echo "Remote health check:" + echo " sudo bash -lc 'echo \"itscreen-x11 active=\$(systemctl is-active itscreen-x11.service 2>/dev/null || true) enabled=\$(systemctl is-enabled itscreen-x11.service 2>/dev/null || true)\"; echo \"lightdm active=\$(systemctl is-active lightdm.service 2>/dev/null || true) enabled=\$(systemctl is-enabled lightdm.service 2>/dev/null || true)\"; pgrep -a itscreen || true; test -f /var/lib/itscreen/recovery.flag && cat /var/lib/itscreen/recovery.flag || echo \"no recovery flag\"'" } main() { @@ -752,8 +1080,10 @@ main() { install_sudoers install_wrapper install_x11_session_scripts + install_recovery_assets install_systemd_unit enable_managed_services + post_install_self_test print_summary }