lldpd启动脚本分析
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org
#启动顺序
START=90
#创建PID文件
SERVICE_USE_PID=1
find_release_info()
{
[ -s /etc/openwrt_release ] && . /etc/openwrt_release
[ -z "$DISTRIB_DESCRIPTION" ] && [ -s /etc/openwrt_version ] && \
DISTRIB_DESCRIPTION="$(cat /etc/openwrt_version)"
echo "${DISTRIB_DESCRIPTION:-Unknown OpenWrt release} @ $(cat /proc/sys/kernel/hostname)"
}
# 启动
start() {
#需要调用network.sh中的函数,因此加载文件
. /lib/functions/network.sh
#定义变量
local enable_cdp
local enable_fdp
local enable_sonmp
local enable_edp
local lldp_class
local lldp_location
local lldp_description
#需要从/etc/config/lldpd配置文件中取变量,因此加载文件
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 lldp_description 'config' 'lldp_description' "$(find_release_info)"
local ifaces
config_get ifaces 'config' 'interface'
local iface
for iface in $ifaces; do
local ifname=""
if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
append args "-I ${ifname:-$iface}"
fi
done
[ $enable_cdp -gt 0 ] && append args '-c'
[ $enable_fdp -gt 0 ] && append args '-f'
[ $enable_sonmp -gt 0 ] && append args '-s'
[ $enable_edp -gt 0 ] && append args '-e'
user_exists lldp 121 || user_add lldp 121 129
group_exists lldp 129 || group_add lldp 129
#创建文件夹,并加权限
mkdir -p /var/run/lldp
chown lldp:lldp /var/run/lldp
#拉起进程
service_start /usr/sbin/lldpd $args \
${lldp_class:+ -M $lldp_class} \
${lldp_description:+ -S "$lldp_description"}
[ -n "$lldp_location" ] && {
sleep 1
/usr/sbin/lldpctl -L "$lldp_location" > /dev/null 2>&1
}
}
#停止
stop()
{
service_stop /usr/sbin/lldpd
rm -f /var/run/lldpd.socket /var/run/lldpd.pid
}