5d41539a7bd9278b8dbd23554f2adf5c6fe3d9b5
[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 port dns
25
26         config_get ipset "$cfg" ipset
27         [ -n "$ipset" ] || {
28                 echo "No ipset specified for instance $cfg" >&2
29                 return 1
30         }
31
32         config_get dns "$cfg" dns "$DEFNS"
33         [ -n "$dns" ] || {
34                 echo "No DNS server specified for instance $cfg" >&2
35                 return 1
36         }
37
38         config_get port "$cfg" port $((PORT++))
39
40         SERVICE_PID_FILE="/var/run/ipset-dns-$port.pid" \
41                 service_start /usr/sbin/ipset-dns "$ipset" "$port" "$dns"
42 }
43
44 start() {
45         PORT=53001
46         DEFNS="$(find_nameserver)"
47
48         # required by ipset-dns to not daemonize itself
49         export NO_DAEMONIZE=1
50
51         config_load ipset-dns
52         config_foreach start_instance ipset-dns
53 }
54
55 stop() {
56         local pid
57         for pid in /var/run/ipset-dns-*.pid; do
58                 [ -f "$pid" ] || continue
59                 SERVICE_PID_FILE="$pid" \
60                         service_stop /usr/sbin/ipset-dns
61                 rm -f "$pid"
62         done
63 }
64