Subject: pkg/17484: net/bind8 rc.d script doesn't work on older systems
To: None <gnats-bugs@gnats.netbsd.org>
From: Greg A. Woods <woods@weird.com>
List: netbsd-bugs
Date: 07/05/2002 02:27:29
>Number:         17484
>Category:       pkg
>Synopsis:       net/bind8 rc.d script doesn't work on older systems
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    pkg-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 04 23:28:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Greg A. Woods
>Release:        pkgsrc-2002/07/03
>Organization:
Planix, Inc.; Toronto, Ontario; Canada
>Environment:
System: NetBSD
>Description:

	the rc.d script included in net/bind8 doesn't work on older
	systems and even fails strangely on newer systems when 'named'
	is configured to start with custom parameters such as '-u dns'

	this version has been tested on 1.3.2, 1.3.3, 1.4, and 1.5W.

	additionally this version supports a "status" function to check
	the running named process, and allows for defaulting the named
	parameters without confusing the ndc command (which should be
	used to reload and check the running named so that non-root
	users who are authorised to use 'ndc' can also use this script).

>How-To-Repeat:

>Fix:

Index: net/bind8/files/named.sh
===================================================================
RCS file: /cvs/master/m-NetBSD/main/pkgsrc/net/bind8/files/named.sh,v
retrieving revision 1.1
diff -c -r1.1 named.sh
*** net/bind8/files/named.sh	24 Dec 2001 14:10:54 -0000	1.1
--- net/bind8/files/named.sh	4 Apr 2002 23:54:01 -0000
***************
*** 1,4 ****
! #!/bin/sh
  #
  # $NetBSD: named.sh,v 1.1 2001/12/24 14:10:54 abs Exp $
  #
--- 1,4 ----
! #! /bin/sh
  #
  # $NetBSD: named.sh,v 1.1 2001/12/24 14:10:54 abs Exp $
  #
***************
*** 11,80 ****
  
  name="named"
  rcvar=$name
! command="@PREFIX@/sbin/ndc"
  
! named_start=start
  
! # set defaults
! if [ -r /etc/rc.conf ]
! then
  	. /etc/rc.conf
  else
- 	eval ${rcvar}=YES
- fi
  
! # $flags from environment overrides ${rcvar}_flags
! if [ -n "${flags}" ]
! then
! 	eval ${rcvar}_flags="${flags}"
! fi
! 
! named_doit()
! {
! 	action=$1
! 
! 	case ${action} in
! 	start)		echo -n "Starting ${name}: "; action=${named_start} ;;
! 	stop)		echo "Stopping ${name}" ;;
! 	restart)	echo -n "Restarting ${name}: " ;;
! 	esac
  
! 	${command} ${named_flags} ${command_args} ${action}
! }
  
! checkyesno()
! {
! 	eval _value=\$${1}
! 	case $_value in
! 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)	return 0 ;;
! 	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)	return 1 ;;
! 	*)
! 		echo "\$${1} is not set properly."
! 		return 1
! 		;;
! 	esac
! }
  
! cmd=${1:-start}
! case ${cmd} in
! force*)
! 	cmd=${cmd#force}
! 	eval ${rcvar}=YES
! 	;;
! esac
! 
! if checkyesno ${rcvar}
! then
! 	if [ -x ${command} ]
! 	then
! 		case ${cmd} in
! 		restart|start|stop|reload)
! 			${rcvar}_doit ${cmd}
! 			;;
! 		*)
! 			echo 1>&2 "Usage: $0 [restart|start|stop|reload]"
! 			exit 1
  			;;
  		esac
  	fi
  fi
--- 11,212 ----
  
  name="named"
  rcvar=$name
! command="@PREFIX@/sbin/${name}"
! pidfile="/var/run/${name}.pid"
! required_files="/etc/${name}.conf"
  
! named_flags="-u dns"		# default setting -- override in /etc/rc.conf
  
! named_controller_cmd="@PREFIX@/sbin/ndc"
! named_controller_args=""	# default setting -- override in /etc/rc.conf
! 
! reload_cmd="${named_controller_cmd} ${named_controller_args} reload"
! status_cmd="${named_controller_cmd} ${named_controller_args} status"
! 
! # is this NetBSD (1.4V|1.5) [2000/03/10] or newer?
! # 
! if [ -f /etc/rc.subr -a -f /etc/rc.conf -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
! 
! 	. /etc/rc.subr
  	. /etc/rc.conf
+ 
+ 	extra_commands="reload status"
+ 
+ 	# is this NetBSD 1.4Z [2000/05/13] or newer?
+ 	# 
+ 	if type load_rc_config > /dev/null 2>&1 ; then
+ 		load_rc_config ${name}
+ 	fi
+ 	run_rc_command "$1"
+ 
+ # if not then it must be old NetBSD, Solaris, Linux, etc...
+ # 
  else
  
! 	if [ -f /etc/rc.subr ] ; then
! 		. /etc/rc.subr	# NetBSD-1.3 has older checkyesno variant
! 	fi
  
! 	if expr "$(type warn)" : '.* is a shell function$' >/dev/null 2>&1 ; then
! 		:
! 	else
! 		warn ()
! 		{
! 			if expr "$(type logger)" : '.* is .*/logger$' >/dev/null 2>&1 ; then
! 				logger -s -p daemon.warning -t rc "$0: WARNING: $*"
! 			else
! 				echo "$0: WARNING: $*" 1>&2
! 			fi
! 		}
! 	fi
  
! 	# we may not have/want to be so paranoid of overriding 'checkyesno'...
! 	#
! 	if expr "$(type warn)" : '.* is a shell function$' >/dev/null 2>&1 ; then
! 		:
! 	else
! 		# Test contents of $1, as a variable, for on or off values;
! 		# and warn if not set appropriately.
! 		#
! 		checkyesno ()
! 		{
! 			eval _value=\${${1}}
! 			case ${_value} in
! 			[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
! 				return 0
! 				;;
! 			[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
! 				return 1
! 				;;
! 			*)
! 				warn "\${${1}} is not set properly."
! 				return 1
! 				;;
! 			esac
! 		}
! 	fi
  
! 	# wait_for_pids pid [pid ...]
! 	#	spins until none of the pids exist
! 	#
! 	wait_for_pids()
! 	{
! 		_list=$*
! 		if [ -z "$_list" ]; then
! 			return
! 		fi
! 		_prefix=
! 		while true; do
! 			_nlist="";
! 			for _j in $_list; do
! 				if kill -0 $_j 2>/dev/null; then
! 					_nlist="${_nlist}${_nlist:+ }$_j"
! 				fi
! 			done
! 			if [ -z "$_nlist" ]; then
! 				break
! 			fi
! 			_list=$_nlist
! 			echo -n ${_prefix:-"Waiting for PIDS: "}$_list
! 			_prefix=", "
! 			sleep 2
! 		done
! 		if [ -n "$_prefix" ]; then
! 			echo "."
! 		fi
! 	}
! 
! 	# On systems without an /etc/rc.conf the default action must
! 	# be to start the daemon....
! 	#
! 	if [ -f /etc/rc.conf ] ; then
! 		. /etc/rc.conf
! 		cmd=${1}
! 	else
! 		eval ${rcvar}="YES"
! 
! 		# strictly maybe the rule should also be if $0 ends in ".sh"?
! 		#
! 		case ${1} in
! 		"")
! 			cmd="start"
  			;;
  		esac
  	fi
+ 
+ 	case ${cmd} in
+ 	force*)
+ 		cmd=${cmd#force}
+ 		eval ${rcvar}="YES"
+ 		;;
+ 	esac
+ 
+ 	case ${cmd} in
+ 	start)
+ 		if checkyesno ${rcvar}; then
+ 			if [ -f ${pidfile} ] ; then
+ 				read _pid _junk < ${pidfile}
+ 				if kill -0 ${_pid}; then
+ 					warn "${name} is already running as ${_pid}!"
+ 					exit 0
+ 				fi
+ 			fi
+ 			for _f in $required_files; do
+ 				if [ ! -r "${_f}" ]; then
+ 					warn "${_f} is not readable."
+ 				fi
+ 			done
+ 			if [ -x ${command} ] ; then
+ 				eval ${command} \${${name}_flags} && echo "${name} started."
+ 			fi
+ 		fi
+ 		;;
+ 	stop)
+ 		if [ -f ${pidfile} ] ; then
+ 			read _pid _junk < ${pidfile}
+ 			echo "Stopping ${name}."
+ 			kill -15 ${_pid}
+ 			wait_for_pids ${_pid}
+ 		elif checkyesno ${rcvar}; then
+ 			warn " (${pidfile} does not exist; not stopping ${name})"
+ 		fi
+ 		;;
+ 	status)
+ 		if [ -f ${pidfile} ] ; then
+ 			read _pid _junk < ${pidfile}
+ 			# -p and -l are nearly universal these days....
+ 			ps -p ${_pid} -l
+ 			${status_cmd}
+ 		elif checkyesno ${rcvar}; then
+ 			warn "${pidfile} does not exist."
+ 		fi
+ 		;;
+ 	reload)
+ 		if [ -f ${pidfile} ] ; then
+ 			read _pid _junk < ${pidfile}
+ 			echo "Reloading ${name}."
+ 			${reload_cmd}
+ 		elif checkyesno ${rcvar}; then
+ 			warn "${pidfile} does not exist; not reloading ${name}."
+ 		fi
+ 		;;
+ 	restart)
+ 		sh $0 stop
+ 		sh $0 start
+ 		;;
+ 	rcvar)
+ 		echo "# $name"
+ 		if [ -n "$rcvar" ]; then
+ 			if checkyesno ${rcvar}; then
+ 				echo "\$${rcvar}=YES"
+ 			else
+ 				echo "\$${rcvar}=NO"
+ 			fi
+ 		fi
+ 		;;
+ 	*)
+ 		echo "Usage: $0 {start|stop|restart|status|reload}" 1>&2
+ 		exit 64
+ 		;;
+ 	esac
  fi
>Release-Note:
>Audit-Trail:
>Unformatted: