lfs-functions 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Begin /etc/init.d/lfs-functions
  2. # Provides LFS specific functions for LSB style bootscripts
  3. ################################# chkstat() ###################################
  4. # chk_stat checks the status of a script by checking for both a binary file #
  5. # to execute, and if set, a config file that may be needed for the program #
  6. # to run successfully. The calling script will exit with a return value of 5 #
  7. # if the binary does not exist, and a value of 6 if the needed config file is #
  8. # unavailable as per LSB requirements. This function accepts zero, one, or #
  9. # two string arguments. If arguments are passed, the first must be a bin #
  10. # file. If a second argument is passed, it is interpreted as the config #
  11. # file. Optionally, zero arguments can be passed if BIN_FILE, and optinally #
  12. # CONFIG_FILE are set in the calling script. #
  13. ###############################################################################
  14. chk_stat()
  15. {
  16. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  17. BIN_FILE="${1}"
  18. if [ -z "${2}" ]; then
  19. CONFIG_FILE=""
  20. else
  21. CONFIG_FILE="${2}"
  22. fi
  23. elif [ -z "${BIN_FILE}" ]; then
  24. echo "Usage: 'chk_stat BIN_FILE CONFIG_FILE'"
  25. exit 1 # Generic Error
  26. fi
  27. if [ ! -e "${BIN_FILE}" ]; then
  28. log_failure_msg "${BIN_FILE} not installed" &&
  29. exit 5
  30. fi
  31. if [ ! -z "${CONFIG_FILE}" ]; then
  32. if [ ! -e "${CONFIG_FILE}" ]; then
  33. log_failure_msg "${CONFIG_FILE} does not exist" &&
  34. exit 6
  35. fi
  36. fi
  37. }
  38. ################################ loadproc() ###################################
  39. # loadproc is just a wraper to start_daemon for simple scripts, which will #
  40. # require no arguments if $BIN_FILE is set. #
  41. ###############################################################################
  42. loadproc()
  43. {
  44. start_daemon "${BIN_FILE}" "${@}"
  45. }
  46. ################################ endproc() ####################################
  47. # endproc, like loadproc, is just a wraper to killproc for simplicity and is #
  48. # dependent on $BIN_FILE being set. #
  49. ###############################################################################
  50. endproc()
  51. {
  52. killproc "${BIN_FILE}" "${@}"
  53. }
  54. ############################### statusproc() ##################################
  55. # statusproc checks the status of a particular binary and displays the #
  56. # appropriate message (running or not running) and exits on the return value #
  57. # of pidofproc. This function accepts two string arguments or zero arguments #
  58. # if BIN_FILE and MESSAGE are set, else it requires the bin file as the first #
  59. # argument, and the message as the second. Both must be enclosed in quotes. #
  60. ###############################################################################
  61. statusproc()
  62. {
  63. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  64. BIN_FILE="${1}"
  65. MESSAGE="${2}"
  66. elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
  67. echo "Usage: 'statusproc BIN_FILE MESSAGE'"
  68. exit 1 # Generic Error
  69. fi
  70. pidlist=`pidofproc "${BIN_FILE}"`
  71. STATUS=$?
  72. echo "Checking ${MESSAGE} status:"
  73. if [ "${STATUS}" = "0" ]; then
  74. log_success_msg "Running with PID(s) ${pidlist}"
  75. else
  76. log_warning_msg "Not running!"
  77. fi
  78. return "${STATUS}"
  79. }
  80. ############################### reloadproc() ##################################
  81. # reloadproc sends a HUP signal to the running program (relaod configuration) #
  82. # It optionally, using the -force switch, checks the status of a particular #
  83. # program and starts it if it is not already running. This function accepts #
  84. # one optional switch (must be the first argument), and either two, or zero #
  85. # string arguments. If BIN_FILE and MESSAGE are set in the script's #
  86. # environment, it will use those values, else it requires the bin file as #
  87. # the first argument (following -force if used), and the message as the #
  88. # second. Both must be enclosed in quotes. If the force option is used, it #
  89. # follows the LSB definition of 'force-reload' - the program is started if #
  90. # not already running. #
  91. ###############################################################################
  92. reloadproc()
  93. {
  94. local force="0"
  95. if [ "${#}" -gt "0" -a "${1}" = "-force" ]; then
  96. force="1"
  97. shift 1
  98. fi
  99. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  100. BIN_FILE="${1}"
  101. MESSAGE="${2}"
  102. elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
  103. echo "Usage: 'reloadproc BIN_FILE MESSAGE'"
  104. exit 1 # Generic Error
  105. fi
  106. }
  107. ############################## evaluate_retval() ###############################
  108. # evaluate_retval requires that you pass exactly one evaluation parameter of #
  109. # (start, stop, other) based on the previous action that is being evaluated. #
  110. # This function is intended for use with start_daemon and killproc to #
  111. # interpret the LSB exit codes properly, othewise the checks only for success #
  112. # or failure. #
  113. ################################################################################
  114. evaluate_retval()
  115. {
  116. local error_value="${?}"
  117. # Handle LSB defined return values
  118. case "${1}" in
  119. start)
  120. case "${error_value}" in
  121. 0)
  122. log_success_msg "Starting ${MESSAGE} "
  123. return "${error_value}"
  124. ;;
  125. 2)
  126. log_failure_msg "Starting ${MESSAGE} Error: Invalid argument!"
  127. return "${error_value}"
  128. ;;
  129. 5)
  130. log_failure_msg "Starting ${MESSAGE} Error: Not available!"
  131. return "${error_value}"
  132. ;;
  133. *)
  134. log_failure_msg "Starting ${MESSAGE} Error: General failure!"
  135. return "${error_value}"
  136. ;;
  137. esac
  138. ;;
  139. stop)
  140. case "${error_value}" in
  141. 0)
  142. log_success_msg "Stopping ${MESSAGE} "
  143. return "${error_value}"
  144. ;;
  145. 2)
  146. log_failure_msg "Stopping ${MESSAGE} Error: Invalid argument!"
  147. return "${error_value}"
  148. ;;
  149. 5)
  150. log_failure_msg "Stopping ${MESSAGE} Error: Not available!"
  151. return "${error_value}"
  152. ;;
  153. 7)
  154. log_warning_msg "Stopping ${MESSAGE} Warning: Not running!"
  155. return "${error_value}"
  156. ;;
  157. *)
  158. log_failure_msg "Stopping ${MESSAGE} Error: General failure!"
  159. return "${error_value}"
  160. ;;
  161. esac
  162. ;;
  163. force-reload)
  164. message="Forcefully reloading "
  165. ;;
  166. reload)
  167. message="Reloading "
  168. ;;
  169. restart)
  170. message="Restarting "
  171. ;;
  172. try-restart)
  173. message="Trying restart "
  174. ;;
  175. standard)
  176. # $message or $MESSAGE must be set, but not both in order
  177. # to use the 'standard' target.
  178. ;;
  179. esac
  180. # Print messages for the generic force-reload, reload, restart,
  181. # and try-restart targets
  182. if [ "${error_value}" = "0" ]
  183. then
  184. log_success_msg "${message}${MESSAGE} "
  185. return "${error_value}"
  186. else
  187. log_failure_msg "${message}${MESSAGE} "
  188. return "${error_value}"
  189. fi
  190. }