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