xref: /netbsd-src/external/bsd/nsd/dist/contrib/nsd.init (revision d83a80ee7fb31190352cf1f781441e06ca6a86db)
1#!/bin/sh
2#
3# nsdc.sh -- a shell script to manage the beast
4#
5# Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
6#
7# See LICENSE for the license.
8#
9# (numbers are runlevels startpriority killpriority).
10
11# chkconfig: 2345 45 74
12# description: NSD, authoritative only high performance name server.
13
14# configuration file default
15configfile="/etc/nsd.conf"
16
17# The directory where NSD binaries reside
18sbindir="/usr/sbin"
19
20#
21# You sure heard this many times before: NO USER SERVICEABLE PARTS BELOW
22#
23
24# see if user selects a different config file, with -c <filename>
25if test "x$1" = "x-c"; then
26	shift
27	if [ -e $1 ]; then
28		configfile=$1
29		shift
30	else
31		echo "`basename $0`: Config file "$1" does not exist."
32		exit 1
33	fi
34fi
35
36# locate nsd-checkconf : in sbindir, PATH, nsdc_dir or .
37nsd_checkconf=""
38if [ -e ${sbindir}/nsd-checkconf ]; then
39	nsd_checkconf=${sbindir}/nsd-checkconf
40else
41	if which nsd-checkconf >/dev/null 2>&1 ; then
42		if which nsd-checkconf 2>&1 | grep "^[Nn]o " >/dev/null; then
43			nsd_checkconf=""
44		else
45			nsd_checkconf=`which nsd-checkconf`
46		fi
47	fi
48	if [ -z "${nsd_checkconf}" -a -e `dirname $0`/nsd-checkconf ]; then
49		nsd_checkconf=`dirname $0`/nsd-checkconf
50	fi
51	if [ -z "${nsd_checkconf}" -a -e ./nsd-checkconf ]; then
52		nsd_checkconf=./nsd-checkconf
53	fi
54	if [ -z "${nsd_checkconf}" ]; then
55		echo "`basename $0`: Could not find nsd programs" \
56			"in $sbindir, in PATH=$PATH, in cwd=`pwd`," \
57			"or in dir of nsdc=`dirname $0`"
58		exit 1
59	fi
60fi
61
62usage() {
63	echo "Usage: `basename $0` [-c configfile] {start|stop|reload|restart|"
64	echo "				running}"
65	echo "options:"
66	echo "	-c configfile	Use specified configfile (default: @nsdconfigfile@)."
67	echo "commands:"
68	echo "	start		Start nsd server."
69	echo "	stop		Stop nsd server."
70	echo "	reload		Nsd server reloads database file."
71	echo "	restart		Stop the nsd server and start it again."
72	echo "	running		Prints message and exit nonzero if server not running."
73}
74
75# check the config syntax before using it
76${nsd_checkconf} ${configfile}
77if test $? -ne 0 ; then
78	usage
79	exit 1
80fi
81
82# Read some settings from the config file.
83pidfile=`${nsd_checkconf} -o pidfile ${configfile}`
84zonesdir=`${nsd_checkconf} -o zonesdir ${configfile}`
85sbindir=`dirname ${nsd_checkconf}`
86
87# move to zonesdir (if specified), and make absolute pathnames.
88if test -n "${zonesdir}"; then
89       zonesdir=`dirname ${zonesdir}/.`
90       if echo "${zonesdir}" | grep "^[^/]" >/dev/null; then
91               zonesdir=`pwd`/${zonesdir}
92       fi
93       if echo "${pidfile}" | grep "^[^/]" >/dev/null; then
94               pidfile=${zonesdir}/${pidfile}
95       fi
96fi
97
98# for bash: -C or noclobber. For tcsh: noclobber. For bourne: -C.
99noclobber_set="set -C"
100# ugly check for tcsh
101if echo @shell@ | grep tcsh >/dev/null; then
102	noclobber_set="set noclobber"
103fi
104
105#
106# useful routines
107#
108signal() {
109	if [ -s ${pidfile} ]
110	then
111		kill -"$1" `cat ${pidfile}` && return 0
112	else
113		echo "nsd is not running"
114	fi
115	return 1
116}
117
118do_start() {
119	if test -x ${sbindir}/nsd; then
120		${sbindir}/nsd -c ${configfile}
121		test $? = 0 || (echo "nsd startup failed."; exit 1)
122	else
123		echo "${sbindir}/nsd not an executable file, nsd startup failed."; exit 1
124	fi
125}
126
127controlled_sleep() {
128	if [ $1 -ge 25 ]; then
129		sleep 1
130	fi
131}
132
133controlled_stop() {
134	pid=$1
135	try=1
136
137	while [ $try -ne 0 ]; do
138		if [ ${try} -gt 50 ]; then
139			echo "nsdc stop failed"
140			return 1
141		else
142			if [ $try -eq 1 ]; then
143				kill -TERM ${pid}
144			else
145				kill -TERM ${pid} >/dev/null 2>&1
146			fi
147
148			# really stopped?
149			kill -0 ${pid} >/dev/null 2>&1
150			if [ $? -eq 0 ]; then
151				controlled_sleep ${try}
152				try=`expr ${try} + 1`
153			else
154				try=0
155			fi
156		fi
157	done
158
159	return 0
160}
161
162do_controlled_stop() {
163	if [ -s ${pidfile} ]; then
164		pid=`cat ${pidfile}`
165		controlled_stop ${pid} && return 0
166	else
167		echo "nsd is not running, starting anyway" && return 0
168	fi
169	return 1
170}
171
172do_stop() {
173	signal "TERM"
174}
175
176do_reload() {
177	signal "HUP"
178}
179
180case "$1" in
181start)
182	if test -s ${pidfile} && kill -"0" `cat ${pidfile}`
183	then
184		(echo "process `cat ${pidfile}` exists, please use restart"; exit 1)
185	else
186		do_start
187	fi
188	;;
189stop)
190	do_stop
191	;;
192stats)
193	signal "USR1"
194	;;
195reload)
196	do_reload
197	;;
198running)
199	signal "0"
200	;;
201restart)
202	do_controlled_stop && do_start
203	;;
204*)
205	usage
206	;;
207esac
208
209exit $?
210