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