init-functions 29 KB

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