4e9efcae0de0e445baf6f43966f90675484d3ea7
[openwrt/staging/chunkeey.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 Lede 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
61 config_load 'lldpd'
62 config_get_bool enable_cdp 'config' 'enable_cdp' 0
63 config_get_bool enable_fdp 'config' 'enable_fdp' 0
64 config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
65 config_get_bool enable_edp 'config' 'enable_edp' 0
66 config_get lldp_class 'config' 'lldp_class'
67 config_get lldp_location 'config' 'lldp_location'
68 config_get_bool readonly_mode 'config' 'readonly_mode' 0
69
70 mkdir -p /var/run/lldp
71 chown lldp:lldp /var/run/lldp
72
73 # When lldpd starts, it also loads up what we write in this config file
74 write_lldpd_conf
75
76 procd_open_instance
77 procd_set_param command /usr/sbin/lldpd
78 procd_append_param command -d # don't daemonize, procd will handle that for us
79
80 [ $enable_cdp -gt 0 ] && procd_append_param command '-c'
81 [ $enable_fdp -gt 0 ] && procd_append_param command '-f'
82 [ $enable_sonmp -gt 0 ] && procd_append_param command '-s'
83 [ $enable_edp -gt 0 ] && procd_append_param command '-e'
84 [ $readonly_mode -gt 0 ] && procd_append_param command '-r'
85 [ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
86
87 # set auto respawn behavior
88 procd_set_param respawn
89 procd_append_param respawn 3600
90 procd_append_param respawn 5
91 procd_append_param respawn -1
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 }