7b732791eae34ed4609a90ac5f0d7d4127dc3f99
[openwrt.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 SERVICE_DAEMONIZE=1
7 SERVICE_WRITE_PID=1
8
9
10 find_nameserver() {
11         . /lib/functions/network.sh
12
13         local tmp
14         if network_find_wan tmp && network_get_dnsserver tmp "$tmp"; then
15                 echo "${tmp%% *}"
16                 return 0
17         fi
18
19         return 1
20 }
21
22 start_instance() {
23         local cfg="$1"
24         local ipset ipset6 port dns
25
26         config_get ipset "$cfg" ipset
27         config_get ipset6 "$cfg" ipset6
28         [ -n "$ipset$ipset6" ] || {
29                 echo "No ipset specified for instance $cfg" >&2
30                 return 1
31         }
32
33         config_get dns "$cfg" dns "$DEFNS"
34         [ -n "$dns" ] || {
35                 echo "No DNS server specified for instance $cfg" >&2
36                 return 1
37         }
38
39         config_get port "$cfg" port $((PORT++))
40
41         SERVICE_PID_FILE="/var/run/ipset-dns-$port.pid" \
42                 service_start /usr/sbin/ipset-dns "$ipset" "$ipset6" "$port" "$dns"
43 }
44
45 start() {
46         PORT=53001
47         DEFNS="$(find_nameserver)"
48
49         # required by ipset-dns to not daemonize itself
50         export NO_DAEMONIZE=1
51
52         config_load ipset-dns
53         config_foreach start_instance ipset-dns
54 }
55
56 stop() {
57         local pid
58         for pid in /var/run/ipset-dns-*.pid; do
59                 [ -f "$pid" ] || continue
60                 SERVICE_PID_FILE="$pid" \
61                         service_stop /usr/sbin/ipset-dns
62                 rm -f "$pid"
63         done
64 }
65