You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=97
  4. STOP=01
  5. append_string() {
  6. varname="$1"
  7. add="$2"
  8. separator="${3:- }"
  9. actual
  10. eval "actual=\$$varname"
  11. new="${actual:+$actual$separator}$add"
  12. eval "$varname=\$new"
  13. }
  14. time_to_seconds() {
  15. time=$1
  16. { [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } ||
  17. { [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } ||
  18. { [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m} * 60)); } ||
  19. { [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h} * 3600)); } ||
  20. { [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d} * 86400)); }
  21. echo $seconds
  22. unset seconds
  23. unset time
  24. }
  25. config_watchcat() {
  26. # Read config
  27. config_get period "$1" period "120"
  28. config_get mode "$1" mode "ping_reboot"
  29. config_get pinghosts "$1" pinghosts "8.8.8.8"
  30. config_get pingperiod "$1" pingperiod "60"
  31. config_get forcedelay "$1" forcedelay "60"
  32. config_get pingsize "$1" pingsize "standard"
  33. config_get interface "$1" interface
  34. config_get mmifacename "$1" mmifacename
  35. config_get_bool unlockbands "$1" unlockbands "0"
  36. # Fix potential typo in mode and provide backward compatibility.
  37. [ "$mode" = "allways" ] && mode="periodic_reboot"
  38. [ "$mode" = "always" ] && mode="periodic_reboot"
  39. [ "$mode" = "ping" ] && mode="ping_reboot"
  40. # Checks for settings common to all operation modes
  41. if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then
  42. append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; "
  43. fi
  44. period="$(time_to_seconds "$period")"
  45. [ "$period" -ge 1 ] ||
  46. append_string "error" "period has invalid format. Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
  47. # ping_reboot mode and restart_iface mode specific checks
  48. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ]; then
  49. if [ -z "$error" ]; then
  50. pingperiod_default="$((period / 5))"
  51. pingperiod="$(time_to_seconds "$pingperiod")"
  52. if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
  53. pingperiod="$(time_to_seconds "$pingperiod_default")"
  54. append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
  55. fi
  56. if [ "$pingperiod" -lt 0 ]; then
  57. append_string "warn" "pingperiod cannot be a negative value." "; "
  58. fi
  59. if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
  60. 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."
  61. fi
  62. fi
  63. fi
  64. # ping_reboot mode and periodic_reboot mode specific checks
  65. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
  66. forcedelay="$(time_to_seconds "$forcedelay")"
  67. fi
  68. [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
  69. [ -n "$error" ] && {
  70. logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
  71. return
  72. }
  73. # Need to conditionally run mode functions because they have different signatures
  74. case "$mode" in
  75. periodic_reboot)
  76. procd_open_instance "watchcat_${1}"
  77. procd_set_param command /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay"
  78. procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
  79. procd_close_instance
  80. ;;
  81. ping_reboot)
  82. procd_open_instance "watchcat_${1}"
  83. procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize"
  84. procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
  85. procd_close_instance
  86. ;;
  87. restart_iface)
  88. procd_open_instance "watchcat_${1}"
  89. procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename"
  90. procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
  91. procd_close_instance
  92. ;;
  93. *)
  94. echo "Error starting Watchcat service. Invalid mode selection: $mode"
  95. ;;
  96. esac
  97. }
  98. start_service() {
  99. config_load watchcat
  100. config_foreach config_watchcat watchcat
  101. }
  102. service_triggers() {
  103. procd_add_reload_trigger "watchcat"
  104. }