summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdconfig/startup/share/rcvar.subr
diff options
context:
space:
mode:
authordteske <dteske@FreeBSD.org>2012-09-18 22:28:42 +0000
committerdteske <dteske@FreeBSD.org>2012-09-18 22:28:42 +0000
commit282d6b7f2c0f1fb51d911f75ef9989f62e389985 (patch)
tree3ef909c692976c6a6b0854f8d722182e78ff8915 /usr.sbin/bsdconfig/startup/share/rcvar.subr
parent969b25f00f504248a5c234274661472d753475ad (diff)
downloadFreeBSD-src-282d6b7f2c0f1fb51d911f75ef9989f62e389985.zip
FreeBSD-src-282d6b7f2c0f1fb51d911f75ef9989f62e389985.tar.gz
Move major includes into /usr/share/bsdconfig for easy external access.
Reviewed by: adrian (co-mentor) Approved by: adrian (co-mentor)
Diffstat (limited to 'usr.sbin/bsdconfig/startup/share/rcvar.subr')
-rw-r--r--usr.sbin/bsdconfig/startup/share/rcvar.subr206
1 files changed, 206 insertions, 0 deletions
diff --git a/usr.sbin/bsdconfig/startup/share/rcvar.subr b/usr.sbin/bsdconfig/startup/share/rcvar.subr
new file mode 100644
index 0000000..3cd60e8
--- /dev/null
+++ b/usr.sbin/bsdconfig/startup/share/rcvar.subr
@@ -0,0 +1,206 @@
+if [ ! "$_STARTUP_RCVAR_SUBR" ]; then _STARTUP_RCVAR_SUBR=1
+#
+# Copyright (c) 2006-2012 Devin Teske
+# All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+############################################################ INCLUDES
+
+BSDCFG_SHARE="/usr/share/bsdconfig"
+. $BSDCFG_SHARE/common.subr || exit 1
+f_include $BSDCFG_SHARE/sysrc.subr
+
+############################################################ CONFIGURATION
+
+#
+# Default path to the `/etc/rc.d' directory where service(8) scripts are stored
+#
+: ${ETC_RC_D:=/etc/rc.d}
+
+#
+# Default path to `/etc/rc.subr' (for find_local_scripts_new())
+#
+: ${ETC_RC_SUBR:=/etc/rc.subr}
+
+############################################################ GLOBALS
+
+#
+# Initialize in-memory cache variables
+#
+STARTUP_RCVAR_MAP=
+_STARTUP_RCVAR_MAP=
+
+#
+# Define what an rcvar looks like
+#
+STARTUP_RCVAR_REGEX='[[:alpha:]_][[:alnum:]_]*="([Yy][Ee][Ss]|[Nn][Oo])"'
+
+#
+# Default path to on-disk cache file(s)
+#
+STARTUP_RCVAR_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcvar_map.cache"
+
+############################################################ FUNCTIONS
+
+# f_startup_rcvar_map
+#
+# Produce a map (beit from in-memory cache or on-disk cache) of rc.d scripts
+# and their associated rcvar's. The map returned has the following format:
+#
+# rcvar default script description
+#
+# With each as follows:
+#
+# rcvar the variable used to enable this rc.d script
+# default default value for this variable
+# script the rc.d script in-question
+# description description of the variable from rc.conf(5) defaults
+#
+f_startup_rcvar_map()
+{
+ # If the in-memory cached value is available, return it immediately
+ if [ "$_STARTUP_RCVAR_MAP" ]; then
+ echo "$STARTUP_RCVAR_MAP"
+ return $SUCCESS
+ fi
+
+ #
+ # create the in-memory cache (potentially from validated on-disk cache)
+ #
+
+ # Get a list of /etc/rc.d scripts ...
+ local file rc_script_list=
+ for file in "$ETC_RC_D"/*; do
+ [ -f "$file" ] || continue
+ [ -x "$file" ] || continue
+ rc_script_list="$rc_script_list${rc_script_list:+ }$file"
+ done
+ # ... and /usr/local/etc/rc.d scripts
+ rc_script_list="$rc_script_list${rc_script_list:+ }$(
+ local_startup=$( f_sysrc_get local_startup )
+ f_include "$ETC_RC_SUBR"
+ find_local_scripts_new
+ echo $local_rc
+ )"
+
+ #
+ # Calculate a digest given the checksums of all dependencies (scripts and
+ # the defaults file). This digest will be used to determine if an on-disk
+ # global persistant cache file (containg this digest on the first line)
+ # is valid and can be used to quickly populate the cache value for
+ # immediate return.
+ #
+ local rc_script_list_digest
+ rc_script_list_digest=$( cd "$ETC_RC_D" &&
+ cksum "$RC_DEFAULTS" $rc_script_list | md5 )
+
+ #
+ # Check to see if the global persistant cache file exists
+ #
+ if [ -f "$STARTUP_RCVAR_MAP_CACHEFILE" ]; then
+ #
+ # Attempt to populate the in-memory cache with the (soon to be)
+ # be validated on-disk cache. If validation fails, fall-back to
+ # the current value and provide error exit status.
+ #
+ STARTUP_RCVAR_MAP=$(
+ ( # Get digest as the first word on the first line
+ read digest rest_ignored
+
+ #
+ # If the stored digest matches the calculated-one
+ # populate the in-memory cache from the on-disk
+ # cache and provide success exit status.
+ #
+ if [ "$digest" = "$rc_script_list_digest" ]; then
+ cat
+ exit $SUCCESS
+ else
+ # Otherwise, return the current value
+ echo "$STARTUP_RCVAR_MAP"
+ exit $FAILURE
+ fi
+ ) < "$STARTUP_RCVAR_MAP_CACHEFILE"
+ )
+ export STARTUP_RCVAR_MAP
+ if [ $? -eq $SUCCESS ]; then
+ export _STARTUP_RCVAR_MAP=1
+ echo "$STARTUP_RCVAR_MAP"
+ return $SUCCESS
+ fi
+ # Otherwise, fall-through to create in-memory cache from scratch
+ fi
+
+ #
+ # If we reach this point, we need to generate the data from scratch
+ # (and after we do, we'll attempt to create the global persistant
+ # cache file to speed up future executions).
+ #
+
+ STARTUP_RCVAR_MAP=$(
+ for script in $rc_script_list; do
+ rcvar_list=$( $script rcvar | awk -F= \
+ -v script="$script" '
+ /^'"$STARTUP_RCVAR_REGEX"'/ {
+ if ( $2 ~ /^"[Yy][Ee][Ss]"$/ )
+ print $1 ",YES"
+ else
+ print $1 ",NO"
+ }' )
+ for entry in $rcvar_list; do
+ rcvar="${entry%%,*}"
+ rcvar_default=$( f_sysrc_get_default "$rcvar" )
+ [ "$rcvar_default" ] ||
+ rcvar_default="${entry#*,}"
+ rcvar_desc=$( f_sysrc_desc "$rcvar" )
+ echo $rcvar ${rcvar_default:-NO} \
+ $script "$rcvar_desc"
+ done
+ done | sort -u
+ )
+ export STARTUP_RCVAR_MAP
+ export _STARTUP_RCVAR_MAP=1
+ echo "$STARTUP_RCVAR_MAP"
+
+ #
+ # Attempt to create the persistant global cache
+ #
+
+ # Create a new temporary file to write to
+ local tmpfile="$( mktemp -t "$pgm" )"
+ [ "$tmpfile" ] || return $FAILURE
+
+ # Write the temporary file contents
+ echo "$rc_script_list_digest" > "$tmpfile"
+ echo "$STARTUP_RCVAR_MAP" >> "$tmpfile"
+
+ # Finally, move the temporary file into place
+ case "$STARTUP_RCVAR_MAP_CACHEFILE" in
+ */*) f_quietly mkdir -p "${STARTUP_RCVAR_MAP_CACHEFILE%/*}"
+ esac
+ mv "$tmpfile" "$STARTUP_RCVAR_MAP_CACHEFILE"
+}
+
+fi # ! $_STARTUP_RCVAR_SUBR
OpenPOWER on IntegriCloud