Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/watchcat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=watchcat
PKG_VERSION:=1
PKG_RELEASE:=19
PKG_RELEASE:=20

PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
PKG_LICENSE:=GPL-2.0
Expand Down
49 changes: 27 additions & 22 deletions utils/watchcat/files/watchcat.init
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh /etc/rc.common
# shellcheck shell=busybox

USE_PROCD=1

Expand All @@ -17,21 +18,23 @@ append_string() {
}

time_to_seconds() {
time=$1
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
echo "$seconds"
unset seconds
unset time
}

config_watchcat() {
# Read config
local enabled period mode pinghosts pingperiod forcedelay pingsize interface mmifacename unlockbands addressfamily script
config_get_bool enabled "$1" enabled 1
config_get period "$1" period "120"
config_get mode "$1" mode "ping_reboot"
config_get pinghosts "$1" pinghosts "8.8.8.8"
Expand All @@ -44,11 +47,17 @@ config_watchcat() {
config_get addressfamily "$1" addressfamily "any"
config_get script "$1" script

# Skip disabled instances
[ "$enabled" = 1 ] || return

# Fix potential typo in mode and provide backward compatibility.
[ "$mode" = "allways" ] && mode="periodic_reboot"
[ "$mode" = "always" ] && mode="periodic_reboot"
[ "$mode" = "ping" ] && mode="ping_reboot"

# forward declaration for append_string
export error warn

# Checks for settings common to all operation modes
if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ] && [ "$mode" != "run_script" ]; then
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface' or 'run_script'" "; "
Expand All @@ -61,7 +70,7 @@ config_watchcat() {
# ping_reboot mode and restart_iface mode specific checks
if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ] || [ "$mode" = "run_script" ]; then
if [ -z "$error" ]; then
pingperiod_default="$((period / 5))"
local pingperiod_default="$((period / 5))"
pingperiod="$(time_to_seconds "$pingperiod")"

if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
Expand Down Expand Up @@ -90,40 +99,36 @@ config_watchcat() {

[ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
[ -n "$error" ] && {
logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
logger -s -p user.err -t "watchcat" "reboot program $1 not started - $error"
return
}

procd_open_instance "watchcat_${1}"

# 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" "$addressfamily" "$interface"
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" "$addressfamily" "$interface"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a code regression to v17 without any fix proposal, please correct me if I'm wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're wrong.

  1. See what you have passed to argv to shell.
  2. See what you get at ping_hosts="$3"
  3. and what would happen at for host in $ping_hosts; do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for providing the details.

;;
restart_iface)
procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" \"$pinghosts\" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily"
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this a code regression to v17 without any fix proposal, please correct me if I'm wrong.

;;
run_script)
procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" \"$pinghosts\" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script"
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"
procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script"
;;
# *) # impossible case: checked above
# echo "Error starting Watchcat service. Invalid mode selection: $mode"
# ;;
esac

procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
# simplify debugging
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}

start_service() {
Expand Down
Loading