1
0

init-functions 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. # Begin /lib/lsb/init-funtions
  2. # Provides initialization funtions as defined by the Linux Standard Base
  3. # specification, version 3.1.0
  4. # Source rc configuration if not inherited from the environment
  5. if [ "${RC_BASE}" = "" ]; then
  6. . /etc/default/rc
  7. fi
  8. ###############################################################################
  9. # start_daemon() #
  10. # Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
  11. # #
  12. # Purpose: This runs the specified program as a daemon #
  13. # #
  14. # Inputs: -f: (force) run the program even if it is already running. #
  15. # -n nicelevel: specify a nice level. See 'man nice(1)'. #
  16. # -p pidfile: use the specified file to determine PIDs. #
  17. # pathname: the complete path to the specified program #
  18. # args: additional arguments passed to the program (pathname) #
  19. # #
  20. # Return values (as defined by LSB exit codes): #
  21. # 0 - program is running or service is OK #
  22. # 1 - generic or unspecified error #
  23. # 2 - invalid or excessive argument(s) #
  24. # 5 - program is not installed #
  25. ###############################################################################
  26. start_daemon()
  27. {
  28. local force=""
  29. local nice="0"
  30. local pidfile=""
  31. local pidlist=""
  32. local retval=""
  33. # Process arguments
  34. while true
  35. do
  36. case "${1}" in
  37. -f)
  38. force="1"
  39. shift 1
  40. ;;
  41. -n)
  42. nice="${2}"
  43. shift 2
  44. ;;
  45. -p)
  46. pidfile="${2}"
  47. shift 2
  48. ;;
  49. -*)
  50. return 2
  51. ;;
  52. *)
  53. program="${1}"
  54. break
  55. ;;
  56. esac
  57. done
  58. # Check for a valid program
  59. if [ ! -e "${program}" ]
  60. then
  61. return 5
  62. fi
  63. # Execute
  64. if [ -z "${force}" ]
  65. then
  66. if [ -z "${pidfile}" ]
  67. then
  68. # determine the pid by discovery
  69. pidlist=`pidofproc "${1}"`
  70. retval="${?}"
  71. else
  72. # The PID file contains the needed PIDs
  73. # Note that by LSB requirement, the path must be given to pidofproc,
  74. # however, it is not used by the current implementation or standard.
  75. pidlist=`pidofproc -p "${pidfile}" "${1}"`
  76. retval="${?}"
  77. fi
  78. # return a value ONLY
  79. # It is the init script's (or distribution's functions) responsibilty
  80. # to log messages!
  81. case "${retval}" in
  82. 0)
  83. # program is already running correctly, this is a
  84. # succesful start.
  85. return 0
  86. ;;
  87. 1)
  88. # program is not running, but an invalid pid file exists
  89. # remove the pid file and continue
  90. rm -f "${pidfile}"
  91. ;;
  92. 3)
  93. # program is not running and no pidfile exists
  94. # do nothing here, let start_deamon continue.
  95. ;;
  96. *)
  97. # Others as returned by status values shall not be interpreted
  98. # and returned as an unspecified error.
  99. return 1
  100. ;;
  101. esac
  102. fi
  103. # do the start!
  104. nice -n "${nice}" "${@}"
  105. }
  106. ###############################################################################
  107. # killproc() #
  108. # Usage: killproc [-p pidfile] pathname [signal] #
  109. # #
  110. # Purpose: Send control signals to running processes #
  111. # #
  112. # Inputs: -p pidfile, uses the specified pidfile #
  113. # pathname, pathname to the specified program #
  114. # signal, send this signal to pathname #
  115. # #
  116. # Return values (as defined by LSB exit codes): #
  117. # 0 - program (pathname) has stopped/is already stopped or a #
  118. # running program has been sent specified signal and stopped #
  119. # successfully #
  120. # 1 - generic or unspecified error #
  121. # 2 - invalid or excessive argument(s) #
  122. # 5 - program is not installed #
  123. # 7 - program is not running and a signal was supplied #
  124. ###############################################################################
  125. killproc()
  126. {
  127. local pidfile
  128. local program
  129. local prefix
  130. local progname
  131. local signal="-TERM"
  132. local fallback="-KILL"
  133. local nosig
  134. local pidlist
  135. local retval
  136. local pid
  137. local delay="30"
  138. local piddead
  139. local dtime
  140. # Process arguments
  141. while true
  142. do
  143. case "${1}" in
  144. -p)
  145. pidfile="${2}"
  146. shift 2
  147. ;;
  148. *)
  149. program="${1}"
  150. if [ -n "${2}" ]
  151. then
  152. signal="${2}"
  153. fallback=""
  154. else
  155. nosig=1
  156. fi
  157. # error on additional arguments
  158. if [ -n "${3}" ]
  159. then
  160. return 2
  161. else
  162. break
  163. fi
  164. ;;
  165. esac
  166. done
  167. # Check for a valid program
  168. if [ ! -e "${program}" ]
  169. then
  170. return 5
  171. fi
  172. # Check for a valid signal
  173. check_signal "${signal}"
  174. if [ "${?}" -ne "0" ]
  175. then
  176. return 2
  177. fi
  178. # Get a list of pids
  179. if [ -z "${pidfile}" ]
  180. then
  181. # determine the pid by discovery
  182. pidlist=`pidofproc "${1}"`
  183. retval="${?}"
  184. else
  185. # The PID file contains the needed PIDs
  186. # Note that by LSB requirement, the path must be given to pidofproc,
  187. # however, it is not used by the current implementation or standard.
  188. pidlist=`pidofproc -p "${pidfile}" "${1}"`
  189. retval="${?}"
  190. fi
  191. # return a value ONLY
  192. # It is the init script's (or distribution's functions) responsibilty
  193. # to log messages!
  194. case "${retval}" in
  195. 0)
  196. # program is running correctly
  197. # do nothing here, let killproc continue.
  198. ;;
  199. 1)
  200. # program is not running, but an invalid pid file exists
  201. # remove the pid file.
  202. rm -f "${pidfile}"
  203. # this is only a success if no signal was passed.
  204. if [ -n "${nosig}" ]
  205. then
  206. return 0
  207. else
  208. return 7
  209. fi
  210. ;;
  211. 3)
  212. # program is not running and no pidfile exists
  213. # this is only a success if no signal was passed.
  214. if [ -n "${nosig}" ]
  215. then
  216. return 0
  217. else
  218. return 7
  219. fi
  220. ;;
  221. *)
  222. # Others as returned by status values shall not be interpreted
  223. # and returned as an unspecified error.
  224. return 1
  225. ;;
  226. esac
  227. # perform different actions for exit signals and control signals
  228. check_sig_type "${signal}"
  229. if [ "${?}" -eq "0" ] # signal is used to terminate the program
  230. then
  231. # account for empty pidlist (pid file still exists and nosignal was given)
  232. if [ "${pidlist}" != "" ]; then
  233. #kill the list of pids
  234. for pid in ${pidlist}
  235. do
  236. kill -0 "${pid}" 2> /dev/null
  237. if [ "${?}" -ne "0" ]; then
  238. # process is dead, continue to next and assume all is well
  239. continue
  240. else
  241. kill "${signal}" "${pid}" 2> /dev/null
  242. # Wait up to ${delay}/10 seconds to for "${pid}" to
  243. # terminate in 10ths of a second
  244. while [ "${delay}" -ne "0" ]
  245. do
  246. kill -0 "${pid}" 2> /dev/null || piddead="1"
  247. if [ "${piddead}" = "1" ]
  248. then
  249. break
  250. fi
  251. sleep 0.1
  252. delay="$(( ${delay} - 1 ))"
  253. done
  254. # If a fallback is set, and program is still running, then
  255. # use the fallback
  256. if [ -n "${fallback}" -a "${piddead}" != "1" ]
  257. then
  258. kill "${fallback}" "${pid}" 2> /dev/null
  259. sleep 1
  260. # Check again, and fail if still running
  261. kill -0 "${pid}" 2> /dev/null && return 1
  262. else
  263. # just check one last time and if still alive, fail
  264. sleep 1
  265. kill -0 "${pid}" 2> /dev/null && return 1
  266. fi
  267. fi
  268. done
  269. fi
  270. # Check for and remove stale PID files.
  271. if [ -z "${pidfile}" ]
  272. then
  273. #find the basename of $program
  274. prefix=`echo "${program}" | sed 's/[^/]*$//'`
  275. progname=`echo "${program}" | sed "s@${prefix}@@"`
  276. if [ -e "/var/run/${progname}.pid" ]
  277. then
  278. rm -f "/var/run/${progname}.pid" 2> /dev/null
  279. fi
  280. else
  281. if [ -e "${pidfile}" ]
  282. then
  283. rm -f "${pidfile}" 2> /dev/null
  284. fi
  285. fi
  286. # For signals that do not expect a program to exit, simply
  287. # let kill do it's job, and evaluate kills return for value
  288. else # check_sig_type - signal is not used to terminate program
  289. for pid in ${pidlist}
  290. do
  291. kill "${signal}" "${pid}"
  292. if [ "${?}" -ne "0" ]; then
  293. return 1
  294. fi
  295. done
  296. fi
  297. }
  298. ###############################################################################
  299. # pidofproc() #
  300. # Usage: pidofproc [-p pidfile] pathname #
  301. # #
  302. # Purpose: This function returns one or more pid(s) for a particular daemon #
  303. # #
  304. # Inputs: -p pidfile, use the specified pidfile instead of pidof #
  305. # pathname, path to the specified program #
  306. # #
  307. # Return values (as defined by LSB status codes): #
  308. # 0 - Success (PIDs to stdout) #
  309. # 1 - Program is dead, PID file still exists (remaining PIDs output) #
  310. # 3 - Program is not running (no output) #
  311. ###############################################################################
  312. pidofproc()
  313. {
  314. local pidfile
  315. local program
  316. local prefix
  317. local progname
  318. local pidlist
  319. local lpids
  320. local exitstatus="0"
  321. # Process arguments
  322. while true
  323. do
  324. case "${1}" in
  325. -p)
  326. pidfile="${2}"
  327. shift 2
  328. ;;
  329. *)
  330. program="${1}"
  331. if [ -n "${2}" ]
  332. then
  333. # Too many arguments
  334. # Since this is status, return unknown
  335. return 4
  336. else
  337. break
  338. fi
  339. ;;
  340. esac
  341. done
  342. # If a PID file is not specified, try and find one.
  343. if [ -z "${pidfile}" ]
  344. then
  345. # get the program's basename
  346. prefix=`echo "${program}" | sed 's/[^/]*$//'`
  347. progname=`echo "${program}" | sed "s@${prefix}@@"`
  348. # if a PID file exists with that name, assume that is it.
  349. if [ -e "/var/run/${progname}.pid" ]
  350. then
  351. pidfile="/var/run/${progname}.pid"
  352. fi
  353. fi
  354. # if a PID file is set and exists, use it.
  355. if [ -n "${pidfile}" -a -e "${pidfile}" ]
  356. then
  357. # use the value in the first line of the pidfile
  358. pidlist=`/bin/head -n1 "${pidfile}"`
  359. # This can optionally be written as 'sed 1q' to repalce 'head -n1'
  360. # should LFS move /bin/head to /usr/bin/head
  361. else
  362. # use pidof
  363. pidlist=`pidof "${program}"`
  364. fi
  365. # Figure out if all listed PIDs are running.
  366. for pid in ${pidlist}
  367. do
  368. kill -0 ${pid} 2> /dev/null
  369. if [ "${?}" -eq "0" ]; then
  370. lpids="${pids}${pid} "
  371. else
  372. exitstatus="1"
  373. fi
  374. done
  375. if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
  376. return 3
  377. else
  378. echo "${lpids}"
  379. return "${exitstatus}"
  380. fi
  381. }
  382. ###############################################################################
  383. # log_success_msg() #
  384. # Usage: log_success_msg [$MESSAGE | "message"] #
  385. # #
  386. # Purpose: Print a successful status message to the screen and optionally #
  387. # a boot log file. #
  388. # #
  389. # Inputs: accepts one string value, either a quoted string or optionally #
  390. # the value of $MESSAGE if set in the running environment. #
  391. # #
  392. # Return values: Not used #
  393. ###############################################################################
  394. log_success_msg()
  395. {
  396. echo -n -e "${PREFIX_SUCCESS}${@}"
  397. echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
  398. if [ "${BOOTLOG_ENAB}" = "yes" ]; then
  399. if [ $( hostname ) = "(none)" ]; then
  400. BTTIMESPEC=""
  401. else
  402. BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
  403. fi
  404. if [ "${RUNLEVEL}" != "0" -a "${RUNLEVEL}" != "6" ]; then
  405. echo "${BTTIMESPEC}bootlog: ${@} Successful" >> /run/.bootlog
  406. fi
  407. fi
  408. return 0
  409. }
  410. ###############################################################################
  411. # log_failure_msg() #
  412. # Usage: log_failure_msg [$MESSAGE | "message"] #
  413. # #
  414. # Purpose: Print a failure status message to the screen and optionally #
  415. # a boot log file. #
  416. # #
  417. # Inputs: accepts one string value, either a quoted string or optionally #
  418. # the value of $MESSAGE if set in the running environment. #
  419. # #
  420. # Return values: Not used #
  421. ###############################################################################
  422. log_failure_msg()
  423. {
  424. echo -n -e "${PREFIX_FAILURE}${@}"
  425. echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
  426. if [ "${BOOTLOG_ENAB}" = "yes" ]; then
  427. if [ $( hostname ) = "(none)" ]; then
  428. BTTIMESPEC=""
  429. else
  430. BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
  431. fi
  432. if [ "${RUNLEVEL}" != "0" -a "${RUNLEVEL}" != "6" ]; then
  433. echo "${BTTIMESPEC}bootlog: ${@} Failed!" >> /run/.bootlog
  434. fi
  435. fi
  436. return 0
  437. }
  438. ###############################################################################
  439. # log_warning_msg() #
  440. # Usage: log_warning_msg [$MESSAGE | "message"] #
  441. # #
  442. # Purpose: Print a warning status message to the screen and optionally #
  443. # a boot log file. #
  444. # #
  445. # Inputs: accepts one string value, either a quoted string or optionally #
  446. # the value of $MESSAGE if set in the running environment. #
  447. # #
  448. # Return values: Not used #
  449. ###############################################################################
  450. log_warning_msg()
  451. {
  452. echo -n -e "${PREFIX_WARNING}${@}"
  453. echo -e "${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
  454. if [ "${BOOTLOG_ENAB}" = "yes" ]; then
  455. if [ $( hostname ) = "(none)" ]; then
  456. BTTIMESPEC=""
  457. else
  458. BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
  459. fi
  460. if [ "${RUNLEVEL}" != "0" -a "${RUNLEVEL}" != "6" ]; then
  461. echo "${BTTIMESPEC}bootlog: ${@} Warning" >> /run/.bootlog
  462. fi
  463. fi
  464. return 0
  465. }
  466. # The remaining fucntions are distro specific and are not defined by the LSB
  467. ###############################################################################
  468. # check_signal() #
  469. # Usage: check_signal [ -{signal} | {signal} ] #
  470. # #
  471. # Purpose: Check for a valid signal. This is not defined by any LSB draft, #
  472. # however, it is required to check the signals to determine if the #
  473. # signals chosen are invalid arguments to the other functions. #
  474. # #
  475. # Inputs: accepts a single string value in the form or -{signal} or {signal} #
  476. # #
  477. # Return values: #
  478. # 0 - Success (signal is valid #
  479. # 1 - Signal is not valid #
  480. ###############################################################################
  481. check_signal()
  482. {
  483. local valsig
  484. # Add error handling for invalid signals
  485. valsig="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
  486. valsig="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
  487. valsig="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
  488. valsig="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
  489. valsig="${valsig} -11 -13 -14 -15"
  490. echo "${valsig}" | grep -- " ${1} " > /dev/null
  491. if [ "${?}" -eq "0" ]
  492. then
  493. return 0
  494. else
  495. return 1
  496. fi
  497. }
  498. ###############################################################################
  499. # check_sig_type() #
  500. # Usage: check_signal [ -{signal} | {signal} ] #
  501. # #
  502. # Purpose: Check if signal is a program termination signal or a control #
  503. # signal. This is not defined by any LSB draft, however, it is #
  504. # required to check the signals to determine if they are intended #
  505. # to end a program or simply to control it. #
  506. # #
  507. # Inputs: accepts a single string value in the form or -{signal} or {signal} #
  508. # #
  509. # Return values: #
  510. # 0 - Signal is used for program termination #
  511. # 1 - Signal is used for program control #
  512. ###############################################################################
  513. check_sig_type()
  514. {
  515. local valsig
  516. # The list of termination signals (limited to generally used items)
  517. valsig="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"
  518. echo "${valsig}" | grep -- " ${1} " > /dev/null
  519. if [ "${?}" -eq "0" ]
  520. then
  521. return 0
  522. else
  523. return 1
  524. fi
  525. }
  526. ###############################################################################
  527. # chkstat() #
  528. # Usage: chckstat BIN_FILE {CONFIG_FILE} #
  529. # #
  530. # Purpose: chk_stat checks the status of a script by checking for both a #
  531. # binary file to execute, and if set, a config file that may be #
  532. # needed for the program to run successfully. #
  533. # #
  534. # Inputs: accepts first argument of an executable file, and optionally a #
  535. # second arugument of a configuration file. If BIN_FILE and #
  536. # CONFIG_FILE are set in the calling environment, either or both #
  537. # arguments may be omitted. #
  538. # #
  539. # Return values: #
  540. # 0 - The executable, and optionally the configuration file exists #
  541. # 2 - Invalid or excessive arguments #
  542. # 5 - BIN_FILE does not exist #
  543. # 6 - CONFIG_FILE (if set) does not exist #
  544. ###############################################################################
  545. chk_stat()
  546. {
  547. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  548. BIN_FILE="${1}"
  549. if [ -z "${2}" ]; then
  550. CONFIG_FILE=""
  551. else
  552. CONFIG_FILE="${2}"
  553. fi
  554. elif [ -z "${BIN_FILE}" ]; then
  555. echo "Usage: 'chk_stat BIN_FILE CONFIG_FILE'"
  556. exit 1 # Generic Error
  557. fi
  558. if [ ! -e "${BIN_FILE}" ]; then
  559. log_failure_msg "${BIN_FILE} not installed" &&
  560. exit 5
  561. fi
  562. if [ ! -z "${CONFIG_FILE}" ]; then
  563. if [ ! -e "${CONFIG_FILE}" ]; then
  564. log_failure_msg "${CONFIG_FILE} does not exist" &&
  565. exit 6
  566. fi
  567. fi
  568. }
  569. ###############################################################################
  570. # loadproc() #
  571. # Usage: loadproc {arguments} #
  572. # #
  573. # Purpose: loadproc is just a wrapper to start_daemon for simple scripts, #
  574. # which will require no aruguments if $BIN_FILE is set. #
  575. # #
  576. # Inputs: Any optional arguments passed to loadproc will be passed on to the #
  577. # executable defined by $BIN_FILE. #
  578. # #
  579. # Return values: (none) #
  580. ###############################################################################
  581. loadproc()
  582. {
  583. start_daemon "${BIN_FILE}" "${@}"
  584. }
  585. ###############################################################################
  586. # endproc() #
  587. # Usage: endproc {arguments} #
  588. # #
  589. # Purpose: endproc is just a wrapper to killproc for simple scripts, which #
  590. # which will require no aruguments if $BIN_FILE is set. #
  591. # #
  592. # Inputs: Any optional arguments passed to endproc will be passed on to the #
  593. # executable defined by $BIN_FILE. #
  594. # #
  595. # Return values: (none) #
  596. ###############################################################################
  597. endproc()
  598. {
  599. killproc "${BIN_FILE}" "${@}"
  600. }
  601. ###############################################################################
  602. # statusproc() #
  603. # Usage: statusproc $BIN_FILE $MESSAGE #
  604. # #
  605. # Purpose: stautsproc is just a wrapper to pidofproc for simple scripts, #
  606. # which will require no aruguments if $BIN_FILE and MESSAGE are set. #
  607. # #
  608. # Inputs: accepts first argument of an executable file, and a second message #
  609. # arugument "MESSAGE" to be displayed. If BIN_FILE and MESSAGE are #
  610. # set in the calling environment, both arguments may be omitted. #
  611. # #
  612. # Return values: exit values of pidofproc #
  613. ###############################################################################
  614. statusproc()
  615. {
  616. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  617. BIN_FILE="${1}"
  618. MESSAGE="${2}"
  619. elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
  620. echo "Usage: 'statusproc BIN_FILE MESSAGE'"
  621. exit 1 # Generic Error
  622. fi
  623. pidlist=`pidofproc "${BIN_FILE}"`
  624. STATUS=$?
  625. echo "Checking ${MESSAGE} status:"
  626. if [ "${STATUS}" -eq "0" ]; then
  627. log_success_msg "Running with PID(s) ${pidlist}"
  628. else
  629. log_warning_msg "Not running!"
  630. fi
  631. return "${STATUS}"
  632. }
  633. ###############################################################################
  634. # reloadproc() #
  635. # Usage: reloadproc {--force} $BIN_FILE $MESSAGE #
  636. # #
  637. # Purpose: reloadproc sends a HUP signal to the running program (relaod #
  638. # configuration). It optionally, using the -force switch, checks the #
  639. # status of a particular program and starts it if it is not already #
  640. # running. #
  641. # #
  642. # Inputs: accepts one optional switch (must be the first argument), and #
  643. # either two, or zero string arguments. If BIN_FILE and MESSAGE are #
  644. # set in the calling envirnoment it will use those values, else it #
  645. # requires the bin file as the first argument (following -force if #
  646. # used), and the message as the second. If the --force argument is #
  647. # given, it follows the LSB definition of 'force-reload' - the #
  648. # program is started if not already running. #
  649. # #
  650. # Return values: 1 - generic error #
  651. ###############################################################################
  652. reloadproc()
  653. {
  654. local force="0"
  655. if [ "${#}" -gt "0" -a "${1}" = "-force" ]; then
  656. force="1"
  657. shift 1
  658. fi
  659. if [ "${#}" -gt "0" -a "${#}" -lt "3" ]; then
  660. BIN_FILE="${1}"
  661. MESSAGE="${2}"
  662. elif [ -z "${BIN_FILE}" -o -z "${MESSAGE}" ]; then
  663. echo "Usage: 'reloadproc BIN_FILE MESSAGE'"
  664. exit 1 # Generic Error
  665. fi
  666. }
  667. ###############################################################################
  668. # evaluate_retval() #
  669. # Usage: evaluate_retval \ #
  670. # [standard|start|stop|reload|force-reload|restart|try-restart] #
  671. # #
  672. # Purpose: determines the sucess or failure of a previous command based on #
  673. # LSB exit values, and prints messages to the screen using the #
  674. # log_*_msg() functions. #
  675. # #
  676. # Inputs: accepts one argument which determines the output of the message #
  677. # displayed on the screen based on the LSB input values for init #
  678. # scripts. The 'standard' argument makes no changes to the value of #
  679. # $message or $MESSAGE, but only one can be set in the calling #
  680. # environment. #
  681. # #
  682. # Return values: (none) #
  683. ###############################################################################
  684. evaluate_retval()
  685. {
  686. local error_value="${?}"
  687. # Handle LSB defined return values
  688. case "${1}" in
  689. start)
  690. case "${error_value}" in
  691. 0)
  692. log_success_msg "Starting ${MESSAGE} "
  693. return "${error_value}"
  694. ;;
  695. 2)
  696. log_failure_msg "Starting ${MESSAGE} Error: Invalid argument!"
  697. return "${error_value}"
  698. ;;
  699. 5)
  700. log_failure_msg "Starting ${MESSAGE} Error: Not available!"
  701. return "${error_value}"
  702. ;;
  703. *)
  704. log_failure_msg "Starting ${MESSAGE} Error: General failure!"
  705. return "${error_value}"
  706. ;;
  707. esac
  708. ;;
  709. stop)
  710. case "${error_value}" in
  711. 0)
  712. log_success_msg "Stopping ${MESSAGE} "
  713. return "${error_value}"
  714. ;;
  715. 2)
  716. log_failure_msg "Stopping ${MESSAGE} Error: Invalid argument!"
  717. return "${error_value}"
  718. ;;
  719. 5)
  720. log_failure_msg "Stopping ${MESSAGE} Error: Not available!"
  721. return "${error_value}"
  722. ;;
  723. 7)
  724. log_warning_msg "Stopping ${MESSAGE} Warning: Not running!"
  725. return "${error_value}"
  726. ;;
  727. *)
  728. log_failure_msg "Stopping ${MESSAGE} Error: General failure!"
  729. return "${error_value}"
  730. ;;
  731. esac
  732. ;;
  733. force-reload)
  734. message="Forcefully reloading "
  735. ;;
  736. reload)
  737. message="Reloading "
  738. ;;
  739. restart)
  740. message="Restarting "
  741. ;;
  742. try-restart)
  743. message="Trying restart "
  744. ;;
  745. standard)
  746. # $message or $MESSAGE must be set, but not both in order
  747. # to use the 'standard' target.
  748. ;;
  749. esac
  750. # Print messages for the generic force-reload, reload, restart,
  751. # and try-restart targets
  752. if [ "${error_value}" -eq "0" ]
  753. then
  754. log_success_msg "${message}${MESSAGE} "
  755. return "${error_value}"
  756. else
  757. log_failure_msg "${message}${MESSAGE} "
  758. return "${error_value}"
  759. fi
  760. }
  761. # End /lib/lsb/init-functions