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