watchcat: update to support procd
authorNicholas Smith <nicholas@nbembedded.com>
Tue, 26 Jan 2021 00:35:57 +0000 (10:35 +1000)
committerNicholas Smith <nicholas@nbembedded.com>
Wed, 24 Feb 2021 10:50:09 +0000 (20:50 +1000)
Signed-off-by: Nicholas Smith <nicholas@nbembedded.com>
utils/watchcat/Makefile
utils/watchcat/files/initd_watchcat [deleted file]
utils/watchcat/files/migrate-watchcat [new file with mode: 0644]
utils/watchcat/files/uci_defaults_watchcat [deleted file]
utils/watchcat/files/watchcat.config [new file with mode: 0644]
utils/watchcat/files/watchcat.init [new file with mode: 0644]

index 0cbf11ddab01022a4bd675179d489c04c76ab249..cb80a6a10ee9c38dae9431e1709ba6b187f1e96d 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=watchcat
 PKG_VERSION:=1
-PKG_RELEASE:=11
+PKG_RELEASE:=12
 
 PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
 PKG_LICENSE:=GPL-2.0
@@ -19,16 +19,16 @@ include $(INCLUDE_DIR)/package.mk
 define Package/watchcat
   SECTION:=utils
   CATEGORY:=Utilities
-  TITLE:=Enable the configuration of programed reboots
+  TITLE:=Enable the configuration of programmed reboots or network interface restarts
   PKGARCH:=all
 endef
 
 define Package/watchcat/description
-Allows to configure a periodically reboot, or after losing internet connectivity. Configured trough UCI /etc/config/system.
+Restart network interfaces or reboot if pings to hosts fail, or set up periodic reboots. Configured via UCI /etc/config/watchcat
 endef
 
 define Package/watchcat/conffiles
-/etc/config/system
+/etc/config/watchcat
 endef
 
 define Build/Compile
@@ -36,11 +36,13 @@ endef
 
 define Package/watchcat/install
        $(INSTALL_DIR) $(1)/etc/init.d
-       $(INSTALL_BIN) ./files/initd_watchcat $(1)/etc/init.d/watchcat
+       $(INSTALL_BIN) ./files/watchcat.init $(1)/etc/init.d/watchcat
        $(INSTALL_DIR) $(1)/usr/bin
        $(INSTALL_BIN) ./files/watchcat.sh $(1)/usr/bin/watchcat.sh
+       $(INSTALL_DIR) $(1)/etc/config
+       $(INSTALL_DATA) ./files/watchcat.config $(1)/etc/config/watchcat
        $(INSTALL_DIR) $(1)/etc/uci-defaults
-       $(INSTALL_BIN) ./files/uci_defaults_watchcat $(1)/etc/uci-defaults/50-watchcat
+       $(INSTALL_BIN) ./files/migrate-watchcat $(1)/etc/uci-defaults/migrate-watchcat
 endef
 
 $(eval $(call BuildPackage,watchcat))
diff --git a/utils/watchcat/files/initd_watchcat b/utils/watchcat/files/initd_watchcat
deleted file mode 100644 (file)
index 95f5927..0000000
+++ /dev/null
@@ -1,142 +0,0 @@
-#!/bin/sh /etc/rc.common
-
-START=97
-
-PIDFILE="/tmp/run/watchcat"
-
-append_string() {
-       varname="$1"
-       add="$2"
-       separator="${3:- }"
-       actual
-       eval "actual=\$$varname"
-
-       new="${actual:+$actual$separator}$add"
-       eval "$varname=\$new"
-}
-
-time_to_seconds() {
-       time=$1
-
-       { [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } ||
-               { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } ||
-               { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } ||
-               { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } ||
-               { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
-
-       echo $seconds
-       unset seconds
-       unset time
-}
-
-load_watchcat() {
-       config_get period "$1" period "120"
-       config_get mode "$1" mode "restart_iface"
-       config_get pinghosts "$1" pinghosts "8.8.8.8"
-       config_get pingperiod "$1" pingperiod "60"
-       config_get forcedelay "$1" forcedelay "60"
-       config_get pingsize "$1" pingsize "standard"
-       config_get interface "$1" interface
-       config_get mmifacename "$1" mmifacename
-       config_get unlockbands "$1" unlockbands "0"
-
-       # Fix potential typo in mode and provide backward compatibility.
-       [ "$mode" = "allways" ] && mode="periodic_reboot"
-       [ "$mode" = "always" ] && mode="periodic_reboot"
-       [ "$mode" = "ping" ] && mode="ping_reboot"
-       
-       error=""
-       warn=""
-
-       # Checks for settings common to all operation modes
-       if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then
-               append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; "
-       fi
-
-       period="$(time_to_seconds "$period")"
-       [ "$period" -ge 1 ] ||
-               append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
-
-       # ping_reboot mode and restart_iface mode specific checks
-       if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ]; then
-
-               if [ -z "$error" ]; then
-
-                       pingperiod_default="$((period / 5))"
-
-                       pingperiod="$(time_to_seconds "$pingperiod")"
-                       if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
-                               pingperiod="$(time_to_seconds "$pingperiod_default")"
-                               append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
-                       fi
-
-                       if [ "$pingperiod" -lt 0 ]; then
-                               append_string "warn" "pingperiod cannot be a negative value." "; "
-                       fi
-
-                       if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
-                               append_string "error" "Check interval is less than 30s. For robust operation with ModemManager modem interfaces it is recommended to set the period to at least 30s."
-                       fi
-               fi
-       fi
-
-       # ping_reboot mode and periodic_reboot mode specific checks
-       if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
-               forcedelay="$(time_to_seconds "$forcedelay")"
-       fi
-
-       [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
-       [ -n "$error" ] && {
-               logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
-               return
-       }
-
-       case "$mode" in
-       periodic_reboot)
-               /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay" &
-               logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
-               ;;
-       ping_reboot)
-               /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" &
-               logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay;pingsize=$pingsize)"
-               ;;
-       restart_iface)
-               /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" &
-               logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;pingsize=$pingsize;interface=$interface;mmifacename=$mmifacename;unlockbands=$unlockbands)"
-               ;;
-       *)
-               echo "Error starting Watchcat service. Invalid mode selection: $mode"
-               ;;
-       esac
-
-       echo $! >>"${PIDFILE}.pids"
-}
-
-stop() {
-       if [ -f "${PIDFILE}.pids" ]; then
-               logger -p user.info -t "watchcat" "stopping all tasks"
-
-               while read pid; do
-                       kill -KILL "$pid"
-               done <"${PIDFILE}.pids"
-
-               rm "${PIDFILE}.pids"
-
-               logger -p user.info -t "watchcat" "all tasks stopped"
-       else
-               logger -p user.info -t "watchcat" "no tasks running"
-       fi
-}
-
-start() {
-       [ -f "${PIDFILE}.pids" ] && stop
-
-       config_load system
-       if [ -n "$(uci show system.@watchcat[0])" ]; then # at least one watchcat section exists
-               logger -p user.info -t "watchcat" "starting all tasks"
-               config_foreach load_watchcat watchcat
-               logger -p user.info -t "watchcat" "all tasks started"
-       else
-               logger -p user.info -t "watchcat" "no tasks defined"
-       fi
-}
diff --git a/utils/watchcat/files/migrate-watchcat b/utils/watchcat/files/migrate-watchcat
new file mode 100644 (file)
index 0000000..93b7ce2
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+. /lib/functions.sh
+
+upgrade_watchcat() {
+        local cfg="$1"
+
+        config_get period "$cfg" period
+        config_get mode "$cfg" mode
+        config_get pinghosts "$cfg" pinghosts
+        config_get forcedelay "$cfg" forcedelay
+
+        [ -f "/etc/config/watchcat" ] || touch /etc/config/watchcat
+        uci_add watchcat watchcat
+        uci_set watchcat @watchcat[-1] period "$period"
+        uci_set watchcat @watchcat[-1] mode "$mode"
+        uci_set watchcat @watchcat[-1] pinghosts "$pinghosts"
+        uci_set watchcat @watchcat[-1] forcedelay "$forcedelay"
+
+        uci_remove system "$cfg"
+}
+
+config_load system
+config_foreach upgrade_watchcat watchcat
+
+uci_commit watchcat
+uci commit system
diff --git a/utils/watchcat/files/uci_defaults_watchcat b/utils/watchcat/files/uci_defaults_watchcat
deleted file mode 100644 (file)
index da49932..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-
-uci -q show system.@watchcat[0] || {
-       uci add system watchcat
-       uci set system.@watchcat[0].period=6h
-       uci set system.@watchcat[0].mode=ping_reboot
-       uci set system.@watchcat[0].pinghosts=8.8.8.8
-       uci set system.@watchcat[0].forcedelay=30
-       uci commit
-}
diff --git a/utils/watchcat/files/watchcat.config b/utils/watchcat/files/watchcat.config
new file mode 100644 (file)
index 0000000..ed6544c
--- /dev/null
@@ -0,0 +1,5 @@
+config watchcat
+       option period '6h'
+       option mode 'ping_reboot'
+       option pinghosts '8.8.8.8'
+       option forcedelay '30'
diff --git a/utils/watchcat/files/watchcat.init b/utils/watchcat/files/watchcat.init
new file mode 100644 (file)
index 0000000..a48d3fd
--- /dev/null
@@ -0,0 +1,124 @@
+#!/bin/sh /etc/rc.common
+
+USE_PROCD=1
+
+START=97
+STOP=01
+
+append_string() {
+       varname="$1"
+       add="$2"
+       separator="${3:- }"
+       actual
+       eval "actual=\$$varname"
+
+       new="${actual:+$actual$separator}$add"
+       eval "$varname=\$new"
+}
+
+time_to_seconds() {
+       time=$1
+
+       { [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } ||
+               { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } ||
+               { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } ||
+               { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } ||
+               { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
+
+       echo $seconds
+       unset seconds
+       unset time
+}
+
+config_watchcat() {
+       # Read config
+       config_get period "$1" period "120"
+       config_get mode "$1" mode "ping_reboot"
+       config_get pinghosts "$1" pinghosts "8.8.8.8"
+       config_get pingperiod "$1" pingperiod "60"
+       config_get forcedelay "$1" forcedelay "60"
+       config_get pingsize "$1" pingsize "standard"
+       config_get interface "$1" interface
+       config_get mmifacename "$1" mmifacename
+       config_get_bool unlockbands "$1" unlockbands "0"
+
+       # Fix potential typo in mode and provide backward compatibility.
+       [ "$mode" = "allways" ] && mode="periodic_reboot"
+       [ "$mode" = "always" ] && mode="periodic_reboot"
+       [ "$mode" = "ping" ] && mode="ping_reboot"
+  
+       # Checks for settings common to all operation modes
+       if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then
+               append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; "
+       fi
+
+       period="$(time_to_seconds "$period")"
+       [ "$period" -ge 1 ] ||
+               append_string "error" "period has invalid format. Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
+
+       # ping_reboot mode and restart_iface mode specific checks
+       if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ]; then
+               if [ -z "$error" ]; then
+                       pingperiod_default="$((period / 5))"
+                       pingperiod="$(time_to_seconds "$pingperiod")"
+
+                       if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
+                               pingperiod="$(time_to_seconds "$pingperiod_default")"
+                               append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
+                       fi
+
+                       if [ "$pingperiod" -lt 0 ]; then
+                               append_string "warn" "pingperiod cannot be a negative value." "; "
+                       fi
+
+                       if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
+                               append_string "error" "Check interval is less than 30s. For robust operation with ModemManager modem interfaces it is recommended to set the period to at least 30s."
+                       fi
+               fi
+       fi
+
+       # ping_reboot mode and periodic_reboot mode specific checks
+       if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
+               forcedelay="$(time_to_seconds "$forcedelay")"
+       fi
+
+       [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
+       [ -n "$error" ] && {
+               logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
+               return
+       }
+       
+       # Need to conditionally run mode functions because they have different signatures
+       case "$mode" in
+               periodic_reboot)
+                       procd_open_instance "watchcat_${1}"
+                       procd_set_param command /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay"
+                       procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
+                       procd_close_instance
+                       ;;
+               ping_reboot)
+                       procd_open_instance "watchcat_${1}"
+                       procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize"
+                       procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
+                       procd_close_instance
+                       ;;
+               restart_iface)
+                       procd_open_instance "watchcat_${1}"
+                       procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename"
+                       procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
+                       procd_close_instance
+                       ;;
+               *)
+                       echo "Error starting Watchcat service. Invalid mode selection: $mode"
+                       ;;
+       esac
+}
+
+start_service() {
+       config_load watchcat
+       config_foreach config_watchcat watchcat
+}
+
+service_triggers() {
+       procd_add_reload_trigger "watchcat"
+}