relay.init 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (c) 2011-2012 OpenWrt.org
  3. START=80
  4. USE_PROCD=1
  5. PROG=/usr/sbin/relayd
  6. validate_proto_relayd()
  7. {
  8. uci_validate_section network "interface" "${1}" \
  9. 'network:list(string)' \
  10. 'expiry:uinteger:30' \
  11. 'retry:uinteger:5' \
  12. 'table:range(0, 65535):16800' \
  13. 'forward_bcast:bool:1' \
  14. 'forward_dhcp:bool:1'
  15. }
  16. resolve_ifname() {
  17. grep -qs "^ *$1:" /proc/net/dev && {
  18. procd_append_param command -I "$1"
  19. append ifaces "$1"
  20. }
  21. }
  22. resolve_network() {
  23. local ifn
  24. fixup_interface "$1"
  25. config_get ifn "$1" ifname
  26. [ -z "$ifn" ] && return 1
  27. resolve_ifname "$ifn"
  28. }
  29. start_relay() {
  30. local cfg="$1"
  31. local args=""
  32. local ifaces=""
  33. config_get proto "$cfg" proto
  34. [ "$proto" = "relay" ] || return 0
  35. config_get_bool disabled "$cfg" disabled 0
  36. [ "$disabled" -gt 0 ] && return 0
  37. SERVICE_DAEMONIZE=1
  38. SERVICE_WRITE_PID=1
  39. SERVICE_PID_FILE="/var/run/relay-$cfg.pid"
  40. [ -f "$SERVICE_PID_FILE" ] && {
  41. if grep -q relayd "/proc/$(cat $SERVICE_PID_FILE)/cmdline"; then
  42. return 0
  43. else
  44. rm -f "$SERVICE_PID_FILE"
  45. fi
  46. }
  47. procd_open_instance
  48. procd_set_param command "$PROG"
  49. local net networks
  50. config_get networks "$cfg" network
  51. for net in $networks; do
  52. resolve_network "$net" || {
  53. return 1
  54. }
  55. done
  56. local ifn ifnames
  57. config_get ifnames "$cfg" ifname
  58. for ifn in $ifnames; do
  59. resolve_ifname "$ifn"
  60. done
  61. local ipaddr
  62. config_get ipaddr "$cfg" ipaddr
  63. [ -n "$ipaddr" ] && procd_append_param command -L "$ipaddr"
  64. local gateway
  65. config_get gateway "$cfg" gateway
  66. [ -n "$gateway" ] && procd_append_param command -G "$gateway"
  67. local expiry # = 30
  68. config_get expiry "$cfg" expiry
  69. [ -n "$expiry" ] && procd_append_param command "$expiry"
  70. local retry # = 5
  71. config_get retry "$cfg" retry
  72. [ -n "$retry" ] && procd_append_param command -p "$retry"
  73. local table # = 16800
  74. config_get table "$cfg" table
  75. [ -n "$table" ] && procd_append_param command -T "$table"
  76. local fwd_bcast # = 1
  77. config_get_bool fwd_bcast "$cfg" forward_bcast 1
  78. [ $fwd_bcast -eq 1 ] && procd_append_param command "-B"
  79. local fwd_dhcp # = 1
  80. config_get_bool fwd_dhcp "$cfg" forward_dhcp 1
  81. [ $fwd_dhcp -eq 1 ] && procd_append_param command "-D"
  82. procd_close_instance
  83. }
  84. service_triggers()
  85. {
  86. procd_add_reload_trigger "network"
  87. }
  88. start_service() {
  89. include /lib/network
  90. config_load network
  91. config_foreach start_relay interface
  92. }