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