]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/bsdconfig/startup/share/rcvar.subr
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / bsdconfig / startup / share / rcvar.subr
1 if [ ! "$_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 # $FreeBSD$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." startup/rcvar.subr
34 f_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 #
53 STARTUP_RCVAR_MAP=
54 _STARTUP_RCVAR_MAP=
55
56 #
57 # Define what an rcvar looks like
58 #
59 STARTUP_RCVAR_REGEX='[[:alpha:]_][[:alnum:]_]*="([Yy][Ee][Ss]|[Nn][Oo])"'
60
61 #
62 # Default path to on-disk cache file(s)
63 #
64 STARTUP_RCVAR_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcvar_map.cache"
65
66 ############################################################ FUNCTIONS
67
68 # f_startup_rcvar_map [$var_to_set]
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 #
82 # If $var_to_set is missing or NULL, the map is printed to standard output for
83 # capturing in a sub-shell (which is less-recommended because of performance
84 # degredation; for example, when called in a loop).
85 #
86 f_startup_rcvar_map()
87 {
88         local __funcname=f_startup_rcvar_map
89         local __var_to_set="$1"
90
91         # If the in-memory cached value is available, return it immediately
92         if [ "$_STARTUP_RCVAR_MAP" ]; then
93                 if [ "$__var_to_set" ]; then
94                         setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
95                 else
96                         echo "$STARTUP_RCVAR_MAP"
97                 fi
98                 return $SUCCESS
99         fi
100
101         #
102         # create the in-memory cache (potentially from validated on-disk cache)
103         #
104
105         # Get a list of /etc/rc.d scripts ...
106         local __file __rc_script_list=
107         for __file in "$ETC_RC_D"/*; do
108                 [ -f "$__file" ] || continue
109                 [ -x "$__file" ] || continue
110                 __rc_script_list="$__rc_script_list $__file"
111         done
112         # ... and /usr/local/etc/rc.d scripts
113         __rc_script_list="$__rc_script_list $(
114                 local_startup=$( f_sysrc_get local_startup )
115                 f_include "$ETC_RC_SUBR"
116                 find_local_scripts_new
117                 echo $local_rc
118         )"
119         __rc_script_list="${__rc_script_list# }" # Trim leading space
120
121         #
122         # Calculate a digest given the checksums of all dependencies (scripts
123         # and the defaults file). This digest will be used to determine if an
124         # on-disk global persistant cache file (containg this digest on the
125         # first line) is valid and can be used to quickly populate the cache
126         # value for immediate return.
127         #
128         local __rc_script_list_digest
129         __rc_script_list_digest=$( cd "$ETC_RC_D" 2> /dev/null &&
130                 cksum "$RC_DEFAULTS" $__rc_script_list 2> /dev/null | md5 )
131
132         #
133         # Check to see if the global persistant cache file exists
134         #
135         if [ -f "$STARTUP_RCVAR_MAP_CACHEFILE" ]; then
136                 #
137                 # Attempt to populate the in-memory cache with the (soon to be)
138                 # validated on-disk cache. If validation fails, fall-back to
139                 # the current value and return error.
140                 #
141                 STARTUP_RCVAR_MAP=$(
142                         (       # Get digest as first word on first line
143                                 read digest rest_ignored
144
145                                 #
146                                 # If the stored digest matches the calculated-
147                                 # one populate the in-memory cache from the on-
148                                 # disk cache and return success.
149                                 #
150                                 if [ "$digest" = "$__rc_script_list_digest" ]
151                                 then
152                                         cat
153                                         exit $SUCCESS
154                                 else
155                                         # Otherwise, return the current value
156                                         echo "$STARTUP_RCVAR_MAP"
157                                         exit $FAILURE
158                                 fi
159                         ) < "$STARTUP_RCVAR_MAP_CACHEFILE"
160                 )
161                 local __retval=$?
162                 export STARTUP_RCVAR_MAP # Make children faster (export cache)
163                 if [ $__retval -eq $SUCCESS ]; then
164                         export _STARTUP_RCVAR_MAP=1
165                         if [ "$__var_to_set" ]; then
166                                 setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
167                         else
168                                 echo "$STARTUP_RCVAR_MAP"
169                         fi
170                         return $SUCCESS
171                 fi
172                 # Otherwise, fall-thru to create in-memory cache from scratch
173         fi
174
175         #
176         # If we reach this point, we need to generate the data from scratch
177         # (and after we do, we'll attempt to create the global persistant
178         # cache file to speed up future executions).
179         #
180
181         STARTUP_RCVAR_MAP=$(
182                 for script in $__rc_script_list; do
183                         rcvar_list=$( $script rcvar 2> /dev/null | awk -F= \
184                                 -v script="$script" '
185                                 /^'"$STARTUP_RCVAR_REGEX"'/ {
186                                         if ( $2 ~ /^"[Yy][Ee][Ss]"$/ )
187                                                 print $1 ",YES"
188                                         else
189                                                 print $1 ",NO"
190                                 }' )
191                         for entry in $rcvar_list; do
192                                 rcvar="${entry%%,*}"
193                                 rcvar_default=$( f_sysrc_get_default "$rcvar" )
194                                 [ "$rcvar_default" ] ||
195                                         rcvar_default="${entry#*,}"
196                                 rcvar_desc=$( f_sysrc_desc "$rcvar" )
197                                 echo $rcvar ${rcvar_default:-NO} \
198                                      $script "$rcvar_desc"
199                         done
200                 done | sort -u
201         )
202         export STARTUP_RCVAR_MAP
203         export _STARTUP_RCVAR_MAP=1
204         if [ "$__var_to_set" ]; then
205                 setvar "$__var_to_set" "$STARTUP_RCVAR_MAP"
206         else
207                 echo "$STARTUP_RCVAR_MAP"
208         fi
209
210         #
211         # Attempt to create/update the persistant global cache
212         #
213
214         # Create a new temporary file to write to
215         local __tmpfile
216         f_eval_catch -dk __tmpfile $__funcname mktemp \
217                 'mktemp -t "%s"' "$__tmpfile" || return $FAILURE
218
219         # Write the temporary file contents
220         echo "$__rc_script_list_digest" > "$__tmpfile"
221         echo "$STARTUP_RCVAR_MAP" >> "$__tmpfile"
222
223         # Finally, move the temporary file into place
224         case "$STARTUP_RCVAR_MAP_CACHEFILE" in
225         */*) f_eval_catch -d $__funcname mkdir \
226                 'mkdir -p "%s"' "${STARTUP_RCVAR_MAP_CACHEFILE%/*}"
227         esac
228         f_eval_catch -d $__funcname mv \
229                 'mv "%s" "%s"' "$__tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE"
230 }
231
232 ############################################################ MAIN
233
234 f_dprintf "%s: Successfully loaded." startup/rcvar.subr
235
236 fi # ! $_STARTUP_RCVAR_SUBR