rcvar.subr revision 244727
1238438Sdteskeif [ ! "$_STARTUP_RCVAR_SUBR" ]; then _STARTUP_RCVAR_SUBR=1 2238438Sdteske# 3238438Sdteske# Copyright (c) 2006-2012 Devin Teske 4238438Sdteske# All Rights Reserved. 5238438Sdteske# 6238438Sdteske# Redistribution and use in source and binary forms, with or without 7238438Sdteske# modification, are permitted provided that the following conditions 8238438Sdteske# are met: 9238438Sdteske# 1. Redistributions of source code must retain the above copyright 10238438Sdteske# notice, this list of conditions and the following disclaimer. 11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12238438Sdteske# notice, this list of conditions and the following disclaimer in the 13238438Sdteske# documentation and/or other materials provided with the distribution. 14238438Sdteske# 15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16238438Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18238438Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20238438Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25238438Sdteske# SUCH DAMAGE. 26238438Sdteske# 27238438Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/startup/share/rcvar.subr 244727 2012-12-27 07:52:50Z dteske $ 28238438Sdteske# 29238438Sdteske############################################################ INCLUDES 30238438Sdteske 31240684SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32240684Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33244675Sdteskef_dprintf "%s: loading includes..." startup/rcvar.subr 34240684Sdteskef_include $BSDCFG_SHARE/sysrc.subr 35238438Sdteske 36238438Sdteske############################################################ CONFIGURATION 37238438Sdteske 38238438Sdteske# 39238438Sdteske# Default path to the `/etc/rc.d' directory where service(8) scripts are stored 40238438Sdteske# 41238438Sdteske: ${ETC_RC_D:=/etc/rc.d} 42238438Sdteske 43238438Sdteske# 44238438Sdteske# Default path to `/etc/rc.subr' (for find_local_scripts_new()) 45238438Sdteske# 46238438Sdteske: ${ETC_RC_SUBR:=/etc/rc.subr} 47238438Sdteske 48238438Sdteske############################################################ GLOBALS 49238438Sdteske 50238438Sdteske# 51238438Sdteske# Initialize in-memory cache variables 52238438Sdteske# 53238438SdteskeSTARTUP_RCVAR_MAP= 54238438Sdteske_STARTUP_RCVAR_MAP= 55238438Sdteske 56238438Sdteske# 57238438Sdteske# Define what an rcvar looks like 58238438Sdteske# 59238438SdteskeSTARTUP_RCVAR_REGEX='[[:alpha:]_][[:alnum:]_]*="([Yy][Ee][Ss]|[Nn][Oo])"' 60238438Sdteske 61238438Sdteske# 62238438Sdteske# Default path to on-disk cache file(s) 63238438Sdteske# 64238438SdteskeSTARTUP_RCVAR_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcvar_map.cache" 65238438Sdteske 66238438Sdteske############################################################ FUNCTIONS 67238438Sdteske 68238438Sdteske# f_startup_rcvar_map 69238438Sdteske# 70238438Sdteske# Produce a map (beit from in-memory cache or on-disk cache) of rc.d scripts 71238438Sdteske# and their associated rcvar's. The map returned has the following format: 72238438Sdteske# 73238438Sdteske# rcvar default script description 74238438Sdteske# 75238438Sdteske# With each as follows: 76238438Sdteske# 77238438Sdteske# rcvar the variable used to enable this rc.d script 78238438Sdteske# default default value for this variable 79238438Sdteske# script the rc.d script in-question 80238438Sdteske# description description of the variable from rc.conf(5) defaults 81238438Sdteske# 82238438Sdteskef_startup_rcvar_map() 83238438Sdteske{ 84238438Sdteske # If the in-memory cached value is available, return it immediately 85238438Sdteske if [ "$_STARTUP_RCVAR_MAP" ]; then 86238438Sdteske echo "$STARTUP_RCVAR_MAP" 87238438Sdteske return $SUCCESS 88238438Sdteske fi 89238438Sdteske 90238438Sdteske # 91238438Sdteske # create the in-memory cache (potentially from validated on-disk cache) 92238438Sdteske # 93238438Sdteske 94238438Sdteske # Get a list of /etc/rc.d scripts ... 95238438Sdteske local file rc_script_list= 96238438Sdteske for file in "$ETC_RC_D"/*; do 97238438Sdteske [ -f "$file" ] || continue 98238438Sdteske [ -x "$file" ] || continue 99238438Sdteske rc_script_list="$rc_script_list${rc_script_list:+ }$file" 100238438Sdteske done 101238438Sdteske # ... and /usr/local/etc/rc.d scripts 102238438Sdteske rc_script_list="$rc_script_list${rc_script_list:+ }$( 103238438Sdteske local_startup=$( f_sysrc_get local_startup ) 104238438Sdteske f_include "$ETC_RC_SUBR" 105238438Sdteske find_local_scripts_new 106238438Sdteske echo $local_rc 107238438Sdteske )" 108238438Sdteske 109238438Sdteske # 110244727Sdteske # Calculate a digest given the checksums of all dependencies (scripts 111244727Sdteske # and the defaults file). This digest will be used to determine if an 112244727Sdteske # on-disk global persistant cache file (containg this digest on the 113244727Sdteske # first line) is valid and can be used to quickly populate the cache 114244727Sdteske # value for immediate return. 115238438Sdteske # 116238438Sdteske local rc_script_list_digest 117238438Sdteske rc_script_list_digest=$( cd "$ETC_RC_D" && 118238438Sdteske cksum "$RC_DEFAULTS" $rc_script_list | md5 ) 119238438Sdteske 120238438Sdteske # 121238438Sdteske # Check to see if the global persistant cache file exists 122238438Sdteske # 123238438Sdteske if [ -f "$STARTUP_RCVAR_MAP_CACHEFILE" ]; then 124238438Sdteske # 125238438Sdteske # Attempt to populate the in-memory cache with the (soon to be) 126238438Sdteske # be validated on-disk cache. If validation fails, fall-back to 127238438Sdteske # the current value and provide error exit status. 128238438Sdteske # 129238438Sdteske STARTUP_RCVAR_MAP=$( 130238438Sdteske ( # Get digest as the first word on the first line 131238438Sdteske read digest rest_ignored 132238438Sdteske 133238438Sdteske # 134244727Sdteske # If the stored digest matches the calculated- 135244727Sdteske # one populate the in-memory cache from the on- 136244727Sdteske # disk cache and provide success exit status. 137238438Sdteske # 138244727Sdteske if [ "$digest" = "$rc_script_list_digest" ] 139244727Sdteske then 140238438Sdteske cat 141238438Sdteske exit $SUCCESS 142238438Sdteske else 143238438Sdteske # Otherwise, return the current value 144238438Sdteske echo "$STARTUP_RCVAR_MAP" 145238438Sdteske exit $FAILURE 146238438Sdteske fi 147238438Sdteske ) < "$STARTUP_RCVAR_MAP_CACHEFILE" 148238438Sdteske ) 149238438Sdteske export STARTUP_RCVAR_MAP 150238438Sdteske if [ $? -eq $SUCCESS ]; then 151238438Sdteske export _STARTUP_RCVAR_MAP=1 152238438Sdteske echo "$STARTUP_RCVAR_MAP" 153238438Sdteske return $SUCCESS 154238438Sdteske fi 155244727Sdteske # Otherwise, fall-thru to create in-memory cache from scratch 156238438Sdteske fi 157238438Sdteske 158238438Sdteske # 159238438Sdteske # If we reach this point, we need to generate the data from scratch 160238438Sdteske # (and after we do, we'll attempt to create the global persistant 161238438Sdteske # cache file to speed up future executions). 162238438Sdteske # 163238438Sdteske 164238438Sdteske STARTUP_RCVAR_MAP=$( 165238438Sdteske for script in $rc_script_list; do 166238438Sdteske rcvar_list=$( $script rcvar | awk -F= \ 167238438Sdteske -v script="$script" ' 168238438Sdteske /^'"$STARTUP_RCVAR_REGEX"'/ { 169238438Sdteske if ( $2 ~ /^"[Yy][Ee][Ss]"$/ ) 170238438Sdteske print $1 ",YES" 171238438Sdteske else 172238438Sdteske print $1 ",NO" 173238438Sdteske }' ) 174238438Sdteske for entry in $rcvar_list; do 175238438Sdteske rcvar="${entry%%,*}" 176238438Sdteske rcvar_default=$( f_sysrc_get_default "$rcvar" ) 177238438Sdteske [ "$rcvar_default" ] || 178238438Sdteske rcvar_default="${entry#*,}" 179238438Sdteske rcvar_desc=$( f_sysrc_desc "$rcvar" ) 180238438Sdteske echo $rcvar ${rcvar_default:-NO} \ 181238438Sdteske $script "$rcvar_desc" 182238438Sdteske done 183238438Sdteske done | sort -u 184238438Sdteske ) 185238438Sdteske export STARTUP_RCVAR_MAP 186238438Sdteske export _STARTUP_RCVAR_MAP=1 187238438Sdteske echo "$STARTUP_RCVAR_MAP" 188238438Sdteske 189238438Sdteske # 190238438Sdteske # Attempt to create the persistant global cache 191238438Sdteske # 192238438Sdteske 193238438Sdteske # Create a new temporary file to write to 194238438Sdteske local tmpfile="$( mktemp -t "$pgm" )" 195238438Sdteske [ "$tmpfile" ] || return $FAILURE 196238438Sdteske 197238438Sdteske # Write the temporary file contents 198238438Sdteske echo "$rc_script_list_digest" > "$tmpfile" 199238438Sdteske echo "$STARTUP_RCVAR_MAP" >> "$tmpfile" 200238438Sdteske 201238438Sdteske # Finally, move the temporary file into place 202238438Sdteske case "$STARTUP_RCVAR_MAP_CACHEFILE" in 203238438Sdteske */*) f_quietly mkdir -p "${STARTUP_RCVAR_MAP_CACHEFILE%/*}" 204238438Sdteske esac 205238438Sdteske mv "$tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE" 206238438Sdteske} 207238438Sdteske 208244675Sdteske############################################################ MAIN 209244675Sdteske 210244675Sdteskef_dprintf "%s: Successfully loaded." startup/rcvar.subr 211244675Sdteske 212238438Sdteskefi # ! $_STARTUP_RCVAR_SUBR 213