summaryrefslogtreecommitdiff
path: root/nonsystemd/unbound-openrc/unbound.initd
blob: df20216b97787d0ff7774654be0c0c9d256e8ea1 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/openrc-run
# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

UNBOUND_BINARY=${UNBOUND_BINARY:-"/usr/bin/unbound"}
UNBOUND_CACHEFILE=${UNBOUND_CACHEFILE:-"/var/lib/unbound/${SVCNAME}.cache"}
UNBOUND_CHECKCONF=${UNBOUND_CHECKCONF:-"/usr/bin/unbound-checkconf"}
UNBOUND_CONFFILE=${UNBOUND_CONFFILE:-"/etc/unbound/${SVCNAME}.conf"}
UNBOUND_CONTROL=${UNBOUND_CONTROL:-"/usr/bin/unbound-control"}
UNBOUND_PIDFILE=${UNBOUND_PIDFILE:-"/run/unbound.pid"}
UNBOUND_SSDARGS=${UNBOUND_SSDARGS:-"--wait 1000"}
UNBOUND_TERMTIMEOUT=${UNBOUND_TERMTIMEOUT:-"TERM/25/KILL/5"}
UNBOUND_OPTS=${UNBOUND_OPTS:-""}
UNBOUND_LOAD_CACHE_TIMEOUT=${UNBOUND_LOAD_CACHE_TIMEOUT:-"30"}

getconfig() {
	local key="$1"
	local value_default="$2"
	local value=

	if service_started ; then
		value="$(service_get_value "${key}")"
	fi

	if [ -z "${value}" ] && [ -n "${UNBOUND_CONFFILE}" ] && [ -r "${UNBOUND_CONFFILE}" ] ; then
		value=$("${UNBOUND_CHECKCONF}" -o ${key} "${UNBOUND_CONFFILE}")
	fi

	if [ -z "${value}" ] ; then
		# Value not explicitly set in the configfile or configfile does not exist
		# or is not readable
		echo "${value_default}"
	else
		echo "${value}"
	fi

	return 0
}

command=${UNBOUND_BINARY}
command_args="${UNBOUND_OPTS} -c \"${UNBOUND_CONFFILE}\""
start_stop_daemon_args="${UNBOUND_SSDARGS}"
pidfile="$(getconfig pidfile /run/unbound.pid)"
retry="${UNBOUND_TERMTIMEOUT}"

required_files="${UNBOUND_CONFFILE}"

name="unbound daemon"
extra_commands="configtest"
extra_started_commands="reload save_cache"
description="unbound is a Domain Name Server (DNS) that is used to resolve host names to IP address."
description_configtest="Run syntax tests for configuration files only."
description_reload="Kills all children and reloads the configuration."
description_save_cache="Saves the current cache to disk."

depend() {
	use net logger
	provide dns
	after auth-dns
}

configtest() {
	local _config_status=

	ebegin "Checking ${SVCNAME} configuration"
	"${UNBOUND_CHECKCONF}" "${UNBOUND_CONFFILE}" 1>/dev/null 2>&1
	_config_status=$?

	if [ ${_config_status} -ne 0 ] ; then
		# Run command again but this time we will show the output
		# Ugly, but ...
		"${UNBOUND_CHECKCONF}" "${UNBOUND_CONFFILE}"
	else
		if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
			local _is_control_enabled=$(getconfig control-enable no)
			if [ "${_is_control_enabled}" != "yes" ] ; then
				eerror "Cannot preserve cache: control-enable is 'no' in the config file!"
				_config_status=2
			fi
		fi
	fi

	eend ${_config_status} "failed, please correct errors above"
}

save_cache() {
	if [ "${RC_CMD}" != "restart" ] ; then
		UNBOUND_PRESERVE_CACHE=1 configtest || return 1
	fi

	ebegin "Saving cache to '${UNBOUND_CACHEFILE}'"
	${UNBOUND_CONTROL} -c "${UNBOUND_CONFFILE}" dump_cache > "${UNBOUND_CACHEFILE}"
	eend $?
}

start_pre() {
	if [ "${RC_CMD}" != "restart" ] ; then
		configtest || return 1
	fi
}

start_post() {
	if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
		if [ -s "${UNBOUND_CACHEFILE}" ] ; then
			ebegin "Loading cache from '${UNBOUND_CACHEFILE}'"
			# Loading cache can fail which would block this runscript.
			# Using `timeout` from coreutils will be our safeguard ...
			timeout -k 5 ${UNBOUND_LOAD_CACHE_TIMEOUT} ${UNBOUND_CONTROL} -q -c "${UNBOUND_CONFFILE}" load_cache < "${UNBOUND_CACHEFILE}"
			eend $?
		else
			ewarn "Loading cache from '${UNBOUND_CACHEFILE}' skipped: File does not exists or is empty!"
		fi
	fi

	# It is not a fatal error if preserved cache could not be loaded
	return 0
}

stop_pre() {
	if [ "${RC_CMD}" = "restart" ] ; then
		configtest || return 1
	fi

	if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
		save_cache
	fi

	# It is not a fatal error if cache cannot be preserved
	return 0
}

reload() {
	configtest || return 1
	ebegin "Reloading ${SVCNAME}"
	start-stop-daemon --signal HUP --pidfile "${pidfile}"
	eend $?
}