rc.subr revision 1.70
1# $NetBSD: rc.subr,v 1.70 2007/12/31 15:31:40 ad Exp $
2#
3# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Luke Mewburn.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. All advertising materials mentioning features or use of this software
18#    must display the following acknowledgement:
19#        This product includes software developed by the NetBSD
20#        Foundation, Inc. and its contributors.
21# 4. Neither the name of The NetBSD Foundation nor the names of its
22#    contributors may be used to endorse or promote products derived
23#    from this software without specific prior written permission.
24#
25# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35# POSSIBILITY OF SUCH DAMAGE.
36#
37# rc.subr
38#	functions used by various rc scripts
39#
40
41: ${rcvar_manpage:='rc.conf(5)'}
42: ${RC_PID:=$$} ; export RC_PID
43
44#
45#	functions
46#	---------
47
48#
49# checkyesno var
50#	Test $1 variable, and warn if not set to YES or NO.
51#	Return 0 if it's "yes" (et al), nonzero otherwise.
52#
53checkyesno()
54{
55	eval _value=\$${1}
56	case $_value in
57
58		#	"yes", "true", "on", or "1"
59	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
60		return 0
61		;;
62
63		#	"no", "false", "off", or "0"
64	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
65		return 1
66		;;
67	*)
68		warn "\$${1} is not set properly - see ${rcvar_manpage}."
69		return 1
70		;;
71	esac
72}
73
74#
75# reverse_list list
76#	print the list in reverse order
77#
78reverse_list()
79{
80	_revlist=
81	for _revfile; do
82		_revlist="$_revfile $_revlist"
83	done
84	echo $_revlist
85}
86
87#
88# If booting directly to multiuser, send SIGTERM to
89# the parent (/etc/rc) to abort the boot.
90# Otherwise just exit.
91#
92stop_boot()
93{
94	if [ "$autoboot" = yes ]; then
95		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
96		kill -TERM ${RC_PID}
97	fi
98	exit 1
99}
100
101#
102# mount_critical_filesystems type
103#	Go through the list of critical filesystems as provided in
104#	the rc.conf(5) variable $critical_filesystems_${type}, checking
105#	each one to see if it is mounted, and if it is not, mounting it.
106#
107mount_critical_filesystems()
108{
109	eval _fslist=\$critical_filesystems_${1}
110	for _fs in $_fslist; do
111		mount | (
112			_ismounted=false
113			while read what _on on _type type; do
114				if [ $on = $_fs ]; then
115					_ismounted=true
116				fi
117			done
118			if $_ismounted; then
119				:
120			else
121				mount $_fs >/dev/null 2>&1
122			fi
123		)
124	done
125}
126
127#
128# check_pidfile pidfile procname [interpreter]
129#	Parses the first line of pidfile for a PID, and ensures
130#	that the process is running and matches procname.
131#	Prints the matching PID upon success, nothing otherwise.
132#	interpreter is optional; see _find_processes() for details.
133#
134check_pidfile()
135{
136	_pidfile=$1
137	_procname=$2
138	_interpreter=$3
139	if [ -z "$_pidfile" -o -z "$_procname" ]; then
140		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
141	fi
142	if [ ! -f $_pidfile ]; then
143		return
144	fi
145	read _pid _junk < $_pidfile
146	if [ -z "$_pid" ]; then
147		return
148	fi
149	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
150}
151
152#
153# check_process procname [interpreter]
154#	Ensures that a process (or processes) named procname is running.
155#	Prints a list of matching PIDs.
156#	interpreter is optional; see _find_processes() for details.
157#
158check_process()
159{
160	_procname=$1
161	_interpreter=$2
162	if [ -z "$_procname" ]; then
163		err 3 'USAGE: check_process procname [interpreter]'
164	fi
165	_find_processes $_procname ${_interpreter:-.} '-ax'
166}
167
168#
169# _find_processes procname interpreter psargs
170#	Search for procname in the output of ps generated by psargs.
171#	Prints the PIDs of any matching processes, space separated.
172#
173#	If interpreter == ".", check the following variations of procname
174#	against the first word of each command:
175#		procname
176#		`basename procname`
177#		`basename procname` + ":"
178#		"(" + `basename procname` + ")"
179#
180#	If interpreter != ".", read the first line of procname, remove the
181#	leading #!, normalise whitespace, append procname, and attempt to
182#	match that against each command, either as is, or with extra words
183#	at the end.  As an alternative, to deal with interpreted deaemons
184#	using perl, the basename of the interpreter plus a colon is also
185#	tried as the prefix to procname.
186#
187_find_processes()
188{
189	if [ $# -ne 3 ]; then
190		err 3 'USAGE: _find_processes procname interpreter psargs'
191	fi
192	_procname=$1
193	_interpreter=$2
194	_psargs=$3
195
196	_pref=
197	_procnamebn=${_procname##*/}
198	if [ $_interpreter != "." ]; then	# an interpreted script
199		read _interp < ${_chroot:-}/$_procname	# read interpreter name
200		_interp=${_interp#\#!}		# strip #!
201		set -- $_interp
202		if [ $_interpreter != $1 ]; then
203			warn "\$command_interpreter $_interpreter != $1"
204		fi
205		_interp="$* $_procname"		# cleanup spaces, add _procname
206		_interpbn=${1##*/}
207		_fp_args='_argv'
208		_fp_match='case "$_argv" in
209		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
210	else					# a normal daemon
211		_fp_args='_arg0 _argv'
212		_fp_match='case "$_arg0" in
213		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
214	fi
215
216	_proccheck='
217		ps -o "pid,command" '"$_psargs"' |
218		while read _npid '"$_fp_args"'; do
219			case "$_npid" in
220			    PID)
221				continue ;;
222			esac ; '"$_fp_match"'
223				echo -n "$_pref$_npid" ;
224				_pref=" "
225				;;
226			esac
227		done'
228
229#echo 1>&2 "proccheck is :$_proccheck:"
230	eval $_proccheck
231}
232
233#
234# wait_for_pids pid [pid ...]
235#	spins until none of the pids exist
236#
237wait_for_pids()
238{
239	_list="$@"
240	if [ -z "$_list" ]; then
241		return
242	fi
243	_prefix=
244	while true; do
245		_nlist="";
246		for _j in $_list; do
247			if kill -0 $_j 2>/dev/null; then
248				_nlist="${_nlist}${_nlist:+ }$_j"
249			fi
250		done
251		if [ -z "$_nlist" ]; then
252			break
253		fi
254		_list=$_nlist
255		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
256		_prefix=", "
257		sleep 2
258	done
259	if [ -n "$_prefix" ]; then
260		echo "."
261	fi
262}
263
264#
265# run_rc_command argument
266#	Search for argument in the list of supported commands, which is:
267#		"start stop restart rcvar status poll ${extra_commands}"
268#	If there's a match, run ${argument}_cmd or the default method
269#	(see below).
270#
271#	If argument has a given prefix, then change the operation as follows:
272#		Prefix	Operation
273#		------	---------
274#		fast	Skip the pid check, and set rc_fast=yes
275#		force	Set ${rcvar} to YES, and set rc_force=yes
276#		one	Set ${rcvar} to YES
277#
278#	The following globals are used:
279#
280#	Name		Needed	Purpose
281#	----		------	-------
282#	name		y	Name of script.
283#
284#	command		n	Full path to command.
285#				Not needed if ${rc_arg}_cmd is set for
286#				each keyword.
287#
288#	command_args	n	Optional args/shell directives for command.
289#
290#	command_interpreter n	If not empty, command is interpreted, so
291#				call check_{pidfile,process}() appropriately.
292#
293#	extra_commands	n	List of extra commands supported.
294#
295#	pidfile		n	If set, use check_pidfile $pidfile $command,
296#				otherwise use check_process $command.
297#				In either case, only check if $command is set.
298#
299#	procname	n	Process name to check for instead of $command.
300#
301#	rcvar		n	This is checked with checkyesno to determine
302#				if the action should be run.
303#
304#	${name}_chroot	n	Directory to chroot to before running ${command}
305#				Requires /usr to be mounted.
306#
307#	${name}_chdir	n	Directory to cd to before running ${command}
308#				(if not using ${name}_chroot).
309#
310#	${name}_flags	n	Arguments to call ${command} with.
311#				NOTE:	$flags from the parent environment
312#					can be used to override this.
313#
314#	${name}_nice	n	Nice level to run ${command} at.
315#
316#	${name}_user	n	User to run ${command} as, using su(1) if not
317#				using ${name}_chroot.
318#				Requires /usr to be mounted.
319#
320#	${name}_group	n	Group to run chrooted ${command} as.
321#				Requires /usr to be mounted.
322#
323#	${name}_groups	n	Comma separated list of supplementary groups
324#				to run the chrooted ${command} with.
325#				Requires /usr to be mounted.
326#
327#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
328#				Otherwise, use default command (see below)
329#
330#	${rc_arg}_precmd n	If set, run just before performing the
331#				${rc_arg}_cmd method in the default
332#				operation (i.e, after checking for required
333#				bits and process (non)existence).
334#				If this completes with a non-zero exit code,
335#				don't run ${rc_arg}_cmd.
336#
337#	${rc_arg}_postcmd n	If set, run just after performing the
338#				${rc_arg}_cmd method, if that method
339#				returned a zero exit code.
340#
341#	required_dirs	n	If set, check for the existence of the given
342#				directories before running the default
343#				(re)start command.
344#
345#	required_files	n	If set, check for the readability of the given
346#				files before running the default (re)start
347#				command.
348#
349#	required_vars	n	If set, perform checkyesno on each of the
350#				listed variables before running the default
351#				(re)start command.
352#
353#	Default behaviour for a given argument, if no override method is
354#	provided:
355#
356#	Argument	Default behaviour
357#	--------	-----------------
358#	start		if !running && checkyesno ${rcvar}
359#				${command}
360#
361#	stop		if ${pidfile}
362#				rc_pid=$(check_pidfile $pidfile $command)
363#			else
364#				rc_pid=$(check_process $command)
365#			kill $sig_stop $rc_pid
366#			wait_for_pids $rc_pid
367#			($sig_stop defaults to TERM.)
368#
369#	reload		Similar to stop, except use $sig_reload instead,
370#			and doesn't wait_for_pids.
371#			$sig_reload defaults to HUP.
372#
373#	restart		Run `stop' then `start'.
374#
375#	status		Show if ${command} is running, etc.
376#
377#	poll		Wait for ${command} to exit.
378#
379#	rcvar		Display what rc.conf variable is used (if any).
380#
381#	Variables available to methods, and after run_rc_command() has
382#	completed:
383#
384#	Variable	Purpose
385#	--------	-------
386#	rc_arg		Argument to command, after fast/force/one processing
387#			performed
388#
389#	rc_flags	Flags to start the default command with.
390#			Defaults to ${name}_flags, unless overridden
391#			by $flags from the environment.
392#			This variable may be changed by the precmd method.
393#
394#	rc_pid		PID of command (if appropriate)
395#
396#	rc_fast		Not empty if "fast" was provided (q.v.)
397#
398#	rc_force	Not empty if "force" was provided (q.v.)
399#
400#
401run_rc_command()
402{
403	rc_arg=$1
404	if [ -z "$name" ]; then
405		err 3 'run_rc_command: $name is not set.'
406	fi
407
408	_rc_prefix=
409	case "$rc_arg" in
410	fast*)				# "fast" prefix; don't check pid
411		rc_arg=${rc_arg#fast}
412		rc_fast=yes
413		;;
414	force*)				# "force" prefix; always run
415		rc_force=yes
416		_rc_prefix=force
417		rc_arg=${rc_arg#${_rc_prefix}}
418		if [ -n "${rcvar}" ]; then
419			eval ${rcvar}=YES
420		fi
421		;;
422	one*)				# "one" prefix; set ${rcvar}=yes
423		_rc_prefix=one
424		rc_arg=${rc_arg#${_rc_prefix}}
425		if [ -n "${rcvar}" ]; then
426			eval ${rcvar}=YES
427		fi
428		;;
429	esac
430
431	_keywords="start stop restart rcvar $extra_commands"
432	rc_pid=
433	_pidcmd=
434	_procname=${procname:-${command}}
435
436					# setup pid check command if not fast
437	if [ -z "$rc_fast" -a -n "$_procname" ]; then
438		if [ -n "$pidfile" ]; then
439			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
440		else
441			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
442		fi
443		if [ -n "$_pidcmd" ]; then
444			_keywords="${_keywords} status poll"
445		fi
446	fi
447
448	if [ -z "$rc_arg" ]; then
449		rc_usage "$_keywords"
450	fi
451
452	if [ -n "$flags" ]; then	# allow override from environment
453		rc_flags=$flags
454	else
455		eval rc_flags=\$${name}_flags
456	fi
457	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
458	    _nice=\$${name}_nice	_user=\$${name}_user \
459	    _group=\$${name}_group	_groups=\$${name}_groups
460
461	if [ -n "$_user" ]; then	# unset $_user if running as that user
462		if [ "$_user" = "$(id -un)" ]; then
463			unset _user
464		fi
465	fi
466
467					# if ${rcvar} is set, and $1 is not
468					# "rcvar", then run
469					#	checkyesno ${rcvar}
470					# and return if that failed
471					#
472	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
473		if ! checkyesno ${rcvar}; then
474			return 0
475		fi
476	fi
477
478	eval $_pidcmd			# determine the pid if necessary
479
480	for _elem in $_keywords; do
481		if [ "$_elem" != "$rc_arg" ]; then
482			continue
483		fi
484
485					# if there's a custom ${XXX_cmd},
486					# run that instead of the default
487					#
488		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
489		    _postcmd=\$${rc_arg}_postcmd
490		if [ -n "$_cmd" ]; then
491					# if the precmd failed and force
492					# isn't set, exit
493					#
494			if ! eval $_precmd && [ -z "$rc_force" ]; then
495				return 1
496			fi
497
498			if ! eval $_cmd && [ -z "$rc_force" ]; then
499				return 1
500			fi
501			eval $_postcmd
502			return 0
503		fi
504
505		case "$rc_arg" in	# default operations...
506
507		status)
508			if [ -n "$rc_pid" ]; then
509				echo "${name} is running as pid $rc_pid."
510			else
511				echo "${name} is not running."
512				return 1
513			fi
514			;;
515
516		start)
517			if [ -n "$rc_pid" ]; then
518				echo 1>&2 "${name} already running? (pid=$rc_pid)."
519				exit 1
520			fi
521
522			if [ ! -x ${_chroot}${command} ]; then
523				return 0
524			fi
525
526					# check for required variables,
527					# directories, and files
528					#
529			for _f in $required_vars; do
530				if ! checkyesno $_f; then
531					warn "\$${_f} is not enabled."
532					if [ -z "$rc_force" ]; then
533						return 1
534					fi
535				fi
536			done
537			for _f in $required_dirs; do
538				if [ ! -d "${_f}/." ]; then
539					warn "${_f} is not a directory."
540					if [ -z "$rc_force" ]; then
541						return 1
542					fi
543				fi
544			done
545			for _f in $required_files; do
546				if [ ! -r "${_f}" ]; then
547					warn "${_f} is not readable."
548					if [ -z "$rc_force" ]; then
549						return 1
550					fi
551				fi
552			done
553
554					# if the precmd failed and force
555					# isn't set, exit
556					#
557			if ! eval $_precmd && [ -z "$rc_force" ]; then
558				return 1
559			fi
560
561					# setup the command to run, and run it
562					#
563			echo "Starting ${name}."
564			if [ -n "$_chroot" ]; then
565				_doit="\
566${_nice:+nice -n $_nice }\
567chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
568$_chroot $command $rc_flags $command_args"
569			else
570				_doit="\
571${_chdir:+cd $_chdir; }\
572${_nice:+nice -n $_nice }\
573$command $rc_flags $command_args"
574				if [ -n "$_user" ]; then
575				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
576				fi
577			fi
578
579					# if the cmd failed and force
580					# isn't set, exit
581					#
582			if ! eval $_doit && [ -z "$rc_force" ]; then
583				return 1
584			fi
585
586					# finally, run postcmd
587					#
588			eval $_postcmd
589			;;
590
591		stop)
592			if [ -z "$rc_pid" ]; then
593				if [ -n "$pidfile" ]; then
594					echo 1>&2 \
595				    "${name} not running? (check $pidfile)."
596				else
597					echo 1>&2 "${name} not running?"
598				fi
599				exit 1
600			fi
601
602					# if the precmd failed and force
603					# isn't set, exit
604					#
605			if ! eval $_precmd && [ -z "$rc_force" ]; then
606				return 1
607			fi
608
609					# send the signal to stop
610					#
611			echo "Stopping ${name}."
612			_doit="kill -${sig_stop:-TERM} $rc_pid"
613			if [ -n "$_user" ]; then
614				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
615			fi
616
617					# if the stop cmd failed and force
618					# isn't set, exit
619					#
620			if ! eval $_doit && [ -z "$rc_force" ]; then
621				return 1
622			fi
623
624					# wait for the command to exit,
625					# and run postcmd.
626			wait_for_pids $rc_pid
627			eval $_postcmd
628			;;
629
630		reload)
631			if [ -z "$rc_pid" ]; then
632				if [ -n "$pidfile" ]; then
633					echo 1>&2 \
634				    "${name} not running? (check $pidfile)."
635				else
636					echo 1>&2 "${name} not running?"
637				fi
638				exit 1
639			fi
640			echo "Reloading ${name} config files."
641			if ! eval $_precmd && [ -z "$rc_force" ]; then
642				return 1
643			fi
644			_doit="kill -${sig_reload:-HUP} $rc_pid"
645			if [ -n "$_user" ]; then
646				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
647			fi
648			if ! eval $_doit && [ -z "$rc_force" ]; then
649				return 1
650			fi
651			eval $_postcmd
652			;;
653
654		restart)
655			if ! eval $_precmd && [ -z "$rc_force" ]; then
656				return 1
657			fi
658					# prevent restart being called more
659					# than once by any given script
660					#
661			if ${_rc_restart_done:-false}; then
662				return 0
663			fi
664			_rc_restart_done=true
665
666			( $0 ${_rc_prefix}stop )
667			$0 ${_rc_prefix}start
668
669			eval $_postcmd
670			;;
671
672		poll)
673			if [ -n "$rc_pid" ]; then
674				wait_for_pids $rc_pid
675			fi
676			;;
677
678		rcvar)
679			echo "# $name"
680			if [ -n "$rcvar" ]; then
681				if checkyesno ${rcvar}; then
682					echo "\$${rcvar}=YES"
683				else
684					echo "\$${rcvar}=NO"
685				fi
686			fi
687			;;
688
689		*)
690			rc_usage "$_keywords"
691			;;
692
693		esac
694		return 0
695	done
696
697	echo 1>&2 "$0: unknown directive '$rc_arg'."
698	rc_usage "$_keywords"
699	exit 1
700}
701
702#
703# run_rc_script file arg
704#	Start the script `file' with `arg', and correctly handle the
705#	return value from the script.  If `file' ends with `.sh', it's
706#	sourced into the current environment.  If `file' appears to be
707#	a backup or scratch file, ignore it.  Otherwise if it's
708#	executable run as a child process.
709#
710run_rc_script()
711{
712	_file=$1
713	_arg=$2
714	if [ -z "$_file" -o -z "$_arg" ]; then
715		err 3 'USAGE: run_rc_script file arg'
716	fi
717
718	unset	name command command_args command_interpreter \
719		extra_commands pidfile procname \
720		rcvar required_dirs required_files required_vars
721	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
722
723	case "$_file" in
724	*.sh)				# run in current shell
725		set $_arg ; . $_file
726		;;
727	*[~#]|*.OLD|*.orig|*,v)		# scratch file; skip
728		warn "Ignoring scratch file $_file"
729		;;
730	*)				# run in subshell
731		if [ -x $_file ]; then
732			if [ -n "$rc_fast_and_loose" ]; then
733				set $_arg ; . $_file
734			else
735				( set $_arg ; . $_file )
736			fi
737		fi
738		;;
739	esac
740}
741
742#
743# load_rc_config command
744#	Source in the configuration file for a given command.
745#
746load_rc_config()
747{
748	_command=$1
749	if [ -z "$_command" ]; then
750		err 3 'USAGE: load_rc_config command'
751	fi
752
753	if ${_rc_conf_loaded:-false}; then
754		:
755	else
756		. /etc/rc.conf
757		_rc_conf_loaded=true
758	fi
759	if [ -f /etc/rc.conf.d/"$_command" ]; then
760		. /etc/rc.conf.d/"$_command"
761	fi
762}
763
764#
765# load_rc_config_var cmd var
766#	Read the rc.conf(5) var for cmd and set in the
767#	current shell, using load_rc_config in a subshell to prevent
768#	unwanted side effects from other variable assignments.
769#
770load_rc_config_var()
771{
772	if [ $# -ne 2 ]; then
773		err 3 'USAGE: load_rc_config_var cmd var'
774	fi
775	eval $(eval '(
776		load_rc_config '$1' >/dev/null;
777                if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
778			echo '$2'=\'\''${'$2'}\'\'';
779		fi
780	)' )
781}
782
783#
784# rc_usage commands
785#	Print a usage string for $0, with `commands' being a list of
786#	valid commands.
787#
788rc_usage()
789{
790	echo -n 1>&2 "Usage: $0 [fast|force|one]("
791
792	_sep=
793	for _elem; do
794		echo -n 1>&2 "$_sep$_elem"
795		_sep="|"
796	done
797	echo 1>&2 ")"
798	exit 1
799}
800
801#
802# err exitval message
803#	Display message to stderr and log to the syslog, and exit with exitval.
804#
805err()
806{
807	exitval=$1
808	shift
809
810	if [ -x /usr/bin/logger ]; then
811		logger "$0: ERROR: $*"
812	fi
813	echo 1>&2 "$0: ERROR: $*"
814	exit $exitval
815}
816
817#
818# warn message
819#	Display message to stderr and log to the syslog.
820#
821warn()
822{
823	if [ -x /usr/bin/logger ]; then
824		logger "$0: WARNING: $*"
825	fi
826	echo 1>&2 "$0: WARNING: $*"
827}
828
829#
830# backup_file action file cur backup
831#	Make a backup copy of `file' into `cur', and save the previous
832#	version of `cur' as `backup' or use rcs for archiving.
833#
834#	This routine checks the value of the backup_uses_rcs variable,
835#	which can be either YES or NO.
836#
837#	The `action' keyword can be one of the following:
838#
839#	add		`file' is now being backed up (and is possibly
840#			being reentered into the backups system).  `cur'
841#			is created and RCS files, if necessary, are
842#			created as well.
843#
844#	update		`file' has changed and needs to be backed up.
845#			If `cur' exists, it is copied to to `back' or
846#			checked into RCS (if the repository file is old),
847#			and then `file' is copied to `cur'.  Another RCS
848#			check in done here if RCS is being used.
849#
850#	remove		`file' is no longer being tracked by the backups
851#			system.  If RCS is not being used, `cur' is moved
852#			to `back', otherwise an empty file is checked in,
853#			and then `cur' is removed.
854#
855#
856backup_file()
857{
858	_action=$1
859	_file=$2
860	_cur=$3
861	_back=$4
862
863	if checkyesno backup_uses_rcs; then
864		_msg0="backup archive"
865		_msg1="update"
866
867		# ensure that history file is not locked
868		if [ -f $_cur,v ]; then
869			rcs -q -u -U -M $_cur
870		fi
871
872		# ensure after switching to rcs that the
873		# current backup is not lost
874		if [ -f $_cur ]; then
875			# no archive, or current newer than archive
876			if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
877				ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
878				rcs -q -kb -U $_cur
879				co -q -f -u $_cur
880			fi
881		fi
882
883		case $_action in
884		add|update)
885			cp -p $_file $_cur
886			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
887			rcs -q -kb -U $_cur
888			co -q -f -u $_cur
889			chown root:wheel $_cur $_cur,v
890			;;
891		remove)
892			cp /dev/null $_cur
893			ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
894			rcs -q -kb -U $_cur
895			chown root:wheel $_cur $_cur,v
896			rm $_cur
897			;;
898		esac
899	else
900		case $_action in
901		add|update)
902			if [ -f $_cur ]; then
903				cp -p $_cur $_back
904			fi
905			cp -p $_file $_cur
906			chown root:wheel $_cur
907			;;
908		remove)
909			mv -f $_cur $_back
910			;;
911		esac
912	fi
913}
914
915_rc_subr_loaded=:
916