lldpd.init 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 libreCMC 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 lldp_hostname
  24. config_get lldp_hostname 'config' 'lldp_hostname' "$(cat /proc/sys/kernel/hostname)"
  25. local ifaces
  26. config_get ifaces 'config' 'interface'
  27. local iface ifnames=""
  28. for iface in $ifaces; do
  29. local ifname=""
  30. if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
  31. append ifnames "${ifname:-$iface}" ","
  32. fi
  33. done
  34. # Clear out the config file first
  35. echo -n > "$LLDPD_CONF"
  36. [ -n "$ifnames" ] && echo "configure system interface pattern" "$ifnames" >> "$LLDPD_CONF"
  37. [ -n "$lldp_description" ] && echo "configure system description" "\"$lldp_description\"" >> "$LLDPD_CONF"
  38. [ -n "$lldp_hostname" ] && echo "configure system hostname" "\"$lldp_hostname\"" >> "$LLDPD_CONF"
  39. # Since lldpd's sysconfdir is /tmp, we'll symlink /etc/lldpd.d to /tmp/$LLDPD_CONFS_DIR
  40. [ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
  41. }
  42. start_service() {
  43. local enable_cdp
  44. local enable_fdp
  45. local enable_sonmp
  46. local enable_edp
  47. local lldp_class
  48. local lldp_location
  49. local readonly_mode
  50. local agentxsocket
  51. config_load 'lldpd'
  52. config_get_bool enable_cdp 'config' 'enable_cdp' 0
  53. config_get_bool enable_fdp 'config' 'enable_fdp' 0
  54. config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
  55. config_get_bool enable_edp 'config' 'enable_edp' 0
  56. config_get lldp_class 'config' 'lldp_class'
  57. config_get lldp_location 'config' 'lldp_location'
  58. config_get_bool readonly_mode 'config' 'readonly_mode' 0
  59. config_get agentxsocket 'config' 'agentxsocket'
  60. mkdir -p /var/run/lldp
  61. chown lldp:lldp /var/run/lldp
  62. # When lldpd starts, it also loads up what we write in this config file
  63. write_lldpd_conf
  64. procd_open_instance
  65. procd_set_param command /usr/sbin/lldpd
  66. procd_append_param command -d # don't daemonize, procd will handle that for us
  67. [ $enable_cdp -gt 0 ] && procd_append_param command '-c'
  68. [ $enable_fdp -gt 0 ] && procd_append_param command '-f'
  69. [ $enable_sonmp -gt 0 ] && procd_append_param command '-s'
  70. [ $enable_edp -gt 0 ] && procd_append_param command '-e'
  71. [ $readonly_mode -gt 0 ] && procd_append_param command '-r'
  72. [ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
  73. [ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"
  74. # set auto respawn behavior
  75. procd_set_param respawn
  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. }