1
0

lldpd.init 3.4 KB

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