rc.subr revision 1.70.8.1
1# $NetBSD: rc.subr,v 1.70.8.1 2008/05/18 12:29:47 yamt 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#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30# rc.subr
31#	functions used by various rc scripts
32#
33
34: ${rcvar_manpage:='rc.conf(5)'}
35: ${RC_PID:=$$} ; export RC_PID
36
37#
38#	functions
39#	---------
40
41#
42# checkyesno var
43#	Test $1 variable, and warn if not set to YES or NO.
44#	Return 0 if it's "yes" (et al), nonzero otherwise.
45#
46checkyesno()
47{
48	eval _value=\$${1}
49	case $_value in
50
51		#	"yes", "true", "on", or "1"
52	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
53		return 0
54		;;
55
56		#	"no", "false", "off", or "0"
57	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
58		return 1
59		;;
60	*)
61		warn "\$${1} is not set properly - see ${rcvar_manpage}."
62		return 1
63		;;
64	esac
65}
66
67#
68# reverse_list list
69#	print the list in reverse order
70#
71reverse_list()
72{
73	_revlist=
74	for _revfile; do
75		_revlist="$_revfile $_revlist"
76	done
77	echo $_revlist
78}
79
80#
81# If booting directly to multiuser, send SIGTERM to
82# the parent (/etc/rc) to abort the boot.
83# Otherwise just exit.
84#
85stop_boot()
86{
87	if [ "$autoboot" = yes ]; then
88		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
89		kill -TERM ${RC_PID}
90	fi
91	exit 1
92}
93
94#
95# mount_critical_filesystems type
96#	Go through the list of critical filesystems as provided in
97#	the rc.conf(5) variable $critical_filesystems_${type}, checking
98#	each one to see if it is mounted, and if it is not, mounting it.
99#
100mount_critical_filesystems()
101{
102	eval _fslist=\$critical_filesystems_${1}
103	for _fs in $_fslist; do
104		mount | (
105			_ismounted=false
106			while read what _on on _type type; do
107				if [ $on = $_fs ]; then
108					_ismounted=true
109				fi
110			done
111			if $_ismounted; then
112				:
113			else
114				mount $_fs >/dev/null 2>&1
115			fi
116		)
117	done
118}
119
120#
121# check_pidfile pidfile procname [interpreter]
122#	Parses the first line of pidfile for a PID, and ensures
123#	that the process is running and matches procname.
124#	Prints the matching PID upon success, nothing otherwise.
125#	interpreter is optional; see _find_processes() for details.
126#
127check_pidfile()
128{
129	_pidfile=$1
130	_procname=$2
131	_interpreter=$3
132	if [ -z "$_pidfile" -o -z "$_procname" ]; then
133		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
134	fi
135	if [ ! -f $_pidfile ]; then
136		return
137	fi
138	read _pid _junk < $_pidfile
139	if [ -z "$_pid" ]; then
140		return
141	fi
142	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
143}
144
145#
146# check_process procname [interpreter]
147#	Ensures that a process (or processes) named procname is running.
148#	Prints a list of matching PIDs.
149#	interpreter is optional; see _find_processes() for details.
150#
151check_process()
152{
153	_procname=$1
154	_interpreter=$2
155	if [ -z "$_procname" ]; then
156		err 3 'USAGE: check_process procname [interpreter]'
157	fi
158	_find_processes $_procname ${_interpreter:-.} '-ax'
159}
160
161#
162# _find_processes procname interpreter psargs
163#	Search for procname in the output of ps generated by psargs.
164#	Prints the PIDs of any matching processes, space separated.
165#
166#	If interpreter == ".", check the following variations of procname
167#	against the first word of each command:
168#		procname
169#		`basename procname`
170#		`basename procname` + ":"
171#		"(" + `basename procname` + ")"
172#
173#	If interpreter != ".", read the first line of procname, remove the
174#	leading #!, normalise whitespace, append procname, and attempt to
175#	match that against each command, either as is, or with extra words
176#	at the end.  As an alternative, to deal with interpreted deaemons
177#	using perl, the basename of the interpreter plus a colon is also
178#	tried as the prefix to procname.
179#
180_find_processes()
181{
182	if [ $# -ne 3 ]; then
183		err 3 'USAGE: _find_processes procname interpreter psargs'
184	fi
185	_procname=$1
186	_interpreter=$2
187	_psargs=$3
188
189	_pref=
190	_procnamebn=${_procname##*/}
191	if [ $_interpreter != "." ]; then	# an interpreted script
192		read _interp < ${_chroot:-}/$_procname	# read interpreter name
193		_interp=${_interp#\#!}		# strip #!
194		set -- $_interp
195		if [ $_interpreter != $1 ]; then
196			warn "\$command_interpreter $_interpreter != $1"
197		fi
198		_interp="$* $_procname"		# cleanup spaces, add _procname
199		_interpbn=${1##*/}
200		_fp_args='_argv'
201		_fp_match='case "$_argv" in
202		    ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)'
203	else					# a normal daemon
204		_fp_args='_arg0 _argv'
205		_fp_match='case "$_arg0" in
206		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
207	fi
208
209	_proccheck='
210		ps -o "pid,command" '"$_psargs"' |
211		while read _npid '"$_fp_args"'; do
212			case "$_npid" in
213			    PID)
214				continue ;;
215			esac ; '"$_fp_match"'
216				echo -n "$_pref$_npid" ;
217				_pref=" "
218				;;
219			esac
220		done'
221
222#echo 1>&2 "proccheck is :$_proccheck:"
223	eval $_proccheck
224}
225
226#
227# wait_for_pids pid [pid ...]
228#	spins until none of the pids exist
229#
230wait_for_pids()
231{
232	_list="$@"
233	if [ -z "$_list" ]; then
234		return
235	fi
236	_prefix=
237	while true; do
238		_nlist="";
239		for _j in $_list; do
240			if kill -0 $_j 2>/dev/null; then
241				_nlist="${_nlist}${_nlist:+ }$_j"
242			fi
243		done
244		if [ -z "$_nlist" ]; then
245			break
246		fi
247		_list=$_nlist
248		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
249		_prefix=", "
250		sleep 2
251	done
252	if [ -n "$_prefix" ]; then
253		echo "."
254	fi
255}
256
257#
258# run_rc_command argument
259#	Search for argument in the list of supported commands, which is:
260#		"start stop restart rcvar status poll ${extra_commands}"
261#	If there's a match, run ${argument}_cmd or the default method
262#	(see below).
263#
264#	If argument has a given prefix, then change the operation as follows:
265#		Prefix	Operation
266#		------	---------
267#		fast	Skip the pid check, and set rc_fast=yes
268#		force	Set ${rcvar} to YES, and set rc_force=yes
269#		one	Set ${rcvar} to YES
270#
271#	The following globals are used:
272#
273#	Name		Needed	Purpose
274#	----		------	-------
275#	name		y	Name of script.
276#
277#	command		n	Full path to command.
278#				Not needed if ${rc_arg}_cmd is set for
279#				each keyword.
280#
281#	command_args	n	Optional args/shell directives for command.
282#
283#	command_interpreter n	If not empty, command is interpreted, so
284#				call check_{pidfile,process}() appropriately.
285#
286#	extra_commands	n	List of extra commands supported.
287#
288#	pidfile		n	If set, use check_pidfile $pidfile $command,
289#				otherwise use check_process $command.
290#				In either case, only check if $command is set.
291#
292#	procname	n	Process name to check for instead of $command.
293#
294#	rcvar		n	This is checked with checkyesno to determine
295#				if the action should be run.
296#
297#	${name}_chroot	n	Directory to chroot to before running ${command}
298#				Requires /usr to be mounted.
299#
300#	${name}_chdir	n	Directory to cd to before running ${command}
301#				(if not using ${name}_chroot).
302#
303#	${name}_flags	n	Arguments to call ${command} with.
304#				NOTE:	$flags from the parent environment
305#					can be used to override this.
306#
307#	${name}_nice	n	Nice level to run ${command} at.
308#
309#	${name}_user	n	User to run ${command} as, using su(1) if not
310#				using ${name}_chroot.
311#				Requires /usr to be mounted.
312#
313#	${name}_group	n	Group to run chrooted ${command} as.
314#				Requires /usr to be mounted.
315#
316#	${name}_groups	n	Comma separated list of supplementary groups
317#				to run the chrooted ${command} with.
318#				Requires /usr to be mounted.
319#
320#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
321#				Otherwise, use default command (see below)
322#
323#	${rc_arg}_precmd n	If set, run just before performing the
324#				${rc_arg}_cmd method in the default
325#				operation (i.e, after checking for required
326#				bits and process (non)existence).
327#				If this completes with a non-zero exit code,
328#				don't run ${rc_arg}_cmd.
329#
330#	${rc_arg}_postcmd n	If set, run just after performing the
331#				${rc_arg}_cmd method, if that method
332#				returned a zero exit code.
333#
334#	required_dirs	n	If set, check for the existence of the given
335#				directories before running the default
336#				(re)start command.
337#
338#	required_files	n	If set, check for the readability of the given
339#				files before running the default (re)start
340#				command.
341#
342#	required_vars	n	If set, perform checkyesno on each of the
343#				listed variables before running the default
344#				(re)start command.
345#
346#	Default behaviour for a given argument, if no override method is
347#	provided:
348#
349#	Argument	Default behaviour
350#	--------	-----------------
351#	start		if !running && checkyesno ${rcvar}
352#				${command}
353#
354#	stop		if ${pidfile}
355#				rc_pid=$(check_pidfile $pidfile $command)
356#			else
357#				rc_pid=$(check_process $command)
358#			kill $sig_stop $rc_pid
359#			wait_for_pids $rc_pid
360#			($sig_stop defaults to TERM.)
361#
362#	reload		Similar to stop, except use $sig_reload instead,
363#			and doesn't wait_for_pids.
364#			$sig_reload defaults to HUP.
365#
366#	restart		Run `stop' then `start'.
367#
368#	status		Show if ${command} is running, etc.
369#
370#	poll		Wait for ${command} to exit.
371#
372#	rcvar		Display what rc.conf variable is used (if any).
373#
374#	Variables available to methods, and after run_rc_command() has
375#	completed:
376#
377#	Variable	Purpose
378#	--------	-------
379#	rc_arg		Argument to command, after fast/force/one processing
380#			performed
381#
382#	rc_flags	Flags to start the default command with.
383#			Defaults to ${name}_flags, unless overridden
384#			by $flags from the environment.
385#			This variable may be changed by the precmd method.
386#
387#	rc_pid		PID of command (if appropriate)
388#
389#	rc_fast		Not empty if "fast" was provided (q.v.)
390#
391#	rc_force	Not empty if "force" was provided (q.v.)
392#
393#
394run_rc_command()
395{
396	rc_arg=$1
397	if [ -z "$name" ]; then
398		err 3 'run_rc_command: $name is not set.'
399	fi
400
401	_rc_prefix=
402	case "$rc_arg" in
403	fast*)				# "fast" prefix; don't check pid
404		rc_arg=${rc_arg#fast}
405		rc_fast=yes
406		;;
407	force*)				# "force" prefix; always run
408		rc_force=yes
409		_rc_prefix=force
410		rc_arg=${rc_arg#${_rc_prefix}}
411		if [ -n "${rcvar}" ]; then
412			eval ${rcvar}=YES
413		fi
414		;;
415	one*)				# "one" prefix; set ${rcvar}=yes
416		_rc_prefix=one
417		rc_arg=${rc_arg#${_rc_prefix}}
418		if [ -n "${rcvar}" ]; then
419			eval ${rcvar}=YES
420		fi
421		;;
422	esac
423
424	_keywords="start stop restart rcvar $extra_commands"
425	rc_pid=
426	_pidcmd=
427	_procname=${procname:-${command}}
428
429					# setup pid check command if not fast
430	if [ -z "$rc_fast" -a -n "$_procname" ]; then
431		if [ -n "$pidfile" ]; then
432			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
433		else
434			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
435		fi
436		if [ -n "$_pidcmd" ]; then
437			_keywords="${_keywords} status poll"
438		fi
439	fi
440
441	if [ -z "$rc_arg" ]; then
442		rc_usage "$_keywords"
443	fi
444
445	if [ -n "$flags" ]; then	# allow override from environment
446		rc_flags=$flags
447	else
448		eval rc_flags=\$${name}_flags
449	fi
450	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
451	    _nice=\$${name}_nice	_user=\$${name}_user \
452	    _group=\$${name}_group	_groups=\$${name}_groups
453
454	if [ -n "$_user" ]; then	# unset $_user if running as that user
455		if [ "$_user" = "$(id -un)" ]; then
456			unset _user
457		fi
458	fi
459
460					# if ${rcvar} is set, and $1 is not
461					# "rcvar", then run
462					#	checkyesno ${rcvar}
463					# and return if that failed
464					#
465	if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
466		if ! checkyesno ${rcvar}; then
467			return 0
468		fi
469	fi
470
471	eval $_pidcmd			# determine the pid if necessary
472
473	for _elem in $_keywords; do
474		if [ "$_elem" != "$rc_arg" ]; then
475			continue
476		fi
477
478					# if there's a custom ${XXX_cmd},
479					# run that instead of the default
480					#
481		eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
482		    _postcmd=\$${rc_arg}_postcmd
483		if [ -n "$_cmd" ]; then
484					# if the precmd failed and force
485					# isn't set, exit
486					#
487			if ! eval $_precmd && [ -z "$rc_force" ]; then
488				return 1
489			fi
490
491			if ! eval $_cmd && [ -z "$rc_force" ]; then
492				return 1
493			fi
494			eval $_postcmd
495			return 0
496		fi
497
498		case "$rc_arg" in	# default operations...
499
500		status)
501			if [ -n "$rc_pid" ]; then
502				echo "${name} is running as pid $rc_pid."
503			else
504				echo "${name} is not running."
505				return 1
506			fi
507			;;
508
509		start)
510			if [ -n "$rc_pid" ]; then
511				echo 1>&2 "${name} already running? (pid=$rc_pid)."
512				exit 1
513			fi
514
515			if [ ! -x ${_chroot}${command} ]; then
516				return 0
517			fi
518
519					# check for required variables,
520					# directories, and files
521					#
522			for _f in $required_vars; do
523				if ! checkyesno $_f; then
524					warn "\$${_f} is not enabled."
525					if [ -z "$rc_force" ]; then
526						return 1
527					fi
528				fi
529			done
530			for _f in $required_dirs; do
531				if [ ! -d "${_f}/." ]; then
532					warn "${_f} is not a directory."
533					if [ -z "$rc_force" ]; then
534						return 1
535					fi
536				fi
537			done
538			for _f in $required_files; do
539				if [ ! -r "${_f}" ]; then
540					warn "${_f} is not readable."
541					if [ -z "$rc_force" ]; then
542						return 1
543					fi
544				fi
545			done
546
547					# if the precmd failed and force
548					# isn't set, exit
549					#
550			if ! eval $_precmd && [ -z "$rc_force" ]; then
551				return 1
552			fi
553
554					# setup the command to run, and run it
555					#
556			echo "Starting ${name}."
557			if [ -n "$_chroot" ]; then
558				_doit="\
559${_nice:+nice -n $_nice }\
560chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
561$_chroot $command $rc_flags $command_args"
562			else
563				_doit="\
564${_chdir:+cd $_chdir; }\
565${_nice:+nice -n $_nice }\
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 1>&2 \
588				    "${name} not running? (check $pidfile)."
589				else
590					echo 1>&2 "${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 1>&2 \
627				    "${name} not running? (check $pidfile)."
628				else
629					echo 1>&2 "${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
908_rc_subr_loaded=:
909