rcvar.subr revision 244675
1if [ ! "$_STARTUP_RCVAR_SUBR" ]; then _STARTUP_RCVAR_SUBR=1
2#
3# Copyright (c) 2006-2012 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 (INLUDING, 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# $FreeBSD: head/usr.sbin/bsdconfig/startup/share/rcvar.subr 244675 2012-12-25 10:47:45Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." startup/rcvar.subr
34f_include $BSDCFG_SHARE/sysrc.subr
35
36############################################################ CONFIGURATION
37
38#
39# Default path to the `/etc/rc.d' directory where service(8) scripts are stored
40#
41: ${ETC_RC_D:=/etc/rc.d}
42
43#
44# Default path to `/etc/rc.subr' (for find_local_scripts_new())
45#
46: ${ETC_RC_SUBR:=/etc/rc.subr}
47
48############################################################ GLOBALS
49
50#
51# Initialize in-memory cache variables
52#
53STARTUP_RCVAR_MAP=
54_STARTUP_RCVAR_MAP=
55
56#
57# Define what an rcvar looks like
58#
59STARTUP_RCVAR_REGEX='[[:alpha:]_][[:alnum:]_]*="([Yy][Ee][Ss]|[Nn][Oo])"'
60
61#
62# Default path to on-disk cache file(s)
63#
64STARTUP_RCVAR_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcvar_map.cache"
65
66############################################################ FUNCTIONS
67
68# f_startup_rcvar_map
69#
70# Produce a map (beit from in-memory cache or on-disk cache) of rc.d scripts
71# and their associated rcvar's. The map returned has the following format:
72#
73# 	rcvar default script description
74#
75# With each as follows:
76#
77# 	rcvar         the variable used to enable this rc.d script
78# 	default       default value for this variable
79# 	script        the rc.d script in-question
80# 	description   description of the variable from rc.conf(5) defaults
81#
82f_startup_rcvar_map()
83{
84	# If the in-memory cached value is available, return it immediately
85	if [ "$_STARTUP_RCVAR_MAP" ]; then
86		echo "$STARTUP_RCVAR_MAP"
87		return $SUCCESS
88	fi
89
90	#
91	# create the in-memory cache (potentially from validated on-disk cache)
92	#
93
94	# Get a list of /etc/rc.d scripts ...
95	local file rc_script_list=
96	for file in "$ETC_RC_D"/*; do
97		[ -f "$file" ] || continue
98		[ -x "$file" ] || continue
99		rc_script_list="$rc_script_list${rc_script_list:+ }$file"
100	done
101	# ... and /usr/local/etc/rc.d scripts
102	rc_script_list="$rc_script_list${rc_script_list:+ }$(
103		local_startup=$( f_sysrc_get local_startup )
104		f_include "$ETC_RC_SUBR"
105		find_local_scripts_new
106		echo $local_rc
107	)"
108
109	#
110	# Calculate a digest given the checksums of all dependencies (scripts and
111	# the defaults file). This digest will be used to determine if an on-disk
112	# global persistant cache file (containg this digest on the first line)
113	# is valid and can be used to quickly populate the cache value for
114	# immediate return.
115	#
116	local rc_script_list_digest
117	rc_script_list_digest=$( cd "$ETC_RC_D" &&
118		cksum "$RC_DEFAULTS" $rc_script_list | md5 )
119
120	#
121	# Check to see if the global persistant cache file exists
122	#
123	if [ -f "$STARTUP_RCVAR_MAP_CACHEFILE" ]; then
124		#
125		# Attempt to populate the in-memory cache with the (soon to be)
126		# be validated on-disk cache. If validation fails, fall-back to
127		# the current value and provide error exit status.
128		#
129		STARTUP_RCVAR_MAP=$(
130			(	# Get digest as the first word on the first line
131				read digest rest_ignored
132
133				#
134				# If the stored digest matches the calculated-one
135				# populate the in-memory cache from the on-disk
136				# cache and provide success exit status.
137				#
138				if [ "$digest" = "$rc_script_list_digest" ]; then
139					cat
140					exit $SUCCESS
141				else
142					# Otherwise, return the current value
143					echo "$STARTUP_RCVAR_MAP"
144					exit $FAILURE
145				fi
146			) < "$STARTUP_RCVAR_MAP_CACHEFILE"
147		)
148		export STARTUP_RCVAR_MAP
149		if [ $? -eq $SUCCESS ]; then
150			export _STARTUP_RCVAR_MAP=1
151			echo "$STARTUP_RCVAR_MAP"
152			return $SUCCESS
153		fi
154		# Otherwise, fall-through to create in-memory cache from scratch
155	fi
156
157	#
158	# If we reach this point, we need to generate the data from scratch
159	# (and after we do, we'll attempt to create the global persistant
160	# cache file to speed up future executions).
161	#
162
163	STARTUP_RCVAR_MAP=$(
164		for script in $rc_script_list; do
165			rcvar_list=$( $script rcvar | awk -F= \
166				-v script="$script" '
167		              	/^'"$STARTUP_RCVAR_REGEX"'/ {
168		              		if ( $2 ~ /^"[Yy][Ee][Ss]"$/ )
169		              			print $1 ",YES"
170		              		else
171		              			print $1 ",NO"
172		              	}' )
173			for entry in $rcvar_list; do
174				rcvar="${entry%%,*}"
175				rcvar_default=$( f_sysrc_get_default "$rcvar" )
176				[ "$rcvar_default" ] ||
177					rcvar_default="${entry#*,}"
178				rcvar_desc=$( f_sysrc_desc "$rcvar" )
179				echo $rcvar ${rcvar_default:-NO} \
180				     $script "$rcvar_desc"
181			done
182		done | sort -u
183	)
184	export STARTUP_RCVAR_MAP
185	export _STARTUP_RCVAR_MAP=1
186	echo "$STARTUP_RCVAR_MAP"
187
188	#
189	# Attempt to create the persistant global cache
190	#
191
192	# Create a new temporary file to write to
193	local tmpfile="$( mktemp -t "$pgm" )"
194	[ "$tmpfile" ] || return $FAILURE
195
196	# Write the temporary file contents
197	echo "$rc_script_list_digest" > "$tmpfile"
198	echo "$STARTUP_RCVAR_MAP" >> "$tmpfile"
199
200	# Finally, move the temporary file into place
201	case "$STARTUP_RCVAR_MAP_CACHEFILE" in
202	*/*) f_quietly mkdir -p "${STARTUP_RCVAR_MAP_CACHEFILE%/*}"
203	esac
204	mv "$tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE"
205}
206
207############################################################ MAIN
208
209f_dprintf "%s: Successfully loaded." startup/rcvar.subr
210
211fi # ! $_STARTUP_RCVAR_SUBR
212