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