diff --git a/agent/agent.sh b/agent/agent.sh index 57111ed..556571c 100644 --- a/agent/agent.sh +++ b/agent/agent.sh @@ -1,10 +1,18 @@ #!/usr/bin/env bash set -u -declare nmi +# Functions + +snore() +{ + local IFS + [[ -n "${_snore_fd:-}" ]] || exec {_snore_fd}<> <(:) + read ${1:+-t "$1"} -u $_snore_fd || : +} function checkNMI { + local nmi nmi=$(awk '/NMI/ {for (i=2; i<=NF; i++) if ($i ~ /^[0-9]+$/) {sum+=$i}} END {print sum}' /proc/interrupts) if [ "$nmi" != "" ] && [ "$nmi" -ne "0" ]; then @@ -12,15 +20,28 @@ function checkNMI { echo "Received shutdown request through NMI.." > /dev/ttyS0 /usr/syno/sbin/synoshutdown -s > /dev/null - exit 0 + exit fi } +finish() { + + echo "Shutting down agent.." > /dev/ttyS0 + exit + +} + +trap finish SIGINT SIGTERM + +# Setup serialport + chmod 666 /dev/ttyS0 checkNMI +# Install packages + first_run=false for filename in /usr/local/packages/*.spk; do @@ -50,14 +71,17 @@ else fi +# Display message in docker log output + echo "-------------------------------------------" > /dev/ttyS0 echo " You can now login to DSM at port 5000 " > /dev/ttyS0 echo "-------------------------------------------" > /dev/ttyS0 +# Wait for NMI interrupt as a shutdown signal + while true; do checkNMI - sleep 2 + snore 2 done - diff --git a/run/install.sh b/run/install.sh index bf34a22..d78c5ea 100644 --- a/run/install.sh +++ b/run/install.sh @@ -160,7 +160,7 @@ cp /agent/service.sh $LOC/agent.sh chmod +x $LOC/agent.sh # Store agent version -echo "2" > "$STORAGE"/"$BASE".agent +echo "3" > "$STORAGE"/"$BASE".agent echo "Install: Installing system partition..." diff --git a/run/power.sh b/run/power.sh index 4fc7b72..81efb6a 100644 --- a/run/power.sh +++ b/run/power.sh @@ -12,7 +12,6 @@ _QEMU_SHUTDOWN_COUNTER=/run/qemu.counter rm -f "${_QEMU_PID}" rm -f "${_QEMU_SHUTDOWN_COUNTER}" -# Allows for troubleshooting signals sent to the process _trap(){ func="$1" ; shift for sig ; do @@ -20,6 +19,13 @@ _trap(){ done } +snore() +{ + local IFS + [[ -n "${_snore_fd:-}" ]] || exec {_snore_fd}<> <(:) + read ${1:+-t "$1"} -u $_snore_fd || : +} + _graceful_shutdown(){ local QEMU_MONPORT="${QEMU_MONPORT:-7100}" diff --git a/run/run.sh b/run/run.sh index c88ab72..af76aab 100755 --- a/run/run.sh +++ b/run/run.sh @@ -52,7 +52,7 @@ set -m ) set +m -# Since we have to start the process with -m, we need to poll every intervall if it's still running +# Since we started the QEMU process with -m, we need to poll if it's still running while [ -d "/proc/$(cat ${_QEMU_PID})" ]; do - sleep 1 + snore 1 done