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