manage-functions 12 KB

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