lldpd: Init config read on reload
authorStephen Howell <howels@allthatwemight.be>
Sat, 16 Dec 2023 22:32:19 +0000 (22:32 +0000)
committerStijn Tintel <stijn@linux-ipv6.be>
Thu, 8 Feb 2024 10:35:36 +0000 (12:35 +0200)
Init script reload with trigger to detect config file update.
Reload command added to attempt non-impactful lldpd reload where
lldpcli can be used to update config without process restart.
Config hash function used to track whether process restart is needed.

Signed-off-by: Stephen Howell <howels@allthatwemight.be>
package/network/services/lldpd/files/lldpd.init

index e1e8ac9fd4a5f7b068b7c119bc4e7e60a72f975b..a52e6e0ec4618a3942e481c9079b744d507315d6 100644 (file)
@@ -5,11 +5,17 @@ START=90
 STOP=01
 
 USE_PROCD=1
+LLDPDBIN=/usr/sbin/lldpd
 LLDPCLI=/usr/sbin/lldpcli
 LLDPSOCKET=/var/run/lldpd.socket
 LLDPD_CONF=/tmp/lldpd.conf
 LLDPD_CONFS_DIR=/tmp/lldpd.d
 
+LLDPD_RUN=/var/run/lldpd
+LLDPD_RESTART_HASH=${LLDPD_RUN}/lldpd.restart_hash
+
+. "$IPKG_INSTROOT/lib/functions/network.sh"
+
 find_release_info()
 {
        [ -s /etc/os-release ] && . /etc/os-release
@@ -19,10 +25,27 @@ find_release_info()
        echo "${PRETTY_NAME:-Unknown OpenWrt release} @ $(cat /proc/sys/kernel/hostname)"
 }
 
+get_config_restart_hash() {
+       local var="$1"
+       local _string _hash v
+
+       config_load 'lldpd'
+
+       config_get      v 'config' 'lldp_class'; append _string "$v" ","
+       config_get      v 'config' 'agentxsocket'; append _string "$v" ","
+       config_get_bool v 'config' 'readonly_mode'; append _string "$v" ","
+       config_get_bool v 'config' 'enable_lldp' 1; append _string "$v" ","
+       config_get_bool v 'config' 'enable_cdp'; append _string "$v" ","
+       config_get_bool v 'config' 'enable_edp'; append _string "$v" ","
+       config_get_bool v 'config' 'enable_fdp'; append _string "$v" ","
+       config_get_bool v 'config' 'enable_sonmp'; append _string "$v" ","
+
+       _hash=`echo -n "${_string}" | md5sum | awk '{ print \$1 }'`
+       export -n "$var=$_hash"
+}
+
 write_lldpd_conf()
 {
-       . /lib/functions/network.sh
-
        local lldp_description
 
        config_load 'lldpd'
@@ -60,10 +83,6 @@ write_lldpd_conf()
        [ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
 }
 
-service_triggers() {
-       procd_add_reload_trigger "lldpd"
-}
-
 start_service() {
 
        local enable_cdp
@@ -85,14 +104,15 @@ start_service() {
        config_get_bool readonly_mode 'config' 'readonly_mode' 0
        config_get agentxsocket 'config' 'agentxsocket'
 
-       mkdir -p /var/run/lldp
-       chown lldp:lldp /var/run/lldp
+       mkdir -p ${LLDPD_RUN}
+       chown lldp:lldp ${LLDPD_RUN}
 
        # When lldpd starts, it also loads up what we write in this config file
        write_lldpd_conf
 
        procd_open_instance
-       procd_set_param command /usr/sbin/lldpd -d
+       procd_set_param command ${LLDPDBIN}
+       procd_append_param command -d
 
        [ $enable_cdp -gt 0 ] && procd_append_param command '-c'
        [ $enable_fdp -gt 0 ] && procd_append_param command '-f'
@@ -102,13 +122,38 @@ start_service() {
        [ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
        [ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"
 
+    # Overwrite default configuration locations processed by lldpcli at start
+       procd_append_param command -O "$LLDPD_CONF"
+
+       local restart_hash
+       get_config_restart_hash restart_hash
+       echo -n "$restart_hash" > $LLDPD_RESTART_HASH
+
        # set auto respawn behavior
        procd_set_param respawn
        procd_close_instance
 }
 
+service_triggers() {
+       procd_add_config_trigger "config.change" "lldpd" /etc/init.d/lldpd reload
+}
+
 reload_service() {
        running || return 1
+       
+       local running_hash=""
+       local config_hash=""
+
+       get_config_restart_hash config_hash
+       if [ -f ${LLDPD_RESTART_HASH} ]; then running_hash=`cat $LLDPD_RESTART_HASH`; fi
+
+       if [ "x$running_hash" != "x$config_hash" ]; then
+               # Restart LLDPd
+               # Some parameters can't be configured at runtime
+               restart
+               return 0
+       fi
+       
        $LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
                pause
                unconfigure lldp custom-tlv
@@ -130,5 +175,6 @@ reload_service() {
 }
 
 stop_service() {
-       rm -rf /var/run/lldp $LLDPSOCKET
+       rm -rf ${LLDPD_RUN} $LLDPSOCKET 2>/dev/null
 }
+