Unverified Commit 8ec7d30b authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Refactor shutdown logic

parent 77c164a3
Loading
Loading
Loading
Loading
+71 −112
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

: "${QEMU_TIMEOUT:="110"}"  # QEMU Termination timeout
: "${SHUTDOWN:="Y"}"        # Graceful ACPI shutdown
: "${TIMEOUT:="110"}"       # QEMU termination timeout

# Configure QEMU for graceful shutdown

QEMU_TERM=""
QEMU_PTY="$QEMU_DIR/qemu.pty"
QEMU_LOG="$QEMU_DIR/qemu.log"
QEMU_OUT="$QEMU_DIR/qemu.out"
QEMU_END="$QEMU_DIR/qemu.end"

_trap() {
  local func="$1"; shift
  local sig
  for sig; do
    trap "$func $sig" "$sig"
  done
}

app() {
  echo "$APP" && return 0
}

boot() {

  [ -f "$QEMU_END" ] && return 0
@@ -68,39 +71,23 @@ ready() {

finish() {

  local pid
  local cnt=0
  local i=0
  local pid=""
  local reason=$1
  local pids=(
        "/var/run/tpm.pid"
        "/var/run/wsdd.pid"
        "/var/run/samba/nmbd.pid"
        "/var/run/samba/smbd.pid"
      )
  local app="$(app)"
  local pids=( "$SMB_PID" "$NMB_PID" "$DDN_PID" "$TPM_PID" \
               "$WSD_PID" "$WEB_PID" "$PASST_PID" "$DNSMASQ_PID" )

  touch "$QEMU_END"
  (( reason != 0 )) && (( reason != 143 )) && echo "QEMU exitcode: $reason"

  if [ -s "$QEMU_PID" ]; then

    pid=$(<"$QEMU_PID")
    echo && error "Forcefully terminating Windows, reason: $reason..."
    { kill -15 "$pid" || true; } 2>/dev/null

    while isAlive "$pid"; do

      sleep 1
      (( cnt++ ))

      # Workaround for zombie pid
      [ ! -s "$QEMU_PID" ] && break

      if [ "$cnt" -eq 5 ]; then
        echo && error "QEMU did not terminate itself, forcefully killing process..."
        { kill -9 "$pid" || true; } 2>/dev/null
    if read -r pid <"$QEMU_PID"; then
      if [ -n "$pid" ] && isAlive "$pid"; then
        echo && error "Forcefully terminating $app, reason..."
        { kill -9 -- "$pid" || :; } 2>/dev/null
      fi
    fi

    done

  fi

  if [ ! -f "$STORAGE/windows.boot" ] && [ -f "$BOOT" ]; then
@@ -115,130 +102,102 @@ finish() {
    fi
  fi

  for pid in "${pids[@]}"; do
      if [[ -s "$pid" ]]; then 
          pKill "$(<"$pid")"
      fi
      rm -f "$pid"
  done 

  mKill "${pids[@]}"
  closeNetwork
  
  sleep 0.5
  echo "❯ Shutdown completed!"

  exit "$reason"
}

terminal() {

  local dev=""

  if [ -s "$QEMU_OUT" ]; then

    local msg
    msg=$(<"$QEMU_OUT")

    if [ -n "$msg" ]; then

      if [[ "${msg,,}" != "char"* ||  "$msg" != *"serial0)" ]]; then
        echo "$msg"
      fi

      dev="${msg#*/dev/p}"
      dev="/dev/p${dev%% *}"

    fi
  fi

  if [ ! -c "$dev" ]; then
    dev=$(echo 'info chardev' | nc -q 1 -w 1 localhost "$MON_PORT" | tr -d '\000')
    dev="${dev#*serial0}"
    dev="${dev#*pty:}"
    dev="${dev%%$'\n'*}"
    dev="${dev%%$'\r'*}"
  if [ -n "$pid" ] && ! waitPid "$pid" 100; then
    warn "Timed out while waiting for $app to exit!"
  fi

  if [ ! -c "$dev" ]; then
    error "Device '$dev' not found!"
    finish 34 && return 34
  fi
  echo && echo "❯ Shutdown completed!"

  QEMU_TERM="$dev"
  return 0
  exit "$reason"
}

_graceful_shutdown() {

  local sig="$1"
  local pid=""
  local code=0

  case "$sig" in
    SIGTERM) code=143 ;;
    SIGINT)  code=130 ;;
    SIGHUP)  code=129 ;;
    SIGABRT) code=134 ;;
    SIGINT)  code=130 ;;
    SIGQUIT) code=131 ;;
    SIGABRT) code=134 ;;
    SIGTERM) code=143 ;;
  esac

  if [ -f "$QEMU_END" ]; then
    info "Received $1 while already shutting down..."
    echo && info "Received $1 while already shutting down..."
    return
  fi

  set +e
  touch "$QEMU_END"
  info "Received $1, sending ACPI shutdown signal..."

  if [ ! -s "$QEMU_PID" ]; then
    error "QEMU PID file does not exist?"
    finish "$code" && return "$code"
  fi
  echo && info "Received $1, sending ACPI shutdown signal..."

  local pid=""
  pid=$(<"$QEMU_PID")
  local app="$(app)"

  if [ ! -s "$QEMU_PID" ] || ! read -r pid <"$QEMU_PID"; then
    warn "QEMU PID file ($QEMU_PID) does not exist?"
    finish "$code"
  fi
  
  if ! isAlive "$pid"; then
    error "QEMU process does not exist?"
    finish "$code" && return "$code"
  if [ -z "$pid" ] || ! isAlive "$pid"; then
    warn "QEMU process with PID $pid does not exist?"
    finish "$code"
  fi

  if ! ready; then
    info "Cannot send ACPI signal during Windows setup, aborting..."
    finish "$code" && return "$code"
    finish "$code"
  fi

  # Send ACPI shutdown signal
  echo 'system_powerdown' | nc -q 1 -w 1 localhost "$MON_PORT" > /dev/null
  local cnt=0 abort=0 factor=2 offset=3 min max

  local cnt=0
  while [ "$cnt" -lt "$QEMU_TIMEOUT" ]; do
  [[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT=13
  [ "$TIMEOUT" -ge 15 ] && factor=3 && offset=4
  [ "$TIMEOUT" -ge 30 ] && factor=4 && offset=5
  min=$((factor + offset + 1))
  [ "$TIMEOUT" -lt "$min" ] && TIMEOUT="$min"
  max=$(( TIMEOUT - offset ))
  abort=$(( max - factor ))

    sleep 1
    (( cnt++ ))
  while [ "$cnt" -le "$max" ]; do

    sleep 1 &
    local slp=$!

    ! isAlive "$pid" && break
    # Workaround for zombie pid
    [ ! -s "$QEMU_PID" ] && break

    info "Waiting for Windows to shutdown... ($cnt/$QEMU_TIMEOUT)"
    if [ "$cnt" -ne "$abort" ]; then
      if [ "$cnt" -gt 0 ]; then
        info "Waiting for $app to shut down... ($cnt/$max)"
      fi
    else
      info "$app is still running, sending SIGTERM... ($cnt/$max)"
      { kill -15 -- "$pid" || true; } 2>/dev/null
    fi

    # Send ACPI shutdown signal
    echo 'system_powerdown' | nc -q 1 -w 1 localhost "$MON_PORT" > /dev/null
    if [ -S "$QEMU_DIR/monitor.sock" ]; then
      nc -q 1 -w 1 -U "$QEMU_DIR/monitor.sock" > /dev/null <<<'system_powerdown' || :
    fi

  done
    wait $slp
    (( cnt++ ))

  if [ "$cnt" -ge "$QEMU_TIMEOUT" ]; then
    error "Shutdown timeout reached, aborting..."
  fi
  done

  finish "$code" && return "$code"
  finish "$code"
}

touch "$QEMU_LOG"

SERIAL="pty"
MONITOR="telnet:localhost:$MON_PORT,server,nowait,nodelay -daemonize -D $QEMU_LOG"
[[ "$SHUTDOWN" != [Yy1]* ]] && return 0
[ -n "${QEMU_TIMEOUT:-}" ] && TIMEOUT="$QEMU_TIMEOUT"

_trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT