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