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