xref: /freebsd/usr.sbin/bsdconfig/startup/share/rcvar.subr (revision 2008043f386721d58158e37e0d7e50df8095942d)
1if [ ! "$_STARTUP_RCVAR_SUBR" ]; then _STARTUP_RCVAR_SUBR=1
2#
3# Copyright (c) 2006-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." startup/rcvar.subr
33f_include $BSDCFG_SHARE/sysrc.subr
34
35############################################################ CONFIGURATION
36
37#
38# Default path to the `/etc/rc.d' directory where service(8) scripts are stored
39#
40: ${ETC_RC_D:=/etc/rc.d}
41
42#
43# Default path to `/etc/rc.subr' (for find_local_scripts_new())
44#
45: ${ETC_RC_SUBR:=/etc/rc.subr}
46
47############################################################ GLOBALS
48
49#
50# Initialize in-memory cache variables
51#
52STARTUP_RCVAR_MAP=
53_STARTUP_RCVAR_MAP=
54
55#
56# Define what an rcvar looks like
57#
58STARTUP_RCVAR_REGEX='[[:alpha:]_][[:alnum:]_]*="([Yy][Ee][Ss]|[Nn][Oo])"'
59
60#
61# Default path to on-disk cache file(s)
62#
63STARTUP_RCVAR_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcvar_map.cache"
64
65############################################################ FUNCTIONS
66
67# f_startup_rcvar_map [$var_to_set]
68#
69# Produce a map (beit from in-memory cache or on-disk cache) of rc.d scripts
70# and their associated rcvar's. The map returned has the following format:
71#
72# 	rcvar default script description
73#
74# With each as follows:
75#
76# 	rcvar         the variable used to enable this rc.d script
77# 	default       default value for this variable
78# 	script        the rc.d script in-question
79# 	description   description of the variable from rc.conf(5) defaults
80#
81# If $var_to_set is missing or NULL, the map is printed to standard output for
82# capturing in a sub-shell (which is less-recommended because of performance
83# degredation; for example, when called in a loop).
84#
85f_startup_rcvar_map()
86{
87	local __funcname=f_startup_rcvar_map
88	local __var_to_set="$1"
89
90	# If the in-memory cached value is available, return it immediately
91	if [ "$_STARTUP_RCVAR_MAP" ]; then
92		if [ "$__var_to_set" ]; then
93			setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
94		else
95			echo "$STARTUP_RCVAR_MAP"
96		fi
97		return $SUCCESS
98	fi
99
100	#
101	# create the in-memory cache (potentially from validated on-disk cache)
102	#
103
104	# Get a list of /etc/rc.d scripts ...
105	local __file __rc_script_list=
106	for __file in "$ETC_RC_D"/*; do
107		[ -f "$__file" ] || continue
108		[ -x "$__file" ] || continue
109		__rc_script_list="$__rc_script_list $__file"
110	done
111	# ... and /usr/local/etc/rc.d scripts
112	__rc_script_list="$__rc_script_list $(
113		local_startup=$( f_sysrc_get local_startup )
114		f_include "$ETC_RC_SUBR"
115		find_local_scripts_new
116		echo $local_rc
117	)"
118	__rc_script_list="${__rc_script_list# }" # Trim leading space
119
120	#
121	# Calculate a digest given the checksums of all dependencies (scripts
122	# and the defaults file). This digest will be used to determine if an
123	# on-disk global persistent cache file (containg this digest on the
124	# first line) is valid and can be used to quickly populate the cache
125	# value for immediate return.
126	#
127	local __rc_script_list_digest
128	__rc_script_list_digest=$( cd "$ETC_RC_D" 2> /dev/null &&
129		cksum "$RC_DEFAULTS" $__rc_script_list 2> /dev/null | md5 )
130
131	#
132	# Check to see if the global persistent cache file exists
133	#
134	if [ -f "$STARTUP_RCVAR_MAP_CACHEFILE" ]; then
135		#
136		# Attempt to populate the in-memory cache with the (soon to be)
137		# validated on-disk cache. If validation fails, fall-back to
138		# the current value and return error.
139		#
140		STARTUP_RCVAR_MAP=$(
141			(	# Get digest as first word on first line
142				read digest rest_ignored
143
144				#
145				# If the stored digest matches the calculated-
146				# one populate the in-memory cache from the on-
147				# disk cache and return success.
148				#
149				if [ "$digest" = "$__rc_script_list_digest" ]
150				then
151					cat
152					exit $SUCCESS
153				else
154					# Otherwise, return the current value
155					echo "$STARTUP_RCVAR_MAP"
156					exit $FAILURE
157				fi
158			) < "$STARTUP_RCVAR_MAP_CACHEFILE"
159		)
160		local __retval=$?
161		export STARTUP_RCVAR_MAP # Make children faster (export cache)
162		if [ $__retval -eq $SUCCESS ]; then
163			export _STARTUP_RCVAR_MAP=1
164			if [ "$__var_to_set" ]; then
165				setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
166			else
167				echo "$STARTUP_RCVAR_MAP"
168			fi
169			return $SUCCESS
170		fi
171		# Otherwise, fall-thru to create in-memory cache from scratch
172	fi
173
174	#
175	# If we reach this point, we need to generate the data from scratch
176	# (and after we do, we'll attempt to create the global persistent
177	# cache file to speed up future executions).
178	#
179
180	STARTUP_RCVAR_MAP=$(
181		for script in $__rc_script_list; do
182			rcvar_list=$( $script rcvar 2> /dev/null | awk -F= \
183				-v script="$script" '
184		              	/^'"$STARTUP_RCVAR_REGEX"'/ {
185		              		if ( $2 ~ /^"[Yy][Ee][Ss]"$/ )
186		              			print $1 ",YES"
187		              		else
188		              			print $1 ",NO"
189		              	}' )
190			for entry in $rcvar_list; do
191				rcvar="${entry%%,*}"
192				rcvar_default=$( f_sysrc_get_default "$rcvar" )
193				[ "$rcvar_default" ] ||
194					rcvar_default="${entry#*,}"
195				rcvar_desc=$( f_sysrc_desc "$rcvar" )
196				echo $rcvar ${rcvar_default:-NO} \
197				     $script "$rcvar_desc"
198			done
199		done | sort -u
200	)
201	export STARTUP_RCVAR_MAP
202	export _STARTUP_RCVAR_MAP=1
203	if [ "$__var_to_set" ]; then
204		setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
205	else
206		echo "$STARTUP_RCVAR_MAP"
207	fi
208
209	#
210	# Attempt to create/update the persistent global cache
211	#
212
213	# Create a new temporary file to write to
214	local __tmpfile
215	f_eval_catch -dk __tmpfile $__funcname mktemp \
216		'mktemp -t "%s"' "$__tmpfile" || return $FAILURE
217
218	# Write the temporary file contents
219	echo "$__rc_script_list_digest" > "$__tmpfile"
220	echo "$STARTUP_RCVAR_MAP" >> "$__tmpfile"
221
222	# Finally, move the temporary file into place
223	case "$STARTUP_RCVAR_MAP_CACHEFILE" in
224	*/*) f_eval_catch -d $__funcname mkdir \
225		'mkdir -p "%s"' "${STARTUP_RCVAR_MAP_CACHEFILE%/*}"
226	esac
227	f_eval_catch -d $__funcname mv \
228		'mv "%s" "%s"' "$__tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE"
229}
230
231############################################################ MAIN
232
233f_dprintf "%s: Successfully loaded." startup/rcvar.subr
234
235fi # ! $_STARTUP_RCVAR_SUBR
236