summaryrefslogtreecommitdiff
path: root/package/network/services/ipset-dns/files/ipset-dns.init
blob: 7b732791eae34ed4609a90ac5f0d7d4127dc3f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh /etc/rc.common
# Copyright (C) 2013 OpenWrt.org

START=61

SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1


find_nameserver() {
	. /lib/functions/network.sh

	local tmp
	if network_find_wan tmp && network_get_dnsserver tmp "$tmp"; then
		echo "${tmp%% *}"
		return 0
	fi

	return 1
}

start_instance() {
	local cfg="$1"
	local ipset ipset6 port dns

	config_get ipset "$cfg" ipset
	config_get ipset6 "$cfg" ipset6
	[ -n "$ipset$ipset6" ] || {
		echo "No ipset specified for instance $cfg" >&2
		return 1
	}

	config_get dns "$cfg" dns "$DEFNS"
	[ -n "$dns" ] || {
		echo "No DNS server specified for instance $cfg" >&2
		return 1
	}

	config_get port "$cfg" port $((PORT++))

	SERVICE_PID_FILE="/var/run/ipset-dns-$port.pid" \
		service_start /usr/sbin/ipset-dns "$ipset" "$ipset6" "$port" "$dns"
}

start() {
	PORT=53001
	DEFNS="$(find_nameserver)"

	# required by ipset-dns to not daemonize itself
	export NO_DAEMONIZE=1

	config_load ipset-dns
	config_foreach start_instance ipset-dns
}

stop() {
	local pid
	for pid in /var/run/ipset-dns-*.pid; do
		[ -f "$pid" ] || continue
		SERVICE_PID_FILE="$pid" \
			service_stop /usr/sbin/ipset-dns
		rm -f "$pid"
	done
}