1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
|