init-functions 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. #!/bin/sh
  2. ########################################################################
  3. #
  4. # Begin /lib/lsb/init-funtions
  5. #
  6. # Description : Run Level Control Functions
  7. #
  8. # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
  9. # : DJ Lucas - dj@linuxfromscratch.org
  10. # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
  11. #
  12. # Version : LFS 7.0
  13. #
  14. # Notes : With code based on Matthias Benkmann's simpleinit-msb
  15. # http://winterdrache.de/linux/newboot/index.html
  16. #
  17. # The file should be located in /lib/lsb
  18. #
  19. ########################################################################
  20. ## Environmental setup
  21. # Setup default values for environment
  22. umask 022
  23. export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
  24. ## Set color commands, used via echo
  25. # Please consult `man console_codes for more information
  26. # under the "ECMA-48 Set Graphics Rendition" section
  27. #
  28. # Warning: when switching from a 8bit to a 9bit font,
  29. # the linux console will reinterpret the bold (1;) to
  30. # the top 256 glyphs of the 9bit font. This does
  31. # not affect framebuffer consoles
  32. NORMAL="\\033[0;39m" # Standard console grey
  33. SUCCESS="\\033[1;32m" # Success is green
  34. WARNING="\\033[1;33m" # Warnings are yellow
  35. FAILURE="\\033[1;31m" # Failures are red
  36. INFO="\\033[1;36m" # Information is light cyan
  37. BRACKET="\\033[1;34m" # Brackets are blue
  38. # Use a colored prefix
  39. BMPREFIX=" "
  40. SUCCESS_PREFIX="${SUCCESS} * ${NORMAL}"
  41. FAILURE_PREFIX="${FAILURE}*****${NORMAL}"
  42. WARNING_PREFIX="${WARNING} *** ${NORMAL}"
  43. SKIP_PREFIX="${INFO} S ${NORMAL}"
  44. SUCCESS_SUFFIX="${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
  45. FAILURE_SUFFIX="${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
  46. WARNING_SUFFIX="${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
  47. SKIP_SUFFIX="${BRACKET}[${INFO} SKIP ${BRACKET}]${NORMAL}"
  48. BOOTLOG=/run/bootlog
  49. KILLDELAY=3
  50. # Set any user specified environment variables e.g. HEADLESS
  51. [ -r /etc/sysconfig/rc.site ] && . /etc/sysconfig/rc.site
  52. ## Screen Dimensions
  53. # Find current screen size
  54. if [ -z "${COLUMNS}" ]; then
  55. COLUMNS=$(stty size)
  56. COLUMNS=${COLUMNS##* }
  57. fi
  58. # When using remote connections, such as a serial port, stty size returns 0
  59. if [ "${COLUMNS}" = "0" ]; then
  60. COLUMNS=80
  61. fi
  62. ## Measurements for positioning result messages
  63. COL=$((${COLUMNS} - 8))
  64. WCOL=$((${COL} - 2))
  65. ## Set Cursor Position Commands, used via echo
  66. SET_COL="\\033[${COL}G" # at the $COL char
  67. SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
  68. CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
  69. CURS_ZERO="\\033[0G"
  70. ################################################################################
  71. # start_daemon() #
  72. # Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
  73. # #
  74. # Purpose: This runs the specified program as a daemon #
  75. # #
  76. # Inputs: -f: (force) run the program even if it is already running. #
  77. # -n nicelevel: specify a nice level. See 'man nice(1)'. #
  78. # -p pidfile: use the specified file to determine PIDs. #
  79. # pathname: the complete path to the specified program #
  80. # args: additional arguments passed to the program (pathname) #
  81. # #
  82. # Return values (as defined by LSB exit codes): #
  83. # 0 - program is running or service is OK #
  84. # 1 - generic or unspecified error #
  85. # 2 - invalid or excessive argument(s) #
  86. # 5 - program is not installed #
  87. ################################################################################
  88. start_daemon()
  89. {
  90. local force=""
  91. local nice="0"
  92. local pidfile=""
  93. local pidlist=""
  94. local retval=""
  95. # Process arguments
  96. while true
  97. do
  98. case "${1}" in
  99. -f)
  100. force="1"
  101. shift 1
  102. ;;
  103. -n)
  104. nice="${2}"
  105. shift 2
  106. ;;
  107. -p)
  108. pidfile="${2}"
  109. shift 2
  110. ;;
  111. -*)
  112. return 2
  113. ;;
  114. *)
  115. program="${1}"
  116. break
  117. ;;
  118. esac
  119. done
  120. # Check for a valid program
  121. if [ ! -e "${program}" ]; then return 5; fi
  122. # Execute
  123. if [ -z "${force}" ]; then
  124. if [ -z "${pidfile}" ]; then
  125. # Determine the pid by discovery
  126. pidlist=`pidofproc "${1}"`
  127. retval="${?}"
  128. else
  129. # The PID file contains the needed PIDs
  130. # Note that by LSB requirement, the path must be given to pidofproc,
  131. # however, it is not used by the current implementation or standard.
  132. pidlist=`pidofproc -p "${pidfile}" "${1}"`
  133. retval="${?}"
  134. fi
  135. # Return a value ONLY
  136. # It is the init script's (or distribution's functions) responsibilty
  137. # to log messages!
  138. case "${retval}" in
  139. 0)
  140. # Program is already running correctly, this is a
  141. # successful start.
  142. return 0
  143. ;;
  144. 1)
  145. # Program is not running, but an invalid pid file exists
  146. # remove the pid file and continue
  147. rm -f "${pidfile}"
  148. ;;
  149. 3)
  150. # Program is not running and no pidfile exists
  151. # do nothing here, let start_deamon continue.
  152. ;;
  153. *)
  154. # Others as returned by status values shall not be interpreted
  155. # and returned as an unspecified error.
  156. return 1
  157. ;;
  158. esac
  159. fi
  160. # Do the start!
  161. nice -n "${nice}" "${@}"
  162. }
  163. ################################################################################
  164. # killproc() #
  165. # Usage: killproc [-p pidfile] pathname [signal] #
  166. # #
  167. # Purpose: Send control signals to running processes #
  168. # #
  169. # Inputs: -p pidfile, uses the specified pidfile #
  170. # pathname, pathname to the specified program #
  171. # signal, send this signal to pathname #
  172. # #
  173. # Return values (as defined by LSB exit codes): #
  174. # 0 - program (pathname) has stopped/is already stopped or a #
  175. # running program has been sent specified signal and stopped #
  176. # successfully #
  177. # 1 - generic or unspecified error #
  178. # 2 - invalid or excessive argument(s) #
  179. # 5 - program is not installed #
  180. # 7 - program is not running and a signal was supplied #
  181. ################################################################################
  182. killproc()
  183. {
  184. local pidfile
  185. local program
  186. local prefix
  187. local progname
  188. local signal="-TERM"
  189. local fallback="-KILL"
  190. local nosig
  191. local pidlist
  192. local retval
  193. local pid
  194. local delay="30"
  195. local piddead
  196. local dtime
  197. # Process arguments
  198. while true; do
  199. case "${1}" in
  200. -p)
  201. pidfile="${2}"
  202. shift 2
  203. ;;
  204. *)
  205. program="${1}"
  206. if [ -n "${2}" ]; then
  207. signal="${2}"
  208. fallback=""
  209. else
  210. nosig=1
  211. fi
  212. # Error on additional arguments
  213. if [ -n "${3}" ]; then
  214. return 2
  215. else
  216. break
  217. fi
  218. ;;
  219. esac
  220. done
  221. # Check for a valid program
  222. if [ ! -e "${program}" ]; then return 5; fi
  223. # Check for a valid signal
  224. check_signal "${signal}"
  225. if [ "${?}" -ne "0" ]; then return 2; fi
  226. # Get a list of pids
  227. if [ -z "${pidfile}" ]; then
  228. # determine the pid by discovery
  229. pidlist=`pidofproc "${1}"`
  230. retval="${?}"
  231. else
  232. # The PID file contains the needed PIDs
  233. # Note that by LSB requirement, the path must be given to pidofproc,
  234. # however, it is not used by the current implementation or standard.
  235. pidlist=`pidofproc -p "${pidfile}" "${1}"`
  236. retval="${?}"
  237. fi
  238. # Return a value ONLY
  239. # It is the init script's (or distribution's functions) responsibilty
  240. # to log messages!
  241. case "${retval}" in
  242. 0)
  243. # Program is running correctly
  244. # Do nothing here, let killproc continue.
  245. ;;
  246. 1)
  247. # Program is not running, but an invalid pid file exists
  248. # Remove the pid file.
  249. rm -f "${pidfile}"
  250. # This is only a success if no signal was passed.
  251. if [ -n "${nosig}" ]; then
  252. return 0
  253. else
  254. return 7
  255. fi
  256. ;;
  257. 3)
  258. # Program is not running and no pidfile exists
  259. # This is only a success if no signal was passed.
  260. if [ -n "${nosig}" ]; then
  261. return 0
  262. else
  263. return 7
  264. fi
  265. ;;
  266. *)
  267. # Others as returned by status values shall not be interpreted
  268. # and returned as an unspecified error.
  269. return 1
  270. ;;
  271. esac
  272. # Perform different actions for exit signals and control signals
  273. check_sig_type "${signal}"
  274. if [ "${?}" -eq "0" ]; then # Signal is used to terminate the program
  275. # Account for empty pidlist (pid file still exists and no
  276. # signal was given)
  277. if [ "${pidlist}" != "" ]; then
  278. # Kill the list of pids
  279. for pid in ${pidlist}; do
  280. kill -0 "${pid}" 2> /dev/null
  281. if [ "${?}" -ne "0" ]; then
  282. # Process is dead, continue to next and assume all is well
  283. continue
  284. else
  285. kill "${signal}" "${pid}" 2> /dev/null
  286. # Wait up to ${delay}/10 seconds to for "${pid}" to
  287. # terminate in 10ths of a second
  288. while [ "${delay}" -ne "0" ]; do
  289. kill -0 "${pid}" 2> /dev/null || piddead="1"
  290. if [ "${piddead}" = "1" ]; then break; fi
  291. sleep 0.1
  292. delay="$(( ${delay} - 1 ))"
  293. done
  294. # If a fallback is set, and program is still running, then
  295. # use the fallback
  296. if [ -n "${fallback}" -a "${piddead}" != "1" ]; then
  297. kill "${fallback}" "${pid}" 2> /dev/null
  298. sleep 1
  299. # Check again, and fail if still running
  300. kill -0 "${pid}" 2> /dev/null && return 1
  301. fi
  302. fi
  303. done
  304. fi
  305. # Check for and remove stale PID files.
  306. if [ -z "${pidfile}" ]; then
  307. # Find the basename of $program
  308. prefix=`echo "${program}" | sed 's/[^/]*$//'`
  309. progname=`echo "${program}" | sed "s@${prefix}@@"`
  310. if [ -e "/var/run/${progname}.pid" ]; then
  311. rm -f "/var/run/${progname}.pid" 2> /dev/null
  312. fi
  313. else
  314. if [ -e "${pidfile}" ]; then rm -f "${pidfile}" 2> /dev/null; fi
  315. fi
  316. # For signals that do not expect a program to exit, simply
  317. # let kill do its job, and evaluate kill's return for value
  318. else # check_sig_type - signal is not used to terminate program
  319. for pid in ${pidlist}; do
  320. kill "${signal}" "${pid}"
  321. if [ "${?}" -ne "0" ]; then return 1; fi
  322. done
  323. fi
  324. }
  325. ################################################################################
  326. # pidofproc() #
  327. # Usage: pidofproc [-p pidfile] pathname #
  328. # #
  329. # Purpose: This function returns one or more pid(s) for a particular daemon #
  330. # #
  331. # Inputs: -p pidfile, use the specified pidfile instead of pidof #
  332. # pathname, path to the specified program #
  333. # #
  334. # Return values (as defined by LSB status codes): #
  335. # 0 - Success (PIDs to stdout) #
  336. # 1 - Program is dead, PID file still exists (remaining PIDs output) #
  337. # 3 - Program is not running (no output) #
  338. ################################################################################
  339. pidofproc()
  340. {
  341. local pidfile
  342. local program
  343. local prefix
  344. local progname
  345. local pidlist
  346. local lpids
  347. local exitstatus="0"
  348. # Process arguments
  349. while true; do
  350. case "${1}" in
  351. -p)
  352. pidfile="${2}"
  353. shift 2
  354. ;;
  355. *)
  356. program="${1}"
  357. if [ -n "${2}" ]; then
  358. # Too many arguments
  359. # Since this is status, return unknown
  360. return 4
  361. else
  362. break
  363. fi
  364. ;;
  365. esac
  366. done
  367. # If a PID file is not specified, try and find one.
  368. if [ -z "${pidfile}" ]; then
  369. # Get the program's basename
  370. prefix=`echo "${program}" | sed 's/[^/]*$//'`
  371. if [ -z "${prefix}" ]; then
  372. progname="${program}"
  373. else
  374. progname=`echo "${program}" | sed "s@${prefix}@@"`
  375. fi
  376. # If a PID file exists with that name, assume that is it.
  377. if [ -e "/var/run/${progname}.pid" ]; then
  378. pidfile="/var/run/${progname}.pid"
  379. fi
  380. fi
  381. # If a PID file is set and exists, use it.
  382. if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
  383. # Use the value in the first line of the pidfile
  384. pidlist=`/bin/head -n1 "${pidfile}"`
  385. # This can optionally be written as 'sed 1q' to repalce 'head -n1'
  386. # should LFS move /bin/head to /usr/bin/head
  387. else
  388. # Use pidof
  389. pidlist=`pidof "${program}"`
  390. fi
  391. # Figure out if all listed PIDs are running.
  392. for pid in ${pidlist}; do
  393. kill -0 ${pid} 2> /dev/null
  394. if [ "${?}" -eq "0" ]; then
  395. lpids="${lpids}${pid} "
  396. else
  397. exitstatus="1"
  398. fi
  399. done
  400. if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
  401. return 3
  402. else
  403. echo "${lpids}"
  404. return "${exitstatus}"
  405. fi
  406. }
  407. ################################################################################
  408. # statusproc() #
  409. # Usage: statusproc [-p pidfile] pathname #
  410. # #
  411. # Purpose: This function prints the status of a particular daemon to stdout #
  412. # #
  413. # Inputs: -p pidfile, use the specified pidfile instead of pidof #
  414. # pathname, path to the specified program #
  415. # #
  416. # Return values: #
  417. # 0 - Status printed #
  418. # 1 - Input error. The daemon to check was not specified. #
  419. ################################################################################
  420. statusproc()
  421. {
  422. local pidfile
  423. local pidlist
  424. if [ "${#}" = "0" ]; then
  425. echo "Usage: statusproc [-p pidfle] {program}"
  426. exit 1
  427. fi
  428. # Process arguments
  429. while true; do
  430. case "${1}" in
  431. -p)
  432. pidfile="${2}"
  433. shift 2
  434. ;;
  435. *)
  436. if [ -n "${2}" ]; then
  437. echo "Too many arguments"
  438. return 1
  439. else
  440. break
  441. fi
  442. ;;
  443. esac
  444. done
  445. if [ -n "${pidfile}" ]; then
  446. pidlist=`pidofproc -p "${pidfile}" $@`
  447. else
  448. pidlist=`pidofproc $@`
  449. fi
  450. # Trim trailing blanks
  451. pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`
  452. base="${1##*/}"
  453. if [ -n "${pidlist}" ]; then
  454. /bin/echo -e "${INFO}${base} is running with Process" \
  455. "ID(s) ${pidlist}.${NORMAL}"
  456. else
  457. if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
  458. /bin/echo -e "${WARNING}${1} is not running but" \
  459. "/var/run/${base}.pid exists.${NORMAL}"
  460. else
  461. if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
  462. /bin/echo -e "${WARNING}${1} is not running" \
  463. "but ${pidfile} exists.${NORMAL}"
  464. else
  465. /bin/echo -e "${INFO}${1} is not running.${NORMAL}"
  466. fi
  467. fi
  468. fi
  469. }
  470. ################################################################################
  471. # timespec() #
  472. # #
  473. # Purpose: An internal utility function to format a timestamp #
  474. # a boot log file. Sets the STAMP variable. #
  475. # #
  476. # Return value: Not used #
  477. ################################################################################
  478. timespec()
  479. {
  480. STAMP="$(echo `date +"%b %d %T %:z"` `hostname`) "
  481. return 0
  482. }
  483. ################################################################################
  484. # log_success_msg() #
  485. # Usage: log_success_msg ["message"] #
  486. # #
  487. # Purpose: Print a successful status message to the screen and #
  488. # a boot log file. #
  489. # #
  490. # Inputs: $@ - Message #
  491. # #
  492. # Return values: Not used #
  493. ################################################################################
  494. log_success_msg()
  495. {
  496. /bin/echo -n -e "${BMPREFIX}${@}"
  497. /bin/echo -e "${CURS_ZERO}${SUCCESS_PREFIX}${SET_COL}${SUCCESS_SUFFIX}"
  498. # Strip non-printable characters from log file
  499. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  500. timespec
  501. /bin/echo -e "${STAMP} ${logmessage} OK" >> ${BOOTLOG}
  502. return 0
  503. }
  504. log_success_msg2()
  505. {
  506. /bin/echo -n -e "${BMPREFIX}${@}"
  507. /bin/echo -e "${CURS_ZERO}${SUCCESS_PREFIX}${SET_COL}${SUCCESS_SUFFIX}"
  508. echo " OK" >> ${BOOTLOG}
  509. return 0
  510. }
  511. ################################################################################
  512. # log_failure_msg() #
  513. # Usage: log_failure_msg ["message"] #
  514. # #
  515. # Purpose: Print a failure status message to the screen and #
  516. # a boot log file. #
  517. # #
  518. # Inputs: $@ - Message #
  519. # #
  520. # Return values: Not used #
  521. ################################################################################
  522. log_failure_msg()
  523. {
  524. /bin/echo -n -e "${BMPREFIX}${@}"
  525. /bin/echo -e "${CURS_ZERO}${FAILURE_PREFIX}${SET_COL}${FAILURE_SUFFIX}"
  526. # Strip non-printable characters from log file
  527. timespec
  528. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  529. /bin/echo -e "${STAMP} ${logmessage} FAIL" >> ${BOOTLOG}
  530. return 0
  531. }
  532. log_failure_msg2()
  533. {
  534. /bin/echo -n -e "${BMPREFIX}${@}"
  535. /bin/echo -e "${CURS_ZERO}${FAILURE_PREFIX}${SET_COL}${FAILURE_SUFFIX}"
  536. echo "FAIL" >> ${BOOTLOG}
  537. return 0
  538. }
  539. ################################################################################
  540. # log_warning_msg() #
  541. # Usage: log_warning_msg ["message"] #
  542. # #
  543. # Purpose: Print a warning status message to the screen and #
  544. # a boot log file. #
  545. # #
  546. # Return values: Not used #
  547. ################################################################################
  548. log_warning_msg()
  549. {
  550. /bin/echo -n -e "${BMPREFIX}${@}"
  551. /bin/echo -e "${CURS_ZERO}${WARNING_PREFIX}${SET_COL}${WARNING_SUFFIX}"
  552. # Strip non-printable characters from log file
  553. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  554. timespec
  555. /bin/echo -e "${STAMP} ${logmessage} WARN" >> ${BOOTLOG}
  556. return 0
  557. }
  558. log_skip_msg()
  559. {
  560. /bin/echo -n -e "${BMPREFIX}${@}"
  561. /bin/echo -e "${CURS_ZERO}${SKIP_PREFIX}${SET_COL}${SKIP_SUFFIX}"
  562. # Strip non-printable characters from log file
  563. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  564. /bin/echo "SKIP" >> ${BOOTLOG}
  565. return 0
  566. }
  567. ################################################################################
  568. # log_info_msg() #
  569. # Usage: log_info_msg message #
  570. # #
  571. # Purpose: Print an information message to the screen and #
  572. # a boot log file. Does not print a trailing newline character. #
  573. # #
  574. # Return values: Not used #
  575. ################################################################################
  576. log_info_msg()
  577. {
  578. /bin/echo -n -e "${BMPREFIX}${@}"
  579. # Strip non-printable characters from log file
  580. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  581. timespec
  582. /bin/echo -n -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
  583. return 0
  584. }
  585. log_info_msg2()
  586. {
  587. /bin/echo -n -e "${@}"
  588. # Strip non-printable characters from log file
  589. logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
  590. /bin/echo -n -e "${logmessage}" >> ${BOOTLOG}
  591. return 0
  592. }
  593. ################################################################################
  594. # evaluate_retval() #
  595. # Usage: Evaluate a return value and print success or failyure as appropriate #
  596. # #
  597. # Purpose: Convenience function to terminate an info message #
  598. # #
  599. # Return values: Not used #
  600. ################################################################################
  601. evaluate_retval()
  602. {
  603. local error_value="${?}"
  604. if [ ${error_value} = 0 ]; then
  605. log_success_msg2
  606. else
  607. log_failure_msg2
  608. fi
  609. }
  610. ################################################################################
  611. # check_signal() #
  612. # Usage: check_signal [ -{signal} | {signal} ] #
  613. # #
  614. # Purpose: Check for a valid signal. This is not defined by any LSB draft, #
  615. # however, it is required to check the signals to determine if the #
  616. # signals chosen are invalid arguments to the other functions. #
  617. # #
  618. # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
  619. # #
  620. # Return values: #
  621. # 0 - Success (signal is valid #
  622. # 1 - Signal is not valid #
  623. ################################################################################
  624. check_signal()
  625. {
  626. local valsig
  627. # Add error handling for invalid signals
  628. valsig="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
  629. valsig="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
  630. valsig="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
  631. valsig="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
  632. valsig="${valsig} -11 -13 -14 -15"
  633. echo "${valsig}" | grep -- " ${1} " > /dev/null
  634. if [ "${?}" -eq "0" ]; then
  635. return 0
  636. else
  637. return 1
  638. fi
  639. }
  640. ################################################################################
  641. # check_sig_type() #
  642. # Usage: check_signal [ -{signal} | {signal} ] #
  643. # #
  644. # Purpose: Check if signal is a program termination signal or a control signal #
  645. # This is not defined by any LSB draft, however, it is required to #
  646. # check the signals to determine if they are intended to end a #
  647. # program or simply to control it. #
  648. # #
  649. # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
  650. # #
  651. # Return values: #
  652. # 0 - Signal is used for program termination #
  653. # 1 - Signal is used for program control #
  654. ################################################################################
  655. check_sig_type()
  656. {
  657. local valsig
  658. # The list of termination signals (limited to generally used items)
  659. valsig="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"
  660. echo "${valsig}" | grep -- " ${1} " > /dev/null
  661. if [ "${?}" -eq "0" ]; then
  662. return 0
  663. else
  664. return 1
  665. fi
  666. }
  667. ################################################################################
  668. # wait_for_user() #
  669. # #
  670. # Purpose: Wait for the user to respond if not a headless system #
  671. # #
  672. ################################################################################
  673. wait_for_user()
  674. {
  675. # Wait for the user by default
  676. [ "${HEADLESS=0}" = "0" ] && read ENTER
  677. return 0
  678. }
  679. ################################################################################
  680. # is_true() #
  681. # #
  682. # Purpose: Utility to test if a variable is true | yes | 1 #
  683. # #
  684. ################################################################################
  685. is_true()
  686. {
  687. [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] || [ "$1" = "y" ] ||
  688. [ "$1" = "t" ]
  689. }
  690. # End /lib/lsb/init-functions