From 35e39dbb9008f73b6bc6750544ef09ac82832cee Mon Sep 17 00:00:00 2001 From: Maarten van Gompel Date: Mon, 7 Jun 2021 23:31:21 +0200 Subject: [PATCH] implemented sxmo_networkmonitor to monitor when the network goes up/down and execute hooks This implement a dbus monitor for networkamanger and is useful to start/stop/restart certain things in user-defined hooks when connectivity is gained/lost. By default all it does is ensure the status bar is updated quickly to reflect the network status. Signed-off-by: Maarten van Gompel --- scripts/core/sxmo_networkmonitor.sh | 44 +++++++++++++++++++++++++++++ scripts/core/sxmo_xinit.sh | 1 + 2 files changed, 45 insertions(+) create mode 100755 scripts/core/sxmo_networkmonitor.sh diff --git a/scripts/core/sxmo_networkmonitor.sh b/scripts/core/sxmo_networkmonitor.sh new file mode 100755 index 0000000..66f8a43 --- /dev/null +++ b/scripts/core/sxmo_networkmonitor.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env sh + +# include common definitions +# shellcheck source=scripts/core/sxmo_common.sh +. "$(dirname "$0")/sxmo_common.sh" + +gracefulexit() { + sxmo_statusbarupdate.sh + sleep 1 + echo "sxmo_networkmonitor: gracefully exiting (on signal or after error)">&2 + kill -9 0 +} + +trap "gracefulexit" INT TERM + +dbus-monitor --system "interface='org.freedesktop.NetworkManager',type='signal',member='StateChanged'" | \ + while read -r line; do + if echo "$line" | grep -E "^signal.*StateChanged"; then + # shellcheck disable=SC2034 + read -r newstate + if echo "$newstate" | grep "int32 70"; then + #network just connected (70=NM_STATE_CONNECTED_GLOBAL) + echo "network up">&2 + if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/network-up" ]; then + "$XDG_CONFIG_HOME/sxmo/hooks/network-up" & + fi + sxmo_statusbarupdate.sh + elif echo "$newstate" | grep "int32 20"; then + #network just disconnected (20=NM_STATE_DISCONNECTED) + echo "network down">&2 + if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/network-down" ]; then + "$XDG_CONFIG_HOME/sxmo/hooks/network-down" & + fi + sxmo_statusbarupdate.sh + elif echo "$newstate" | grep "int32 30"; then + #network is going down (30=NM_STATE_DISCONNECTING) + if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/network-pre-down" ]; then + "$XDG_CONFIG_HOME/sxmo/hooks/network-pre-down" & + fi + fi + fi + done & + +wait diff --git a/scripts/core/sxmo_xinit.sh b/scripts/core/sxmo_xinit.sh index 2d99502..25fa47f 100755 --- a/scripts/core/sxmo_xinit.sh +++ b/scripts/core/sxmo_xinit.sh @@ -69,6 +69,7 @@ daemons() { daemonsneedingdbus() { dunst -conf /usr/share/sxmo/appcfg/dunst.conf & sxmo_notificationmonitor.sh & + sxmo_networkmonitor.sh & sxmo_lisgdstart.sh & }