Unverified Commit 118d9a56 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

fix: Disable traps in subshell (#1094)

parent 741704be
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ rm -f "$BALLOONING_PID"
# may be killed by the OOM killer if the ballooning driver cannot reclaim memory from the guest fast enough.

balloon() {
  trap - SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT

  # Wait for qemu PID file to be created
  while [ ! -f "$QEMU_PID" ]; do
+11 −11
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ finish() {
          143 ) display="SIGTERM" ;;
        esac
        echo && error "Forcefully terminating $(app), reason: $display..."
        { kill -9 -- "$pid" && wait $! || :; } 2>/dev/null
        { kill -9 -- "$pid" || :; } 2>/dev/null
      fi
    fi
  fi
@@ -59,7 +59,6 @@ finish() {
  fi

  echo && echo "❯ Shutdown completed!"

  exit "$reason"
}

@@ -96,15 +95,16 @@ _graceful_shutdown() {
    finish "$code"
  fi

  local cnt=0 abort=0 factor=2 offset=3 min max
  local cnt=0 abort=0 factor=3 offset=3 min max name

  [[ "$TIMEOUT" =~ ^[0-9]+$ ]] || TIMEOUT=13
  [ "$TIMEOUT" -ge 15 ] && factor=3 && offset=4
  [ "$TIMEOUT" -ge 30 ] && factor=4 && offset=5
  [ "$TIMEOUT" -ge 15 ] && factor=4 && offset=4
  [ "$TIMEOUT" -ge 30 ] && factor=5 && offset=5
  min=$((factor + offset + 1))
  [ "$TIMEOUT" -lt "$min" ] && TIMEOUT="$min"
  max=$(( TIMEOUT - offset ))
  abort=$(( max - factor ))
  name="$(app)"

  while [ "$cnt" -le "$max" ]; do

@@ -117,11 +117,11 @@ _graceful_shutdown() {

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

    # Send ACPI shutdown signal
+23 −22
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ waitPid() {
pKill() {
  local pid="$1"

  { kill -15 -- "$pid" && wait $! || :; } 2>/dev/null
  { kill -15 -- "$pid" || :; } 2>/dev/null

  if ! waitPid "$pid" 50; then
    warn "Timed out while waiting for PID $pid"
@@ -89,36 +89,37 @@ fKill() {
  return 0
}

mKill() {
  local pid="" files=("$@")
sKill() {
  local file="$1" pid=""

  for file in "${files[@]}"; do

    [ ! -s "$file" ] && continue
  [ ! -s "$file" ] && return 0
  ! read -r pid <"$file" && return 0

    if read -r pid <"$file"; then
      if [ -n "$pid" ]; then
        { kill -15 -- "$pid" && wait $! || :; } 2>/dev/null
      fi
  if [ -n "$pid" ] && isAlive "$pid"; then
    { kill -15 -- "$pid" || :; } 2>/dev/null
  fi

  return 0
}

mKill() {
  local pid="" files=("$@")

  for file in "${files[@]}"; do
    sKill "$file"
  done

  for file in "${files[@]}"; do

    [ -z "$file" ] && continue
    [ ! -s "$file" ] && continue
    ! read -r pid <"$file" && continue
    [ -z "$pid" ] && continue

    if [ -s "$file" ]; then
      if read -r pid <"$file"; then
        if [ -n "$pid" ]; then
    if waitPid "$pid" 50; then
      rm -f -- "$file"
    else
      warn "Timed out while waiting for PID file: $file"
    fi
        fi
      fi
    fi

  done