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