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