]> git.99rst.org Git - openwrt-packages.git/commitdiff
nut: split interface_triggers function
authorDaniel F. Dickinson <redacted>
Tue, 7 Jul 2026 02:08:01 +0000 (22:08 -0400)
committerJosef Schlehofer <redacted>
Wed, 15 Jul 2026 07:34:39 +0000 (09:34 +0200)
Using add_interface_triggers and check_interface_up is a
better separation of concerns and the code has little in
common (no duplication of code concerns).

While we are at it make both nut-monitor and nut-server failure of
add_interface_triggers non-fatal with a logged error message.

Signed-off-by: Daniel F. Dickinson <redacted>
net/nut/files/nut-monitor.init
net/nut/files/nut-server.init
net/nut/files/nut-service.sh.functions

index 959448b8ccdb0b19b4e3156426601236b2630506..82e63088d1c3007419b7d86434b3a2ba42b17eec 100644 (file)
@@ -92,7 +92,7 @@ service_preconditions() {
                fi
 
                build_config || return 1
-               interface_triggers "check_interface_up" "upsmon" || return 1
+               check_interface_up "upsmon" || return 1
                return 0
                ;;
        *)
@@ -217,8 +217,8 @@ service_triggers() {
 
        # We do not pass a variadic instance name as the nut-monitor initscript does
        # not need an additional instance name argument, only the initscript name
-       interface_triggers "add_trigger" "upsmon" "$interface_reload_delay" "nut-monitor" || {
-               log_error_exit "Failed to add interface triggers" nut-monitor nut-monitor
+       add_interface_triggers "upsmon" "$interface_reload_delay" "nut-monitor" || {
+               log_error "Failed to add interface triggers" nut-monitor nut-monitor
        }
        procd_add_reload_trigger "nut_monitor"
 }
index 1354e90abd2434078a7f8d495dffb3d5b45c6cf9..1467bd516e0be4604acc664f3a8832faa6b698db 100644 (file)
@@ -251,7 +251,7 @@ service_preconditions() {
 
                case $ret in
                0)
-                       interface_triggers "check_interface_up" "upsd" || should_start_srv=false
+                       check_interface_up "upsd" || should_start_srv=false
                        ;;
                *)
                        should_start_srv=false
@@ -264,7 +264,7 @@ service_preconditions() {
 
                case $ret in
                0 | 2)
-                       interface_triggers "check_interface_up" "upsd" || should_stop_srv=true
+                       check_interface_up "upsd" || should_stop_srv=true
                        ;;
                *)
                        should_stop_srv=true
@@ -561,6 +561,8 @@ service_triggers() {
                interface_reload_delay="$DEFAULT_PROCD_INTERFACE_RELOAD_DELAY"
        fi
 
-       interface_triggers "add_trigger" "upsd" "$interface_reload_delay" "nut-server" "upsd"
+       add_interface_triggers "upsd" "$interface_reload_delay" "nut-server" "upsd" || {
+               log_error "Failed to add interface triggers" nut-server nut-server
+       }
        procd_add_reload_trigger "nut_server"
 }
index b750c3bcf6c533032ec7f22023973a3b4458f87f..1e6ab927e02ef7fc5a9dd262a7bbc156e61ec33c 100644 (file)
@@ -243,73 +243,73 @@ service_active_no_instances() {
 }
 
 # Setup triggers in procd for changes to specified network interfaces
-# network_is_up depends on /lib/functions/network.sh having been sourced
-interface_triggers() {
-       local action="$1"
-       local section="$2"
-       local interface_reload_delay="${3:-$DEFAULT_PROCD_INTERFACE_RELOAD_DELAY}"
-       local initscript="$4"
-       if [ $# -ge 4 ]; then
-               shift 4
+add_interface_triggers() {
+       local section="$1"
+       local interface_reload_delay="${2:-$DEFAULT_PROCD_INTERFACE_RELOAD_DELAY}"
+       local initscript="$3"
+       if [ $# -ge 3 ]; then
+               shift 3
        fi
-       local interfaces interface
-       local have_up_interface
-       local trigger_failed
+
+       local interfaces interface trigger_failed
 
        config_get interfaces "$section" triggerlist
 
-       # We list the actions we care about. Other actions are irrelevant.
-       # We do not need to log or otherwise 'notice' other actions
-       case "$action" in
-       add_trigger)
-               if [ -n "$interfaces" ] && [ "$interfaces" != "all" ]; then
-                       set -f
-                       trigger_failed="false"
-                       # interfaces is deliberately unquoted so we get word-splitting for
-                       # the for loop
-                       for interface in $interfaces; do
-                               # We use the variadic third and fourth parameters of procd_add_interface_trigger to
-                               # restart instead of reload on interface up/down events. Additionally the variadic
-                               # fifth and following parameters of procd_add_interface_trigger are passed through to
-                               # the <script>, so the trigger action becomes e.g. /etc/init.d/<script> restart <variadic_vars>.
-                               procd_add_interface_trigger "interface.*.up" "$interface" "/etc/init.d/$initscript" "restart" "$@" || trigger_failed="true"
-                       done
-                       set +f
-                       if [ "$trigger_failed" = "true" ]; then
-                               return 1
-                       fi
-               elif [ "$interfaces" = "all" ]; then
-                       # Add a restart trigger on any interface's up status changing
-                       procd_add_raw_trigger "interface.*.up" "$interface_reload_delay" "/etc/init.d/$initscript" "restart" "$@" || return 1
-               else
-                       return 0 # don't react to interface status
+       if [ -n "$interfaces" ] && [ "$interfaces" != "all" ]; then
+               set -f
+               trigger_failed="false"
+               # interfaces is deliberately unquoted so we get word-splitting for
+               # the for loop
+               for interface in $interfaces; do
+                       # We use the variadic third and fourth parameters of procd_add_interface_trigger to
+                       # restart instead of reload on interface up/down events. Additionally the variadic
+                       # fifth and following parameters of procd_add_interface_trigger are passed through to
+                       # the <script>, so the trigger action becomes e.g. /etc/init.d/<script> restart <variadic_vars>.
+                       procd_add_interface_trigger "interface.*.up" "$interface" "/etc/init.d/$initscript" "restart" "$@" || trigger_failed="true"
+               done
+               set +f
+               if [ "$trigger_failed" = "true" ]; then
+                       return 1
                fi
-               ;;
-       check_interface_up)
-               if [ -n "$interfaces" ] && [ "$interfaces" != "all" ]; then
-                       set -f
-                       have_up_interface="false"
-                       for interface in $interfaces; do
-                               network_is_up "$interface" && have_up_interface="true" && break
-                       done
-                       set +f
-                       if [ "$have_up_interface" = "false" ]; then
-                               log_msg "None of the trigger interfaces are up." "nut-service.sh" "nut-service" "notice"
-                               return 1
-                       fi
-               elif [ "$interfaces" = "all" ]; then
-                       # Return true if any interface is up
-                       # -q for jsonfilter only suppress error output, not messages on
-                       # stdout, redirect to >/dev/null is needed to avoid all output
-                       if ubus -S call network.device status | jsonfilter -q -l 1 -e '$[@.up=true].up' >/dev/null; then
-                               return 0
-                       fi
-                       log_msg "No interfaces are up." "nut-service.sh" "nut-service" "notice"
+       elif [ "$interfaces" = "all" ]; then
+               # Add a restart trigger on any interface's up status changing
+               procd_add_raw_trigger "interface.*.up" "$interface_reload_delay" "/etc/init.d/$initscript" "restart" "$@" || return 1
+       else
+               return 0 # don't react to interface status
+       fi
+       return 0
+}
+
+# availability of network_is_up depends on /lib/functions/network.sh having
+# been sourced
+check_interface_up() {
+       local section="$1"
+       local interfaces interface have_up_interface
+
+       config_get interfaces "$section" triggerlist
+
+       if [ -n "$interfaces" ] && [ "$interfaces" != "all" ]; then
+               set -f
+               have_up_interface="false"
+               for interface in $interfaces; do
+                       network_is_up "$interface" && have_up_interface="true" && break
+               done
+               set +f
+               if [ "$have_up_interface" = "false" ]; then
+                       log_msg "None of the trigger interfaces are up." "nut-service.sh" "nut-service" "notice"
                        return 1
-               else
-                       return 0 # don't depend on interface status
                fi
-               ;;
-       esac
+       elif [ "$interfaces" = "all" ]; then
+               # Return true if any interface is up
+               # -q for jsonfilter only suppress error output, not messages on
+               # stdout, redirect to >/dev/null is needed to avoid all output
+               if ubus -S call network.device status | jsonfilter -q -l 1 -e '$[@.up=true].up' >/dev/null; then
+                       return 0
+               fi
+               log_msg "No interfaces are up." "nut-service.sh" "nut-service" "notice"
+               return 1
+       else
+               return 0 # don't depend on interface status
+       fi
        return 0
 }
git clone https://git.99rst.org/PROJECT