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