summaryrefslogtreecommitdiff
path: root/package/network/services/lldpd/files/lldpd.init
blob: 4495551567effca142a6d05648187b298e29a50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2015 OpenWrt.org

START=90
STOP=01

USE_PROCD=1
LLDPCLI=/usr/sbin/lldpcli
LLDPSOCKET=/var/run/lldpd.socket
LLDPD_CONF=/tmp/lldpd.conf
LLDPD_CONFS_DIR=/tmp/lldpd.d

find_release_info()
{
	[ -s /etc/os-release ] && . /etc/os-release
	[ -z "$PRETTY_NAME" ] && [ -s /etc/openwrt_version ] && \
		PRETTY_NAME="$(cat /etc/openwrt_version)"

	echo "${PRETTY_NAME:-Unknown Lede release} @ $(cat /proc/sys/kernel/hostname)"
}

write_lldpd_conf()
{
	. /lib/functions/network.sh

	local lldp_description

	config_load 'lldpd'
	config_get lldp_description 'config' 'lldp_description' "$(find_release_info)"

	local ifaces
	config_get ifaces 'config' 'interface'

	local iface ifnames=""
	for iface in $ifaces; do
		local ifname=""
		if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
			append ifnames "${ifname:-$iface}" ","
		fi
	done

	# Clear out the config file first
	echo -n > "$LLDPD_CONF"
	[ -n "$ifnames" ] && echo "configure system interface pattern" "$ifnames" >> "$LLDPD_CONF"
	[ -n "$lldp_description" ] && echo "configure system description" "\"$lldp_description\"" >> "$LLDPD_CONF"

	# Since lldpd's sysconfdir is /tmp, we'll symlink /etc/lldpd.d to /tmp/$LLDPD_CONFS_DIR
	[ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
}

start_service() {

	local enable_cdp
	local enable_fdp
	local enable_sonmp
	local enable_edp
	local lldp_class
	local lldp_location
	local readonly_mode
	local agentxsocket

	config_load 'lldpd'
	config_get_bool enable_cdp 'config' 'enable_cdp' 0
	config_get_bool enable_fdp 'config' 'enable_fdp' 0
	config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
	config_get_bool enable_edp 'config' 'enable_edp' 0
	config_get lldp_class 'config' 'lldp_class'
	config_get lldp_location 'config' 'lldp_location'
	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

	# 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
	procd_append_param command -d # don't daemonize, procd will handle that for us

	[ $enable_cdp -gt 0 ] && procd_append_param command '-c'
	[ $enable_fdp -gt 0 ] && procd_append_param command '-f'
	[ $enable_sonmp -gt 0 ] && procd_append_param command '-s'
	[ $enable_edp -gt 0 ] && procd_append_param command '-e'
	[ $readonly_mode -gt 0 ] && procd_append_param command '-r'
	[ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
	[ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"

	# set auto respawn behavior
	procd_set_param respawn
	procd_append_param respawn 3600
	procd_append_param respawn 5
	procd_append_param respawn -1
	procd_close_instance
}

service_running() {
	pgrep -x /usr/sbin/lldpd &> /dev/null
}

reload_service() {
	running || return 1
	$LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
		pause
		unconfigure lldp custom-tlv
		unconfigure system interface pattern
		unconfigure system description
	EOF
	# Rewrite lldpd.conf
	# If something changed it should be included by the lldpcli call
	write_lldpd_conf
	$LLDPCLI -u $LLDPSOCKET -c $LLDPD_CONF -c $LLDPD_CONFS_DIR &> /dev/null
	# Broadcast update over the wire
	$LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
		resume
		update
	EOF
	return 0
}

stop_service() {
	rm -rf /var/run/lldp $LLDPSOCKET
}