xref: /freebsd/libexec/rc/rc.subr (revision a5ad360ff9b7fb2ef3f7f31b8c29c332026b0e01)
10696600cSBjoern A. Zeeb# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
40696600cSBjoern A. Zeeb# All rights reserved.
50696600cSBjoern A. Zeeb#
60696600cSBjoern A. Zeeb# This code is derived from software contributed to The NetBSD Foundation
70696600cSBjoern A. Zeeb# by Luke Mewburn.
80696600cSBjoern A. Zeeb#
90696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
100696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
110696600cSBjoern A. Zeeb# are met:
120696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
130696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
140696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
150696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
160696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
170696600cSBjoern A. Zeeb#
180696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
190696600cSBjoern A. Zeeb# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
200696600cSBjoern A. Zeeb# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210696600cSBjoern A. Zeeb# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
220696600cSBjoern A. Zeeb# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
230696600cSBjoern A. Zeeb# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
240696600cSBjoern A. Zeeb# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
250696600cSBjoern A. Zeeb# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
260696600cSBjoern A. Zeeb# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
270696600cSBjoern A. Zeeb# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
280696600cSBjoern A. Zeeb# POSSIBILITY OF SUCH DAMAGE.
290696600cSBjoern A. Zeeb#
300696600cSBjoern A. Zeeb# rc.subr
310696600cSBjoern A. Zeeb#	functions used by various rc scripts
320696600cSBjoern A. Zeeb#
330696600cSBjoern A. Zeeb
340696600cSBjoern A. Zeeb: ${RC_PID:=$$}; export RC_PID
350696600cSBjoern A. Zeeb
360696600cSBjoern A. Zeeb#
370696600cSBjoern A. Zeeb#	Operating System dependent/independent variables
380696600cSBjoern A. Zeeb#
390696600cSBjoern A. Zeeb
400696600cSBjoern A. Zeebif [ -n "${_rc_subr_loaded}" ]; then
410696600cSBjoern A. Zeeb	return
420696600cSBjoern A. Zeebfi
430696600cSBjoern A. Zeeb
440696600cSBjoern A. Zeeb_rc_subr_loaded="YES"
450696600cSBjoern A. Zeeb
460696600cSBjoern A. ZeebSYSCTL="/sbin/sysctl"
470696600cSBjoern A. ZeebSYSCTL_N="${SYSCTL} -n"
480696600cSBjoern A. ZeebSYSCTL_W="${SYSCTL}"
490696600cSBjoern A. ZeebPROTECT="/usr/bin/protect"
500696600cSBjoern A. ZeebID="/usr/bin/id"
510696600cSBjoern A. ZeebIDCMD="if [ -x $ID ]; then $ID -un; fi"
520696600cSBjoern A. ZeebPS="/bin/ps -ww"
532efbd480SAlexander LeidingerSERVICE=/usr/sbin/service
542efbd480SAlexander LeidingerJAIL_CMD=/usr/sbin/jail
552efbd480SAlexander Leidinger_svcj_generic_params="path=/ mount.nodevfs host=inherit"
560696600cSBjoern A. ZeebJID=0
57f05948d4SEnji CooperCPUSET="/bin/cpuset"
580661f938SMiroslav Lachman
59ac102a2aSKyle Evans# rc_service provides the path to the service script that we are executing.
60ac102a2aSKyle Evans# This is not being set here in an execution context, necessarily, so it's
61ac102a2aSKyle Evans# really just a reasonable guess, and it will get overwritten later if
62ac102a2aSKyle Evans# we are executing from some other means than direct execution by service(8)
63ac102a2aSKyle Evans# or manual invocation of the service script.  The prime example of this is
64ac102a2aSKyle Evans# during system startup, all rc scripts will be invoked via /etc/rc, so
65ac102a2aSKyle Evans# run_rc_script will overwrite rc_service with the file being sourced.
66ac102a2aSKyle Evansrc_service="$0"
670696600cSBjoern A. Zeeb
680696600cSBjoern A. Zeeb#
690696600cSBjoern A. Zeeb#	functions
700696600cSBjoern A. Zeeb#	---------
710696600cSBjoern A. Zeeb
72aa3b7a2fSSimon J. Gerraty# is_verified file
73aa3b7a2fSSimon J. Gerraty#	if VERIEXEC is active check that $file is verified
74aa3b7a2fSSimon J. Gerraty#
75aa3b7a2fSSimon J. GerratyVERIEXEC="/sbin/veriexec"
76aa3b7a2fSSimon J. Gerratyif test -x $VERIEXEC && $VERIEXEC -i active > /dev/null 2>&1; then
77aa3b7a2fSSimon J. Gerraty	is_verified() { $VERIEXEC -x $1; }
78aa3b7a2fSSimon J. Gerratyelse
79aa3b7a2fSSimon J. Gerraty	is_verified() { return 0; }
80aa3b7a2fSSimon J. Gerratyfi
81aa3b7a2fSSimon J. Gerraty
82aa3b7a2fSSimon J. Gerraty# indicate that we have vdot
83aa3b7a2fSSimon J. Gerraty_VDOT_SH=:
84aa3b7a2fSSimon J. Gerraty
85aa3b7a2fSSimon J. Gerraty# current state of O_VERIFY
86aa3b7a2fSSimon J. Gerratyo_verify()
87aa3b7a2fSSimon J. Gerraty{
8815483f96SSimon J. Gerraty	case $(echo $(set -o)) in
8915483f96SSimon J. Gerraty	*verify" "off*) echo off;;
9015483f96SSimon J. Gerraty	*verify" "on*) echo on;;
9115483f96SSimon J. Gerraty	esac
92aa3b7a2fSSimon J. Gerraty}
93aa3b7a2fSSimon J. Gerraty
94aa3b7a2fSSimon J. Gerraty##
95aa3b7a2fSSimon J. Gerraty# o_verify_set want [save]
96aa3b7a2fSSimon J. Gerraty#
97aa3b7a2fSSimon J. Gerraty# record current state of verify in $save
98aa3b7a2fSSimon J. Gerraty# and set it to $want if different
99aa3b7a2fSSimon J. Gerraty#
100aa3b7a2fSSimon J. Gerratyo_verify_set() {
101aa3b7a2fSSimon J. Gerraty	local x=$(o_verify)
102aa3b7a2fSSimon J. Gerraty
103aa3b7a2fSSimon J. Gerraty	[ -z "$x" ] && return 0
104aa3b7a2fSSimon J. Gerraty	[ -z "$2" ] || eval $2=$x
105aa3b7a2fSSimon J. Gerraty	[ "$x" = "$1" ] && return 0
106aa3b7a2fSSimon J. Gerraty	case "$1" in
107aa3b7a2fSSimon J. Gerraty	on)
108aa3b7a2fSSimon J. Gerraty		set -o verify
109aa3b7a2fSSimon J. Gerraty		;;
110aa3b7a2fSSimon J. Gerraty	off)
111aa3b7a2fSSimon J. Gerraty		set +o verify
112aa3b7a2fSSimon J. Gerraty		;;
113aa3b7a2fSSimon J. Gerraty	esac
114aa3b7a2fSSimon J. Gerraty}
115aa3b7a2fSSimon J. Gerraty
116aa3b7a2fSSimon J. Gerraty# for unverified files
117aa3b7a2fSSimon J. Gerratydotted=
118aa3b7a2fSSimon J. Gerratydot()
119aa3b7a2fSSimon J. Gerraty{
120aa3b7a2fSSimon J. Gerraty	local f verify
121aa3b7a2fSSimon J. Gerraty
122aa3b7a2fSSimon J. Gerraty	o_verify_set off verify
123aa3b7a2fSSimon J. Gerraty	for f in "$@"; do
124aa3b7a2fSSimon J. Gerraty		if [ -f $f -a -s $f ]; then
125aa3b7a2fSSimon J. Gerraty			dotted="$dotted $f"
126aa3b7a2fSSimon J. Gerraty			. $f
127aa3b7a2fSSimon J. Gerraty		fi
128aa3b7a2fSSimon J. Gerraty	done
129aa3b7a2fSSimon J. Gerraty	o_verify_set $verify
130aa3b7a2fSSimon J. Gerraty}
131aa3b7a2fSSimon J. Gerraty
132aa3b7a2fSSimon J. Gerraty# try for verified, fallback to safe
133aa3b7a2fSSimon J. Gerratysdot()
134aa3b7a2fSSimon J. Gerraty{
135aa3b7a2fSSimon J. Gerraty	local f
136aa3b7a2fSSimon J. Gerraty
137aa3b7a2fSSimon J. Gerraty	for f in "$@"; do
138aa3b7a2fSSimon J. Gerraty		[ -f $f -a -s $f ] || continue
139aa3b7a2fSSimon J. Gerraty		vdot $f || safe_dot $f
140aa3b7a2fSSimon J. Gerraty	done
141aa3b7a2fSSimon J. Gerraty}
142aa3b7a2fSSimon J. Gerraty
143aa3b7a2fSSimon J. Gerraty# convenience function - skip if not verified
144aa3b7a2fSSimon J. Gerratyvdot()
145aa3b7a2fSSimon J. Gerraty{
146aa3b7a2fSSimon J. Gerraty	local f rc=0 verify
147aa3b7a2fSSimon J. Gerraty
148aa3b7a2fSSimon J. Gerraty	o_verify_set on verify
149aa3b7a2fSSimon J. Gerraty	for f in "$@"; do
150aa3b7a2fSSimon J. Gerraty		[ -f $f -a -s $f ] || continue
151aa3b7a2fSSimon J. Gerraty		if is_verified $f 2> /dev/null; then
152aa3b7a2fSSimon J. Gerraty			dotted="$dotted $f"
153aa3b7a2fSSimon J. Gerraty			. $f
154aa3b7a2fSSimon J. Gerraty		else
155aa3b7a2fSSimon J. Gerraty			rc=80	# EAUTH
156aa3b7a2fSSimon J. Gerraty		fi
157aa3b7a2fSSimon J. Gerraty	done
158aa3b7a2fSSimon J. Gerraty	o_verify_set $verify
159aa3b7a2fSSimon J. Gerraty	return $rc
160aa3b7a2fSSimon J. Gerraty}
161aa3b7a2fSSimon J. Gerraty
162b75bb996SSimon J. Gerraty# Exists [test] file ...
163b75bb996SSimon J. Gerraty# report the first "file" that passes "test" (default -s).
164b75bb996SSimon J. GerratyExists()
165b75bb996SSimon J. Gerraty{
166b75bb996SSimon J. Gerraty	local f _t=-s
167b75bb996SSimon J. Gerraty
168b75bb996SSimon J. Gerraty	while :; do
169b75bb996SSimon J. Gerraty		: 1=$1
170b75bb996SSimon J. Gerraty		case "$1" in
171b75bb996SSimon J. Gerraty		-?)
172b75bb996SSimon J. Gerraty			_t=$1
173b75bb996SSimon J. Gerraty			shift
174b75bb996SSimon J. Gerraty			;;
175b75bb996SSimon J. Gerraty		*)
176b75bb996SSimon J. Gerraty			break
177b75bb996SSimon J. Gerraty			;;
178b75bb996SSimon J. Gerraty		esac
179b75bb996SSimon J. Gerraty	done
180b75bb996SSimon J. Gerraty
181b75bb996SSimon J. Gerraty	for f in "$@"; do
182b75bb996SSimon J. Gerraty		[ $_t $f ] || continue
183b75bb996SSimon J. Gerraty		echo $f
184b75bb996SSimon J. Gerraty		return 0
185b75bb996SSimon J. Gerraty	done
186b75bb996SSimon J. Gerraty	return 1
187b75bb996SSimon J. Gerraty}
188b75bb996SSimon J. Gerraty
189aa3b7a2fSSimon J. Gerraty# do we have $1 (could be a function)
190aa3b7a2fSSimon J. Gerratyhave()
191aa3b7a2fSSimon J. Gerraty{
192aa3b7a2fSSimon J. Gerraty       type "$1" > /dev/null 2>&1
193aa3b7a2fSSimon J. Gerraty}
194aa3b7a2fSSimon J. Gerraty
195aa3b7a2fSSimon J. Gerraty# provide consistent means of logging progress
196aa3b7a2fSSimon J. Gerratyrc_log()
197aa3b7a2fSSimon J. Gerraty{
198aa3b7a2fSSimon J. Gerraty	date "+@ %s [%Y-%m-%d %H:%M:%S %Z] $*"
199aa3b7a2fSSimon J. Gerraty}
200aa3b7a2fSSimon J. Gerraty
201aa3b7a2fSSimon J. Gerraty# only rc_log if tracing enabled
202aa3b7a2fSSimon J. Gerraty# and $level >= $RC_LEVEL
203aa3b7a2fSSimon J. Gerratyrc_trace()
204aa3b7a2fSSimon J. Gerraty{
205aa3b7a2fSSimon J. Gerraty	local level=$1; shift
206aa3b7a2fSSimon J. Gerraty	local cf=/etc/rc.conf.d/rc_trace
207aa3b7a2fSSimon J. Gerraty
208aa3b7a2fSSimon J. Gerraty	if [ -z "$RC_LEVEL" ]; then
209aa3b7a2fSSimon J. Gerraty		[ -f $cf ] || return
21015483f96SSimon J. Gerraty		if [ -s $cf ]; then
21115483f96SSimon J. Gerraty			# don't try to set RC_LEVEL without sed
212b75bb996SSimon J. Gerraty			if [ -n "$SED" ]; then
213b75bb996SSimon J. Gerraty				RC_LEVEL=$($SED -n '/^RC_LEVEL=/ { s/.*=//p;q; }' $cf)
214aa3b7a2fSSimon J. Gerraty				RC_LEVEL=${RC_LEVEL:-0}
215aa3b7a2fSSimon J. Gerraty			fi
21615483f96SSimon J. Gerraty		else
21715483f96SSimon J. Gerraty			RC_LEVEL=0
21815483f96SSimon J. Gerraty		fi
21915483f96SSimon J. Gerraty	fi
220aa3b7a2fSSimon J. Gerraty	[ ${RC_LEVEL:-0} -ge ${level:-0} ] || return
221aa3b7a2fSSimon J. Gerraty	rc_log "$@"
222aa3b7a2fSSimon J. Gerraty}
223aa3b7a2fSSimon J. Gerraty
2240696600cSBjoern A. Zeeb# list_vars pattern
225b2b1708dSConrad Meyer#	List variables matching glob pattern.
2260696600cSBjoern A. Zeeb#
2270696600cSBjoern A. Zeeblist_vars()
2280696600cSBjoern A. Zeeb{
229b2b1708dSConrad Meyer	# Localize 'set' option below.
230b2b1708dSConrad Meyer	local -
231b2b1708dSConrad Meyer	local IFS=$'\n' line varname
232b2b1708dSConrad Meyer
233b2b1708dSConrad Meyer	# Disable path expansion in unquoted 'for' parameters below.
234b2b1708dSConrad Meyer	set -o noglob
235b2b1708dSConrad Meyer
236b2b1708dSConrad Meyer	for line in $(set); do
237b2b1708dSConrad Meyer		varname="${line%%=*}"
238b2b1708dSConrad Meyer
239b2b1708dSConrad Meyer		case "$varname" in
240b2b1708dSConrad Meyer		"$line"|*[!a-zA-Z0-9_]*)
241b2b1708dSConrad Meyer			continue
242b2b1708dSConrad Meyer			;;
243b2b1708dSConrad Meyer		$1)
244b2b1708dSConrad Meyer			echo $varname
245b2b1708dSConrad Meyer			;;
2460696600cSBjoern A. Zeeb		esac
247b2b1708dSConrad Meyer	done
2480696600cSBjoern A. Zeeb}
2490696600cSBjoern A. Zeeb
2500696600cSBjoern A. Zeeb# set_rcvar [var] [defval] [desc]
2510696600cSBjoern A. Zeeb#
2520696600cSBjoern A. Zeeb#	Echo or define a rc.conf(5) variable name.  Global variable
2530696600cSBjoern A. Zeeb#	$rcvars is used.
2540696600cSBjoern A. Zeeb#
2550696600cSBjoern A. Zeeb#	If no argument is specified, echo "${name}_enable".
2560696600cSBjoern A. Zeeb#
2570696600cSBjoern A. Zeeb#	If only a var is specified, echo "${var}_enable".
2580696600cSBjoern A. Zeeb#
2590696600cSBjoern A. Zeeb#	If var and defval are specified, the ${var} is defined as
2600696600cSBjoern A. Zeeb#	rc.conf(5) variable and the default value is ${defvar}.  An
2610696600cSBjoern A. Zeeb#	optional argument $desc can also be specified to add a
2620696600cSBjoern A. Zeeb#	description for that.
2630696600cSBjoern A. Zeeb#
2640696600cSBjoern A. Zeebset_rcvar()
2650696600cSBjoern A. Zeeb{
2660696600cSBjoern A. Zeeb	local _var
2670696600cSBjoern A. Zeeb
2680696600cSBjoern A. Zeeb	case $# in
2690696600cSBjoern A. Zeeb	0)	echo ${name}_enable ;;
2700696600cSBjoern A. Zeeb	1)	echo ${1}_enable ;;
2710696600cSBjoern A. Zeeb	*)
2720696600cSBjoern A. Zeeb		debug "set_rcvar: \$$1=$2 is added" \
2730696600cSBjoern A. Zeeb		    " as a rc.conf(5) variable."
2740696600cSBjoern A. Zeeb		_var=$1
2750696600cSBjoern A. Zeeb		rcvars="${rcvars# } $_var"
2760696600cSBjoern A. Zeeb		eval ${_var}_defval=\"$2\"
2770696600cSBjoern A. Zeeb		shift 2
2780696600cSBjoern A. Zeeb		eval ${_var}_desc=\"$*\"
2790696600cSBjoern A. Zeeb	;;
2800696600cSBjoern A. Zeeb	esac
2810696600cSBjoern A. Zeeb}
2820696600cSBjoern A. Zeeb
2830696600cSBjoern A. Zeeb# set_rcvar_obsolete oldvar [newvar] [msg]
2840696600cSBjoern A. Zeeb#	Define obsolete variable.
2850696600cSBjoern A. Zeeb#	Global variable $rcvars_obsolete is used.
2860696600cSBjoern A. Zeeb#
2870696600cSBjoern A. Zeebset_rcvar_obsolete()
2880696600cSBjoern A. Zeeb{
2890696600cSBjoern A. Zeeb	local _var
2900696600cSBjoern A. Zeeb	_var=$1
2910696600cSBjoern A. Zeeb	debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
2920696600cSBjoern A. Zeeb
2930696600cSBjoern A. Zeeb	rcvars_obsolete="${rcvars_obsolete# } $1"
2940696600cSBjoern A. Zeeb	eval ${1}_newvar=\"$2\"
2950696600cSBjoern A. Zeeb	shift 2
2960696600cSBjoern A. Zeeb	eval ${_var}_obsolete_msg=\"$*\"
2970696600cSBjoern A. Zeeb}
2980696600cSBjoern A. Zeeb
2990696600cSBjoern A. Zeeb#
3000696600cSBjoern A. Zeeb# force_depend script [rcvar]
3010696600cSBjoern A. Zeeb#	Force a service to start. Intended for use by services
3020696600cSBjoern A. Zeeb#	to resolve dependency issues.
3030696600cSBjoern A. Zeeb#	$1 - filename of script, in /etc/rc.d, to run
3040696600cSBjoern A. Zeeb#	$2 - name of the script's rcvar (minus the _enable)
3050696600cSBjoern A. Zeeb#
3060696600cSBjoern A. Zeebforce_depend()
3070696600cSBjoern A. Zeeb{
3080696600cSBjoern A. Zeeb	local _depend _dep_rcvar
3090696600cSBjoern A. Zeeb
3100696600cSBjoern A. Zeeb	_depend="$1"
3110696600cSBjoern A. Zeeb	_dep_rcvar="${2:-$1}_enable"
3120696600cSBjoern A. Zeeb
3130696600cSBjoern A. Zeeb	[ -n "$rc_fast" ] && ! checkyesno always_force_depends &&
3140696600cSBjoern A. Zeeb	    checkyesno $_dep_rcvar && return 0
3150696600cSBjoern A. Zeeb
3160696600cSBjoern A. Zeeb	/etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0
3170696600cSBjoern A. Zeeb
3180696600cSBjoern A. Zeeb	info "${name} depends on ${_depend}, which will be forced to start."
3190696600cSBjoern A. Zeeb	if ! /etc/rc.d/${_depend} forcestart; then
3200696600cSBjoern A. Zeeb		warn "Unable to force ${_depend}. It may already be running."
3210696600cSBjoern A. Zeeb		return 1
3220696600cSBjoern A. Zeeb	fi
3230696600cSBjoern A. Zeeb}
3240696600cSBjoern A. Zeeb
3250696600cSBjoern A. Zeeb#
3260696600cSBjoern A. Zeeb# checkyesno var
3270696600cSBjoern A. Zeeb#	Test $1 variable, and warn if not set to YES or NO.
3280696600cSBjoern A. Zeeb#	Return 0 if it's "yes" (et al), nonzero otherwise.
3290696600cSBjoern A. Zeeb#
3300696600cSBjoern A. Zeebcheckyesno()
3310696600cSBjoern A. Zeeb{
3320696600cSBjoern A. Zeeb	eval _value=\$${1}
3330696600cSBjoern A. Zeeb	debug "checkyesno: $1 is set to $_value."
3340696600cSBjoern A. Zeeb	case $_value in
3350696600cSBjoern A. Zeeb
3360696600cSBjoern A. Zeeb		#	"yes", "true", "on", or "1"
3370696600cSBjoern A. Zeeb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
3380696600cSBjoern A. Zeeb		return 0
3390696600cSBjoern A. Zeeb		;;
3400696600cSBjoern A. Zeeb
3410696600cSBjoern A. Zeeb		#	"no", "false", "off", or "0"
3420696600cSBjoern A. Zeeb	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
3430696600cSBjoern A. Zeeb		return 1
3440696600cSBjoern A. Zeeb		;;
3450696600cSBjoern A. Zeeb	*)
3460696600cSBjoern A. Zeeb		warn "\$${1} is not set properly - see rc.conf(5)."
3470696600cSBjoern A. Zeeb		return 1
3480696600cSBjoern A. Zeeb		;;
3490696600cSBjoern A. Zeeb	esac
3500696600cSBjoern A. Zeeb}
3510696600cSBjoern A. Zeeb
3520696600cSBjoern A. Zeeb#
3530696600cSBjoern A. Zeeb# reverse_list list
3540696600cSBjoern A. Zeeb#	print the list in reverse order
3550696600cSBjoern A. Zeeb#
3560696600cSBjoern A. Zeebreverse_list()
3570696600cSBjoern A. Zeeb{
3580696600cSBjoern A. Zeeb	_revlist=
3590696600cSBjoern A. Zeeb	for _revfile; do
3600696600cSBjoern A. Zeeb		_revlist="$_revfile $_revlist"
3610696600cSBjoern A. Zeeb	done
3620696600cSBjoern A. Zeeb	echo $_revlist
3630696600cSBjoern A. Zeeb}
3640696600cSBjoern A. Zeeb
3650696600cSBjoern A. Zeeb# stop_boot always
3660696600cSBjoern A. Zeeb#	If booting directly to multiuser or $always is enabled,
3670696600cSBjoern A. Zeeb#	send SIGTERM to the parent (/etc/rc) to abort the boot.
3680696600cSBjoern A. Zeeb#	Otherwise just exit.
3690696600cSBjoern A. Zeeb#
3700696600cSBjoern A. Zeebstop_boot()
3710696600cSBjoern A. Zeeb{
3720696600cSBjoern A. Zeeb	local always
3730696600cSBjoern A. Zeeb
3740696600cSBjoern A. Zeeb	case $1 in
3750696600cSBjoern A. Zeeb		#	"yes", "true", "on", or "1"
3760696600cSBjoern A. Zeeb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
3770696600cSBjoern A. Zeeb		always=true
3780696600cSBjoern A. Zeeb		;;
3790696600cSBjoern A. Zeeb	*)
3800696600cSBjoern A. Zeeb		always=false
3810696600cSBjoern A. Zeeb		;;
3820696600cSBjoern A. Zeeb	esac
3830696600cSBjoern A. Zeeb	if [ "$autoboot" = yes -o "$always" = true ]; then
3840696600cSBjoern A. Zeeb		echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!"
3850696600cSBjoern A. Zeeb		kill -TERM ${RC_PID}
3860696600cSBjoern A. Zeeb	fi
3870696600cSBjoern A. Zeeb	exit 1
3880696600cSBjoern A. Zeeb}
3890696600cSBjoern A. Zeeb
3900696600cSBjoern A. Zeeb#
3910696600cSBjoern A. Zeeb# mount_critical_filesystems type
3920696600cSBjoern A. Zeeb#	Go through the list of critical filesystems as provided in
3930696600cSBjoern A. Zeeb#	the rc.conf(5) variable $critical_filesystems_${type}, checking
3940696600cSBjoern A. Zeeb#	each one to see if it is mounted, and if it is not, mounting it.
3950696600cSBjoern A. Zeeb#
3960696600cSBjoern A. Zeebmount_critical_filesystems()
3970696600cSBjoern A. Zeeb{
3980696600cSBjoern A. Zeeb	eval _fslist=\$critical_filesystems_${1}
3990696600cSBjoern A. Zeeb	for _fs in $_fslist; do
4000696600cSBjoern A. Zeeb		mount | (
4010696600cSBjoern A. Zeeb			_ismounted=false
4020696600cSBjoern A. Zeeb			while read what _on on _type type; do
4030696600cSBjoern A. Zeeb				if [ $on = $_fs ]; then
4040696600cSBjoern A. Zeeb					_ismounted=true
4050696600cSBjoern A. Zeeb				fi
4060696600cSBjoern A. Zeeb			done
4070696600cSBjoern A. Zeeb			if $_ismounted; then
4080696600cSBjoern A. Zeeb				:
4090696600cSBjoern A. Zeeb			else
4100696600cSBjoern A. Zeeb				mount $_fs >/dev/null 2>&1
4110696600cSBjoern A. Zeeb			fi
4120696600cSBjoern A. Zeeb		)
4130696600cSBjoern A. Zeeb	done
4140696600cSBjoern A. Zeeb}
4150696600cSBjoern A. Zeeb
4160696600cSBjoern A. Zeeb#
4170696600cSBjoern A. Zeeb# check_pidfile pidfile procname [interpreter]
4180696600cSBjoern A. Zeeb#	Parses the first line of pidfile for a PID, and ensures
4190696600cSBjoern A. Zeeb#	that the process is running and matches procname.
4200696600cSBjoern A. Zeeb#	Prints the matching PID upon success, nothing otherwise.
4210696600cSBjoern A. Zeeb#	interpreter is optional; see _find_processes() for details.
4220696600cSBjoern A. Zeeb#
4230696600cSBjoern A. Zeebcheck_pidfile()
4240696600cSBjoern A. Zeeb{
4250696600cSBjoern A. Zeeb	_pidfile=$1
4260696600cSBjoern A. Zeeb	_procname=$2
4270696600cSBjoern A. Zeeb	_interpreter=$3
4280696600cSBjoern A. Zeeb	if [ -z "$_pidfile" -o -z "$_procname" ]; then
4290696600cSBjoern A. Zeeb		err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
4300696600cSBjoern A. Zeeb	fi
4310696600cSBjoern A. Zeeb	if [ ! -f $_pidfile ]; then
4320696600cSBjoern A. Zeeb		debug "pid file ($_pidfile): not readable."
4330696600cSBjoern A. Zeeb		return
4340696600cSBjoern A. Zeeb	fi
4350696600cSBjoern A. Zeeb	read _pid _junk < $_pidfile
4360696600cSBjoern A. Zeeb	if [ -z "$_pid" ]; then
4370696600cSBjoern A. Zeeb		debug "pid file ($_pidfile): no pid in file."
4380696600cSBjoern A. Zeeb		return
4390696600cSBjoern A. Zeeb	fi
4400696600cSBjoern A. Zeeb	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
4410696600cSBjoern A. Zeeb}
4420696600cSBjoern A. Zeeb
4430696600cSBjoern A. Zeeb#
4440696600cSBjoern A. Zeeb# check_process procname [interpreter]
4450696600cSBjoern A. Zeeb#	Ensures that a process (or processes) named procname is running.
4460696600cSBjoern A. Zeeb#	Prints a list of matching PIDs.
4470696600cSBjoern A. Zeeb#	interpreter is optional; see _find_processes() for details.
4480696600cSBjoern A. Zeeb#
4490696600cSBjoern A. Zeebcheck_process()
4500696600cSBjoern A. Zeeb{
4510696600cSBjoern A. Zeeb	_procname=$1
4520696600cSBjoern A. Zeeb	_interpreter=$2
4530696600cSBjoern A. Zeeb	if [ -z "$_procname" ]; then
4540696600cSBjoern A. Zeeb		err 3 'USAGE: check_process procname [interpreter]'
4550696600cSBjoern A. Zeeb	fi
4560696600cSBjoern A. Zeeb	_find_processes $_procname ${_interpreter:-.} '-ax'
4570696600cSBjoern A. Zeeb}
4580696600cSBjoern A. Zeeb
4590696600cSBjoern A. Zeeb#
4600696600cSBjoern A. Zeeb# _find_processes procname interpreter psargs
4610696600cSBjoern A. Zeeb#	Search for procname in the output of ps generated by psargs.
4620696600cSBjoern A. Zeeb#	Prints the PIDs of any matching processes, space separated.
4630696600cSBjoern A. Zeeb#
4640696600cSBjoern A. Zeeb#	If interpreter == ".", check the following variations of procname
4650696600cSBjoern A. Zeeb#	against the first word of each command:
4660696600cSBjoern A. Zeeb#		procname
4670696600cSBjoern A. Zeeb#		`basename procname`
4680696600cSBjoern A. Zeeb#		`basename procname` + ":"
4690696600cSBjoern A. Zeeb#		"(" + `basename procname` + ")"
4700696600cSBjoern A. Zeeb#		"[" + `basename procname` + "]"
4710696600cSBjoern A. Zeeb#
4720696600cSBjoern A. Zeeb#	If interpreter != ".", read the first line of procname, remove the
4730696600cSBjoern A. Zeeb#	leading #!, normalise whitespace, append procname, and attempt to
4740696600cSBjoern A. Zeeb#	match that against each command, either as is, or with extra words
4750696600cSBjoern A. Zeeb#	at the end.  As an alternative, to deal with interpreted daemons
4760696600cSBjoern A. Zeeb#	using perl, the basename of the interpreter plus a colon is also
4770696600cSBjoern A. Zeeb#	tried as the prefix to procname.
4780696600cSBjoern A. Zeeb#
4790696600cSBjoern A. Zeeb_find_processes()
4800696600cSBjoern A. Zeeb{
4810696600cSBjoern A. Zeeb	if [ $# -ne 3 ]; then
4820696600cSBjoern A. Zeeb		err 3 'USAGE: _find_processes procname interpreter psargs'
4830696600cSBjoern A. Zeeb	fi
4840696600cSBjoern A. Zeeb	_procname=$1
4850696600cSBjoern A. Zeeb	_interpreter=$2
4860696600cSBjoern A. Zeeb	_psargs=$3
4870696600cSBjoern A. Zeeb
4880696600cSBjoern A. Zeeb	_pref=
4890696600cSBjoern A. Zeeb	if [ $_interpreter != "." ]; then	# an interpreted script
4900696600cSBjoern A. Zeeb		_script="${_chroot}${_chroot:+/}$_procname"
4910696600cSBjoern A. Zeeb		if [ -r "$_script" ]; then
4920696600cSBjoern A. Zeeb			read _interp < $_script	# read interpreter name
4930696600cSBjoern A. Zeeb			case "$_interp" in
4940696600cSBjoern A. Zeeb			\#!*)
4950696600cSBjoern A. Zeeb				_interp=${_interp#\#!}	# strip #!
4960696600cSBjoern A. Zeeb				set -- $_interp
4970696600cSBjoern A. Zeeb				case $1 in
4980696600cSBjoern A. Zeeb				*/bin/env)
4990696600cSBjoern A. Zeeb					shift	# drop env to get real name
5000696600cSBjoern A. Zeeb					;;
5010696600cSBjoern A. Zeeb				esac
5020696600cSBjoern A. Zeeb				if [ $_interpreter != $1 ]; then
5030696600cSBjoern A. Zeeb					warn "\$command_interpreter $_interpreter != $1"
5040696600cSBjoern A. Zeeb				fi
5050696600cSBjoern A. Zeeb				;;
5060696600cSBjoern A. Zeeb			*)
5070696600cSBjoern A. Zeeb				warn "no shebang line in $_script"
5080696600cSBjoern A. Zeeb				set -- $_interpreter
5090696600cSBjoern A. Zeeb				;;
5100696600cSBjoern A. Zeeb			esac
5110696600cSBjoern A. Zeeb		else
5120696600cSBjoern A. Zeeb			warn "cannot read shebang line from $_script"
5130696600cSBjoern A. Zeeb			set -- $_interpreter
5140696600cSBjoern A. Zeeb		fi
5150696600cSBjoern A. Zeeb		_interp="$* $_procname"		# cleanup spaces, add _procname
5160696600cSBjoern A. Zeeb		_interpbn=${1##*/}
5170696600cSBjoern A. Zeeb		_fp_args='_argv'
5180696600cSBjoern A. Zeeb		_fp_match='case "$_argv" in
5190696600cSBjoern A. Zeeb		    ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)'
5200696600cSBjoern A. Zeeb	else					# a normal daemon
5210696600cSBjoern A. Zeeb		_procnamebn=${_procname##*/}
5220696600cSBjoern A. Zeeb		_fp_args='_arg0 _argv'
5230696600cSBjoern A. Zeeb		_fp_match='case "$_arg0" in
5240696600cSBjoern A. Zeeb		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
5250696600cSBjoern A. Zeeb	fi
5260696600cSBjoern A. Zeeb
5272efbd480SAlexander Leidinger	if checkyesno ${name}_svcj && [ "${_rc_svcj}" != jailing ]; then
5282efbd480SAlexander Leidinger		JID=$(/usr/sbin/jls -j svcj-${name} jid 2>/dev/null)
5292efbd480SAlexander Leidinger
5302efbd480SAlexander Leidinger		case ${JID} in
5312efbd480SAlexander Leidinger		''|*[!0-9]*)
5322efbd480SAlexander Leidinger			# svcj-jail doesn't exist, fallback to host-check
5332efbd480SAlexander Leidinger			JID=0
5342efbd480SAlexander Leidinger			;;
5352efbd480SAlexander Leidinger		esac
5362efbd480SAlexander Leidinger	fi
5370696600cSBjoern A. Zeeb	_proccheck="\
5380696600cSBjoern A. Zeeb		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
5390696600cSBjoern A. Zeeb		while read _npid _jid '"$_fp_args"'; do
5400696600cSBjoern A. Zeeb			'"$_fp_match"'
5410696600cSBjoern A. Zeeb				if [ "$JID" -eq "$_jid" ];
5420696600cSBjoern A. Zeeb				then echo -n "$_pref$_npid";
5430696600cSBjoern A. Zeeb				_pref=" ";
5440696600cSBjoern A. Zeeb				fi
5450696600cSBjoern A. Zeeb				;;
5460696600cSBjoern A. Zeeb			esac
5470696600cSBjoern A. Zeeb		done'
5480696600cSBjoern A. Zeeb
5490696600cSBjoern A. Zeeb#	debug "in _find_processes: proccheck is ($_proccheck)."
5500696600cSBjoern A. Zeeb	eval $_proccheck
5510696600cSBjoern A. Zeeb}
5520696600cSBjoern A. Zeeb
5530696600cSBjoern A. Zeeb# sort_lite [-b] [-n] [-k POS] [-t SEP]
5540696600cSBjoern A. Zeeb#	A lite version of sort(1) (supporting a few options) that can be used
5550696600cSBjoern A. Zeeb#	before the real sort(1) is available (e.g., in scripts that run prior
5560696600cSBjoern A. Zeeb#	to mountcritremote). Requires only shell built-in functionality.
5570696600cSBjoern A. Zeeb#
5580696600cSBjoern A. Zeebsort_lite()
5590696600cSBjoern A. Zeeb{
5600696600cSBjoern A. Zeeb	local funcname=sort_lite
5610696600cSBjoern A. Zeeb	local sort_sep="$IFS" sort_ignore_leading_space=
5620696600cSBjoern A. Zeeb	local sort_field=0 sort_strict_fields= sort_numeric=
5630696600cSBjoern A. Zeeb	local nitems=0 skip_leading=0 trim=
5640696600cSBjoern A. Zeeb
5650696600cSBjoern A. Zeeb	local OPTIND flag
5660696600cSBjoern A. Zeeb	while getopts bnk:t: flag; do
5670696600cSBjoern A. Zeeb		case "$flag" in
5680696600cSBjoern A. Zeeb		b) sort_ignore_leading_space=1 ;;
5690696600cSBjoern A. Zeeb		n) sort_numeric=1 sort_ignore_leading_space=1 ;;
5700696600cSBjoern A. Zeeb		k) sort_field="${OPTARG%%,*}" ;; # only up to first comma
5710696600cSBjoern A. Zeeb			# NB: Unlike sort(1) only one POS allowed
5720696600cSBjoern A. Zeeb		t) sort_sep="$OPTARG"
5730696600cSBjoern A. Zeeb		   if [ ${#sort_sep} -gt 1 ]; then
5740696600cSBjoern A. Zeeb		   	echo "$funcname: multi-character tab \`$sort_sep'" >&2
5750696600cSBjoern A. Zeeb		   	return 1
5760696600cSBjoern A. Zeeb		   fi
5770696600cSBjoern A. Zeeb		   sort_strict_fields=1
5780696600cSBjoern A. Zeeb		   ;;
5790696600cSBjoern A. Zeeb		\?) return 1 ;;
5800696600cSBjoern A. Zeeb		esac
5810696600cSBjoern A. Zeeb	done
5820696600cSBjoern A. Zeeb	shift $(( $OPTIND - 1 ))
5830696600cSBjoern A. Zeeb
5840696600cSBjoern A. Zeeb	# Create transformation pattern to trim leading text if desired
5850696600cSBjoern A. Zeeb	case "$sort_field" in
5860696600cSBjoern A. Zeeb	""|[!0-9]*|*[!0-9.]*)
5870696600cSBjoern A. Zeeb		echo "$funcname: invalid sort field \`$sort_field'" >&2
5880696600cSBjoern A. Zeeb		return 1
5890696600cSBjoern A. Zeeb		;;
5900696600cSBjoern A. Zeeb	*.*)
5910696600cSBjoern A. Zeeb		skip_leading=${sort_field#*.} sort_field=${sort_field%%.*}
5920696600cSBjoern A. Zeeb		while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do
5930696600cSBjoern A. Zeeb			trim="$trim?" skip_leading=$(( $skip_leading - 1 ))
5940696600cSBjoern A. Zeeb		done
5950696600cSBjoern A. Zeeb	esac
5960696600cSBjoern A. Zeeb
5970696600cSBjoern A. Zeeb	# Copy input to series of local numbered variables
5980696600cSBjoern A. Zeeb	# NB: IFS of NULL preserves leading whitespace
5990696600cSBjoern A. Zeeb	local LINE
6000696600cSBjoern A. Zeeb	while IFS= read -r LINE || [ "$LINE" ]; do
6010696600cSBjoern A. Zeeb		nitems=$(( $nitems + 1 ))
6020696600cSBjoern A. Zeeb		local src_$nitems="$LINE"
6030696600cSBjoern A. Zeeb	done
6040696600cSBjoern A. Zeeb
6050696600cSBjoern A. Zeeb	#
6060696600cSBjoern A. Zeeb	# Sort numbered locals using insertion sort
6070696600cSBjoern A. Zeeb	#
6080696600cSBjoern A. Zeeb	local curitem curitem_orig curitem_mod curitem_haskey
6090696600cSBjoern A. Zeeb	local dest dest_orig dest_mod dest_haskey
6100696600cSBjoern A. Zeeb	local d gt n
6110696600cSBjoern A. Zeeb	local i=1
6120696600cSBjoern A. Zeeb	while [ $i -le $nitems ]; do
6130696600cSBjoern A. Zeeb		curitem_haskey=1 # Assume sort field (-k POS) exists
6140696600cSBjoern A. Zeeb		eval curitem=\"\$src_$i\"
6150696600cSBjoern A. Zeeb		curitem_mod="$curitem" # for modified comparison
6160696600cSBjoern A. Zeeb		curitem_orig="$curitem" # for original comparison
6170696600cSBjoern A. Zeeb
6180696600cSBjoern A. Zeeb		# Trim leading whitespace if desired
6190696600cSBjoern A. Zeeb		if [ "$sort_ignore_leading_space" ]; then
6200696600cSBjoern A. Zeeb			while case "$curitem_orig" in
6210696600cSBjoern A. Zeeb				[$IFS]*) : ;; *) false; esac
6220696600cSBjoern A. Zeeb			do
6230696600cSBjoern A. Zeeb				curitem_orig="${curitem_orig#?}"
6240696600cSBjoern A. Zeeb			done
6250696600cSBjoern A. Zeeb			curitem_mod="$curitem_orig"
6260696600cSBjoern A. Zeeb		fi
6270696600cSBjoern A. Zeeb
6280696600cSBjoern A. Zeeb		# Shift modified comparison value if sort field (-k POS) is > 1
6290696600cSBjoern A. Zeeb		n=$sort_field
6300696600cSBjoern A. Zeeb		while [ $n -gt 1 ]; do
6310696600cSBjoern A. Zeeb			case "$curitem_mod" in
6320696600cSBjoern A. Zeeb			*[$sort_sep]*)
6330696600cSBjoern A. Zeeb				# Cut text up-to (and incl.) first separator
6340696600cSBjoern A. Zeeb				curitem_mod="${curitem_mod#*[$sort_sep]}"
6350696600cSBjoern A. Zeeb
6360696600cSBjoern A. Zeeb				# Skip NULLs unless strict field splitting
6370696600cSBjoern A. Zeeb				[ "$sort_strict_fields" ] ||
6380696600cSBjoern A. Zeeb					[ "${curitem_mod%%[$sort_sep]*}" ] ||
6390696600cSBjoern A. Zeeb					[ $n -eq 2 ] ||
6400696600cSBjoern A. Zeeb					continue
6410696600cSBjoern A. Zeeb				;;
6420696600cSBjoern A. Zeeb			*)
6430696600cSBjoern A. Zeeb				# Asked for a field that doesn't exist
6440696600cSBjoern A. Zeeb				curitem_haskey= break
6450696600cSBjoern A. Zeeb			esac
6460696600cSBjoern A. Zeeb			n=$(( $n - 1 ))
6470696600cSBjoern A. Zeeb		done
6480696600cSBjoern A. Zeeb
6490696600cSBjoern A. Zeeb		# Trim trailing words if sort field >= 1
6500696600cSBjoern A. Zeeb		[ $sort_field -ge 1 -a "$sort_numeric" ] &&
6510696600cSBjoern A. Zeeb			curitem_mod="${curitem_mod%%[$sort_sep]*}"
6520696600cSBjoern A. Zeeb
6530696600cSBjoern A. Zeeb		# Apply optional trim (-k POS.TRIM) to cut leading characters
6540696600cSBjoern A. Zeeb		curitem_mod="${curitem_mod#$trim}"
6550696600cSBjoern A. Zeeb
6560696600cSBjoern A. Zeeb		# Determine the type of modified comparison to use initially
6570696600cSBjoern A. Zeeb		# NB: Prefer numerical if requested but fallback to standard
6580696600cSBjoern A. Zeeb		case "$curitem_mod" in
6590696600cSBjoern A. Zeeb		""|[!0-9]*) # NULL or begins with non-number
6600696600cSBjoern A. Zeeb			gt=">"
6610696600cSBjoern A. Zeeb			[ "$sort_numeric" ] && curitem_mod=0
6620696600cSBjoern A. Zeeb			;;
6630696600cSBjoern A. Zeeb		*)
6640696600cSBjoern A. Zeeb			if [ "$sort_numeric" ]; then
6650696600cSBjoern A. Zeeb				gt="-gt"
6660696600cSBjoern A. Zeeb				curitem_mod="${curitem_mod%%[!0-9]*}"
6670696600cSBjoern A. Zeeb					# NB: trailing non-digits removed
6680696600cSBjoern A. Zeeb					# otherwise numeric comparison fails
6690696600cSBjoern A. Zeeb			else
6700696600cSBjoern A. Zeeb				gt=">"
6710696600cSBjoern A. Zeeb			fi
6720696600cSBjoern A. Zeeb		esac
6730696600cSBjoern A. Zeeb
6740696600cSBjoern A. Zeeb		# If first time through, short-circuit below position-search
6750696600cSBjoern A. Zeeb		if [ $i -le 1 ]; then
6760696600cSBjoern A. Zeeb			d=0
6770696600cSBjoern A. Zeeb		else
6780696600cSBjoern A. Zeeb			d=1
6790696600cSBjoern A. Zeeb		fi
6800696600cSBjoern A. Zeeb
6810696600cSBjoern A. Zeeb		#
6820696600cSBjoern A. Zeeb		# Find appropriate element position
6830696600cSBjoern A. Zeeb		#
6840696600cSBjoern A. Zeeb		while [ $d -gt 0 ]
6850696600cSBjoern A. Zeeb		do
6860696600cSBjoern A. Zeeb			dest_haskey=$curitem_haskey
6870696600cSBjoern A. Zeeb			eval dest=\"\$dest_$d\"
6880696600cSBjoern A. Zeeb			dest_mod="$dest" # for modified comparison
6890696600cSBjoern A. Zeeb			dest_orig="$dest" # for original comparison
6900696600cSBjoern A. Zeeb
6910696600cSBjoern A. Zeeb			# Trim leading whitespace if desired
6920696600cSBjoern A. Zeeb			if [ "$sort_ignore_leading_space" ]; then
6930696600cSBjoern A. Zeeb				while case "$dest_orig" in
6940696600cSBjoern A. Zeeb					[$IFS]*) : ;; *) false; esac
6950696600cSBjoern A. Zeeb				do
6960696600cSBjoern A. Zeeb					dest_orig="${dest_orig#?}"
6970696600cSBjoern A. Zeeb				done
6980696600cSBjoern A. Zeeb				dest_mod="$dest_orig"
6990696600cSBjoern A. Zeeb			fi
7000696600cSBjoern A. Zeeb
7010696600cSBjoern A. Zeeb			# Shift modified value if sort field (-k POS) is > 1
7020696600cSBjoern A. Zeeb			n=$sort_field
7030696600cSBjoern A. Zeeb			while [ $n -gt 1 ]; do
7040696600cSBjoern A. Zeeb				case "$dest_mod" in
7050696600cSBjoern A. Zeeb				*[$sort_sep]*)
7060696600cSBjoern A. Zeeb					# Cut text up-to (and incl.) 1st sep
7070696600cSBjoern A. Zeeb					dest_mod="${dest_mod#*[$sort_sep]}"
7080696600cSBjoern A. Zeeb
7090696600cSBjoern A. Zeeb					# Skip NULLs unless strict fields
7100696600cSBjoern A. Zeeb					[ "$sort_strict_fields" ] ||
7110696600cSBjoern A. Zeeb					    [ "${dest_mod%%[$sort_sep]*}" ] ||
7120696600cSBjoern A. Zeeb					    [ $n -eq 2 ] ||
7130696600cSBjoern A. Zeeb					    continue
7140696600cSBjoern A. Zeeb					;;
7150696600cSBjoern A. Zeeb				*)
7160696600cSBjoern A. Zeeb					# Asked for a field that doesn't exist
7170696600cSBjoern A. Zeeb					dest_haskey= break
7180696600cSBjoern A. Zeeb				esac
7190696600cSBjoern A. Zeeb				n=$(( $n - 1 ))
7200696600cSBjoern A. Zeeb			done
7210696600cSBjoern A. Zeeb
7220696600cSBjoern A. Zeeb			# Trim trailing words if sort field >= 1
7230696600cSBjoern A. Zeeb			[ $sort_field -ge 1 -a "$sort_numeric" ] &&
7240696600cSBjoern A. Zeeb				dest_mod="${dest_mod%%[$sort_sep]*}"
7250696600cSBjoern A. Zeeb
7260696600cSBjoern A. Zeeb			# Apply optional trim (-k POS.TRIM), cut leading chars
7270696600cSBjoern A. Zeeb			dest_mod="${dest_mod#$trim}"
7280696600cSBjoern A. Zeeb
7290696600cSBjoern A. Zeeb			# Determine type of modified comparison to use
7300696600cSBjoern A. Zeeb			# NB: Prefer numerical if requested, fallback to std
7310696600cSBjoern A. Zeeb			case "$dest_mod" in
7320696600cSBjoern A. Zeeb			""|[!0-9]*) # NULL or begins with non-number
7330696600cSBjoern A. Zeeb				gt=">"
7340696600cSBjoern A. Zeeb				[ "$sort_numeric" ] && dest_mod=0
7350696600cSBjoern A. Zeeb				;;
7360696600cSBjoern A. Zeeb			*)
7370696600cSBjoern A. Zeeb				if [ "$sort_numeric" ]; then
7380696600cSBjoern A. Zeeb					gt="-gt"
7390696600cSBjoern A. Zeeb					dest_mod="${dest_mod%%[!0-9]*}"
7400696600cSBjoern A. Zeeb						# NB: kill trailing non-digits
7410696600cSBjoern A. Zeeb						# for numeric comparison safety
7420696600cSBjoern A. Zeeb				else
7430696600cSBjoern A. Zeeb					gt=">"
7440696600cSBjoern A. Zeeb				fi
7450696600cSBjoern A. Zeeb			esac
7460696600cSBjoern A. Zeeb
7470696600cSBjoern A. Zeeb			# Break if we've found the proper element position
7480696600cSBjoern A. Zeeb			if [ "$curitem_haskey" -a "$dest_haskey" ]; then
7490696600cSBjoern A. Zeeb				if [ "$dest_mod" = "$curitem_mod" ]; then
7500696600cSBjoern A. Zeeb					[ "$dest_orig" ">" "$curitem_orig" ] &&
7510696600cSBjoern A. Zeeb						break
7520696600cSBjoern A. Zeeb				elif [ "$dest_mod" $gt "$curitem_mod" ] \
7530696600cSBjoern A. Zeeb					2> /dev/null
7540696600cSBjoern A. Zeeb				then
7550696600cSBjoern A. Zeeb					break
7560696600cSBjoern A. Zeeb				fi
7570696600cSBjoern A. Zeeb			else
7580696600cSBjoern A. Zeeb				[ "$dest_orig" ">" "$curitem_orig" ] && break
7590696600cSBjoern A. Zeeb			fi
7600696600cSBjoern A. Zeeb
7610696600cSBjoern A. Zeeb			# Break if we've hit the end
7620696600cSBjoern A. Zeeb			[ $d -ge $i ] && break
7630696600cSBjoern A. Zeeb
7640696600cSBjoern A. Zeeb			d=$(( $d + 1 ))
7650696600cSBjoern A. Zeeb		done
7660696600cSBjoern A. Zeeb
7670696600cSBjoern A. Zeeb		# Shift remaining positions forward, making room for new item
7680696600cSBjoern A. Zeeb		n=$i
7690696600cSBjoern A. Zeeb		while [ $n -ge $d ]; do
7700696600cSBjoern A. Zeeb			# Shift destination item forward one placement
7710696600cSBjoern A. Zeeb			eval dest_$(( $n + 1 ))=\"\$dest_$n\"
7720696600cSBjoern A. Zeeb			n=$(( $n - 1 ))
7730696600cSBjoern A. Zeeb		done
7740696600cSBjoern A. Zeeb
7750696600cSBjoern A. Zeeb		# Place the element
7760696600cSBjoern A. Zeeb		if [ $i -eq 1 ]; then
7770696600cSBjoern A. Zeeb			local dest_1="$curitem"
7780696600cSBjoern A. Zeeb		else
7790696600cSBjoern A. Zeeb			local dest_$d="$curitem"
7800696600cSBjoern A. Zeeb		fi
7810696600cSBjoern A. Zeeb
7820696600cSBjoern A. Zeeb		i=$(( $i + 1 ))
7830696600cSBjoern A. Zeeb	done
7840696600cSBjoern A. Zeeb
7850696600cSBjoern A. Zeeb	# Print sorted results
7860696600cSBjoern A. Zeeb	d=1
7870696600cSBjoern A. Zeeb	while [ $d -le $nitems ]; do
7880696600cSBjoern A. Zeeb		eval echo \"\$dest_$d\"
7890696600cSBjoern A. Zeeb		d=$(( $d + 1 ))
7900696600cSBjoern A. Zeeb	done
7910696600cSBjoern A. Zeeb}
7920696600cSBjoern A. Zeeb
7930696600cSBjoern A. Zeeb#
7940696600cSBjoern A. Zeeb# wait_for_pids pid [pid ...]
7950696600cSBjoern A. Zeeb#	spins until none of the pids exist
7960696600cSBjoern A. Zeeb#
7970696600cSBjoern A. Zeebwait_for_pids()
7980696600cSBjoern A. Zeeb{
7990696600cSBjoern A. Zeeb	local _list _prefix _nlist _j
8000696600cSBjoern A. Zeeb
8010696600cSBjoern A. Zeeb	_list="$@"
8020696600cSBjoern A. Zeeb	if [ -z "$_list" ]; then
8030696600cSBjoern A. Zeeb		return
8040696600cSBjoern A. Zeeb	fi
8050696600cSBjoern A. Zeeb	_prefix=
8060696600cSBjoern A. Zeeb	while true; do
8070696600cSBjoern A. Zeeb		_nlist="";
8080696600cSBjoern A. Zeeb		for _j in $_list; do
8090696600cSBjoern A. Zeeb			if kill -0 $_j 2>/dev/null; then
8100696600cSBjoern A. Zeeb				_nlist="${_nlist}${_nlist:+ }$_j"
8110696600cSBjoern A. Zeeb				[ -n "$_prefix" ] && sleep 1
8120696600cSBjoern A. Zeeb			fi
8130696600cSBjoern A. Zeeb		done
8140696600cSBjoern A. Zeeb		if [ -z "$_nlist" ]; then
8150696600cSBjoern A. Zeeb			break
8160696600cSBjoern A. Zeeb		fi
8170696600cSBjoern A. Zeeb		_list=$_nlist
8180696600cSBjoern A. Zeeb		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
8190696600cSBjoern A. Zeeb		_prefix=", "
8200696600cSBjoern A. Zeeb		pwait $_list 2>/dev/null
8210696600cSBjoern A. Zeeb	done
8220696600cSBjoern A. Zeeb	if [ -n "$_prefix" ]; then
8230696600cSBjoern A. Zeeb		echo "."
8240696600cSBjoern A. Zeeb	fi
8250696600cSBjoern A. Zeeb}
8260696600cSBjoern A. Zeeb
8270696600cSBjoern A. Zeeb#
8280696600cSBjoern A. Zeeb# get_pidfile_from_conf string file
8290696600cSBjoern A. Zeeb#
8300696600cSBjoern A. Zeeb#	Takes a string to search for in the specified file.
8310696600cSBjoern A. Zeeb#	Ignores lines with traditional comment characters.
8320696600cSBjoern A. Zeeb#
8330696600cSBjoern A. Zeeb# Example:
8340696600cSBjoern A. Zeeb#
8350696600cSBjoern A. Zeeb# if get_pidfile_from_conf string file; then
8360696600cSBjoern A. Zeeb#	pidfile="$_pidfile_from_conf"
8370696600cSBjoern A. Zeeb# else
8380696600cSBjoern A. Zeeb#	pidfile='appropriate default'
8390696600cSBjoern A. Zeeb# fi
8400696600cSBjoern A. Zeeb#
8410696600cSBjoern A. Zeebget_pidfile_from_conf()
8420696600cSBjoern A. Zeeb{
8430696600cSBjoern A. Zeeb	if [ -z "$1" -o -z "$2" ]; then
8440696600cSBjoern A. Zeeb		err 3 "USAGE: get_pidfile_from_conf string file ($name)"
8450696600cSBjoern A. Zeeb	fi
8460696600cSBjoern A. Zeeb
8470696600cSBjoern A. Zeeb	local string file line
8480696600cSBjoern A. Zeeb
8490696600cSBjoern A. Zeeb	string="$1" ; file="$2"
8500696600cSBjoern A. Zeeb
8510696600cSBjoern A. Zeeb	if [ ! -s "$file" ]; then
8520696600cSBjoern A. Zeeb		err 3 "get_pidfile_from_conf: $file does not exist ($name)"
8530696600cSBjoern A. Zeeb	fi
8540696600cSBjoern A. Zeeb
8550696600cSBjoern A. Zeeb	while read line; do
8560696600cSBjoern A. Zeeb		case "$line" in
8570696600cSBjoern A. Zeeb		*[#\;]*${string}*)	continue ;;
8580696600cSBjoern A. Zeeb		*${string}*)		break ;;
8590696600cSBjoern A. Zeeb		esac
8600696600cSBjoern A. Zeeb	done < $file
8610696600cSBjoern A. Zeeb
8620696600cSBjoern A. Zeeb	if [ -n "$line" ]; then
8630696600cSBjoern A. Zeeb		line=${line#*/}
8640696600cSBjoern A. Zeeb		_pidfile_from_conf="/${line%%[\"\;]*}"
8650696600cSBjoern A. Zeeb	else
8660696600cSBjoern A. Zeeb		return 1
8670696600cSBjoern A. Zeeb	fi
8680696600cSBjoern A. Zeeb}
8690696600cSBjoern A. Zeeb
8700696600cSBjoern A. Zeeb#
8710696600cSBjoern A. Zeeb# check_startmsgs
8720696600cSBjoern A. Zeeb#	If rc_quiet is set (usually as a result of using faststart at
8730696600cSBjoern A. Zeeb#	boot time) check if rc_startmsgs is enabled.
8740696600cSBjoern A. Zeeb#
8750696600cSBjoern A. Zeebcheck_startmsgs()
8760696600cSBjoern A. Zeeb{
8770696600cSBjoern A. Zeeb	if [ -n "$rc_quiet" ]; then
8780696600cSBjoern A. Zeeb		checkyesno rc_startmsgs
8790696600cSBjoern A. Zeeb	else
8800696600cSBjoern A. Zeeb		return 0
8810696600cSBjoern A. Zeeb	fi
8820696600cSBjoern A. Zeeb}
8830696600cSBjoern A. Zeeb
8840696600cSBjoern A. Zeeb#
885325ebf37SJose Luis Duran# startmsg
886325ebf37SJose Luis Duran#	Preferred method to use when displaying start messages in lieu of echo.
887325ebf37SJose Luis Duran#
888325ebf37SJose Luis Duranstartmsg()
889325ebf37SJose Luis Duran{
890325ebf37SJose Luis Duran	check_startmsgs && echo "$@"
891325ebf37SJose Luis Duran}
892325ebf37SJose Luis Duran
893325ebf37SJose Luis Duran#
8940696600cSBjoern A. Zeeb# run_rc_command argument
8950696600cSBjoern A. Zeeb#	Search for argument in the list of supported commands, which is:
8960696600cSBjoern A. Zeeb#		"start stop restart rcvar status poll ${extra_commands}"
8970696600cSBjoern A. Zeeb#	If there's a match, run ${argument}_cmd or the default method
8980696600cSBjoern A. Zeeb#	(see below).
8990696600cSBjoern A. Zeeb#
9000696600cSBjoern A. Zeeb#	If argument has a given prefix, then change the operation as follows:
9010696600cSBjoern A. Zeeb#		Prefix	Operation
9020696600cSBjoern A. Zeeb#		------	---------
9030696600cSBjoern A. Zeeb#		fast	Skip the pid check, and set rc_fast=yes, rc_quiet=yes
9040696600cSBjoern A. Zeeb#		force	Set ${rcvar} to YES, and set rc_force=yes
9050696600cSBjoern A. Zeeb#		one	Set ${rcvar} to YES
9060696600cSBjoern A. Zeeb#		quiet	Don't output some diagnostics, and set rc_quiet=yes
9070696600cSBjoern A. Zeeb#
9080696600cSBjoern A. Zeeb#	The following globals are used:
9090696600cSBjoern A. Zeeb#
9100696600cSBjoern A. Zeeb#	Name		Needed	Purpose
9110696600cSBjoern A. Zeeb#	----		------	-------
9120696600cSBjoern A. Zeeb#	name		y	Name of script.
9130696600cSBjoern A. Zeeb#
9140696600cSBjoern A. Zeeb#	command		n	Full path to command.
9150696600cSBjoern A. Zeeb#				Not needed if ${rc_arg}_cmd is set for
9160696600cSBjoern A. Zeeb#				each keyword.
9170696600cSBjoern A. Zeeb#
9180696600cSBjoern A. Zeeb#	command_args	n	Optional args/shell directives for command.
9190696600cSBjoern A. Zeeb#
9200696600cSBjoern A. Zeeb#	command_interpreter n	If not empty, command is interpreted, so
9210696600cSBjoern A. Zeeb#				call check_{pidfile,process}() appropriately.
9220696600cSBjoern A. Zeeb#
9230696600cSBjoern A. Zeeb#	desc		n	Description of script.
9240696600cSBjoern A. Zeeb#
9250696600cSBjoern A. Zeeb#	extra_commands	n	List of extra commands supported.
9260696600cSBjoern A. Zeeb#
9270696600cSBjoern A. Zeeb#	pidfile		n	If set, use check_pidfile $pidfile $command,
9280696600cSBjoern A. Zeeb#				otherwise use check_process $command.
9290696600cSBjoern A. Zeeb#				In either case, only check if $command is set.
9300696600cSBjoern A. Zeeb#
9310696600cSBjoern A. Zeeb#	procname	n	Process name to check for instead of $command.
9320696600cSBjoern A. Zeeb#
9330696600cSBjoern A. Zeeb#	rcvar		n	This is checked with checkyesno to determine
9340696600cSBjoern A. Zeeb#				if the action should be run.
9350696600cSBjoern A. Zeeb#
9360696600cSBjoern A. Zeeb#	${name}_program	n	Full path to command.
9370696600cSBjoern A. Zeeb#				Meant to be used in /etc/rc.conf to override
9380696600cSBjoern A. Zeeb#				${command}.
9390696600cSBjoern A. Zeeb#
9400696600cSBjoern A. Zeeb#	${name}_chroot	n	Directory to chroot to before running ${command}
9410696600cSBjoern A. Zeeb#				Requires /usr to be mounted.
9420696600cSBjoern A. Zeeb#
9430696600cSBjoern A. Zeeb#	${name}_chdir	n	Directory to cd to before running ${command}
9440696600cSBjoern A. Zeeb#				(if not using ${name}_chroot).
9450696600cSBjoern A. Zeeb#
9460661f938SMiroslav Lachman#	${name}_cpuset	n	A list of CPUs to run ${command} on.
9470661f938SMiroslav Lachman#				Requires /usr to be mounted.
9480661f938SMiroslav Lachman#
9490696600cSBjoern A. Zeeb#	${name}_flags	n	Arguments to call ${command} with.
9500696600cSBjoern A. Zeeb#				NOTE:	$flags from the parent environment
9510696600cSBjoern A. Zeeb#					can be used to override this.
9520696600cSBjoern A. Zeeb#
9530696600cSBjoern A. Zeeb#	${name}_env	n	Environment variables to run ${command} with.
9540696600cSBjoern A. Zeeb#
9550696600cSBjoern A. Zeeb#	${name}_env_file n	File to source variables to run ${command} with.
9560696600cSBjoern A. Zeeb#
9570696600cSBjoern A. Zeeb#	${name}_fib	n	Routing table number to run ${command} with.
9580696600cSBjoern A. Zeeb#
9590696600cSBjoern A. Zeeb#	${name}_nice	n	Nice level to run ${command} at.
9600696600cSBjoern A. Zeeb#
9610696600cSBjoern A. Zeeb#	${name}_oomprotect n	Don't kill ${command} when swap space is exhausted.
9620696600cSBjoern A. Zeeb#
963160a2f2cSEugene Grosbein#	${name}_umask	n	The file creation mask to run ${command} with.
964160a2f2cSEugene Grosbein#
9650696600cSBjoern A. Zeeb#	${name}_user	n	User to run ${command} as, using su(1) if not
9660696600cSBjoern A. Zeeb#				using ${name}_chroot.
9670696600cSBjoern A. Zeeb#				Requires /usr to be mounted.
9680696600cSBjoern A. Zeeb#
9690696600cSBjoern A. Zeeb#	${name}_group	n	Group to run chrooted ${command} as.
9700696600cSBjoern A. Zeeb#				Requires /usr to be mounted.
9710696600cSBjoern A. Zeeb#
9720696600cSBjoern A. Zeeb#	${name}_groups	n	Comma separated list of supplementary groups
9730696600cSBjoern A. Zeeb#				to run the chrooted ${command} with.
9740696600cSBjoern A. Zeeb#				Requires /usr to be mounted.
9750696600cSBjoern A. Zeeb#
9760696600cSBjoern A. Zeeb#	${name}_prepend	n	Command added before ${command}.
9770696600cSBjoern A. Zeeb#
97811333dd5SFranco Fichtner#	${name}_setup	n	Command executed during start, restart and
97911333dd5SFranco Fichtner#				reload before ${rc_arg}_precmd is run.
980c9be47b3SFranco Fichtner#
9810696600cSBjoern A. Zeeb#	${name}_login_class n	Login class to use, else "daemon".
9820696600cSBjoern A. Zeeb#
9830696600cSBjoern A. Zeeb#	${name}_limits	n	limits(1) to apply to ${command}.
9840696600cSBjoern A. Zeeb#
985af1b0aa5SEugene Grosbein#	${name}_offcmd	n	If set, run during start
986af1b0aa5SEugene Grosbein#				if a service is not enabled.
98732a579e4SEugene Grosbein#
9880696600cSBjoern A. Zeeb#	${rc_arg}_cmd	n	If set, use this as the method when invoked;
9890696600cSBjoern A. Zeeb#				Otherwise, use default command (see below)
9900696600cSBjoern A. Zeeb#
9910696600cSBjoern A. Zeeb#	${rc_arg}_precmd n	If set, run just before performing the
9920696600cSBjoern A. Zeeb#				${rc_arg}_cmd method in the default
9930696600cSBjoern A. Zeeb#				operation (i.e, after checking for required
9940696600cSBjoern A. Zeeb#				bits and process (non)existence).
9950696600cSBjoern A. Zeeb#				If this completes with a non-zero exit code,
9960696600cSBjoern A. Zeeb#				don't run ${rc_arg}_cmd.
9970696600cSBjoern A. Zeeb#
9980696600cSBjoern A. Zeeb#	${rc_arg}_postcmd n	If set, run just after performing the
9990696600cSBjoern A. Zeeb#				${rc_arg}_cmd method, if that method
10000696600cSBjoern A. Zeeb#				returned a zero exit code.
10010696600cSBjoern A. Zeeb#
10020696600cSBjoern A. Zeeb#	required_dirs	n	If set, check for the existence of the given
10030696600cSBjoern A. Zeeb#				directories before running a (re)start command.
10040696600cSBjoern A. Zeeb#
10050696600cSBjoern A. Zeeb#	required_files	n	If set, check for the readability of the given
10060696600cSBjoern A. Zeeb#				files before running a (re)start command.
10070696600cSBjoern A. Zeeb#
10080696600cSBjoern A. Zeeb#	required_modules n	If set, ensure the given kernel modules are
10090696600cSBjoern A. Zeeb#				loaded before running a (re)start command.
10100696600cSBjoern A. Zeeb#				The check and possible loads are actually
10110696600cSBjoern A. Zeeb#				done after start_precmd so that the modules
10120696600cSBjoern A. Zeeb#				aren't loaded in vain, should the precmd
10130696600cSBjoern A. Zeeb#				return a non-zero status to indicate a error.
10140696600cSBjoern A. Zeeb#				If a word in the list looks like "foo:bar",
10150696600cSBjoern A. Zeeb#				"foo" is the KLD file name and "bar" is the
10160696600cSBjoern A. Zeeb#				module name.  If a word looks like "foo~bar",
10170696600cSBjoern A. Zeeb#				"foo" is the KLD file name and "bar" is a
10180696600cSBjoern A. Zeeb#				egrep(1) pattern matching the module name.
10190696600cSBjoern A. Zeeb#				Otherwise the module name is assumed to be
10200696600cSBjoern A. Zeeb#				the same as the KLD file name, which is most
10210696600cSBjoern A. Zeeb#				common.  See load_kld().
10220696600cSBjoern A. Zeeb#
10230696600cSBjoern A. Zeeb#	required_vars	n	If set, perform checkyesno on each of the
10240696600cSBjoern A. Zeeb#				listed variables before running the default
10250696600cSBjoern A. Zeeb#				(re)start command.
10260696600cSBjoern A. Zeeb#
10270696600cSBjoern A. Zeeb#	Default behaviour for a given argument, if no override method is
10280696600cSBjoern A. Zeeb#	provided:
10290696600cSBjoern A. Zeeb#
10300696600cSBjoern A. Zeeb#	Argument	Default behaviour
10310696600cSBjoern A. Zeeb#	--------	-----------------
10320696600cSBjoern A. Zeeb#	start		if !running && checkyesno ${rcvar}
10330696600cSBjoern A. Zeeb#				${command}
10340696600cSBjoern A. Zeeb#
10350696600cSBjoern A. Zeeb#	stop		if ${pidfile}
10360696600cSBjoern A. Zeeb#				rc_pid=$(check_pidfile $pidfile $command)
10370696600cSBjoern A. Zeeb#			else
10380696600cSBjoern A. Zeeb#				rc_pid=$(check_process $command)
10390696600cSBjoern A. Zeeb#			kill $sig_stop $rc_pid
10400696600cSBjoern A. Zeeb#			wait_for_pids $rc_pid
10410696600cSBjoern A. Zeeb#			($sig_stop defaults to TERM.)
10420696600cSBjoern A. Zeeb#
10430696600cSBjoern A. Zeeb#	reload		Similar to stop, except use $sig_reload instead,
10447f6754d9SMateusz Piotrowski#			and don't wait_for_pids.
10450696600cSBjoern A. Zeeb#			$sig_reload defaults to HUP.
10460696600cSBjoern A. Zeeb#			Note that `reload' isn't provided by default,
10470696600cSBjoern A. Zeeb#			it should be enabled via $extra_commands.
10480696600cSBjoern A. Zeeb#
10490696600cSBjoern A. Zeeb#	restart		Run `stop' then `start'.
10500696600cSBjoern A. Zeeb#
10510696600cSBjoern A. Zeeb#	status		Show if ${command} is running, etc.
10520696600cSBjoern A. Zeeb#
10530696600cSBjoern A. Zeeb#	poll		Wait for ${command} to exit.
10540696600cSBjoern A. Zeeb#
10550696600cSBjoern A. Zeeb#	rcvar		Display what rc.conf variable is used (if any).
10560696600cSBjoern A. Zeeb#
10570696600cSBjoern A. Zeeb#	enabled		Return true if the service is enabled.
10580696600cSBjoern A. Zeeb#
10590696600cSBjoern A. Zeeb#	describe	Show the service's description
10600696600cSBjoern A. Zeeb#
10610696600cSBjoern A. Zeeb#	extracommands	Show the service's extra commands
10620696600cSBjoern A. Zeeb#
10630696600cSBjoern A. Zeeb#	Variables available to methods, and after run_rc_command() has
10640696600cSBjoern A. Zeeb#	completed:
10650696600cSBjoern A. Zeeb#
10660696600cSBjoern A. Zeeb#	Variable	Purpose
10670696600cSBjoern A. Zeeb#	--------	-------
10680696600cSBjoern A. Zeeb#	rc_arg		Argument to command, after fast/force/one processing
10690696600cSBjoern A. Zeeb#			performed
10700696600cSBjoern A. Zeeb#
10710696600cSBjoern A. Zeeb#	rc_flags	Flags to start the default command with.
10720696600cSBjoern A. Zeeb#			Defaults to ${name}_flags, unless overridden
10730696600cSBjoern A. Zeeb#			by $flags from the environment.
10740696600cSBjoern A. Zeeb#			This variable may be changed by the precmd method.
10750696600cSBjoern A. Zeeb#
1076478e7696SKyle Evans#	rc_service	Path to the service being executed, in case the service
1077478e7696SKyle Evans#			needs to re-invoke itself.
1078ac102a2aSKyle Evans#
10790696600cSBjoern A. Zeeb#	rc_pid		PID of command (if appropriate)
10800696600cSBjoern A. Zeeb#
10810696600cSBjoern A. Zeeb#	rc_fast		Not empty if "fast" was provided (q.v.)
10820696600cSBjoern A. Zeeb#
10830696600cSBjoern A. Zeeb#	rc_force	Not empty if "force" was provided (q.v.)
10840696600cSBjoern A. Zeeb#
10850696600cSBjoern A. Zeeb#	rc_quiet	Not empty if "quiet" was provided
10860696600cSBjoern A. Zeeb#
10870696600cSBjoern A. Zeeb#
10880696600cSBjoern A. Zeebrun_rc_command()
10890696600cSBjoern A. Zeeb{
10900696600cSBjoern A. Zeeb	_return=0
10910696600cSBjoern A. Zeeb	rc_arg=$1
10920696600cSBjoern A. Zeeb	if [ -z "$name" ]; then
10930696600cSBjoern A. Zeeb		err 3 'run_rc_command: $name is not set.'
10940696600cSBjoern A. Zeeb	fi
10950696600cSBjoern A. Zeeb
1096dc501a9eSSimon J. Gerraty	DebugOn rc:all rc:all:$rc_arg rc:$name rc:$name:$rc_arg $name:$rc_arg
1097aa3b7a2fSSimon J. Gerraty
10980696600cSBjoern A. Zeeb	# Don't repeat the first argument when passing additional command-
10990696600cSBjoern A. Zeeb	# line arguments to the command subroutines.
11000696600cSBjoern A. Zeeb	#
11010696600cSBjoern A. Zeeb	shift 1
11020696600cSBjoern A. Zeeb	rc_extra_args="$*"
11030696600cSBjoern A. Zeeb
11040696600cSBjoern A. Zeeb	_rc_prefix=
11050696600cSBjoern A. Zeeb	case "$rc_arg" in
11060696600cSBjoern A. Zeeb	fast*)				# "fast" prefix; don't check pid
11070696600cSBjoern A. Zeeb		rc_arg=${rc_arg#fast}
11080696600cSBjoern A. Zeeb		rc_fast=yes
11090696600cSBjoern A. Zeeb		rc_quiet=yes
11100696600cSBjoern A. Zeeb		;;
11110696600cSBjoern A. Zeeb	force*)				# "force" prefix; always run
11120696600cSBjoern A. Zeeb		rc_force=yes
11130696600cSBjoern A. Zeeb		_rc_prefix=force
11140696600cSBjoern A. Zeeb		rc_arg=${rc_arg#${_rc_prefix}}
11150696600cSBjoern A. Zeeb		if [ -n "${rcvar}" ]; then
11160696600cSBjoern A. Zeeb			eval ${rcvar}=YES
11170696600cSBjoern A. Zeeb		fi
11180696600cSBjoern A. Zeeb		;;
11190696600cSBjoern A. Zeeb	one*)				# "one" prefix; set ${rcvar}=yes
11200696600cSBjoern A. Zeeb		_rc_prefix=one
11210696600cSBjoern A. Zeeb		rc_arg=${rc_arg#${_rc_prefix}}
11220696600cSBjoern A. Zeeb		if [ -n "${rcvar}" ]; then
11230696600cSBjoern A. Zeeb			eval ${rcvar}=YES
11240696600cSBjoern A. Zeeb		fi
11250696600cSBjoern A. Zeeb		;;
11260696600cSBjoern A. Zeeb	quiet*)				# "quiet" prefix; omit some messages
11270696600cSBjoern A. Zeeb		_rc_prefix=quiet
11280696600cSBjoern A. Zeeb		rc_arg=${rc_arg#${_rc_prefix}}
11290696600cSBjoern A. Zeeb		rc_quiet=yes
11300696600cSBjoern A. Zeeb		;;
11310696600cSBjoern A. Zeeb	esac
11320696600cSBjoern A. Zeeb
11330696600cSBjoern A. Zeeb	eval _override_command=\$${name}_program
11340696600cSBjoern A. Zeeb	command=${_override_command:-$command}
11350696600cSBjoern A. Zeeb
1136da45b4daSDevin Teske	_keywords="start stop restart rcvar enable disable delete enabled describe extracommands $extra_commands"
11370696600cSBjoern A. Zeeb	rc_pid=
11380696600cSBjoern A. Zeeb	_pidcmd=
11390696600cSBjoern A. Zeeb	_procname=${procname:-${command}}
11400696600cSBjoern A. Zeeb
11410661f938SMiroslav Lachman	eval _cpuset=\$${name}_cpuset
11420661f938SMiroslav Lachman
11430661f938SMiroslav Lachman	# Loose validation of the configured cpuset; just make sure it starts
11440661f938SMiroslav Lachman	# with a number.  There have also been cases in the past where a hyphen
11450661f938SMiroslav Lachman	# in a service name has caused eval errors, which trickle down into
11460661f938SMiroslav Lachman	# various variables; don't let a situation like that break a bunch of
11470661f938SMiroslav Lachman	# services just because of cpuset(1).
11480661f938SMiroslav Lachman	case "$_cpuset" in
11490661f938SMiroslav Lachman	[0-9]*)	;;
11500661f938SMiroslav Lachman	*)	_cpuset="" ;;
11510661f938SMiroslav Lachman	esac
11520661f938SMiroslav Lachman
11530661f938SMiroslav Lachman	_cpusetcmd=
1154f05948d4SEnji Cooper	if [ -n "$_cpuset" ]; then
11550661f938SMiroslav Lachman		_cpusetcmd="$CPUSET -l $_cpuset"
11560661f938SMiroslav Lachman	fi
11570661f938SMiroslav Lachman
11582efbd480SAlexander Leidinger	# If a specific jail has a specific svcj request, honor it (YES/NO).
11592efbd480SAlexander Leidinger	# If not (variable empty), evaluate the global svcj catch-all.
11602efbd480SAlexander Leidinger	# A global YES can be overriden by a specific NO, and a global NO is overriden
11612efbd480SAlexander Leidinger	# by a specific YES.
11622efbd480SAlexander Leidinger	eval _svcj=\$${name}_svcj
11632efbd480SAlexander Leidinger	if [ -z "$_svcj" ]; then
11642efbd480SAlexander Leidinger		_svcj=${svcj_all_enable}
11652efbd480SAlexander Leidinger		if [ -z "$_svcj" ]; then
11662efbd480SAlexander Leidinger			eval ${name}_svcj=NO
11672efbd480SAlexander Leidinger		fi
11682efbd480SAlexander Leidinger	fi
11692efbd480SAlexander Leidinger
11700696600cSBjoern A. Zeeb					# setup pid check command
11710696600cSBjoern A. Zeeb	if [ -n "$_procname" ]; then
11720696600cSBjoern A. Zeeb		if [ -n "$pidfile" ]; then
11730696600cSBjoern A. Zeeb			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
11740696600cSBjoern A. Zeeb		else
11750696600cSBjoern A. Zeeb			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
11760696600cSBjoern A. Zeeb		fi
11770696600cSBjoern A. Zeeb		_keywords="${_keywords} status poll"
11782651609fSMaxim Sobolev	else
11792651609fSMaxim Sobolev		if [ ! -z "${status_cmd}" ]
11802651609fSMaxim Sobolev		then
11812651609fSMaxim Sobolev			_keywords="${_keywords} status"
11822651609fSMaxim Sobolev		fi
11830696600cSBjoern A. Zeeb	fi
11840696600cSBjoern A. Zeeb
11850696600cSBjoern A. Zeeb	if [ -z "$rc_arg" ]; then
11860696600cSBjoern A. Zeeb		rc_usage $_keywords
11870696600cSBjoern A. Zeeb	fi
11880696600cSBjoern A. Zeeb
11890696600cSBjoern A. Zeeb	if [ "$rc_arg" = "enabled" ] ; then
11900696600cSBjoern A. Zeeb		checkyesno ${rcvar}
11910696600cSBjoern A. Zeeb		return $?
11920696600cSBjoern A. Zeeb	fi
11930696600cSBjoern A. Zeeb
11940696600cSBjoern A. Zeeb	if [ -n "$flags" ]; then	# allow override from environment
11950696600cSBjoern A. Zeeb		rc_flags=$flags
11960696600cSBjoern A. Zeeb	else
11970696600cSBjoern A. Zeeb		eval rc_flags=\$${name}_flags
11980696600cSBjoern A. Zeeb	fi
11990696600cSBjoern A. Zeeb	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
12000696600cSBjoern A. Zeeb	    _nice=\$${name}_nice	_user=\$${name}_user \
12010696600cSBjoern A. Zeeb	    _group=\$${name}_group	_groups=\$${name}_groups \
12020696600cSBjoern A. Zeeb	    _fib=\$${name}_fib		_env=\$${name}_env \
12030696600cSBjoern A. Zeeb	    _prepend=\$${name}_prepend	_login_class=\${${name}_login_class:-daemon} \
12040696600cSBjoern A. Zeeb	    _limits=\$${name}_limits	_oomprotect=\$${name}_oomprotect \
1205160a2f2cSEugene Grosbein	    _setup=\$${name}_setup	_env_file=\$${name}_env_file \
12062efbd480SAlexander Leidinger	    _umask=\$${name}_umask	_svcj_options=\$${name}_svcj_options
12070696600cSBjoern A. Zeeb
12080696600cSBjoern A. Zeeb	if [ -n "$_env_file" ] && [ -r "${_env_file}" ]; then	# load env from file
12090696600cSBjoern A. Zeeb		set -a
12100696600cSBjoern A. Zeeb		. $_env_file
12110696600cSBjoern A. Zeeb		set +a
12120696600cSBjoern A. Zeeb	fi
12130696600cSBjoern A. Zeeb
12140696600cSBjoern A. Zeeb	if [ -n "$_user" ]; then	# unset $_user if running as that user
12150696600cSBjoern A. Zeeb		if [ "$_user" = "$(eval $IDCMD)" ]; then
12160696600cSBjoern A. Zeeb			unset _user
12170696600cSBjoern A. Zeeb		fi
12180696600cSBjoern A. Zeeb	fi
12190696600cSBjoern A. Zeeb
12202efbd480SAlexander Leidinger	if [ -n "$_svcj_options" ]; then	# translate service jail options
12212efbd480SAlexander Leidinger		_svcj_cmd_options=""
12222efbd480SAlexander Leidinger
1223a70ecfb1SAlexander Leidinger		_svcj_sysvipc_x=0
12242efbd480SAlexander Leidinger		for _svcj_option in $_svcj_options; do
12252efbd480SAlexander Leidinger			case "$_svcj_option" in
12262efbd480SAlexander Leidinger				mlock)
12272efbd480SAlexander Leidinger					_svcj_cmd_options="allow.mlock ${_svcj_cmd_options}"
12282efbd480SAlexander Leidinger					;;
12292efbd480SAlexander Leidinger				netv4)
12302efbd480SAlexander Leidinger					_svcj_cmd_options="ip4=inherit allow.reserved_ports ${_svcj_cmd_options}"
12312efbd480SAlexander Leidinger					;;
12322efbd480SAlexander Leidinger				netv6)
12332efbd480SAlexander Leidinger					_svcj_cmd_options="ip6=inherit allow.reserved_ports ${_svcj_cmd_options}"
12342efbd480SAlexander Leidinger					;;
12352efbd480SAlexander Leidinger				net_basic)
12362efbd480SAlexander Leidinger					_svcj_cmd_options="ip4=inherit ip6=inherit allow.reserved_ports ${_svcj_cmd_options}"
12372efbd480SAlexander Leidinger					;;
12382efbd480SAlexander Leidinger				net_raw)
12392efbd480SAlexander Leidinger					_svcj_cmd_options="allow.raw_sockets ${_svcj_cmd_options}"
12402efbd480SAlexander Leidinger					;;
12412efbd480SAlexander Leidinger				net_all)
12422efbd480SAlexander Leidinger					_svcj_cmd_options="allow.socket_af allow.raw_sockets allow.reserved_ports ip4=inherit ip6=inherit ${_svcj_cmd_options}"
12432efbd480SAlexander Leidinger					;;
12442efbd480SAlexander Leidinger				nfsd)
12452efbd480SAlexander Leidinger					_svcj_cmd_options="allow.nfsd enforce_statfs=1 ${_svcj_cmd_options}"
12462efbd480SAlexander Leidinger					;;
12472efbd480SAlexander Leidinger				sysvipc)
1248a70ecfb1SAlexander Leidinger					_svcj_sysvipc_x=$((${_svcj_sysvipc_x} + 1))
12492efbd480SAlexander Leidinger					_svcj_cmd_options="sysvmsg=inherit sysvsem=inherit sysvshm=inherit  ${_svcj_cmd_options}"
12502efbd480SAlexander Leidinger					;;
1251a70ecfb1SAlexander Leidinger				sysvipcnew)
1252a70ecfb1SAlexander Leidinger					_svcj_sysvipc_x=$((${_svcj_sysvipc_x} + 1))
1253a70ecfb1SAlexander Leidinger					_svcj_cmd_options="sysvmsg=new sysvsem=new sysvshm=new ${_svcj_cmd_options}"
1254a70ecfb1SAlexander Leidinger					;;
12552efbd480SAlexander Leidinger				vmm)
12562efbd480SAlexander Leidinger					_svcj_cmd_options="allow.vmm ${_svcj_cmd_options}"
12572efbd480SAlexander Leidinger					;;
12582efbd480SAlexander Leidinger				*)
12592efbd480SAlexander Leidinger					echo ${name}: unknown service jail option: $_svcj_option
12602efbd480SAlexander Leidinger					;;
12612efbd480SAlexander Leidinger			esac
12622efbd480SAlexander Leidinger		done
1263a70ecfb1SAlexander Leidinger		if [ ${_svcj_sysvipc_x} -gt 1 ]; then
1264a70ecfb1SAlexander Leidinger			echo -n "ERROR: more than one sysvipc option is "
1265a70ecfb1SAlexander Leidinger			echo "specified in ${name}_svcj_options: $_svcj_options"
1266a70ecfb1SAlexander Leidinger			return 1
1267a70ecfb1SAlexander Leidinger		fi
12682efbd480SAlexander Leidinger	fi
12692efbd480SAlexander Leidinger
12700696600cSBjoern A. Zeeb	[ -z "$autoboot" ] && eval $_pidcmd	# determine the pid if necessary
12710696600cSBjoern A. Zeeb
12720696600cSBjoern A. Zeeb	for _elem in $_keywords; do
12730696600cSBjoern A. Zeeb		if [ "$_elem" != "$rc_arg" ]; then
12740696600cSBjoern A. Zeeb			continue
12750696600cSBjoern A. Zeeb		fi
1276da45b4daSDevin Teske					# if ${rcvar} is set, $1 is not "rcvar", "describe",
1277ba793728SDaniel Tameling					# "enable", "delete" or "status", and ${rc_pid} is
1278ba793728SDaniel Tameling					# not set, run:
12790696600cSBjoern A. Zeeb					#	checkyesno ${rcvar}
12800696600cSBjoern A. Zeeb					# and return if that failed
12810696600cSBjoern A. Zeeb					#
12820696600cSBjoern A. Zeeb		if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \
1283da45b4daSDevin Teske		    -a "$rc_arg" != "delete" -a "$rc_arg" != "enable" \
1284ba793728SDaniel Tameling		    -a "$rc_arg" != "describe" -a "$rc_arg" != "status" ] ||
12850696600cSBjoern A. Zeeb		    [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then
12860696600cSBjoern A. Zeeb			if ! checkyesno ${rcvar}; then
128732a579e4SEugene Grosbein			    [ "$rc_arg" = "start" ] && _run_rc_offcmd
128832a579e4SEugene Grosbein			    if [ -z "${rc_quiet}" ]; then
12890696600cSBjoern A. Zeeb				echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to "
12900696600cSBjoern A. Zeeb				echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' "
12910696600cSBjoern A. Zeeb				echo "instead of '${rc_arg}'."
129232a579e4SEugene Grosbein			    fi
12930696600cSBjoern A. Zeeb			    return 0
12940696600cSBjoern A. Zeeb			fi
12950696600cSBjoern A. Zeeb		fi
12960696600cSBjoern A. Zeeb
12970696600cSBjoern A. Zeeb		if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then
12980696600cSBjoern A. Zeeb			if [ -z "$rc_quiet" ]; then
12990696600cSBjoern A. Zeeb				echo 1>&2 "${name} already running? " \
13000696600cSBjoern A. Zeeb				    "(pid=$rc_pid)."
13010696600cSBjoern A. Zeeb			fi
13020696600cSBjoern A. Zeeb			return 1
13030696600cSBjoern A. Zeeb		fi
13040696600cSBjoern A. Zeeb
13050696600cSBjoern A. Zeeb		# if there's a custom ${XXX_cmd},
13060696600cSBjoern A. Zeeb		# run that instead of the default
13070696600cSBjoern A. Zeeb		#
13080696600cSBjoern A. Zeeb		eval _cmd=\$${rc_arg}_cmd \
13090696600cSBjoern A. Zeeb		     _precmd=\$${rc_arg}_precmd \
13100696600cSBjoern A. Zeeb		     _postcmd=\$${rc_arg}_postcmd
13110696600cSBjoern A. Zeeb
13120696600cSBjoern A. Zeeb		if [ -n "$_cmd" ]; then
1313aa3b7a2fSSimon J. Gerraty			rc_trace 1 "$_cmd"
1314d15e810dSMateusz Piotrowski			if [ -n "$_env" ]; then
1315d15e810dSMateusz Piotrowski				eval "export -- $_env"
1316d15e810dSMateusz Piotrowski			fi
13172efbd480SAlexander Leidinger
13182efbd480SAlexander Leidinger			if [ "${_rc_svcj}" != jailing ]; then
131911333dd5SFranco Fichtner				# service can redefine all so
132011333dd5SFranco Fichtner				# check for valid setup target
132111333dd5SFranco Fichtner				if [ "$rc_arg" = 'start' -o \
132211333dd5SFranco Fichtner				    "$rc_arg" = 'restart' -o \
132311333dd5SFranco Fichtner				    "$rc_arg" = 'reload' ]; then
132411333dd5SFranco Fichtner					_run_rc_setup || \
132511333dd5SFranco Fichtner					    warn "failed to setup ${name}"
132611333dd5SFranco Fichtner				fi
13270696600cSBjoern A. Zeeb				_run_rc_precmd || return 1
13282efbd480SAlexander Leidinger			fi
13292efbd480SAlexander Leidinger			if ! checkyesno ${name}_svcj; then
13300661f938SMiroslav Lachman				_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1
13312efbd480SAlexander Leidinger			else
13322efbd480SAlexander Leidinger				case "$rc_arg" in
13332efbd480SAlexander Leidinger				start)
13342efbd480SAlexander Leidinger					if [ "${_rc_svcj}" != jailing ]; then
13352efbd480SAlexander Leidinger						_return=1
13362d08f6b5SAlexander Leidinger						_do_jailing=1
13372d08f6b5SAlexander Leidinger
13382d08f6b5SAlexander Leidinger						if check_jail jailed; then
13392d08f6b5SAlexander Leidinger							if [ $(${SYSCTL_N} security.jail.children.max) -eq 0 ]; then
13402d08f6b5SAlexander Leidinger								echo ERROR: jail parameter children.max is set to 0, can not create a new service jail.
13412d08f6b5SAlexander Leidinger								_do_jailing=0
13422d08f6b5SAlexander Leidinger							else
13432d08f6b5SAlexander Leidinger								_free_jails=$(($(${SYSCTL_N} security.jail.children.max) - $(${SYSCTL_N} security.jail.children.cur)))
13442d08f6b5SAlexander Leidinger								if [ ${_free_jails} -eq 0 ]; then
13452d08f6b5SAlexander Leidinger									echo ERROR: max number of jail children reached, can not create a new service jail.
13462d08f6b5SAlexander Leidinger									_do_jailing=0
13472d08f6b5SAlexander Leidinger
13482d08f6b5SAlexander Leidinger								fi
13492d08f6b5SAlexander Leidinger							fi
13502d08f6b5SAlexander Leidinger						fi
13512d08f6b5SAlexander Leidinger						if [ ${_do_jailing} -eq 1 ]; then
13522efbd480SAlexander Leidinger							$JAIL_CMD -c $_svcj_generic_params $_svcj_cmd_options \
13532efbd480SAlexander Leidinger							    exec.start="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}start $rc_extra_args" \
13542efbd480SAlexander Leidinger							    exec.stop="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}stop $rc_extra_args" \
13552efbd480SAlexander Leidinger							    exec.consolelog="/var/log/svcj_${name}_console.log" \
13562efbd480SAlexander Leidinger							    name=svcj-${name} && _return=0
13572d08f6b5SAlexander Leidinger						fi
13582efbd480SAlexander Leidinger					else
13592efbd480SAlexander Leidinger						_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1
13602efbd480SAlexander Leidinger					fi
13612efbd480SAlexander Leidinger					;;
13622efbd480SAlexander Leidinger				stop)
13632efbd480SAlexander Leidinger					if [ "${_rc_svcj}" != jailing ]; then
13642efbd480SAlexander Leidinger						$SERVICE -E _rc_svcj=jailing -j svcj-${name} ${name} ${_rc_prefix}stop $rc_extra_args || _return=1
13652efbd480SAlexander Leidinger						$JAIL_CMD -r svcj-${name} 2>/dev/null
13662efbd480SAlexander Leidinger					else
13672efbd480SAlexander Leidinger						_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1
13682efbd480SAlexander Leidinger					fi
13692efbd480SAlexander Leidinger					;;
13702efbd480SAlexander Leidinger				restart|status) ;; # no special case needed for svcj or handled somewhere else
13712efbd480SAlexander Leidinger				*)
13722efbd480SAlexander Leidinger					eval _rc_svcj_extra_cmd=\$${name}_${rc_arg}_svcj_enable
13732efbd480SAlexander Leidinger					: ${_rc_svcj_extra_cmd:=NO}
13742efbd480SAlexander Leidinger					if checkyesno _rc_svcj_extra_cmd && [ "${_rc_svcj}" != jailing ]; then
13752efbd480SAlexander Leidinger						$SERVICE -v -E _rc_svcj=jailing -j svcj-${name} ${name} ${_rc_prefix}${rc_arg} $rc_extra_args || _return=1
13762efbd480SAlexander Leidinger					else
13772efbd480SAlexander Leidinger						_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1
13782efbd480SAlexander Leidinger					fi
13792efbd480SAlexander Leidinger					;;
13802efbd480SAlexander Leidinger				esac
13812efbd480SAlexander Leidinger			fi
13822efbd480SAlexander Leidinger			if [ "${_rc_svcj}" != jailing ]; then
13830696600cSBjoern A. Zeeb				_run_rc_postcmd
13842efbd480SAlexander Leidinger			fi
13850696600cSBjoern A. Zeeb			return $_return
13860696600cSBjoern A. Zeeb		fi
13870696600cSBjoern A. Zeeb
13880696600cSBjoern A. Zeeb		case "$rc_arg" in	# default operations...
13890696600cSBjoern A. Zeeb
13900696600cSBjoern A. Zeeb		describe)
13910696600cSBjoern A. Zeeb			if [ -n "$desc" ]; then
13920696600cSBjoern A. Zeeb				echo "$desc"
13930696600cSBjoern A. Zeeb			fi
13940696600cSBjoern A. Zeeb			;;
13950696600cSBjoern A. Zeeb
13960696600cSBjoern A. Zeeb		extracommands)
13970696600cSBjoern A. Zeeb			echo "$extra_commands"
13980696600cSBjoern A. Zeeb			;;
13990696600cSBjoern A. Zeeb
1400da45b4daSDevin Teske		enable)
1401da45b4daSDevin Teske			_out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=YES") &&
1402da45b4daSDevin Teske				echo "$name enabled in ${_out%%:*}"
1403da45b4daSDevin Teske			;;
1404da45b4daSDevin Teske
1405da45b4daSDevin Teske		disable)
1406da45b4daSDevin Teske			_out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=NO") &&
1407da45b4daSDevin Teske				echo "$name disabled in ${_out%%:*}"
1408da45b4daSDevin Teske			;;
1409da45b4daSDevin Teske
1410da45b4daSDevin Teske		delete)
1411da45b4daSDevin Teske			_files=
1412*401516dbSMateusz Piotrowski			for _file in $(/usr/sbin/sysrc -lEs "$name"); do
1413*401516dbSMateusz Piotrowski				_out=$(/usr/sbin/sysrc -Fif $_file "$rcvar") && _files="$_files $_file"
1414da45b4daSDevin Teske			done
1415da45b4daSDevin Teske			/usr/sbin/sysrc -x "$rcvar" && echo "$rcvar deleted in ${_files# }"
1416da45b4daSDevin Teske				# delete file in rc.conf.d if desired and empty.
1417da45b4daSDevin Teske			checkyesno service_delete_empty || _files=
1418da45b4daSDevin Teske			for _file in $_files; do
1419da45b4daSDevin Teske				[ "$_file" = "${_file#*/rc.conf.d/}" ] && continue
1420da45b4daSDevin Teske				[ $(/usr/bin/stat -f%z $_file) -gt 0 ] && continue
1421da45b4daSDevin Teske				/bin/rm "$_file" && echo "Empty file $_file removed"
1422da45b4daSDevin Teske			done
1423da45b4daSDevin Teske			;;
1424da45b4daSDevin Teske
14250696600cSBjoern A. Zeeb		status)
14260696600cSBjoern A. Zeeb			_run_rc_precmd || return 1
14270696600cSBjoern A. Zeeb			if [ -n "$rc_pid" ]; then
14280696600cSBjoern A. Zeeb				echo "${name} is running as pid $rc_pid."
14290696600cSBjoern A. Zeeb			else
14300696600cSBjoern A. Zeeb				echo "${name} is not running."
14310696600cSBjoern A. Zeeb				return 1
14320696600cSBjoern A. Zeeb			fi
14330696600cSBjoern A. Zeeb			_run_rc_postcmd
14340696600cSBjoern A. Zeeb			;;
14350696600cSBjoern A. Zeeb
14360696600cSBjoern A. Zeeb		start)
14370696600cSBjoern A. Zeeb			if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then
14380696600cSBjoern A. Zeeb				warn "run_rc_command: cannot run $command"
14390696600cSBjoern A. Zeeb				return 1
14400696600cSBjoern A. Zeeb			fi
14410696600cSBjoern A. Zeeb
14422efbd480SAlexander Leidinger			if [ "${_rc_svcj}" != jailing ]; then
144311333dd5SFranco Fichtner				_run_rc_setup || warn "failed to setup ${name}"
144411333dd5SFranco Fichtner
14450696600cSBjoern A. Zeeb				if ! _run_rc_precmd; then
14460696600cSBjoern A. Zeeb					warn "failed precmd routine for ${name}"
14470696600cSBjoern A. Zeeb					return 1
14480696600cSBjoern A. Zeeb				fi
14492efbd480SAlexander Leidinger			fi
14502efbd480SAlexander Leidinger
14512efbd480SAlexander Leidinger			if checkyesno ${name}_svcj; then
14522efbd480SAlexander Leidinger				if [ "${_rc_svcj}" != jailing ]; then
14532d08f6b5SAlexander Leidinger					if check_jail jailed; then
14542d08f6b5SAlexander Leidinger						if [ $(${SYSCTL_N} security.jail.children.max) -eq 0 ]; then
14552d08f6b5SAlexander Leidinger							echo ERROR: jail parameter children.max is set to 0, can not create a new service jail.
14562d08f6b5SAlexander Leidinger							return 1
14572d08f6b5SAlexander Leidinger						else
14582d08f6b5SAlexander Leidinger							_free_jails=$(($(${SYSCTL_N} security.jail.children.max) - $(${SYSCTL_N} security.jail.children.cur)))
14592d08f6b5SAlexander Leidinger							if [ ${_free_jails} -eq 0 ]; then
14602d08f6b5SAlexander Leidinger								echo ERROR: max number of jail children reached, can not create a new service jail.
14612d08f6b5SAlexander Leidinger								return 1
14622d08f6b5SAlexander Leidinger							fi
14632d08f6b5SAlexander Leidinger						fi
14642d08f6b5SAlexander Leidinger					fi
14652efbd480SAlexander Leidinger					$JAIL_CMD -c $_svcj_generic_params $_svcj_cmd_options\
14662efbd480SAlexander Leidinger					    exec.start="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}start $rc_extra_args" \
14672efbd480SAlexander Leidinger					    exec.stop="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}stop $rc_extra_args" \
14682efbd480SAlexander Leidinger					    exec.consolelog="/var/log/svcj_${name}_console.log" \
14692efbd480SAlexander Leidinger					    name=svcj-${name} || return 1
14702efbd480SAlexander Leidinger				fi
14712efbd480SAlexander Leidinger			fi
14720696600cSBjoern A. Zeeb
14730696600cSBjoern A. Zeeb			# setup the full command to run
14740696600cSBjoern A. Zeeb			#
1475325ebf37SJose Luis Duran			startmsg "Starting ${name}."
14760696600cSBjoern A. Zeeb			if [ -n "$_chroot" ]; then
14770696600cSBjoern A. Zeeb				_cd=
14780696600cSBjoern A. Zeeb				_doit="\
14790696600cSBjoern A. Zeeb${_nice:+nice -n $_nice }\
14800661f938SMiroslav Lachman$_cpusetcmd \
14810696600cSBjoern A. Zeeb${_fib:+setfib -F $_fib }\
14820696600cSBjoern A. Zeeb${_env:+env $_env }\
14830696600cSBjoern A. Zeebchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
14840696600cSBjoern A. Zeeb$_chroot $command $rc_flags $command_args"
14850696600cSBjoern A. Zeeb			else
14860696600cSBjoern A. Zeeb				_cd="${_chdir:+cd $_chdir && }"
14870696600cSBjoern A. Zeeb				_doit="\
14880696600cSBjoern A. Zeeb${_fib:+setfib -F $_fib }\
14890696600cSBjoern A. Zeeb${_env:+env $_env }\
14900661f938SMiroslav Lachman$_cpusetcmd $command $rc_flags $command_args"
14910696600cSBjoern A. Zeeb				if [ -n "$_user" ]; then
14920696600cSBjoern A. Zeeb				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
14930696600cSBjoern A. Zeeb				fi
14940696600cSBjoern A. Zeeb				if [ -n "$_nice" ]; then
14950696600cSBjoern A. Zeeb					if [ -z "$_user" ]; then
14960696600cSBjoern A. Zeeb						_doit="sh -c \"$_doit\""
14970696600cSBjoern A. Zeeb					fi
14980696600cSBjoern A. Zeeb					_doit="nice -n $_nice $_doit"
14990696600cSBjoern A. Zeeb				fi
15000696600cSBjoern A. Zeeb				if [ -n "$_prepend" ]; then
15010696600cSBjoern A. Zeeb					_doit="$_prepend $_doit"
15020696600cSBjoern A. Zeeb				fi
15030696600cSBjoern A. Zeeb			fi
15040696600cSBjoern A. Zeeb
15050696600cSBjoern A. Zeeb			# Prepend default limits
15060696600cSBjoern A. Zeeb			_doit="$_cd limits -C $_login_class $_limits $_doit"
15070696600cSBjoern A. Zeeb
15082efbd480SAlexander Leidinger			local _really_run_it=true
15092efbd480SAlexander Leidinger			if checkyesno ${name}_svcj; then
15102efbd480SAlexander Leidinger				if [ "${_rc_svcj}" != jailing ]; then
15112efbd480SAlexander Leidinger					_really_run_it=false
15122efbd480SAlexander Leidinger				fi
15132efbd480SAlexander Leidinger			fi
15142efbd480SAlexander Leidinger
15152efbd480SAlexander Leidinger			if [ "$_really_run_it" = true ]; then
15160696600cSBjoern A. Zeeb				# run the full command
15170696600cSBjoern A. Zeeb				#
15180696600cSBjoern A. Zeeb				if ! _run_rc_doit "$_doit"; then
15190696600cSBjoern A. Zeeb					warn "failed to start ${name}"
15200696600cSBjoern A. Zeeb					return 1
15210696600cSBjoern A. Zeeb				fi
15222efbd480SAlexander Leidinger			fi
15230696600cSBjoern A. Zeeb
15242efbd480SAlexander Leidinger			if [ "${_rc_svcj}" != jailing ]; then
15250696600cSBjoern A. Zeeb				# finally, run postcmd
15260696600cSBjoern A. Zeeb				#
15270696600cSBjoern A. Zeeb				_run_rc_postcmd
15282efbd480SAlexander Leidinger			fi
15290696600cSBjoern A. Zeeb			;;
15300696600cSBjoern A. Zeeb
15310696600cSBjoern A. Zeeb		stop)
15320696600cSBjoern A. Zeeb			if [ -z "$rc_pid" ]; then
15330696600cSBjoern A. Zeeb				[ -n "$rc_fast" ] && return 0
15340696600cSBjoern A. Zeeb				_run_rc_notrunning
15350696600cSBjoern A. Zeeb				return 1
15360696600cSBjoern A. Zeeb			fi
15370696600cSBjoern A. Zeeb
15380696600cSBjoern A. Zeeb			_run_rc_precmd || return 1
15390696600cSBjoern A. Zeeb
15400696600cSBjoern A. Zeeb			# send the signal to stop
15410696600cSBjoern A. Zeeb			#
15420696600cSBjoern A. Zeeb			echo "Stopping ${name}."
15430696600cSBjoern A. Zeeb			_doit=$(_run_rc_killcmd "${sig_stop:-TERM}")
15440696600cSBjoern A. Zeeb			_run_rc_doit "$_doit" || return 1
15450696600cSBjoern A. Zeeb
15460696600cSBjoern A. Zeeb			# wait for the command to exit,
15470696600cSBjoern A. Zeeb			# and run postcmd.
15480696600cSBjoern A. Zeeb			wait_for_pids $rc_pid
15490696600cSBjoern A. Zeeb
15502efbd480SAlexander Leidinger			if checkyesno ${name}_svcj; then
15512efbd480SAlexander Leidinger				# remove service jail
15522efbd480SAlexander Leidinger				$JAIL_CMD -r svcj-${name} 2>/dev/null
15532efbd480SAlexander Leidinger			fi
15542efbd480SAlexander Leidinger
15550696600cSBjoern A. Zeeb			_run_rc_postcmd
15560696600cSBjoern A. Zeeb			;;
15570696600cSBjoern A. Zeeb
15580696600cSBjoern A. Zeeb		reload)
15590696600cSBjoern A. Zeeb			if [ -z "$rc_pid" ]; then
15600696600cSBjoern A. Zeeb				_run_rc_notrunning
15610696600cSBjoern A. Zeeb				return 1
15620696600cSBjoern A. Zeeb			fi
15630696600cSBjoern A. Zeeb
156411333dd5SFranco Fichtner			_run_rc_setup || warn "failed to setup ${name}"
156511333dd5SFranco Fichtner
15660696600cSBjoern A. Zeeb			_run_rc_precmd || return 1
15670696600cSBjoern A. Zeeb
15680696600cSBjoern A. Zeeb			_doit=$(_run_rc_killcmd "${sig_reload:-HUP}")
15690696600cSBjoern A. Zeeb			_run_rc_doit "$_doit" || return 1
15700696600cSBjoern A. Zeeb
15710696600cSBjoern A. Zeeb			_run_rc_postcmd
15720696600cSBjoern A. Zeeb			;;
15730696600cSBjoern A. Zeeb
15740696600cSBjoern A. Zeeb		restart)
157511333dd5SFranco Fichtner			_run_rc_setup || warn "failed to setup ${name}"
157611333dd5SFranco Fichtner
15770696600cSBjoern A. Zeeb			# prevent restart being called more
15780696600cSBjoern A. Zeeb			# than once by any given script
15790696600cSBjoern A. Zeeb			#
15800696600cSBjoern A. Zeeb			if ${_rc_restart_done:-false}; then
15810696600cSBjoern A. Zeeb				return 0
15820696600cSBjoern A. Zeeb			fi
15830696600cSBjoern A. Zeeb			_rc_restart_done=true
15840696600cSBjoern A. Zeeb
15850696600cSBjoern A. Zeeb			_run_rc_precmd || return 1
15860696600cSBjoern A. Zeeb
15870696600cSBjoern A. Zeeb			# run those in a subshell to keep global variables
15880696600cSBjoern A. Zeeb			( run_rc_command ${_rc_prefix}stop $rc_extra_args )
15890696600cSBjoern A. Zeeb			( run_rc_command ${_rc_prefix}start $rc_extra_args )
15900696600cSBjoern A. Zeeb			_return=$?
15910696600cSBjoern A. Zeeb			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
15920696600cSBjoern A. Zeeb
15930696600cSBjoern A. Zeeb			_run_rc_postcmd
15940696600cSBjoern A. Zeeb			;;
15950696600cSBjoern A. Zeeb
15960696600cSBjoern A. Zeeb		poll)
15970696600cSBjoern A. Zeeb			_run_rc_precmd || return 1
15980696600cSBjoern A. Zeeb			if [ -n "$rc_pid" ]; then
15990696600cSBjoern A. Zeeb				wait_for_pids $rc_pid
16000696600cSBjoern A. Zeeb			fi
16010696600cSBjoern A. Zeeb			_run_rc_postcmd
16020696600cSBjoern A. Zeeb			;;
16030696600cSBjoern A. Zeeb
16040696600cSBjoern A. Zeeb		rcvar)
16050696600cSBjoern A. Zeeb			echo -n "# $name"
16060696600cSBjoern A. Zeeb			if [ -n "$desc" ]; then
16070696600cSBjoern A. Zeeb				echo " : $desc"
16080696600cSBjoern A. Zeeb			else
16090696600cSBjoern A. Zeeb				echo ""
16100696600cSBjoern A. Zeeb			fi
16110696600cSBjoern A. Zeeb			echo "#"
16120696600cSBjoern A. Zeeb			# Get unique vars in $rcvar $rcvars
16130696600cSBjoern A. Zeeb			for _v in $rcvar $rcvars; do
16140696600cSBjoern A. Zeeb				case $v in
16150696600cSBjoern A. Zeeb				$_v\ *|\ *$_v|*\ $_v\ *) ;;
16160696600cSBjoern A. Zeeb				*)	v="${v# } $_v" ;;
16170696600cSBjoern A. Zeeb				esac
16180696600cSBjoern A. Zeeb			done
16190696600cSBjoern A. Zeeb
16200696600cSBjoern A. Zeeb			# Display variables.
16210696600cSBjoern A. Zeeb			for _v in $v; do
16220696600cSBjoern A. Zeeb				if [ -z "$_v" ]; then
16230696600cSBjoern A. Zeeb					continue
16240696600cSBjoern A. Zeeb				fi
16250696600cSBjoern A. Zeeb
16260696600cSBjoern A. Zeeb				eval _desc=\$${_v}_desc
16270696600cSBjoern A. Zeeb				eval _defval=\$${_v}_defval
16280696600cSBjoern A. Zeeb				_h="-"
16290696600cSBjoern A. Zeeb
16300696600cSBjoern A. Zeeb				eval echo \"$_v=\\\"\$$_v\\\"\"
16310696600cSBjoern A. Zeeb				# decode multiple lines of _desc
16320696600cSBjoern A. Zeeb				while [ -n "$_desc" ]; do
16330696600cSBjoern A. Zeeb					case $_desc in
16340696600cSBjoern A. Zeeb					*^^*)
16350696600cSBjoern A. Zeeb						echo "# $_h ${_desc%%^^*}"
16360696600cSBjoern A. Zeeb						_desc=${_desc#*^^}
16370696600cSBjoern A. Zeeb						_h=" "
16380696600cSBjoern A. Zeeb						;;
16390696600cSBjoern A. Zeeb					*)
16400696600cSBjoern A. Zeeb						echo "# $_h ${_desc}"
16410696600cSBjoern A. Zeeb						break
16420696600cSBjoern A. Zeeb						;;
16430696600cSBjoern A. Zeeb					esac
16440696600cSBjoern A. Zeeb				done
16450696600cSBjoern A. Zeeb				echo "#   (default: \"$_defval\")"
16460696600cSBjoern A. Zeeb			done
16470696600cSBjoern A. Zeeb			echo ""
16480696600cSBjoern A. Zeeb			;;
16490696600cSBjoern A. Zeeb
16500696600cSBjoern A. Zeeb		*)
16510696600cSBjoern A. Zeeb			rc_usage $_keywords
16520696600cSBjoern A. Zeeb			;;
16530696600cSBjoern A. Zeeb
16540696600cSBjoern A. Zeeb		esac
16550696600cSBjoern A. Zeeb
16560696600cSBjoern A. Zeeb		# Apply protect(1) to the PID if ${name}_oomprotect is set.
16570696600cSBjoern A. Zeeb		case "$rc_arg" in
16580696600cSBjoern A. Zeeb		start)
16590696600cSBjoern A. Zeeb			# We cannot use protect(1) inside jails.
16600696600cSBjoern A. Zeeb			if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] &&
16610696600cSBjoern A. Zeeb			    [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then
16626ba108e5SMariusz Zaborski				[ -z "${rc_pid}" ] && eval $_pidcmd
16630696600cSBjoern A. Zeeb				case $_oomprotect in
16640696600cSBjoern A. Zeeb				[Aa][Ll][Ll])
166568e035c0SMike Walker					${PROTECT} -d -i -p ${rc_pid}
16660696600cSBjoern A. Zeeb					;;
16670696600cSBjoern A. Zeeb				[Yy][Ee][Ss])
16686ba108e5SMariusz Zaborski					${PROTECT} -p ${rc_pid}
16690696600cSBjoern A. Zeeb					;;
16700696600cSBjoern A. Zeeb				esac
16710696600cSBjoern A. Zeeb			fi
16720696600cSBjoern A. Zeeb		;;
16730696600cSBjoern A. Zeeb		esac
16740696600cSBjoern A. Zeeb
16750696600cSBjoern A. Zeeb		return $_return
16760696600cSBjoern A. Zeeb	done
16770696600cSBjoern A. Zeeb
16780696600cSBjoern A. Zeeb	echo 1>&2 "$0: unknown directive '$rc_arg'."
16790696600cSBjoern A. Zeeb	rc_usage $_keywords
16800696600cSBjoern A. Zeeb	# not reached
16810696600cSBjoern A. Zeeb}
16820696600cSBjoern A. Zeeb
16830696600cSBjoern A. Zeeb#
16840696600cSBjoern A. Zeeb# Helper functions for run_rc_command: common code.
16850696600cSBjoern A. Zeeb# They use such global variables besides the exported rc_* ones:
16860696600cSBjoern A. Zeeb#
16870696600cSBjoern A. Zeeb#	name	       R/W
16880696600cSBjoern A. Zeeb#	------------------
168932a579e4SEugene Grosbein#	_offcmd		R
16900696600cSBjoern A. Zeeb#	_precmd		R
16910696600cSBjoern A. Zeeb#	_postcmd	R
16920696600cSBjoern A. Zeeb#	_return		W
169311333dd5SFranco Fichtner#	_setup		R
16940696600cSBjoern A. Zeeb#
169532a579e4SEugene Grosbein_run_rc_offcmd()
169632a579e4SEugene Grosbein{
169732a579e4SEugene Grosbein	eval _offcmd=\$${name}_offcmd
169832a579e4SEugene Grosbein	if [ -n "$_offcmd" ]; then
169932a579e4SEugene Grosbein		if [ -n "$_env" ]; then
170032a579e4SEugene Grosbein			eval "export -- $_env"
170132a579e4SEugene Grosbein		fi
1702c2db3a0cSEugene Grosbein		debug "run_rc_command: ${name}_offcmd: $_offcmd $rc_extra_args"
170332a579e4SEugene Grosbein		eval "$_offcmd $rc_extra_args"
170432a579e4SEugene Grosbein		_return=$?
170532a579e4SEugene Grosbein	fi
170632a579e4SEugene Grosbein	return 0
170732a579e4SEugene Grosbein}
170832a579e4SEugene Grosbein
17090696600cSBjoern A. Zeeb_run_rc_precmd()
17100696600cSBjoern A. Zeeb{
17110696600cSBjoern A. Zeeb	check_required_before "$rc_arg" || return 1
17120696600cSBjoern A. Zeeb
17130696600cSBjoern A. Zeeb	if [ -n "$_precmd" ]; then
17140696600cSBjoern A. Zeeb		debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args"
17150696600cSBjoern A. Zeeb		eval "$_precmd $rc_extra_args"
17160696600cSBjoern A. Zeeb		_return=$?
17170696600cSBjoern A. Zeeb
17180696600cSBjoern A. Zeeb		# If precmd failed and force isn't set, request exit.
17190696600cSBjoern A. Zeeb		if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
17200696600cSBjoern A. Zeeb			return 1
17210696600cSBjoern A. Zeeb		fi
17220696600cSBjoern A. Zeeb	fi
17230696600cSBjoern A. Zeeb
17240696600cSBjoern A. Zeeb	check_required_after "$rc_arg" || return 1
17250696600cSBjoern A. Zeeb
17260696600cSBjoern A. Zeeb	return 0
17270696600cSBjoern A. Zeeb}
17280696600cSBjoern A. Zeeb
17290696600cSBjoern A. Zeeb_run_rc_postcmd()
17300696600cSBjoern A. Zeeb{
17310696600cSBjoern A. Zeeb	if [ -n "$_postcmd" ]; then
17320696600cSBjoern A. Zeeb		debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args"
17330696600cSBjoern A. Zeeb		eval "$_postcmd $rc_extra_args"
17340696600cSBjoern A. Zeeb		_return=$?
17350696600cSBjoern A. Zeeb	fi
17360696600cSBjoern A. Zeeb	return 0
17370696600cSBjoern A. Zeeb}
17380696600cSBjoern A. Zeeb
173911333dd5SFranco Fichtner_run_rc_setup()
174011333dd5SFranco Fichtner{
174111333dd5SFranco Fichtner	# prevent multiple execution on restart => stop/start split
174211333dd5SFranco Fichtner	if ! ${_rc_restart_done:-false} && [ -n "$_setup" ]; then
174311333dd5SFranco Fichtner		debug "run_rc_command: ${rc_arg}_setup: $_setup"
174411333dd5SFranco Fichtner		eval "$_setup"
174511333dd5SFranco Fichtner		_return=$?
174611333dd5SFranco Fichtner		if [ $_return -ne 0 ]; then
174711333dd5SFranco Fichtner			return 1
174811333dd5SFranco Fichtner		fi
174911333dd5SFranco Fichtner	fi
175011333dd5SFranco Fichtner	return 0
175111333dd5SFranco Fichtner}
175211333dd5SFranco Fichtner
17530696600cSBjoern A. Zeeb_run_rc_doit()
17540696600cSBjoern A. Zeeb{
1755160a2f2cSEugene Grosbein	local _m
1756160a2f2cSEugene Grosbein
17570696600cSBjoern A. Zeeb	debug "run_rc_command: doit: $*"
1758160a2f2cSEugene Grosbein	_m=$(umask)
1759160a2f2cSEugene Grosbein	${_umask:+umask ${_umask}}
17600696600cSBjoern A. Zeeb	eval "$@"
17610696600cSBjoern A. Zeeb	_return=$?
1762160a2f2cSEugene Grosbein	umask ${_m}
17630696600cSBjoern A. Zeeb
17640696600cSBjoern A. Zeeb	# If command failed and force isn't set, request exit.
17650696600cSBjoern A. Zeeb	if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then
17660696600cSBjoern A. Zeeb		return 1
17670696600cSBjoern A. Zeeb	fi
17680696600cSBjoern A. Zeeb
17690696600cSBjoern A. Zeeb	return 0
17700696600cSBjoern A. Zeeb}
17710696600cSBjoern A. Zeeb
17720696600cSBjoern A. Zeeb_run_rc_notrunning()
17730696600cSBjoern A. Zeeb{
17740696600cSBjoern A. Zeeb	local _pidmsg
17750696600cSBjoern A. Zeeb
17760696600cSBjoern A. Zeeb	if [ -n "$pidfile" ]; then
17770696600cSBjoern A. Zeeb		_pidmsg=" (check $pidfile)."
17780696600cSBjoern A. Zeeb	else
17790696600cSBjoern A. Zeeb		_pidmsg=
17800696600cSBjoern A. Zeeb	fi
17810696600cSBjoern A. Zeeb	echo 1>&2 "${name} not running?${_pidmsg}"
17820696600cSBjoern A. Zeeb}
17830696600cSBjoern A. Zeeb
17840696600cSBjoern A. Zeeb_run_rc_killcmd()
17850696600cSBjoern A. Zeeb{
17860696600cSBjoern A. Zeeb	local _cmd
17870696600cSBjoern A. Zeeb
17880696600cSBjoern A. Zeeb	_cmd="kill -$1 $rc_pid"
17890696600cSBjoern A. Zeeb	if [ -n "$_user" ]; then
17900696600cSBjoern A. Zeeb		_cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'"
17910696600cSBjoern A. Zeeb	fi
17920696600cSBjoern A. Zeeb	echo "$_cmd"
17930696600cSBjoern A. Zeeb}
17940696600cSBjoern A. Zeeb
17950696600cSBjoern A. Zeeb#
17960696600cSBjoern A. Zeeb# run_rc_script file arg
17970696600cSBjoern A. Zeeb#	Start the script `file' with `arg', and correctly handle the
17980696600cSBjoern A. Zeeb#	return value from the script.
17990696600cSBjoern A. Zeeb#	If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's
18000696600cSBjoern A. Zeeb#	an old-style startup file.
18010696600cSBjoern A. Zeeb#	If `file' appears to be a backup or scratch file, ignore it.
18020696600cSBjoern A. Zeeb#	Otherwise if it is executable run as a child process.
18030696600cSBjoern A. Zeeb#
18040696600cSBjoern A. Zeebrun_rc_script()
18050696600cSBjoern A. Zeeb{
18060696600cSBjoern A. Zeeb	_file=$1
18070696600cSBjoern A. Zeeb	_arg=$2
18080696600cSBjoern A. Zeeb	if [ -z "$_file" -o -z "$_arg" ]; then
18090696600cSBjoern A. Zeeb		err 3 'USAGE: run_rc_script file arg'
18100696600cSBjoern A. Zeeb	fi
18110696600cSBjoern A. Zeeb
18120696600cSBjoern A. Zeeb	unset	name command command_args command_interpreter \
18130696600cSBjoern A. Zeeb		extra_commands pidfile procname \
18140696600cSBjoern A. Zeeb		rcvar rcvars rcvars_obsolete required_dirs required_files \
18150696600cSBjoern A. Zeeb		required_vars
18160696600cSBjoern A. Zeeb	eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
18170696600cSBjoern A. Zeeb
1818aa3b7a2fSSimon J. Gerraty	rc_trace 0 "$_file $_arg"
1819aa3b7a2fSSimon J. Gerraty	# don't use it if we don't trust it
1820aa3b7a2fSSimon J. Gerraty	is_verified $_file || return
1821aa3b7a2fSSimon J. Gerraty
1822ac102a2aSKyle Evans	rc_service="$_file"
18230696600cSBjoern A. Zeeb	case "$_file" in
18240696600cSBjoern A. Zeeb	/etc/rc.d/*.sh)			# no longer allowed in the base
18250696600cSBjoern A. Zeeb		warn "Ignoring old-style startup script $_file"
18260696600cSBjoern A. Zeeb		;;
18270696600cSBjoern A. Zeeb	*[~#]|*.OLD|*.bak|*.orig|*,v)	# scratch file; skip
18280696600cSBjoern A. Zeeb		warn "Ignoring scratch file $_file"
18290696600cSBjoern A. Zeeb		;;
18300696600cSBjoern A. Zeeb	*)				# run in subshell
18310696600cSBjoern A. Zeeb		if [ -x $_file ]; then
1832aa3b7a2fSSimon J. Gerraty			DebugOn $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg
1833aa3b7a2fSSimon J. Gerraty
1834318d0db5SMitchell Horne			if [ -n "$rc_boottrace" ]; then
1835318d0db5SMitchell Horne				boottrace_fn "$_file" "$_arg"
18360696600cSBjoern A. Zeeb			else
18370696600cSBjoern A. Zeeb				( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
18380696600cSBjoern A. Zeeb				  trap "echo Script $_file interrupted >&2 ; exit 1" 2
18390696600cSBjoern A. Zeeb				  trap "echo Script $_file running >&2" 29
18400696600cSBjoern A. Zeeb				  set $_arg; . $_file )
18410696600cSBjoern A. Zeeb			fi
1842b5f6beefSR. Christian McDonald			DebugOff rc=$? $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg
18430696600cSBjoern A. Zeeb		fi
18440696600cSBjoern A. Zeeb		;;
18450696600cSBjoern A. Zeeb	esac
18460696600cSBjoern A. Zeeb}
18470696600cSBjoern A. Zeeb
1848aa3b7a2fSSimon J. Gerraty#
1849aa3b7a2fSSimon J. Gerraty# run_rc_scripts [options] file [...]
1850aa3b7a2fSSimon J. Gerraty#
1851aa3b7a2fSSimon J. Gerraty# Call `run_rc_script' for each "file" unless already listed in
1852aa3b7a2fSSimon J. Gerraty# $_rc_elem_done.
1853aa3b7a2fSSimon J. Gerraty#
1854aa3b7a2fSSimon J. Gerraty# Options:
1855aa3b7a2fSSimon J. Gerraty#
1856aa3b7a2fSSimon J. Gerraty#	--arg "arg"
1857aa3b7a2fSSimon J. Gerraty#		Pass "arg" to `run_rc_script' default is $_boot.
1858aa3b7a2fSSimon J. Gerraty#
1859aa3b7a2fSSimon J. Gerraty#	--break "marker"
1860aa3b7a2fSSimon J. Gerraty#		If any "file" matches "marker" stop processing.
1861aa3b7a2fSSimon J. Gerraty#
1862aa3b7a2fSSimon J. Gerraty_rc_elem_done=
1863aa3b7a2fSSimon J. Gerratyrun_rc_scripts()
1864aa3b7a2fSSimon J. Gerraty{
1865aa3b7a2fSSimon J. Gerraty	local _arg=${_boot}
1866aa3b7a2fSSimon J. Gerraty	local _rc_elem
1867aa3b7a2fSSimon J. Gerraty	local _rc_breaks=
1868aa3b7a2fSSimon J. Gerraty
1869aa3b7a2fSSimon J. Gerraty	while :; do
1870aa3b7a2fSSimon J. Gerraty		case "$1" in
1871aa3b7a2fSSimon J. Gerraty		--arg)
1872aa3b7a2fSSimon J. Gerraty                        _arg="$2"
1873aa3b7a2fSSimon J. Gerraty                        shift 2
1874aa3b7a2fSSimon J. Gerraty                        ;;
1875aa3b7a2fSSimon J. Gerraty		--break)
1876aa3b7a2fSSimon J. Gerraty                        _rc_breaks="$_rc_breaks $2"
1877aa3b7a2fSSimon J. Gerraty                        shift 2
1878aa3b7a2fSSimon J. Gerraty                        ;;
1879aa3b7a2fSSimon J. Gerraty		*)
1880aa3b7a2fSSimon J. Gerraty                        break
1881aa3b7a2fSSimon J. Gerraty                        ;;
1882aa3b7a2fSSimon J. Gerraty		esac
1883aa3b7a2fSSimon J. Gerraty	done
1884aa3b7a2fSSimon J. Gerraty	for _rc_elem in "$@"; do
1885aa3b7a2fSSimon J. Gerraty		: _rc_elem=$_rc_elem
1886aa3b7a2fSSimon J. Gerraty		case " $_rc_elem_done " in
1887aa3b7a2fSSimon J. Gerraty		*" $_rc_elem "*)
1888aa3b7a2fSSimon J. Gerraty                        continue
1889aa3b7a2fSSimon J. Gerraty                        ;;
1890aa3b7a2fSSimon J. Gerraty		esac
1891aa3b7a2fSSimon J. Gerraty		run_rc_script ${_rc_elem} ${_arg}
1892aa3b7a2fSSimon J. Gerraty		_rc_elem_done="$_rc_elem_done $_rc_elem"
1893aa3b7a2fSSimon J. Gerraty		case " $_rc_breaks " in
1894aa3b7a2fSSimon J. Gerraty		*" ${_rc_elem##*/} "*)
1895aa3b7a2fSSimon J. Gerraty                        break
1896aa3b7a2fSSimon J. Gerraty                        ;;
1897aa3b7a2fSSimon J. Gerraty		esac
1898aa3b7a2fSSimon J. Gerraty	done
1899aa3b7a2fSSimon J. Gerraty}
1900aa3b7a2fSSimon J. Gerraty
1901318d0db5SMitchell Horneboottrace_fn()
1902318d0db5SMitchell Horne{
1903318d0db5SMitchell Horne	local _file _arg
1904318d0db5SMitchell Horne	_file=$1
1905318d0db5SMitchell Horne	_arg=$2
1906318d0db5SMitchell Horne
1907f13275cfSAlex Samorukov	_boot="${_boot}" rc_fast="${rc_fast}" autoboot="${autoboot}" \
1908318d0db5SMitchell Horne	    $boottrace_cmd "$_file" "$_arg"
1909318d0db5SMitchell Horne}
1910318d0db5SMitchell Horne
19110696600cSBjoern A. Zeeb#
19120696600cSBjoern A. Zeeb# load_rc_config [service]
19130696600cSBjoern A. Zeeb#	Source in the configuration file(s) for a given service.
19140696600cSBjoern A. Zeeb#	If no service is specified, only the global configuration
19150696600cSBjoern A. Zeeb#	file(s) will be loaded.
19160696600cSBjoern A. Zeeb#
19170696600cSBjoern A. Zeebload_rc_config()
19180696600cSBjoern A. Zeeb{
1919aa3b7a2fSSimon J. Gerraty	local _name _rcvar_val _var _defval _v _msg _new _d _dot
19200696600cSBjoern A. Zeeb	_name=$1
1921aa3b7a2fSSimon J. Gerraty	_dot=${load_rc_config_reader:-dot}
1922aa3b7a2fSSimon J. Gerraty
1923aa3b7a2fSSimon J. Gerraty	case "$_dot" in
1924aa3b7a2fSSimon J. Gerraty	dot|[sv]dot)
1925aa3b7a2fSSimon J. Gerraty		;;
1926aa3b7a2fSSimon J. Gerraty	*)	warn "Ignoring invalid load_rc_config_reader"
1927aa3b7a2fSSimon J. Gerraty		_dot=dot
1928aa3b7a2fSSimon J. Gerraty		;;
1929aa3b7a2fSSimon J. Gerraty	esac
1930aa3b7a2fSSimon J. Gerraty	case "$1" in
1931aa3b7a2fSSimon J. Gerraty	-s|--safe)
1932aa3b7a2fSSimon J. Gerraty                _dot=sdot
1933aa3b7a2fSSimon J. Gerraty                _name=$2
1934aa3b7a2fSSimon J. Gerraty                shift
1935aa3b7a2fSSimon J. Gerraty                ;;
1936aa3b7a2fSSimon J. Gerraty	-v|--verify)
1937aa3b7a2fSSimon J. Gerraty                _dot=vdot
1938aa3b7a2fSSimon J. Gerraty                _name=$2
1939aa3b7a2fSSimon J. Gerraty                shift
1940aa3b7a2fSSimon J. Gerraty                ;;
1941aa3b7a2fSSimon J. Gerraty	esac
1942aa3b7a2fSSimon J. Gerraty
1943aa3b7a2fSSimon J. Gerraty	DebugOn rc:$_name $_name
19440696600cSBjoern A. Zeeb
19450696600cSBjoern A. Zeeb	if ${_rc_conf_loaded:-false}; then
19460696600cSBjoern A. Zeeb		:
19470696600cSBjoern A. Zeeb	else
19480696600cSBjoern A. Zeeb		if [ -r /etc/defaults/rc.conf ]; then
19490696600cSBjoern A. Zeeb			debug "Sourcing /etc/defaults/rc.conf"
1950aa3b7a2fSSimon J. Gerraty			$_dot /etc/defaults/rc.conf
19510696600cSBjoern A. Zeeb			source_rc_confs
19520696600cSBjoern A. Zeeb		elif [ -r /etc/rc.conf ]; then
19530696600cSBjoern A. Zeeb			debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1954aa3b7a2fSSimon J. Gerraty			$_dot /etc/rc.conf
19550696600cSBjoern A. Zeeb		fi
19560696600cSBjoern A. Zeeb		_rc_conf_loaded=true
19570696600cSBjoern A. Zeeb	fi
19580696600cSBjoern A. Zeeb
19590696600cSBjoern A. Zeeb	# If a service name was specified, attempt to load
19600696600cSBjoern A. Zeeb	# service-specific configuration
19610696600cSBjoern A. Zeeb	if [ -n "$_name" ] ; then
19620696600cSBjoern A. Zeeb		for _d in /etc ${local_startup}; do
19630696600cSBjoern A. Zeeb			_d=${_d%/rc.d}
19640696600cSBjoern A. Zeeb			if [ -f ${_d}/rc.conf.d/"$_name" ]; then
19650696600cSBjoern A. Zeeb				debug "Sourcing ${_d}/rc.conf.d/$_name"
1966aa3b7a2fSSimon J. Gerraty				$_dot ${_d}/rc.conf.d/"$_name"
19670696600cSBjoern A. Zeeb			elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
19680696600cSBjoern A. Zeeb				local _rc
19690696600cSBjoern A. Zeeb				for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
19700696600cSBjoern A. Zeeb					if [ -f "$_rc" ] ; then
19710696600cSBjoern A. Zeeb						debug "Sourcing $_rc"
1972aa3b7a2fSSimon J. Gerraty						$_dot "$_rc"
19730696600cSBjoern A. Zeeb					fi
19740696600cSBjoern A. Zeeb				done
19750696600cSBjoern A. Zeeb			fi
19760696600cSBjoern A. Zeeb		done
19770696600cSBjoern A. Zeeb	fi
19780696600cSBjoern A. Zeeb
19790696600cSBjoern A. Zeeb	# Set defaults if defined.
19800696600cSBjoern A. Zeeb	for _var in $rcvar $rcvars; do
19810696600cSBjoern A. Zeeb		eval _defval=\$${_var}_defval
19820696600cSBjoern A. Zeeb		if [ -n "$_defval" ]; then
19830696600cSBjoern A. Zeeb			eval : \${$_var:=\$${_var}_defval}
19840696600cSBjoern A. Zeeb		fi
19850696600cSBjoern A. Zeeb	done
19860696600cSBjoern A. Zeeb
19870696600cSBjoern A. Zeeb	# check obsolete rc.conf variables
19880696600cSBjoern A. Zeeb	for _var in $rcvars_obsolete; do
19890696600cSBjoern A. Zeeb		eval _v=\$$_var
19900696600cSBjoern A. Zeeb		eval _msg=\$${_var}_obsolete_msg
19910696600cSBjoern A. Zeeb		eval _new=\$${_var}_newvar
19920696600cSBjoern A. Zeeb		case $_v in
19930696600cSBjoern A. Zeeb		"")
19940696600cSBjoern A. Zeeb			;;
19950696600cSBjoern A. Zeeb		*)
19960696600cSBjoern A. Zeeb			if [ -z "$_new" ]; then
19970696600cSBjoern A. Zeeb				_msg="Ignored."
19980696600cSBjoern A. Zeeb			else
19990696600cSBjoern A. Zeeb				eval $_new=\"\$$_var\"
20000696600cSBjoern A. Zeeb				if [ -z "$_msg" ]; then
20010696600cSBjoern A. Zeeb					_msg="Use \$$_new instead."
20020696600cSBjoern A. Zeeb				fi
20030696600cSBjoern A. Zeeb			fi
20040696600cSBjoern A. Zeeb			warn "\$$_var is obsolete.  $_msg"
20050696600cSBjoern A. Zeeb			;;
20060696600cSBjoern A. Zeeb		esac
20070696600cSBjoern A. Zeeb	done
20080696600cSBjoern A. Zeeb}
20090696600cSBjoern A. Zeeb
20100696600cSBjoern A. Zeeb#
20110696600cSBjoern A. Zeeb# load_rc_config_var name var
20120696600cSBjoern A. Zeeb#	Read the rc.conf(5) var for name and set in the
20130696600cSBjoern A. Zeeb#	current shell, using load_rc_config in a subshell to prevent
20140696600cSBjoern A. Zeeb#	unwanted side effects from other variable assignments.
20150696600cSBjoern A. Zeeb#
20160696600cSBjoern A. Zeebload_rc_config_var()
20170696600cSBjoern A. Zeeb{
20180696600cSBjoern A. Zeeb	if [ $# -ne 2 ]; then
20190696600cSBjoern A. Zeeb		err 3 'USAGE: load_rc_config_var name var'
20200696600cSBjoern A. Zeeb	fi
20210696600cSBjoern A. Zeeb	eval $(eval '(
20220696600cSBjoern A. Zeeb		load_rc_config '$1' >/dev/null;
20230696600cSBjoern A. Zeeb		if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then
20240696600cSBjoern A. Zeeb			echo '$2'=\'\''${'$2'}\'\'';
20250696600cSBjoern A. Zeeb		fi
20260696600cSBjoern A. Zeeb	)' )
20270696600cSBjoern A. Zeeb}
20280696600cSBjoern A. Zeeb
20290696600cSBjoern A. Zeeb#
20300696600cSBjoern A. Zeeb# rc_usage commands
20310696600cSBjoern A. Zeeb#	Print a usage string for $0, with `commands' being a list of
20320696600cSBjoern A. Zeeb#	valid commands.
20330696600cSBjoern A. Zeeb#
20340696600cSBjoern A. Zeebrc_usage()
20350696600cSBjoern A. Zeeb{
20360696600cSBjoern A. Zeeb	echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
20370696600cSBjoern A. Zeeb
20380696600cSBjoern A. Zeeb	_sep=
20390696600cSBjoern A. Zeeb	for _elem; do
20400696600cSBjoern A. Zeeb		echo -n 1>&2 "$_sep$_elem"
20410696600cSBjoern A. Zeeb		_sep="|"
20420696600cSBjoern A. Zeeb	done
20430696600cSBjoern A. Zeeb	echo 1>&2 ")"
20440696600cSBjoern A. Zeeb	exit 1
20450696600cSBjoern A. Zeeb}
20460696600cSBjoern A. Zeeb
20470696600cSBjoern A. Zeeb#
20480696600cSBjoern A. Zeeb# err exitval message
20490696600cSBjoern A. Zeeb#	Display message to stderr and log to the syslog, and exit with exitval.
20500696600cSBjoern A. Zeeb#
20510696600cSBjoern A. Zeeberr()
20520696600cSBjoern A. Zeeb{
20530696600cSBjoern A. Zeeb	exitval=$1
20540696600cSBjoern A. Zeeb	shift
20550696600cSBjoern A. Zeeb
20560696600cSBjoern A. Zeeb	if [ -x /usr/bin/logger ]; then
20570696600cSBjoern A. Zeeb		logger "$0: ERROR: $*"
20580696600cSBjoern A. Zeeb	fi
20590696600cSBjoern A. Zeeb	echo 1>&2 "$0: ERROR: $*"
20600696600cSBjoern A. Zeeb	exit $exitval
20610696600cSBjoern A. Zeeb}
20620696600cSBjoern A. Zeeb
20630696600cSBjoern A. Zeeb#
20640696600cSBjoern A. Zeeb# warn message
20650696600cSBjoern A. Zeeb#	Display message to stderr and log to the syslog.
20660696600cSBjoern A. Zeeb#
20670696600cSBjoern A. Zeebwarn()
20680696600cSBjoern A. Zeeb{
20690696600cSBjoern A. Zeeb	if [ -x /usr/bin/logger ]; then
20700696600cSBjoern A. Zeeb		logger "$0: WARNING: $*"
20710696600cSBjoern A. Zeeb	fi
20720696600cSBjoern A. Zeeb	echo 1>&2 "$0: WARNING: $*"
20730696600cSBjoern A. Zeeb}
20740696600cSBjoern A. Zeeb
20750696600cSBjoern A. Zeeb#
20760696600cSBjoern A. Zeeb# info message
20770696600cSBjoern A. Zeeb#	Display informational message to stdout and log to syslog.
20780696600cSBjoern A. Zeeb#
20790696600cSBjoern A. Zeebinfo()
20800696600cSBjoern A. Zeeb{
20810696600cSBjoern A. Zeeb	case ${rc_info} in
20820696600cSBjoern A. Zeeb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
20830696600cSBjoern A. Zeeb		if [ -x /usr/bin/logger ]; then
20840696600cSBjoern A. Zeeb			logger "$0: INFO: $*"
20850696600cSBjoern A. Zeeb		fi
20860696600cSBjoern A. Zeeb		echo "$0: INFO: $*"
20870696600cSBjoern A. Zeeb		;;
20880696600cSBjoern A. Zeeb	esac
20890696600cSBjoern A. Zeeb}
20900696600cSBjoern A. Zeeb
20910696600cSBjoern A. Zeeb#
20920696600cSBjoern A. Zeeb# debug message
20930696600cSBjoern A. Zeeb#	If debugging is enabled in rc.conf output message to stderr.
20940696600cSBjoern A. Zeeb#	BEWARE that you don't call any subroutine that itself calls this
20950696600cSBjoern A. Zeeb#	function.
20960696600cSBjoern A. Zeeb#
20970696600cSBjoern A. Zeebdebug()
20980696600cSBjoern A. Zeeb{
20990696600cSBjoern A. Zeeb	case ${rc_debug} in
21000696600cSBjoern A. Zeeb	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
21010696600cSBjoern A. Zeeb		if [ -x /usr/bin/logger ]; then
21020696600cSBjoern A. Zeeb			logger "$0: DEBUG: $*"
21030696600cSBjoern A. Zeeb		fi
21040696600cSBjoern A. Zeeb		echo 1>&2 "$0: DEBUG: $*"
21050696600cSBjoern A. Zeeb		;;
21060696600cSBjoern A. Zeeb	esac
21070696600cSBjoern A. Zeeb}
21080696600cSBjoern A. Zeeb
21090696600cSBjoern A. Zeeb#
21100696600cSBjoern A. Zeeb# backup_file action file cur backup
21110696600cSBjoern A. Zeeb#	Make a backup copy of `file' into `cur', and save the previous
21120696600cSBjoern A. Zeeb#	version of `cur' as `backup'.
21130696600cSBjoern A. Zeeb#
21140696600cSBjoern A. Zeeb#	The `action' keyword can be one of the following:
21150696600cSBjoern A. Zeeb#
21160696600cSBjoern A. Zeeb#	add		`file' is now being backed up (and is possibly
21170696600cSBjoern A. Zeeb#			being reentered into the backups system).  `cur'
21180696600cSBjoern A. Zeeb#			is created.
21190696600cSBjoern A. Zeeb#
21200696600cSBjoern A. Zeeb#	update		`file' has changed and needs to be backed up.
21210696600cSBjoern A. Zeeb#			If `cur' exists, it is copied to `back'
21220696600cSBjoern A. Zeeb#			and then `file' is copied to `cur'.
21230696600cSBjoern A. Zeeb#
21240696600cSBjoern A. Zeeb#	remove		`file' is no longer being tracked by the backups
21250696600cSBjoern A. Zeeb#			system.  `cur' is moved `back'.
21260696600cSBjoern A. Zeeb#
21270696600cSBjoern A. Zeeb#
21280696600cSBjoern A. Zeebbackup_file()
21290696600cSBjoern A. Zeeb{
21300696600cSBjoern A. Zeeb	_action=$1
21310696600cSBjoern A. Zeeb	_file=$2
21320696600cSBjoern A. Zeeb	_cur=$3
21330696600cSBjoern A. Zeeb	_back=$4
21340696600cSBjoern A. Zeeb
21350696600cSBjoern A. Zeeb	case $_action in
21360696600cSBjoern A. Zeeb	add|update)
21370696600cSBjoern A. Zeeb		if [ -f $_cur ]; then
21380696600cSBjoern A. Zeeb			cp -p $_cur $_back
21390696600cSBjoern A. Zeeb		fi
21400696600cSBjoern A. Zeeb		cp -p $_file $_cur
21410696600cSBjoern A. Zeeb		chown root:wheel $_cur
21420696600cSBjoern A. Zeeb		;;
21430696600cSBjoern A. Zeeb	remove)
21440696600cSBjoern A. Zeeb		mv -f $_cur $_back
21450696600cSBjoern A. Zeeb		;;
21460696600cSBjoern A. Zeeb	esac
21470696600cSBjoern A. Zeeb}
21480696600cSBjoern A. Zeeb
21490696600cSBjoern A. Zeeb# make_symlink src link
21500696600cSBjoern A. Zeeb#	Make a symbolic link 'link' to src from basedir. If the
21510696600cSBjoern A. Zeeb#	directory in which link is to be created does not exist
21520696600cSBjoern A. Zeeb#	a warning will be displayed and an error will be returned.
21530696600cSBjoern A. Zeeb#	Returns 0 on success, 1 otherwise.
21540696600cSBjoern A. Zeeb#
21550696600cSBjoern A. Zeebmake_symlink()
21560696600cSBjoern A. Zeeb{
21570696600cSBjoern A. Zeeb	local src link linkdir _me
21580696600cSBjoern A. Zeeb	src="$1"
21590696600cSBjoern A. Zeeb	link="$2"
21600696600cSBjoern A. Zeeb	linkdir="`dirname $link`"
21610696600cSBjoern A. Zeeb	_me="make_symlink()"
21620696600cSBjoern A. Zeeb
21630696600cSBjoern A. Zeeb	if [ -z "$src" -o -z "$link" ]; then
21640696600cSBjoern A. Zeeb		warn "$_me: requires two arguments."
21650696600cSBjoern A. Zeeb		return 1
21660696600cSBjoern A. Zeeb	fi
21670696600cSBjoern A. Zeeb	if [ ! -d "$linkdir" ]; then
21680696600cSBjoern A. Zeeb		warn "$_me: the directory $linkdir does not exist."
21690696600cSBjoern A. Zeeb		return 1
21700696600cSBjoern A. Zeeb	fi
21710696600cSBjoern A. Zeeb	if ! ln -sf $src $link; then
21720696600cSBjoern A. Zeeb		warn "$_me: unable to make a symbolic link from $link to $src"
21730696600cSBjoern A. Zeeb		return 1
21740696600cSBjoern A. Zeeb	fi
21750696600cSBjoern A. Zeeb	return 0
21760696600cSBjoern A. Zeeb}
21770696600cSBjoern A. Zeeb
21780696600cSBjoern A. Zeeb# devfs_rulesets_from_file file
21790696600cSBjoern A. Zeeb#	Reads a set of devfs commands from file, and creates
21800696600cSBjoern A. Zeeb#	the specified rulesets with their rules. Returns non-zero
21810696600cSBjoern A. Zeeb#	if there was an error.
21820696600cSBjoern A. Zeeb#
21830696600cSBjoern A. Zeebdevfs_rulesets_from_file()
21840696600cSBjoern A. Zeeb{
21850696600cSBjoern A. Zeeb	local file _err _me _opts
21860696600cSBjoern A. Zeeb	file="$1"
21870696600cSBjoern A. Zeeb	_me="devfs_rulesets_from_file"
21880696600cSBjoern A. Zeeb	_err=0
21890696600cSBjoern A. Zeeb
21900696600cSBjoern A. Zeeb	if [ -z "$file" ]; then
21910696600cSBjoern A. Zeeb		warn "$_me: you must specify a file"
21920696600cSBjoern A. Zeeb		return 1
21930696600cSBjoern A. Zeeb	fi
21940696600cSBjoern A. Zeeb	if [ ! -e "$file" ]; then
21950696600cSBjoern A. Zeeb		debug "$_me: no such file ($file)"
21960696600cSBjoern A. Zeeb		return 0
21970696600cSBjoern A. Zeeb	fi
21980696600cSBjoern A. Zeeb
21990696600cSBjoern A. Zeeb	# Disable globbing so that the rule patterns are not expanded
22000696600cSBjoern A. Zeeb	# by accident with matching filesystem entries.
22010696600cSBjoern A. Zeeb	_opts=$-; set -f
22020696600cSBjoern A. Zeeb
22030696600cSBjoern A. Zeeb	debug "reading rulesets from file ($file)"
22040696600cSBjoern A. Zeeb	{ while read line
22050696600cSBjoern A. Zeeb	do
22060696600cSBjoern A. Zeeb		case $line in
22070696600cSBjoern A. Zeeb		\#*)
22080696600cSBjoern A. Zeeb			continue
22090696600cSBjoern A. Zeeb			;;
22100696600cSBjoern A. Zeeb		\[*\]*)
22110696600cSBjoern A. Zeeb			rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
22120696600cSBjoern A. Zeeb			if [ -z "$rulenum" ]; then
22130696600cSBjoern A. Zeeb				warn "$_me: cannot extract rule number ($line)"
22140696600cSBjoern A. Zeeb				_err=1
22150696600cSBjoern A. Zeeb				break
22160696600cSBjoern A. Zeeb			fi
22170696600cSBjoern A. Zeeb			rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
22180696600cSBjoern A. Zeeb			if [ -z "$rulename" ]; then
22190696600cSBjoern A. Zeeb				warn "$_me: cannot extract rule name ($line)"
22200696600cSBjoern A. Zeeb				_err=1
22210696600cSBjoern A. Zeeb				break;
22220696600cSBjoern A. Zeeb			fi
22230696600cSBjoern A. Zeeb			eval $rulename=\$rulenum
22240696600cSBjoern A. Zeeb			debug "found ruleset: $rulename=$rulenum"
22250696600cSBjoern A. Zeeb			if ! /sbin/devfs rule -s $rulenum delset; then
22260696600cSBjoern A. Zeeb				_err=1
22270696600cSBjoern A. Zeeb				break
22280696600cSBjoern A. Zeeb			fi
22290696600cSBjoern A. Zeeb			;;
22300696600cSBjoern A. Zeeb		*)
22310696600cSBjoern A. Zeeb			rulecmd="${line%%"\#*"}"
22320696600cSBjoern A. Zeeb			# evaluate the command incase it includes
22330696600cSBjoern A. Zeeb			# other rules
22340696600cSBjoern A. Zeeb			if [ -n "$rulecmd" ]; then
22350696600cSBjoern A. Zeeb				debug "adding rule ($rulecmd)"
22360696600cSBjoern A. Zeeb				if ! eval /sbin/devfs rule -s $rulenum $rulecmd
22370696600cSBjoern A. Zeeb				then
22380696600cSBjoern A. Zeeb					_err=1
22390696600cSBjoern A. Zeeb					break
22400696600cSBjoern A. Zeeb				fi
22410696600cSBjoern A. Zeeb			fi
22420696600cSBjoern A. Zeeb			;;
22430696600cSBjoern A. Zeeb		esac
22440696600cSBjoern A. Zeeb		if [ $_err -ne 0 ]; then
22450696600cSBjoern A. Zeeb			debug "error in $_me"
22460696600cSBjoern A. Zeeb			break
22470696600cSBjoern A. Zeeb		fi
22480696600cSBjoern A. Zeeb	done } < $file
22490696600cSBjoern A. Zeeb	case $_opts in *f*) ;; *) set +f ;; esac
22500696600cSBjoern A. Zeeb	return $_err
22510696600cSBjoern A. Zeeb}
22520696600cSBjoern A. Zeeb
22530696600cSBjoern A. Zeeb# devfs_init_rulesets
22540696600cSBjoern A. Zeeb#	Initializes rulesets from configuration files. Returns
22550696600cSBjoern A. Zeeb#	non-zero if there was an error.
22560696600cSBjoern A. Zeeb#
22570696600cSBjoern A. Zeebdevfs_init_rulesets()
22580696600cSBjoern A. Zeeb{
22590696600cSBjoern A. Zeeb	local file _me
22600696600cSBjoern A. Zeeb	_me="devfs_init_rulesets"
22610696600cSBjoern A. Zeeb
22620696600cSBjoern A. Zeeb	# Go through this only once
22630696600cSBjoern A. Zeeb	if [ -n "$devfs_rulesets_init" ]; then
22640696600cSBjoern A. Zeeb		debug "$_me: devfs rulesets already initialized"
22650696600cSBjoern A. Zeeb		return
22660696600cSBjoern A. Zeeb	fi
22670696600cSBjoern A. Zeeb	for file in $devfs_rulesets; do
22680696600cSBjoern A. Zeeb		if ! devfs_rulesets_from_file $file; then
22690696600cSBjoern A. Zeeb			warn "$_me: could not read rules from $file"
22700696600cSBjoern A. Zeeb			return 1
22710696600cSBjoern A. Zeeb		fi
22720696600cSBjoern A. Zeeb	done
22730696600cSBjoern A. Zeeb	devfs_rulesets_init=1
22740696600cSBjoern A. Zeeb	debug "$_me: devfs rulesets initialized"
22750696600cSBjoern A. Zeeb	return 0
22760696600cSBjoern A. Zeeb}
22770696600cSBjoern A. Zeeb
22780696600cSBjoern A. Zeeb# devfs_set_ruleset ruleset [dir]
22790696600cSBjoern A. Zeeb#	Sets the default ruleset of dir to ruleset. The ruleset argument
22800696600cSBjoern A. Zeeb#	must be a ruleset name as specified in devfs.rules(5) file.
22810696600cSBjoern A. Zeeb#	Returns non-zero if it could not set it successfully.
22820696600cSBjoern A. Zeeb#
22830696600cSBjoern A. Zeebdevfs_set_ruleset()
22840696600cSBjoern A. Zeeb{
22850696600cSBjoern A. Zeeb	local devdir rs _me
22860696600cSBjoern A. Zeeb	[ -n "$1" ] && eval rs=\$$1 || rs=
22870696600cSBjoern A. Zeeb	[ -n "$2" ] && devdir="-m "$2"" || devdir=
22880696600cSBjoern A. Zeeb	_me="devfs_set_ruleset"
22890696600cSBjoern A. Zeeb
22900696600cSBjoern A. Zeeb	if [ -z "$rs" ]; then
22910696600cSBjoern A. Zeeb		warn "$_me: you must specify a ruleset number"
22920696600cSBjoern A. Zeeb		return 1
22930696600cSBjoern A. Zeeb	fi
22940696600cSBjoern A. Zeeb	debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
22950696600cSBjoern A. Zeeb	if ! /sbin/devfs $devdir ruleset $rs; then
22960696600cSBjoern A. Zeeb		warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
22970696600cSBjoern A. Zeeb		return 1
22980696600cSBjoern A. Zeeb	fi
22990696600cSBjoern A. Zeeb	return 0
23000696600cSBjoern A. Zeeb}
23010696600cSBjoern A. Zeeb
23020696600cSBjoern A. Zeeb# devfs_apply_ruleset ruleset [dir]
23030696600cSBjoern A. Zeeb#	Apply ruleset number $ruleset to the devfs mountpoint $dir.
23040696600cSBjoern A. Zeeb#	The ruleset argument must be a ruleset name as specified
23050696600cSBjoern A. Zeeb#	in a devfs.rules(5) file.  Returns 0 on success or non-zero
23060696600cSBjoern A. Zeeb#	if it could not apply the ruleset.
23070696600cSBjoern A. Zeeb#
23080696600cSBjoern A. Zeebdevfs_apply_ruleset()
23090696600cSBjoern A. Zeeb{
23100696600cSBjoern A. Zeeb	local devdir rs _me
23110696600cSBjoern A. Zeeb	[ -n "$1" ] && eval rs=\$$1 || rs=
23120696600cSBjoern A. Zeeb	[ -n "$2" ] && devdir="-m "$2"" || devdir=
23130696600cSBjoern A. Zeeb	_me="devfs_apply_ruleset"
23140696600cSBjoern A. Zeeb
23150696600cSBjoern A. Zeeb	if [ -z "$rs" ]; then
23160696600cSBjoern A. Zeeb		warn "$_me: you must specify a ruleset"
23170696600cSBjoern A. Zeeb		return 1
23180696600cSBjoern A. Zeeb	fi
23190696600cSBjoern A. Zeeb	debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
23200696600cSBjoern A. Zeeb	if ! /sbin/devfs $devdir rule -s $rs applyset; then
23210696600cSBjoern A. Zeeb		warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
23220696600cSBjoern A. Zeeb		return 1
23230696600cSBjoern A. Zeeb	fi
23240696600cSBjoern A. Zeeb	return 0
23250696600cSBjoern A. Zeeb}
23260696600cSBjoern A. Zeeb
23270696600cSBjoern A. Zeeb# devfs_domount dir [ruleset]
23280696600cSBjoern A. Zeeb#	Mount devfs on dir. If ruleset is specified it is set
23290696600cSBjoern A. Zeeb#	on the mount-point. It must also be a ruleset name as specified
23300696600cSBjoern A. Zeeb#	in a devfs.rules(5) file. Returns 0 on success.
23310696600cSBjoern A. Zeeb#
23320696600cSBjoern A. Zeebdevfs_domount()
23330696600cSBjoern A. Zeeb{
23340696600cSBjoern A. Zeeb	local devdir rs _me
23350696600cSBjoern A. Zeeb	devdir="$1"
23360696600cSBjoern A. Zeeb	[ -n "$2" ] && rs=$2 || rs=
23370696600cSBjoern A. Zeeb	_me="devfs_domount()"
23380696600cSBjoern A. Zeeb
23390696600cSBjoern A. Zeeb	if [ -z "$devdir" ]; then
23400696600cSBjoern A. Zeeb		warn "$_me: you must specify a mount-point"
23410696600cSBjoern A. Zeeb		return 1
23420696600cSBjoern A. Zeeb	fi
23430696600cSBjoern A. Zeeb	debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
23440696600cSBjoern A. Zeeb	if ! mount -t devfs dev "$devdir"; then
23450696600cSBjoern A. Zeeb		warn "$_me: Unable to mount devfs on $devdir"
23460696600cSBjoern A. Zeeb		return 1
23470696600cSBjoern A. Zeeb	fi
23480696600cSBjoern A. Zeeb	if [ -n "$rs" ]; then
23490696600cSBjoern A. Zeeb		devfs_init_rulesets
23500696600cSBjoern A. Zeeb		devfs_set_ruleset $rs $devdir
23510696600cSBjoern A. Zeeb		devfs -m $devdir rule applyset
23520696600cSBjoern A. Zeeb	fi
23530696600cSBjoern A. Zeeb	return 0
23540696600cSBjoern A. Zeeb}
23550696600cSBjoern A. Zeeb
23560696600cSBjoern A. Zeeb# Provide a function for normalizing the mounting of memory
23570696600cSBjoern A. Zeeb# filesystems.  This should allow the rest of the code here to remain
23580696600cSBjoern A. Zeeb# as close as possible between 5-current and 4-stable.
23590696600cSBjoern A. Zeeb#   $1 = size
23600696600cSBjoern A. Zeeb#   $2 = mount point
23610696600cSBjoern A. Zeeb#   $3 = (optional) extra mdmfs flags
23620696600cSBjoern A. Zeebmount_md()
23630696600cSBjoern A. Zeeb{
23640696600cSBjoern A. Zeeb	if [ -n "$3" ]; then
23650696600cSBjoern A. Zeeb		flags="$3"
23660696600cSBjoern A. Zeeb	fi
23670696600cSBjoern A. Zeeb	/sbin/mdmfs $flags -s $1 ${mfs_type} $2
23680696600cSBjoern A. Zeeb}
23690696600cSBjoern A. Zeeb
23700696600cSBjoern A. Zeeb# Code common to scripts that need to load a kernel module
23710696600cSBjoern A. Zeeb# if it isn't in the kernel yet. Syntax:
2372b11974c2SChris Rees#   load_kld [-e regex] [-m module] file
2373b11974c2SChris Rees# where -e or -m chooses the way to check if the module
2374b11974c2SChris Rees# is already loaded:
2375b11974c2SChris Rees#   regex is egrep'd in the output from `kldstat -v',
2376b11974c2SChris Rees#   module is passed to `kldstat -m'.
2377b11974c2SChris Rees# The default way is as though `-m file' were specified.
23780696600cSBjoern A. Zeebload_kld()
23790696600cSBjoern A. Zeeb{
2380b11974c2SChris Rees	local _loaded _mod _opt _re
23810696600cSBjoern A. Zeeb
23820696600cSBjoern A. Zeeb	while getopts "e:m:" _opt; do
23830696600cSBjoern A. Zeeb		case "$_opt" in
2384b11974c2SChris Rees		e) _re="$OPTARG" ;;
2385b11974c2SChris Rees		m) _mod="$OPTARG" ;;
2386b11974c2SChris Rees		*) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;;
23870696600cSBjoern A. Zeeb		esac
23880696600cSBjoern A. Zeeb	done
23890696600cSBjoern A. Zeeb	shift $(($OPTIND - 1))
23900696600cSBjoern A. Zeeb	if [ $# -ne 1 ]; then
2391b11974c2SChris Rees		err 3 'USAGE: load_kld [-e regex] [-m module] file'
23920696600cSBjoern A. Zeeb	fi
2393b11974c2SChris Rees	_mod=${_mod:-$1}
2394b11974c2SChris Rees	_loaded=false
2395b11974c2SChris Rees	if [ -n "$_re" ]; then
2396b11974c2SChris Rees		if kldstat -v | egrep -q -e "$_re"; then
2397b11974c2SChris Rees			_loaded=true
2398b11974c2SChris Rees		fi
2399b11974c2SChris Rees	else
2400b11974c2SChris Rees		if kldstat -q -m "$_mod"; then
2401b11974c2SChris Rees			_loaded=true
2402b11974c2SChris Rees		fi
2403b11974c2SChris Rees	fi
2404b11974c2SChris Rees	if ! $_loaded; then
2405b11974c2SChris Rees		if ! kldload "$1"; then
24060696600cSBjoern A. Zeeb			warn "Unable to load kernel module $1"
24070696600cSBjoern A. Zeeb			return 1
24080696600cSBjoern A. Zeeb		else
24090696600cSBjoern A. Zeeb			info "$1 kernel module loaded."
24105ac2a874SDoug Rabson			if [ -f "/etc/sysctl.kld.d/$1.conf" ]; then
24115ac2a874SDoug Rabson				sysctl -f "/etc/sysctl.kld.d/$1.conf"
241209267cc1SDoug Rabson			fi
24130696600cSBjoern A. Zeeb		fi
2414b11974c2SChris Rees	else
2415b11974c2SChris Rees		debug "load_kld: $1 kernel module already loaded."
2416b11974c2SChris Rees	fi
24170696600cSBjoern A. Zeeb	return 0
24180696600cSBjoern A. Zeeb}
24190696600cSBjoern A. Zeeb
24200696600cSBjoern A. Zeeb# ltr str src dst [var]
24210696600cSBjoern A. Zeeb#	Change every $src in $str to $dst.
24220696600cSBjoern A. Zeeb#	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
24230696600cSBjoern A. Zeeb#	awk(1). If var is non-NULL, set it to the result.
24240696600cSBjoern A. Zeebltr()
24250696600cSBjoern A. Zeeb{
24260696600cSBjoern A. Zeeb	local _str _src _dst _out _com _var
24270696600cSBjoern A. Zeeb	_str="$1"
24280696600cSBjoern A. Zeeb	_src="$2"
24290696600cSBjoern A. Zeeb	_dst="$3"
24300696600cSBjoern A. Zeeb	_var="$4"
24310696600cSBjoern A. Zeeb	_out=""
24320696600cSBjoern A. Zeeb
24330696600cSBjoern A. Zeeb	local IFS="${_src}"
24340696600cSBjoern A. Zeeb	for _com in ${_str}; do
24350696600cSBjoern A. Zeeb		if [ -z "${_out}" ]; then
24360696600cSBjoern A. Zeeb			_out="${_com}"
24370696600cSBjoern A. Zeeb		else
24380696600cSBjoern A. Zeeb			_out="${_out}${_dst}${_com}"
24390696600cSBjoern A. Zeeb		fi
24400696600cSBjoern A. Zeeb	done
24410696600cSBjoern A. Zeeb	if [ -n "${_var}" ]; then
24420696600cSBjoern A. Zeeb		setvar "${_var}" "${_out}"
24430696600cSBjoern A. Zeeb	else
24440696600cSBjoern A. Zeeb		echo "${_out}"
24450696600cSBjoern A. Zeeb	fi
24460696600cSBjoern A. Zeeb}
24470696600cSBjoern A. Zeeb
24480696600cSBjoern A. Zeeb# Creates a list of providers for GELI encryption.
24490696600cSBjoern A. Zeebgeli_make_list()
24500696600cSBjoern A. Zeeb{
24510696600cSBjoern A. Zeeb	local devices devices2
24520696600cSBjoern A. Zeeb	local provider mountpoint type options rest
24530696600cSBjoern A. Zeeb
24540696600cSBjoern A. Zeeb	# Create list of GELI providers from fstab.
24550696600cSBjoern A. Zeeb	while read provider mountpoint type options rest ; do
24560696600cSBjoern A. Zeeb		case ":${options}" in
24570696600cSBjoern A. Zeeb		:*noauto*)
24580696600cSBjoern A. Zeeb			noauto=yes
24590696600cSBjoern A. Zeeb			;;
24600696600cSBjoern A. Zeeb		*)
24610696600cSBjoern A. Zeeb			noauto=no
24620696600cSBjoern A. Zeeb			;;
24630696600cSBjoern A. Zeeb		esac
24640696600cSBjoern A. Zeeb
24650696600cSBjoern A. Zeeb		case ":${provider}" in
24660696600cSBjoern A. Zeeb		:#*)
24670696600cSBjoern A. Zeeb			continue
24680696600cSBjoern A. Zeeb			;;
24690696600cSBjoern A. Zeeb		*.eli)
24700696600cSBjoern A. Zeeb			# Skip swap devices.
24710696600cSBjoern A. Zeeb			if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then
24720696600cSBjoern A. Zeeb				continue
24730696600cSBjoern A. Zeeb			fi
24740696600cSBjoern A. Zeeb			devices="${devices} ${provider}"
24750696600cSBjoern A. Zeeb			;;
24760696600cSBjoern A. Zeeb		esac
24770696600cSBjoern A. Zeeb	done < /etc/fstab
24780696600cSBjoern A. Zeeb
24790696600cSBjoern A. Zeeb	# Append providers from geli_devices.
24800696600cSBjoern A. Zeeb	devices="${devices} ${geli_devices}"
24810696600cSBjoern A. Zeeb
24820696600cSBjoern A. Zeeb	for provider in ${devices}; do
24830696600cSBjoern A. Zeeb		provider=${provider%.eli}
24840696600cSBjoern A. Zeeb		provider=${provider#/dev/}
24850696600cSBjoern A. Zeeb		devices2="${devices2} ${provider}"
24860696600cSBjoern A. Zeeb	done
24870696600cSBjoern A. Zeeb
24880696600cSBjoern A. Zeeb	echo ${devices2}
24890696600cSBjoern A. Zeeb}
24900696600cSBjoern A. Zeeb
24910696600cSBjoern A. Zeeb# Originally, root mount hold had to be released before mounting
24920696600cSBjoern A. Zeeb# the root filesystem.  This delayed the boot, so it was changed
24930696600cSBjoern A. Zeeb# to only wait if the root device isn't readily available.  This
24940696600cSBjoern A. Zeeb# can result in rc scripts executing before all the devices - such
24950696600cSBjoern A. Zeeb# as graid(8), or USB disks - can be accessed.  This function can
24960696600cSBjoern A. Zeeb# be used to explicitly wait for root mount holds to be released.
24970696600cSBjoern A. Zeebroot_hold_wait()
24980696600cSBjoern A. Zeeb{
24990696600cSBjoern A. Zeeb	local wait waited holders
25000696600cSBjoern A. Zeeb
25010696600cSBjoern A. Zeeb	waited=0
25020696600cSBjoern A. Zeeb	while true; do
25030696600cSBjoern A. Zeeb		holders="$(sysctl -n vfs.root_mount_hold)"
25040696600cSBjoern A. Zeeb		if [ -z "${holders}" ]; then
25050696600cSBjoern A. Zeeb			break;
25060696600cSBjoern A. Zeeb		fi
25070696600cSBjoern A. Zeeb		if [ ${waited} -eq 0 ]; then
25080696600cSBjoern A. Zeeb			echo -n "Waiting ${root_hold_delay}s" \
25090696600cSBjoern A. Zeeb			"for the root mount holders: ${holders}"
25100696600cSBjoern A. Zeeb		else
25110696600cSBjoern A. Zeeb			echo -n .
25120696600cSBjoern A. Zeeb		fi
25130696600cSBjoern A. Zeeb		if [ ${waited} -ge ${root_hold_delay} ]; then
25140696600cSBjoern A. Zeeb			echo
25150696600cSBjoern A. Zeeb			break
25160696600cSBjoern A. Zeeb		fi
25170696600cSBjoern A. Zeeb		sleep 1
25180696600cSBjoern A. Zeeb		waited=$(($waited + 1))
25190696600cSBjoern A. Zeeb	done
25200696600cSBjoern A. Zeeb}
25210696600cSBjoern A. Zeeb
25220696600cSBjoern A. Zeeb# Find scripts in local_startup directories that use the old syntax
25230696600cSBjoern A. Zeeb#
25240696600cSBjoern A. Zeebfind_local_scripts_old() {
25250696600cSBjoern A. Zeeb	zlist=''
25260696600cSBjoern A. Zeeb	slist=''
25270696600cSBjoern A. Zeeb	for dir in ${local_startup}; do
25280696600cSBjoern A. Zeeb		if [ -d "${dir}" ]; then
25290696600cSBjoern A. Zeeb			for file in ${dir}/[0-9]*.sh; do
25300696600cSBjoern A. Zeeb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
25310696600cSBjoern A. Zeeb				    continue
25320696600cSBjoern A. Zeeb				zlist="$zlist $file"
25330696600cSBjoern A. Zeeb			done
25340696600cSBjoern A. Zeeb			for file in ${dir}/[!0-9]*.sh; do
25350696600cSBjoern A. Zeeb				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
25360696600cSBjoern A. Zeeb				    continue
25370696600cSBjoern A. Zeeb				slist="$slist $file"
25380696600cSBjoern A. Zeeb			done
25390696600cSBjoern A. Zeeb		fi
25400696600cSBjoern A. Zeeb	done
25410696600cSBjoern A. Zeeb}
25420696600cSBjoern A. Zeeb
25430696600cSBjoern A. Zeebfind_local_scripts_new() {
25440696600cSBjoern A. Zeeb	local_rc=''
25450696600cSBjoern A. Zeeb	for dir in ${local_startup}; do
25460696600cSBjoern A. Zeeb		if [ -d "${dir}" ]; then
25470696600cSBjoern A. Zeeb			for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do
25480696600cSBjoern A. Zeeb				case "$file" in
25493693d914SMina Galić				*.sample|*.pkgsave) ;;
25500696600cSBjoern A. Zeeb				*)	if [ -x "$file" ]; then
25510696600cSBjoern A. Zeeb						local_rc="${local_rc} ${file}"
25520696600cSBjoern A. Zeeb					fi
25530696600cSBjoern A. Zeeb					;;
25540696600cSBjoern A. Zeeb				esac
25550696600cSBjoern A. Zeeb			done
25560696600cSBjoern A. Zeeb		fi
25570696600cSBjoern A. Zeeb	done
25580696600cSBjoern A. Zeeb}
25590696600cSBjoern A. Zeeb
25603693d914SMina Galićfind_system_scripts() {
25613693d914SMina Galić	system_rc=''
25623693d914SMina Galić	for file in /etc/rc.d/*; do
25633693d914SMina Galić		case "${file##*/}" in
25643693d914SMina Galić		*.pkgsave) ;;
25653693d914SMina Galić		*)	if [ -x "$file" ]; then
25663693d914SMina Galić				system_rc="${system_rc} ${file}"
25673693d914SMina Galić			fi
25683693d914SMina Galić			;;
25693693d914SMina Galić		esac
25703693d914SMina Galić	done
25713693d914SMina Galić}
25723693d914SMina Galić
25730696600cSBjoern A. Zeeb# check_required_{before|after} command
25740696600cSBjoern A. Zeeb#	Check for things required by the command before and after its precmd,
25750696600cSBjoern A. Zeeb#	respectively.  The two separate functions are needed because some
25760696600cSBjoern A. Zeeb#	conditions should prevent precmd from being run while other things
25770696600cSBjoern A. Zeeb#	depend on precmd having already been run.
25780696600cSBjoern A. Zeeb#
25790696600cSBjoern A. Zeebcheck_required_before()
25800696600cSBjoern A. Zeeb{
25810696600cSBjoern A. Zeeb	local _f
25820696600cSBjoern A. Zeeb
25830696600cSBjoern A. Zeeb	case "$1" in
25840696600cSBjoern A. Zeeb	start)
25850696600cSBjoern A. Zeeb		for _f in $required_vars; do
25860696600cSBjoern A. Zeeb			if ! checkyesno $_f; then
25870696600cSBjoern A. Zeeb				warn "\$${_f} is not enabled."
25880696600cSBjoern A. Zeeb				if [ -z "$rc_force" ]; then
25890696600cSBjoern A. Zeeb					return 1
25900696600cSBjoern A. Zeeb				fi
25910696600cSBjoern A. Zeeb			fi
25920696600cSBjoern A. Zeeb		done
25930696600cSBjoern A. Zeeb
25940696600cSBjoern A. Zeeb		for _f in $required_dirs; do
25950696600cSBjoern A. Zeeb			if [ ! -d "${_f}/." ]; then
25960696600cSBjoern A. Zeeb				warn "${_f} is not a directory."
25970696600cSBjoern A. Zeeb				if [ -z "$rc_force" ]; then
25980696600cSBjoern A. Zeeb					return 1
25990696600cSBjoern A. Zeeb				fi
26000696600cSBjoern A. Zeeb			fi
26010696600cSBjoern A. Zeeb		done
26020696600cSBjoern A. Zeeb
26030696600cSBjoern A. Zeeb		for _f in $required_files; do
26040696600cSBjoern A. Zeeb			if [ ! -r "${_f}" ]; then
26050696600cSBjoern A. Zeeb				warn "${_f} is not readable."
26060696600cSBjoern A. Zeeb				if [ -z "$rc_force" ]; then
26070696600cSBjoern A. Zeeb					return 1
26080696600cSBjoern A. Zeeb				fi
26090696600cSBjoern A. Zeeb			fi
26100696600cSBjoern A. Zeeb		done
26110696600cSBjoern A. Zeeb		;;
26120696600cSBjoern A. Zeeb	esac
26130696600cSBjoern A. Zeeb
26140696600cSBjoern A. Zeeb	return 0
26150696600cSBjoern A. Zeeb}
26160696600cSBjoern A. Zeeb
26170696600cSBjoern A. Zeebcheck_required_after()
26180696600cSBjoern A. Zeeb{
26190696600cSBjoern A. Zeeb	local _f _args
26200696600cSBjoern A. Zeeb
26210696600cSBjoern A. Zeeb	case "$1" in
26220696600cSBjoern A. Zeeb	start)
26230696600cSBjoern A. Zeeb		for _f in $required_modules; do
26240696600cSBjoern A. Zeeb			case "${_f}" in
26250696600cSBjoern A. Zeeb				*~*)	_args="-e ${_f#*~} ${_f%%~*}" ;;
26260696600cSBjoern A. Zeeb				*:*)	_args="-m ${_f#*:} ${_f%%:*}" ;;
26270696600cSBjoern A. Zeeb				*)	_args="${_f}" ;;
26280696600cSBjoern A. Zeeb			esac
26290696600cSBjoern A. Zeeb			if ! load_kld ${_args}; then
26300696600cSBjoern A. Zeeb				if [ -z "$rc_force" ]; then
26310696600cSBjoern A. Zeeb					return 1
26320696600cSBjoern A. Zeeb				fi
26330696600cSBjoern A. Zeeb			fi
26340696600cSBjoern A. Zeeb		done
26350696600cSBjoern A. Zeeb		;;
26360696600cSBjoern A. Zeeb	esac
26370696600cSBjoern A. Zeeb
26380696600cSBjoern A. Zeeb	return 0
26390696600cSBjoern A. Zeeb}
26400696600cSBjoern A. Zeeb
26410696600cSBjoern A. Zeeb# check_jail mib
26420696600cSBjoern A. Zeeb#	Return true if security.jail.$mib exists and set to 1.
26430696600cSBjoern A. Zeeb
26440696600cSBjoern A. Zeebcheck_jail()
26450696600cSBjoern A. Zeeb{
26460696600cSBjoern A. Zeeb	local _mib _v
26470696600cSBjoern A. Zeeb
26480696600cSBjoern A. Zeeb	_mib=$1
26490696600cSBjoern A. Zeeb	if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then
26500696600cSBjoern A. Zeeb		case $_v in
26510696600cSBjoern A. Zeeb		1)	return 0;;
26520696600cSBjoern A. Zeeb		esac
26530696600cSBjoern A. Zeeb	fi
26540696600cSBjoern A. Zeeb	return 1
26550696600cSBjoern A. Zeeb}
26560696600cSBjoern A. Zeeb
26570696600cSBjoern A. Zeeb# check_kern_features mib
26580696600cSBjoern A. Zeeb#	Return existence of kern.features.* sysctl MIB as true or
26590696600cSBjoern A. Zeeb#	false.  The result will be cached in $_rc_cache_kern_features_
26600696600cSBjoern A. Zeeb#	namespace.  "0" means the kern.features.X exists.
26610696600cSBjoern A. Zeeb
26620696600cSBjoern A. Zeebcheck_kern_features()
26630696600cSBjoern A. Zeeb{
26640696600cSBjoern A. Zeeb	local _v
26650696600cSBjoern A. Zeeb
26660696600cSBjoern A. Zeeb	[ -n "$1" ] || return 1;
26670696600cSBjoern A. Zeeb	eval _v=\$_rc_cache_kern_features_$1
26680696600cSBjoern A. Zeeb	[ -n "$_v" ] && return "$_v";
26690696600cSBjoern A. Zeeb
26700696600cSBjoern A. Zeeb	if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then
26710696600cSBjoern A. Zeeb		eval _rc_cache_kern_features_$1=0
26720696600cSBjoern A. Zeeb		return 0
26730696600cSBjoern A. Zeeb	else
26740696600cSBjoern A. Zeeb		eval _rc_cache_kern_features_$1=1
26750696600cSBjoern A. Zeeb		return 1
26760696600cSBjoern A. Zeeb	fi
26770696600cSBjoern A. Zeeb}
26780696600cSBjoern A. Zeeb
26790696600cSBjoern A. Zeeb# check_namevarlist var
26800696600cSBjoern A. Zeeb#	Return "0" if ${name}_var is reserved in rc.subr.
26810696600cSBjoern A. Zeeb
2682c9be47b3SFranco Fichtner_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend setup"
26830696600cSBjoern A. Zeebcheck_namevarlist()
26840696600cSBjoern A. Zeeb{
26850696600cSBjoern A. Zeeb	local _v
26860696600cSBjoern A. Zeeb
26870696600cSBjoern A. Zeeb	for _v in $_rc_namevarlist; do
26880696600cSBjoern A. Zeeb	case $1 in
26890696600cSBjoern A. Zeeb	$_v)	return 0 ;;
26900696600cSBjoern A. Zeeb	esac
26910696600cSBjoern A. Zeeb	done
26920696600cSBjoern A. Zeeb
26930696600cSBjoern A. Zeeb	return 1
26940696600cSBjoern A. Zeeb}
26950696600cSBjoern A. Zeeb
26960696600cSBjoern A. Zeeb# _echoonce var msg mode
26970696600cSBjoern A. Zeeb#	mode=0: Echo $msg if ${$var} is empty.
26980696600cSBjoern A. Zeeb#	        After doing echo, a string is set to ${$var}.
26990696600cSBjoern A. Zeeb#
27000696600cSBjoern A. Zeeb#	mode=1: Echo $msg if ${$var} is a string with non-zero length.
27010696600cSBjoern A. Zeeb#
27020696600cSBjoern A. Zeeb_echoonce()
27030696600cSBjoern A. Zeeb{
27040696600cSBjoern A. Zeeb	local _var _msg _mode
27050696600cSBjoern A. Zeeb	eval _var=\$$1
27060696600cSBjoern A. Zeeb	_msg=$2
27070696600cSBjoern A. Zeeb	_mode=$3
27080696600cSBjoern A. Zeeb
27090696600cSBjoern A. Zeeb	case $_mode in
27100696600cSBjoern A. Zeeb	1)	[ -n "$_var" ] && echo "$_msg" ;;
27110696600cSBjoern A. Zeeb	*)	[ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;;
27120696600cSBjoern A. Zeeb	esac
27130696600cSBjoern A. Zeeb}
27140696600cSBjoern A. Zeeb
27150696600cSBjoern A. Zeeb# If the loader env variable rc.debug is set, turn on debugging. rc.conf will
27160696600cSBjoern A. Zeeb# still override this, but /etc/defaults/rc.conf can't unconditionally set this
27170696600cSBjoern A. Zeeb# since it would undo what we've done here.
27180696600cSBjoern A. Zeebif kenv -q rc.debug > /dev/null ; then
27190696600cSBjoern A. Zeeb	rc_debug=YES
27200696600cSBjoern A. Zeebfi
2721318d0db5SMitchell Horne
2722318d0db5SMitchell Horneboottrace_cmd=`command -v boottrace`
2723318d0db5SMitchell Horneif [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then
2724318d0db5SMitchell Horne	rc_boottrace=YES
2725318d0db5SMitchell Hornefi
2726aa3b7a2fSSimon J. Gerraty
2727b75bb996SSimon J. GerratySED=${SED:-$(Exists -x /usr/bin/sed /rescue/sed)}
2728b75bb996SSimon J. Gerraty
2729aa3b7a2fSSimon J. Gerraty# Allow for local additions and overrides.
2730aa3b7a2fSSimon J. Gerraty# Use vdot to ensure the file has not been tampered with.
2731aa3b7a2fSSimon J. Gerratyvdot /etc/local.rc.subr
2732aa3b7a2fSSimon J. Gerraty
273315483f96SSimon J. Gerraty# Avoid noise - when we do not have /usr mounted,
273415483f96SSimon J. Gerraty# and we cannot use safe_dot without sed.
273515483f96SSimon J. Gerratyif ! have basename; then
273615483f96SSimon J. Gerraty	basename()
273715483f96SSimon J. Gerraty	{
273815483f96SSimon J. Gerraty		local b=${1%$2}
273915483f96SSimon J. Gerraty		echo ${b##*/}
274015483f96SSimon J. Gerraty	}
274115483f96SSimon J. Gerraty	tty()
274215483f96SSimon J. Gerraty	{
274315483f96SSimon J. Gerraty		return 0
274415483f96SSimon J. Gerraty	}
2745b75bb996SSimon J. Gerraty	# we cannot use safe_dot without sed
2746b75bb996SSimon J. Gerraty	[ -z "$SED" ] && _SAFE_EVAL_SH=:
2747b75bb996SSimon J. Gerratyfi
2748aa3b7a2fSSimon J. Gerraty# safe_eval.sh provides safe_dot - for untrusted files
2749aa3b7a2fSSimon J. Gerraty$_SAFE_EVAL_SH vdot /libexec/safe_eval.sh
2750aa3b7a2fSSimon J. Gerraty$_DEBUG_SH vdot /libexec/debug.sh
2751aa3b7a2fSSimon J. Gerraty
2752aa3b7a2fSSimon J. Gerraty# Ensure we can still operate if debug.sh and
2753aa3b7a2fSSimon J. Gerraty# safe_eval.sh are not found.
27546502c60cSSimon J. Gerratyif ! have DebugOn; then
2755aa3b7a2fSSimon J. Gerraty	DebugOn() { return 0; }
2756b5f6beefSR. Christian McDonald	DebugOff() {
2757b5f6beefSR. Christian McDonald		local _rc=0
2758b5f6beefSR. Christian McDonald		while :
2759b5f6beefSR. Christian McDonald		do
2760b5f6beefSR. Christian McDonald			case "$1" in
2761b5f6beefSR. Christian McDonald			-[eo]) shift;; # ignore it
2762b5f6beefSR. Christian McDonald			rc=*) eval "_$1"; shift;;
2763b5f6beefSR. Christian McDonald			*) break;;
2764b5f6beefSR. Christian McDonald			esac
2765b5f6beefSR. Christian McDonald		done
2766b5f6beefSR. Christian McDonald		return $_rc
2767b5f6beefSR. Christian McDonald	}
2768aa3b7a2fSSimon J. Gerratyfi
27694269d1a2SR. Christian McDonaldif ! have safe_dot; then
2770aa3b7a2fSSimon J. Gerraty	safe_dot() { dot "$@"; }
2771aa3b7a2fSSimon J. Gerratyfi
2772