procd: convert various packages to procd style init.d scripts
[openwrt/svn-archive/archive.git] / package / network / services / ipset-dns / files / ipset-dns.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2013 OpenWrt.org
3
4 START=61
5
6 USE_PROCD=1
7
8 find_nameserver() {
9 . /lib/functions/network.sh
10
11 local tmp
12 if network_find_wan tmp && network_get_dnsserver tmp "$tmp"; then
13 echo "${tmp%% *}"
14 return 0
15 fi
16
17 return 1
18 }
19
20 start_instance() {
21 local cfg="$1"
22 local ipset ipset6 port dns
23
24 config_get ipset "$cfg" ipset
25 config_get ipset6 "$cfg" ipset6
26 [ -n "$ipset$ipset6" ] || {
27 echo "No ipset specified for instance $cfg" >&2
28 return 1
29 }
30
31 config_get dns "$cfg" dns "$DEFNS"
32 [ -n "$dns" ] || {
33 echo "No DNS server specified for instance $cfg" >&2
34 return 1
35 }
36
37 config_get port "$cfg" port $((PORT++))
38
39 procd_open_instance
40 procd_set_param command /usr/sbin/ipset-dns "$ipset" "$ipset6" "$port" "$dns"
41 procd_set_param env NO_DAEMONIZE=1
42 procd_set_param respawn
43 procd_close_instance
44 }
45
46 service_triggers()
47 {
48 procd_add_reload_trigger "ipset-dns"
49 }
50
51 start_service() {
52 PORT=53001
53 DEFNS="$(find_nameserver)"
54
55 config_load ipset-dns
56 config_foreach start_instance ipset-dns
57 }