v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.git] / package / network / services / lldpd / files / lldpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2015 OpenWrt.org
3
4 START=90
5 STOP=01
6
7 USE_PROCD=1
8 LLDPCLI=/usr/sbin/lldpcli
9 LLDPSOCKET=/var/run/lldpd.socket
10 LLDPD_CONF=/tmp/lldpd.conf
11 LLDPD_CONFS_DIR=/tmp/lldpd.d
12
13 find_release_info()
14 {
15         [ -s /etc/os-release ] && . /etc/os-release
16         [ -z "$PRETTY_NAME" ] && [ -s /etc/openwrt_version ] && \
17                 PRETTY_NAME="$(cat /etc/openwrt_version)"
18
19         echo "${PRETTY_NAME:-Unknown OpenWrt release} @ $(cat /proc/sys/kernel/hostname)"
20 }
21
22 write_lldpd_conf()
23 {
24         . /lib/functions/network.sh
25
26         local lldp_description
27
28         config_load 'lldpd'
29         config_get lldp_description 'config' 'lldp_description' "$(find_release_info)"
30
31         local ifaces
32         config_get ifaces 'config' 'interface'
33
34         local iface ifnames=""
35         for iface in $ifaces; do
36                 local ifname=""
37                 if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
38                         append ifnames "${ifname:-$iface}" ","
39                 fi
40         done
41
42         # Clear out the config file first
43         echo -n > "$LLDPD_CONF"
44         [ -n "$ifnames" ] && echo "configure system interface pattern" "$ifnames" >> "$LLDPD_CONF"
45         [ -n "$lldp_description" ] && echo "configure system description" "\"$lldp_description\"" >> "$LLDPD_CONF"
46
47         # Since lldpd's sysconfdir is /tmp, we'll symlink /etc/lldpd.d to /tmp/$LLDPD_CONFS_DIR
48         [ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
49 }
50
51 start_service() {
52
53         local enable_cdp
54         local enable_fdp
55         local enable_sonmp
56         local enable_edp
57         local lldp_class
58         local lldp_location
59         local readonly_mode
60         local agentxsocket
61
62         config_load 'lldpd'
63         config_get_bool enable_cdp 'config' 'enable_cdp' 0
64         config_get_bool enable_fdp 'config' 'enable_fdp' 0
65         config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
66         config_get_bool enable_edp 'config' 'enable_edp' 0
67         config_get lldp_class 'config' 'lldp_class'
68         config_get lldp_location 'config' 'lldp_location'
69         config_get_bool readonly_mode 'config' 'readonly_mode' 0
70         config_get agentxsocket 'config' 'agentxsocket'
71
72         mkdir -p /var/run/lldp
73         chown lldp:lldp /var/run/lldp
74
75         # When lldpd starts, it also loads up what we write in this config file
76         write_lldpd_conf
77
78         procd_open_instance
79         procd_set_param command /usr/sbin/lldpd
80         procd_append_param command -d # don't daemonize, procd will handle that for us
81
82         [ $enable_cdp -gt 0 ] && procd_append_param command '-c'
83         [ $enable_fdp -gt 0 ] && procd_append_param command '-f'
84         [ $enable_sonmp -gt 0 ] && procd_append_param command '-s'
85         [ $enable_edp -gt 0 ] && procd_append_param command '-e'
86         [ $readonly_mode -gt 0 ] && procd_append_param command '-r'
87         [ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
88         [ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"
89
90         # set auto respawn behavior
91         procd_set_param respawn
92         procd_close_instance
93 }
94
95 service_running() {
96         pgrep -x /usr/sbin/lldpd &> /dev/null
97 }
98
99 reload_service() {
100         running || return 1
101         $LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
102                 pause
103                 unconfigure lldp custom-tlv
104                 unconfigure system interface pattern
105                 unconfigure system description
106         EOF
107         # Rewrite lldpd.conf
108         # If something changed it should be included by the lldpcli call
109         write_lldpd_conf
110         $LLDPCLI -u $LLDPSOCKET -c $LLDPD_CONF -c $LLDPD_CONFS_DIR &> /dev/null
111         # Broadcast update over the wire
112         $LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
113                 resume
114                 update
115         EOF
116         return 0
117 }
118
119 stop_service() {
120         rm -rf /var/run/lldp $LLDPSOCKET
121 }