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.

136 lines
4.7 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. local 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. config_get addressfamily "$1" addressfamily "any"
  37. config_get script "$1" script
  38. # Fix potential typo in mode and provide backward compatibility.
  39. [ "$mode" = "allways" ] && mode="periodic_reboot"
  40. [ "$mode" = "always" ] && mode="periodic_reboot"
  41. [ "$mode" = "ping" ] && mode="ping_reboot"
  42. # Checks for settings common to all operation modes
  43. if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ] && [ "$mode" != "run_script" ]; then
  44. append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface' or 'run_script'" "; "
  45. fi
  46. period="$(time_to_seconds "$period")"
  47. [ "$period" -ge 1 ] ||
  48. append_string "error" "period has invalid format. Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
  49. # ping_reboot mode and restart_iface mode specific checks
  50. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ] || [ "$mode" = "run_script" ]; then
  51. if [ -z "$error" ]; then
  52. pingperiod_default="$((period / 5))"
  53. pingperiod="$(time_to_seconds "$pingperiod")"
  54. if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
  55. pingperiod="$(time_to_seconds "$pingperiod_default")"
  56. append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
  57. fi
  58. if [ "$pingperiod" -lt 0 ]; then
  59. append_string "warn" "pingperiod cannot be a negative value." "; "
  60. fi
  61. if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
  62. 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."
  63. fi
  64. fi
  65. fi
  66. if [ "$mode" = "run_script" ] && [ -z "$script" ]; then
  67. append_string "error" "run_script mode requires a script"
  68. fi
  69. # ping_reboot mode and periodic_reboot mode specific checks
  70. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
  71. forcedelay="$(time_to_seconds "$forcedelay")"
  72. fi
  73. [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
  74. [ -n "$error" ] && {
  75. logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
  76. return
  77. }
  78. # Need to conditionally run mode functions because they have different signatures
  79. case "$mode" in
  80. periodic_reboot)
  81. procd_open_instance "watchcat_${1}"
  82. procd_set_param command /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay"
  83. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  84. procd_close_instance
  85. ;;
  86. ping_reboot)
  87. procd_open_instance "watchcat_${1}"
  88. procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" "$addressfamily"
  89. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  90. procd_close_instance
  91. ;;
  92. restart_iface)
  93. procd_open_instance "watchcat_${1}"
  94. procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily"
  95. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  96. procd_close_instance
  97. ;;
  98. run_script)
  99. procd_open_instance "watchcat_${1}"
  100. procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script"
  101. procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
  102. procd_close_instance
  103. ;;
  104. *)
  105. echo "Error starting Watchcat service. Invalid mode selection: $mode"
  106. ;;
  107. esac
  108. }
  109. start_service() {
  110. config_load watchcat
  111. config_foreach config_watchcat watchcat
  112. }
  113. service_triggers() {
  114. procd_add_reload_trigger "watchcat"
  115. }