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