| 1 |
#!/sbin/runscript |
| 2 |
# Copyright 1999-2010 Gentoo Foundation |
| 3 |
# Distributed under the terms of the GNU General Public License v2 |
| 4 |
# $Header: /var/cvsroot/gentoo-x86/net-dns/bind/files/named.init-r9,v 1.2 2010/12/15 23:35:09 idl0r Exp $ |
| 5 |
|
| 6 |
opts="start stop reload restart checkconfig checkzones" |
| 7 |
|
| 8 |
depend() { |
| 9 |
need net |
| 10 |
use logger |
| 11 |
provide dns |
| 12 |
} |
| 13 |
|
| 14 |
NAMED_CONF=${CHROOT}/etc/bind/named.conf |
| 15 |
|
| 16 |
_mount() { |
| 17 |
local from |
| 18 |
local to |
| 19 |
local opts |
| 20 |
|
| 21 |
if [ "${#}" -lt 3 ]; then |
| 22 |
eerror "_mount(): to few arguments" |
| 23 |
return 1 |
| 24 |
fi |
| 25 |
|
| 26 |
from=$1 |
| 27 |
to=$2 |
| 28 |
shift 2 |
| 29 |
|
| 30 |
opts="${*}" |
| 31 |
shift $# |
| 32 |
|
| 33 |
if [ -z "$(awk "\$2 == \"${to}\" { print \$2 }" /proc/mounts)" ]; then |
| 34 |
einfo "mounting ${from} to ${to}" |
| 35 |
mount ${from} ${to} ${opts} || return 1 |
| 36 |
fi |
| 37 |
|
| 38 |
return 0 |
| 39 |
} |
| 40 |
|
| 41 |
_umount() { |
| 42 |
local dir=$1 |
| 43 |
|
| 44 |
if [ -n "$(awk "\$2 == \"${dir}\" { print \$2 }" /proc/mounts)" ]; then |
| 45 |
einfo "umount ${dir}" |
| 46 |
umount ${dir} || return 1 |
| 47 |
fi |
| 48 |
|
| 49 |
return 0 |
| 50 |
} |
| 51 |
|
| 52 |
_get_pidfile() { |
| 53 |
# as suggested in bug #107724, bug 335398#c17 |
| 54 |
[ -n "${PIDFILE}" ] || PIDFILE=${CHROOT}$(\ |
| 55 |
/usr/sbin/named-checkconf -p ${CHROOT:+-t} ${CHROOT} ${NAMED_CONF/${CHROOT}} | grep 'pid-file' | cut -d\" -f2) |
| 56 |
[ -z "${PIDFILE}" ] && PIDFILE=${CHROOT}/var/run/named/named.pid |
| 57 |
} |
| 58 |
|
| 59 |
check_chroot() { |
| 60 |
if [ -n "${CHROOT}" ]; then |
| 61 |
[ ! -d "${CHROOT}" ] && return 1 |
| 62 |
[ ! -d "${CHROOT}/dev" ] || [ ! -d "${CHROOT}/etc" ] || [ ! -d "${CHROOT}/var" ] && return 1 |
| 63 |
[ ! -d "${CHROOT}/var/run" ] || [ ! -d "${CHROOT}/var/log" ] && return 1 |
| 64 |
[ ! -d "${CHROOT}/etc/bind" ] || [ ! -d "${CHROOT}/var/bind" ] && return 1 |
| 65 |
[ ! -d "${CHROOT}/var/log/named" ] && return 1 |
| 66 |
[ ! -e "${CHROOT}/etc/localtime" ] && return 1 |
| 67 |
[ ! -c "${CHROOT}/dev/null" ] || [ ! -c "${CHROOT}/dev/zero" ] && return 1 |
| 68 |
[ ! -c "${CHROOT}/dev/random" ] && [ ! -c "${CHROOT}/dev/urandom" ] && return 1 |
| 69 |
[ "${CHROOT_GEOIP:-0}" -eq 1 ] && [ ! -d "${CHROOT}/usr/share/GeoIP" ] && return 1 |
| 70 |
fi |
| 71 |
|
| 72 |
return 0 |
| 73 |
} |
| 74 |
|
| 75 |
checkconfig() { |
| 76 |
ebegin "Checking named configuration" |
| 77 |
|
| 78 |
if [ ! -f "${NAMED_CONF}" ] ; then |
| 79 |
eerror "No ${NAMED_CONF} file exists!" |
| 80 |
return 1 |
| 81 |
fi |
| 82 |
|
| 83 |
/usr/sbin/named-checkconf ${CHROOT:+-t} ${CHROOT} ${NAMED_CONF/${CHROOT}} || { |
| 84 |
eerror "named-checkconf failed! Please fix your config first." |
| 85 |
return 1 |
| 86 |
} |
| 87 |
|
| 88 |
eend 0 |
| 89 |
return 0 |
| 90 |
} |
| 91 |
|
| 92 |
checkzones() { |
| 93 |
ebegin "Checking named configuration and zones" |
| 94 |
/usr/sbin/named-checkconf -z -j ${CHROOT:+-t} ${CHROOT} ${NAMED_CONF/${CHROOT}} |
| 95 |
eend $? |
| 96 |
} |
| 97 |
|
| 98 |
start() { |
| 99 |
local piddir |
| 100 |
|
| 101 |
ebegin "Starting ${CHROOT:+chrooted }named" |
| 102 |
|
| 103 |
if [ -n "${CHROOT}" ]; then |
| 104 |
check_chroot || { |
| 105 |
eend 1 |
| 106 |
eerror "Your chroot dir ${CHROOT} is inconsistent, please run 'emerge --config net-dns/bind' first" |
| 107 |
return 1 |
| 108 |
} |
| 109 |
|
| 110 |
if [ "${CHROOT_NOMOUNT:-0}" -eq 0 ]; then |
| 111 |
einfo "Mounting chroot dirs" |
| 112 |
_mount /etc/bind ${CHROOT}/etc/bind -o bind |
| 113 |
_mount /var/bind ${CHROOT}/var/bind -o bind |
| 114 |
_mount /var/log/named ${CHROOT}/var/log/named -o bind |
| 115 |
if [ "${CHROOT_GEOIP:-0}" -eq 1 ]; then |
| 116 |
_mount /usr/share/GeoIP ${CHROOT}/usr/share/GeoIP -o bind |
| 117 |
fi |
| 118 |
fi |
| 119 |
fi |
| 120 |
|
| 121 |
checkconfig || { eend 1; return 1; } |
| 122 |
|
| 123 |
# create piddir (usually /var/run/named) if necessary, bug 334535 |
| 124 |
_get_pidfile |
| 125 |
piddir="${PIDFILE%/*}" |
| 126 |
if [ ! -d "${piddir}" ]; then |
| 127 |
checkpath -q -d -o root:named -m 0770 "${piddir}" || { |
| 128 |
eend 1 |
| 129 |
return 1 |
| 130 |
} |
| 131 |
fi |
| 132 |
|
| 133 |
# In case someone have $CPU set in /etc/conf.d/named |
| 134 |
if [ -n "${CPU}" ] && [ "${CPU}" -gt 0 ]; then |
| 135 |
CPU="-n ${CPU}" |
| 136 |
fi |
| 137 |
|
| 138 |
start-stop-daemon --start --pidfile ${PIDFILE} \ |
| 139 |
--nicelevel ${NAMED_NICELEVEL:-0} \ |
| 140 |
--exec /usr/sbin/named \ |
| 141 |
-- -u named ${CPU} ${OPTIONS} ${CHROOT:+-t} ${CHROOT} |
| 142 |
eend $? |
| 143 |
} |
| 144 |
|
| 145 |
stop() { |
| 146 |
local reported=0 |
| 147 |
|
| 148 |
ebegin "Stopping ${CHROOT:+chrooted }named" |
| 149 |
|
| 150 |
# Workaround for now, until openrc's restart has been fixed. |
| 151 |
# openrc doesn't care about a restart() function in init scripts. |
| 152 |
if [ "${RC_CMD}" = "restart" ]; then |
| 153 |
checkconfig || { eend 1; return 1; } |
| 154 |
fi |
| 155 |
|
| 156 |
# -R 10, bug 335398 |
| 157 |
_get_pidfile |
| 158 |
start-stop-daemon --stop --retry 10 --pidfile $PIDFILE \ |
| 159 |
--exec /usr/sbin/named |
| 160 |
|
| 161 |
if [ -n "${CHROOT}" ] && [ "${CHROOT_NOMOUNT:-0}" -eq 0 ]; then |
| 162 |
ebegin "Umounting chroot dirs" |
| 163 |
|
| 164 |
# just to be sure everything gets clean |
| 165 |
while fuser -s ${CHROOT} 2>/dev/null; do |
| 166 |
if [ "${reported}" -eq 0 ]; then |
| 167 |
einfo "Waiting until all named processes are stopped" |
| 168 |
reported=1 |
| 169 |
fi |
| 170 |
sleep 1 |
| 171 |
done |
| 172 |
|
| 173 |
[ "${CHROOT_GEOIP:-0}" -eq 1 ] && _umount ${CHROOT}/usr/share/GeoIP |
| 174 |
_umount ${CHROOT}/etc/bind |
| 175 |
_umount ${CHROOT}/var/log/named |
| 176 |
_umount ${CHROOT}/var/bind |
| 177 |
fi |
| 178 |
|
| 179 |
eend $? |
| 180 |
} |
| 181 |
|
| 182 |
reload() { |
| 183 |
local ret |
| 184 |
|
| 185 |
ebegin "Reloading named.conf and zone files" |
| 186 |
|
| 187 |
checkconfig || { eend 1; return 1; } |
| 188 |
|
| 189 |
_get_pidfile |
| 190 |
if [ -n "${PIDFILE}" ]; then |
| 191 |
# FIXME: Remove --stop and --oknodo as soon as baselayout-1 has been removed... finally... |
| 192 |
start-stop-daemon --stop --oknodo --pidfile $PIDFILE --signal HUP --exec /usr/sbin/named |
| 193 |
ret=$? |
| 194 |
else |
| 195 |
ewarn "Unable to determine the pidfile... this is" |
| 196 |
ewarn "a fallback mode. Please check your installation!" |
| 197 |
|
| 198 |
$RC_SERVICE restart |
| 199 |
ret=$? |
| 200 |
fi |
| 201 |
|
| 202 |
eend $ret |
| 203 |
} |