diff --git a/src/network.sh b/src/network.sh index 5c103bc..151ddc9 100644 --- a/src/network.sh +++ b/src/network.sh @@ -449,6 +449,7 @@ configureDNS() { local gateway="$6" local upstream="${7:-}" local arguments="$DNSMASQ_OPTS" + local pid if ! echo "$gateway" > /run/shm/qemu.gw; then error "Failed to write gateway file." @@ -458,7 +459,10 @@ configureDNS() { enabled "${DNSMASQ_DISABLE:-}" && return 0 enabled "$DEBUG" && echo "Starting dnsmasq daemon..." - [ -s "$DNSMASQ_PID" ] && pKill "$(<"$DNSMASQ_PID")" + if readPidFile pid "$DNSMASQ_PID"; then + pKill "$pid" + fi + rm -f "$DNSMASQ_PID" if isNAT; then diff --git a/src/power.sh b/src/power.sh index 78dcd61..986329d 100644 --- a/src/power.sh +++ b/src/power.sh @@ -69,9 +69,11 @@ readQemuPid() { local -n _pid="$1" local file + local pid for file in "$QEMU_START_PID" "$QEMU_PID"; do - if [ -s "$file" ] && read -r _pid < "$file"; then + if readPidFile pid "$file"; then + _pid="$pid" return 0 fi done diff --git a/src/serial.sh b/src/serial.sh index 7ea3ddc..3eb6260 100644 --- a/src/serial.sh +++ b/src/serial.sh @@ -73,7 +73,7 @@ startHostBinary() { pid=$! fi - echo "$pid" > "$HOST_PID" + printf '%s\n' "$pid" > "$HOST_PID" return 0 } @@ -86,7 +86,7 @@ waitForSocket() { while [ ! -S "$socket" ]; do - if ! read -r pid < "$HOST_PID" || ! isAlive "$pid"; then + if ! readPidFile pid "$HOST_PID" || ! isAlive "$pid"; then error "qemu-host exited unexpectedly!" exit "$exit_code" fi diff --git a/src/server.sh b/src/server.sh index f881e0e..98a404f 100644 --- a/src/server.sh +++ b/src/server.sh @@ -64,7 +64,7 @@ stopWebServer() { local pid - if [ -s "$WEB_PID" ] && read -r pid < "$WEB_PID" && [ -n "$pid" ]; then + if readPidFile pid "$WEB_PID"; then pKill "$pid" 2 if isAlive "$pid"; then @@ -88,7 +88,7 @@ stopWebsocketServer() { local pid - if [ -s "$WSD_PID" ] && read -r pid < "$WSD_PID" && [ -n "$pid" ]; then + if readPidFile pid "$WSD_PID"; then pKill "$pid" 2 if isAlive "$pid"; then diff --git a/src/utils.sh b/src/utils.sh index 3e6401c..7c82b62 100644 --- a/src/utils.sh +++ b/src/utils.sh @@ -7,6 +7,20 @@ info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; } error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; } warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; } +readPidFile() { + + local -n _pid="$1" + local file="$2" + + _pid="" + + [ -s "$file" ] || return 1 + + _pid=$(<"$file") + + [[ "$_pid" =~ ^[1-9][0-9]*$ ]] +} + hasFlag() { # Match a whitespace-delimited token in /proc/cpuinfo @@ -137,9 +151,7 @@ waitPidFile() { local file="$1" local timeout="${2:-10}" - [ ! -s "$file" ] && return 0 - ! read -r pid <"$file" && return 0 - [ -z "$pid" ] && return 0 + ! readPidFile pid "$file" && return 0 while [ -s "$file" ] && isAlive "$pid"; do sleep 0.2 @@ -203,9 +215,7 @@ sKill() { local pid local file="$1" - [ ! -s "$file" ] && return 0 - ! read -r pid <"$file" && return 0 - [ -z "$pid" ] && return 0 + ! readPidFile pid "$file" && return 0 if isAlive "$pid"; then { kill -15 -- "$pid" || :; } 2>/dev/null