summaryrefslogtreecommitdiff
blob: 177be76472405792dcc5c3da57d67aab97942b3a (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/sbin/runscript
# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-dns/bind/files/named.init-r7,v 1.3 2010/05/23 20:44:50 idl0r Exp $

opts="start stop reload restart"

depend() {
	need net
	use logger
	after pg_autovacuum postgresql mysql ldap
	provide dns
}

_mount() {
	local from
	local to
	local opts

	if [[ $# -lt 3 ]];
	then
		eerror "_mount(): to few arguments"
		return 1
	fi

	from=$1
	to=$2
	shift 2
	
	opts="${*}"
	shift $#

	if [[ -z $(grep "${to}" /proc/mounts) ]];
	then
		einfo "mounting ${from} to ${to}"
		mount ${from} ${to} ${opts} || return 1
	fi
}

_umount() {
	local dir=$1
	
	if [[ -n $(grep "${dir}" /proc/mounts) ]];
	then
		einfo "umount ${dir}"
		umount ${dir}
	fi
}

check_chroot() {
	if [[ -n ${CHROOT} ]]; then
		[[ ! -d ${CHROOT} ]] && return 1
		[[ ! -d ${CHROOT}/dev || ! -d ${CHROOT}/etc || ! -d ${CHROOT}/var ]] && return 1
		[[ ! -d ${CHROOT}/var/run || ! -d ${CHROOT}/var/log ]] && return 1
		[[ ! -d ${CHROOT}/etc/bind || ! -d ${CHROOT}/var/bind ]] && return 1
		[[ ! -d ${CHROOT}/var/run/named || ! -d ${CHROOT}/var/log/named ]] && return 1
		[[ ! -e ${CHROOT}/etc/localtime ]] && return 1
		[[ ! -c ${CHROOT}/dev/null || ! -c ${CHROOT}/dev/zero ]] && return 1
		[[ ! -c ${CHROOT}/dev/random && ! -c ${CHROOT}/dev/urandom ]] && return 1	
	fi
	return 0
}

checkconfig() {
	if [ ! -f ${CHROOT}/etc/bind/named.conf ] ; then
		eerror "No ${CHROOT}/etc/bind/named.conf file exists!"
	fi
	
	# In case someone have $CPU set in /etc/conf.d/named
	if [ ${CPU} ] ; then
		CPU="-n ${CPU}"
	fi

	# as suggested in bug #107724
	[ -n "${PIDFILE}" ] || PIDFILE=${CHROOT}$(\
		egrep -v \
			"^([[:cntrl:] ]+(#|//|/\*)|(#|//|/\*))" \
			${CHROOT}/etc/bind/named.conf \
				| egrep -o -m1 "pid\-file +\".+\" *;" \
				| cut -d\" -f2
	)

	KEY="${CHROOT}/etc/bind/rndc.key"
}

start() {
	ebegin "Starting ${CHROOT:+chrooted }named"

	if [[ -n ${CHROOT} ]];
	then
		check_chroot || {
			eerror "Your chroot dir ${CHROOT} is inconsistent, please run 'emerge --config net-dns/bind' first"
			return 1
		}
		einfo "Mounting chroot dirs"
		_mount /etc/bind ${CHROOT}/etc/bind -o bind
		_mount /var/bind ${CHROOT}/var/bind -o bind
		_mount /var/log/named ${CHROOT}/var/log/named -o bind
	fi

	checkconfig || return 1

	start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
		--nicelevel ${NAMED_NICELEVEL:-0} \
		--exec /usr/sbin/named \
		-- -u named ${CPU} ${OPTIONS} ${CHROOT:+-t} ${CHROOT}
	eend $?
}

stop() {
	local reported=0

	ebegin "Stopping ${CHROOT:+chrooted }named"
	checkconfig || return 2
	if [ -f $KEY ] ; then
		rndc $SERVER -k $KEY stop &>/dev/null
	else
		start-stop-daemon --stop --quiet --pidfile $PIDFILE \
			--exec /usr/sbin/named -- stop
	fi

	if [[ -n ${CHROOT} ]];
	then
		einfo "Umounting chroot dirs"

		# just to be sure everything gets clean
		while [[ -n $(fuser ${CHROOT} 2>&1) ]]
		do
			if [[ ${reported} -eq 0 ]];
			then
				einfo "Waiting until all named processes are stopped"
				reported=1
			fi
			sleep 1
		done

		_umount ${CHROOT}/etc/bind
		_umount ${CHROOT}/var/log/named
		_umount ${CHROOT}/var/bind
	fi

	eend $?
}

reload() {
	checkconfig || return 3
	if [ ! -f $PIDFILE ] ; then
		/etc/init.d/named start &>/dev/null
		exit
	fi

	if [ -f $KEY ] ; then
		ebegin "Reloading named.conf and zone files"
		rndc $SERVER -k $KEY reload &>/dev/null
		eend $?
	else /etc/init.d/named restart &>/dev/null
	fi
}