init-functions 25 KB

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