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