1 | #!/bin/bash
|
---|
2 | # Begin /lib/lsb/manage-functions
|
---|
3 |
|
---|
4 | # /lib/lsb/manage-functions contains the functions used by
|
---|
5 | # /lib/lsb/install_initd and /lib/lsb/remove_initd as well as additional helper
|
---|
6 | # functions for use in programs that would provide functionality similar to
|
---|
7 | # the RedHat chkconfig utility, for instance.
|
---|
8 |
|
---|
9 | # source the confif file
|
---|
10 | . /etc/lsb/lsb-config
|
---|
11 |
|
---|
12 | # Define all arrays at script start to avoid scope issues
|
---|
13 | # scriptlist is a list of valid scripts used as an index
|
---|
14 | declare -a scriptlist
|
---|
15 | # fullheaders is a complete set of valid LSB headers, stored in memory for
|
---|
16 | # each indexed script, to avoid multiple disk reads
|
---|
17 | declare -a fullheaders
|
---|
18 |
|
---|
19 | ###############################################################################
|
---|
20 | # get_headers() - Obtains a valid list of scripts contained in ${rcbase} and #
|
---|
21 | # inserts the name of the script into the scriptlist[] array #
|
---|
22 | # for use by all other functions. Additionally, it inserts #
|
---|
23 | # the entire LSB header information from each script into a #
|
---|
24 | # second array, fullheaders[], so that diskreads need only be #
|
---|
25 | # done once #
|
---|
26 | # Returns no value, but populates the variable ${scriptcount} #
|
---|
27 | # and the arrays ${scriptlist} and ${fullheaders} for use #
|
---|
28 | # with other functions in this script. This function is #
|
---|
29 | # called unconditionally at the end of this scrip and is #
|
---|
30 | # provided as a function only for the case that it needs to #
|
---|
31 | # be called again after other operations. #
|
---|
32 | ###############################################################################
|
---|
33 | get_headers()
|
---|
34 | {
|
---|
35 | echo -n "Retrieving script information from disk..."
|
---|
36 | count=1
|
---|
37 | for file in $(find -P /etc/init.d -xdev -perm -u=x | sed -n 2~1p \
|
---|
38 | | sed "s@/etc/init.d/rc@@")
|
---|
39 | do
|
---|
40 | # determine if script is an LSB compliant script
|
---|
41 | grep "### BEGIN INIT INFO" $file > /dev/null
|
---|
42 | if test $? -gt "0"
|
---|
43 | then
|
---|
44 | # this is not a valid script and is ignored
|
---|
45 | # skip the rest of the loop
|
---|
46 | continue
|
---|
47 | fi
|
---|
48 | # determine basename using only bash (is basename a builtin?)
|
---|
49 | filename=$(echo "${file}" | sed "s@${rcbase}/@@")
|
---|
50 | # assign it to an array possition
|
---|
51 | scriptlist["${count}"]="${filename}"
|
---|
52 | # find the begining of the init info for the script
|
---|
53 | begin=$(grep -n "### BEGIN INIT INFO" "${file}" | cut -d: -f1)
|
---|
54 | # find the end of the init info for the script
|
---|
55 | end=$(grep -n "### END INIT INFO" "${file}" | cut -d: -f1)
|
---|
56 | # we'll use the difference between the values in the tail command
|
---|
57 | diff=$(( ${end} - ${begin} ))
|
---|
58 | # assign the entire LSB header information as a single string to the
|
---|
59 | # fullheaders[] array
|
---|
60 | fullheaders["${count}"]=$(head -n "${end}" "${file}" \
|
---|
61 | | tail -n "${diff}")
|
---|
62 | count=$(( ${count} + 1 ))
|
---|
63 | unset begin
|
---|
64 | unset end
|
---|
65 | unset diff
|
---|
66 | unset filename
|
---|
67 | done
|
---|
68 | # a number or array elements would be a nice regular variable assignment
|
---|
69 | scriptcount="${#scriptlist[@]}"
|
---|
70 | unset count
|
---|
71 | echo -e "Completed!"
|
---|
72 | }
|
---|
73 |
|
---|
74 | ###############################################################################
|
---|
75 | # print_headers() - Presents a formatted list of all LSB compliant script #
|
---|
76 | # headers to stdout preceeded by script name for use in #
|
---|
77 | # other scripts #
|
---|
78 | ###############################################################################
|
---|
79 | print_headers()
|
---|
80 | {
|
---|
81 | get_headers
|
---|
82 | count=1
|
---|
83 | while test "${count}" -lt "${scriptcount}"
|
---|
84 | do
|
---|
85 | echo "${scriptlist[$count]}"
|
---|
86 | echo "============================================================="
|
---|
87 | echo "${fullheaders[$count]}"
|
---|
88 | echo ""
|
---|
89 | echo ""
|
---|
90 | count="$(( ${count} + 1 ))"
|
---|
91 | done
|
---|
92 | }
|
---|
93 |
|
---|
94 | ###############################################################################
|
---|
95 | # get_index() - Determines the array index of the specified script #
|
---|
96 | ###############################################################################
|
---|
97 |
|
---|
98 | get_index()
|
---|
99 | {
|
---|
100 | filename=$(echo "${1}" | sed "s@${rcbase}/@@")
|
---|
101 | count=1
|
---|
102 | while test "${count}" -lt "${scriptcount}"
|
---|
103 | do
|
---|
104 | echo "${scriptlist[${count}]}" | grep "${filename}" > /dev/null
|
---|
105 | if test "${?}" -ne "0"
|
---|
106 | then
|
---|
107 | count=$(( ${count} + 1 ))
|
---|
108 | continue
|
---|
109 | else
|
---|
110 | break
|
---|
111 | fi
|
---|
112 | done
|
---|
113 | if test "${filename}" == "${scriptlist[${count}]}"
|
---|
114 | then
|
---|
115 | echo "${count}"
|
---|
116 | else
|
---|
117 | echo "${1} is not a valid LSB init script."
|
---|
118 | exit 1
|
---|
119 | fi
|
---|
120 | unset filename
|
---|
121 | unset count
|
---|
122 | }
|
---|
123 |
|
---|
124 | ###############################################################################
|
---|
125 | # get_lsb_value() - Obtains the LSB Value of $1 for index of script ($2). #
|
---|
126 | ###############################################################################
|
---|
127 | get_lsb_value()
|
---|
128 | {
|
---|
129 | # Probably need some error checking in here
|
---|
130 | echo "${fullheaders[${2}]}" | \
|
---|
131 | grep "^# ${1}" | \
|
---|
132 | sed -e "s@# ${1}:@@" \
|
---|
133 | -e "s/^[ \t]*//"
|
---|
134 | }
|
---|
135 |
|
---|
136 | ###############################################################################
|
---|
137 | # convert_lsb_required() - Converts LSB defined facilities (facility names #
|
---|
138 | # begining with a '$' character) into script names #
|
---|
139 | # for required start/stop #
|
---|
140 | ###############################################################################
|
---|
141 | convert_lsb_required()
|
---|
142 | {
|
---|
143 | local count=0
|
---|
144 | local provides=""
|
---|
145 | local reqfacility=""
|
---|
146 | local reqprovideslist=""
|
---|
147 |
|
---|
148 | for reqfacility in $@
|
---|
149 | do
|
---|
150 | # find the requires and it's index and then find the script name
|
---|
151 | # from the index. Since this is required, exit if it is not found
|
---|
152 | ## If reqfacility is already in script name format, nothing needs to
|
---|
153 | ## be done, just echo it back out. I can't think of an easy way to
|
---|
154 | ## do this right now, the scriptname will be the same as the provides
|
---|
155 | ## anyway, so just let it fly for now...it'll be correct, it just
|
---|
156 | ## takes an extra couple of commands to get the same result.
|
---|
157 | ## Besides, this will do some extra sanity checking in case somebody
|
---|
158 | ## writes a script that isn't named the same as provides, though this
|
---|
159 | ## isn't LSB compliant. Additionally, these same comments apply to
|
---|
160 | ## the convert_lsb_should() fucntion below.
|
---|
161 | count=0
|
---|
162 | while test ${count} -lt ${scriptcount}
|
---|
163 | do
|
---|
164 | count=$(( $count + 1 ))
|
---|
165 | provides="$( get_lsb_value Provides ${count} )"
|
---|
166 | if test "${provides}" = "${reqfacility}"
|
---|
167 | then
|
---|
168 | reqprovideslist="${reqprovideslist} ${scriptlist[$count]}"
|
---|
169 | break
|
---|
170 | fi
|
---|
171 | if test ${count} -eq ${scriptcount}; then
|
---|
172 | # If we've never broken out of the while loop, then this is an
|
---|
173 | # unrecoverable error since it is a required item. Exit now!
|
---|
174 | echo "Error: unable to locate required facility ${reqfacility}!"
|
---|
175 | exit 5
|
---|
176 | fi
|
---|
177 | done
|
---|
178 | done
|
---|
179 | echo "${reqprovideslist}" | sed -e "s/^[ \t]*//" -e "s/^[ \t]*//"
|
---|
180 | }
|
---|
181 |
|
---|
182 | ###############################################################################
|
---|
183 | # convert_lsb_should() - Converts LSB defined facilities (facility names #
|
---|
184 | # begining with a '$' character) into script names for #
|
---|
185 | # should start/stop #
|
---|
186 | ###############################################################################
|
---|
187 |
|
---|
188 | convert_lsb_should()
|
---|
189 | {
|
---|
190 | local count=0
|
---|
191 | local provides=""
|
---|
192 | local optfacility=""
|
---|
193 | local optprovideslist=""
|
---|
194 |
|
---|
195 | for optfacility in $@
|
---|
196 | do
|
---|
197 | # find the should and it's index and then find the script name
|
---|
198 | # from the index. Since this is not an error, simply warn if it
|
---|
199 | # is not found.
|
---|
200 | count=0
|
---|
201 | while test ${count} -lt ${scriptcount}
|
---|
202 | do
|
---|
203 | count=$(( $count + 1 ))
|
---|
204 | provides="$( get_lsb_value Provides ${count} )"
|
---|
205 | if test "${provides}" = "${optfacility}"
|
---|
206 | then
|
---|
207 | optprovideslist="${optprovideslist} ${scriptlist[$count]}"
|
---|
208 | break
|
---|
209 | fi
|
---|
210 | # No need to error or warn on should items, and it's messy if so!
|
---|
211 | done
|
---|
212 | done
|
---|
213 | echo "${optprovideslist}" | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//"
|
---|
214 | }
|
---|
215 |
|
---|
216 | get_headers
|
---|
217 |
|
---|
218 | ###############################################################################
|
---|
219 | # get_lsb_required_value() - Additional function to simplify repetitive tasks #
|
---|
220 | # Obtains the LSB Value of $1 for index of script #
|
---|
221 | # ($2) and immediately converts LSB defined #
|
---|
222 | # facilities (beginning with a '$' character) to a #
|
---|
223 | # script name. If the script is not found, then #
|
---|
224 | # the function exits with an error as per #
|
---|
225 | # convert_lsb_required. #
|
---|
226 | ###############################################################################
|
---|
227 | get_lsb_required_value()
|
---|
228 | {
|
---|
229 | local reqval
|
---|
230 | # Probably need some error checking in here
|
---|
231 | reqval=`echo "${fullheaders[${2}]}" | \
|
---|
232 | grep "^# ${1}" | \
|
---|
233 | sed -e "s@# ${1}:@@" \
|
---|
234 | -e "s/^[ \t]*//"`
|
---|
235 |
|
---|
236 | # If $reqval contains a '$' charcter, then convert it to a script name
|
---|
237 | echo "${reqval}" | grep "\\$" 2>&1 > /dev/null
|
---|
238 | if test "${?}" -eq "0"
|
---|
239 | then
|
---|
240 | reqval=`convert_lsb_required "${reqval}"`
|
---|
241 | fi
|
---|
242 | echo "${reqval}"
|
---|
243 | }
|
---|
244 |
|
---|
245 | ###############################################################################
|
---|
246 | # get_lsb_should_value() - Additional function to simplify repetitive tasks #
|
---|
247 | # Obtains the LSB Value of $1 for index of script #
|
---|
248 | # ($2) and immediately converts LSB defined #
|
---|
249 | # facilities (beginning with a '$' character) to a #
|
---|
250 | # script name. If the script is not found, the #
|
---|
251 | # value is removed from the list as it is optional. #
|
---|
252 | ###############################################################################
|
---|
253 | get_lsb_should_value()
|
---|
254 | {
|
---|
255 | local optval
|
---|
256 | local listitem
|
---|
257 | local optitem
|
---|
258 | # Probably need some error checking in here
|
---|
259 | optval=`echo "${fullheaders[${2}]}" | \
|
---|
260 | grep "^# ${1}" | \
|
---|
261 | sed -e "s@# ${1}:@@" \
|
---|
262 | -e "s/^[ \t]*//"`
|
---|
263 |
|
---|
264 | # If $optval contains a '$' charcter, then convert it to a script name
|
---|
265 | echo "${optval}" | grep "\\$" 2>&1 > /dev/null
|
---|
266 | if test "${?}" -eq "0"
|
---|
267 | then
|
---|
268 | optval=`convert_lsb_should "${optval}"`
|
---|
269 | # if we still have a "$" character, then it's not found and it should
|
---|
270 | # be removed from the list (and it's trailing space if one exists)
|
---|
271 | # since it is optional
|
---|
272 | echo "${optval}" | grep "\\$" 2>&1 > /dev/null
|
---|
273 | if test "${?}" -eq "0"
|
---|
274 | then
|
---|
275 | # Remove the value
|
---|
276 | for listitem in ${optval}
|
---|
277 | do
|
---|
278 | echo "${listitem}" | grep "\\$"
|
---|
279 | if test "${?}" -eq "0"
|
---|
280 | then
|
---|
281 | optval=`echo "${optval}" | sed -e 's@${listitem} @@' \
|
---|
282 | -e 's@${listitem}@@' | \
|
---|
283 | sed -e "s@# ${1}:@@" \
|
---|
284 | -e "s/^[ \t]*//"`
|
---|
285 | fi
|
---|
286 | done
|
---|
287 | fi
|
---|
288 | fi
|
---|
289 | # If a should start value does not have a script associted with it, then
|
---|
290 | # remove it (or it and trailing space) from the list
|
---|
291 | for optitem in ${otpval}
|
---|
292 | do
|
---|
293 | grep "${optitem}" "${statedir}/enabled-scripts" 2>&1 > /dev/null
|
---|
294 | if test "${?}" -ne "0"
|
---|
295 | then
|
---|
296 | optval=`echo "${optval}" | sed -e 's@${otpitem} @@' \
|
---|
297 | -e 's@${optitem}@@' | \
|
---|
298 | sed -e "s@# ${1}:@@" \
|
---|
299 | -e "s/^[ \t]*//"`
|
---|
300 | fi
|
---|
301 | done
|
---|
302 |
|
---|
303 | echo "${optval}"
|
---|
304 | }
|
---|
305 |
|
---|
306 | # End /lib/lsb/manage-functions
|
---|