2 ########################################################################
4 # Begin /lib/lsb/init-funtions
6 # Description : Run Level Control Functions
8 # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
9 # : DJ Lucas - dj@linuxfromscratch.org
10 # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
14 # Notes : With code based on Matthias Benkmann's simpleinit-msb
15 # http://winterdrache.de/linux/newboot/index.html
17 # The file should be located in /lib/lsb
19 ########################################################################
21 ## Environmental setup
22 # Setup default values for environment
24 export PATH
="/bin:/usr/bin:/sbin:/usr/sbin"
27 # Find current screen size
28 if [ -z "${COLUMNS}" ]; then
30 COLUMNS
=${COLUMNS##* }
33 # When using remote connections, such as a serial port, stty size returns 0
34 if [ "${COLUMNS}" = "0" ]; then
38 ## Measurements for positioning result messages
39 COL
=$
((${COLUMNS} - 8))
42 ## Set Cursor Position Commands, used via echo
43 SET_COL
="\\033[${COL}G" # at the $COL char
44 SET_WCOL
="\\033[${WCOL}G" # at the $WCOL char
45 CURS_UP
="\\033[1A\\033[0G" # Up one line, at the 0'th char
47 ## Set color commands, used via echo
48 # Please consult `man console_codes for more information
49 # under the "ECMA-48 Set Graphics Rendition" section
51 # Warning: when switching from a 8bit to a 9bit font,
52 # the linux console will reinterpret the bold (1;) to
53 # the top 256 glyphs of the 9bit font. This does
54 # not affect framebuffer consoles
56 NORMAL
="\\033[0;39m" # Standard console grey
57 SUCCESS
="\\033[1;32m" # Success is green
58 WARNING
="\\033[1;33m" # Warnings are yellow
59 FAILURE
="\\033[1;31m" # Failures are red
60 INFO
="\\033[1;36m" # Information is light cyan
61 BRACKET
="\\033[1;34m" # Brackets are blue
63 BOOTLOG
=/run
/var
/bootlog
66 # Set any user specified environment variables e.g. HEADLESS
67 [ -r /etc
/sysconfig
/rc.site
] && .
/etc
/sysconfig
/rc.site
69 ################################################################################
71 # Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
73 # Purpose: This runs the specified program as a daemon #
75 # Inputs: -f: (force) run the program even if it is already running. #
76 # -n nicelevel: specify a nice level. See 'man nice(1)'. #
77 # -p pidfile: use the specified file to determine PIDs. #
78 # pathname: the complete path to the specified program #
79 # args: additional arguments passed to the program (pathname) #
81 # Return values (as defined by LSB exit codes): #
82 # 0 - program is running or service is OK #
83 # 1 - generic or unspecified error #
84 # 2 - invalid or excessive argument(s) #
85 # 5 - program is not installed #
86 ################################################################################
126 # Check for a valid program
127 if [ ! -e "${program}" ]; then return 5; fi
130 if [ -z "${force}" ]; then
131 if [ -z "${pidfile}" ]; then
132 # Determine the pid by discovery
133 pidlist
=`pidofproc "${1}"`
136 # The PID file contains the needed PIDs
137 # Note that by LSB requirement, the path must be given to pidofproc,
138 # however, it is not used by the current implementation or standard.
139 pidlist
=`pidofproc -p "${pidfile}" "${1}"`
143 # Return a value ONLY
144 # It is the init script's (or distribution's functions) responsibilty
149 # Program is already running correctly, this is a
155 # Program is not running, but an invalid pid file exists
156 # remove the pid file and continue
161 # Program is not running and no pidfile exists
162 # do nothing here, let start_deamon continue.
166 # Others as returned by status values shall not be interpreted
167 # and returned as an unspecified error.
175 nice
-n "${nice}" "${@}"
178 ################################################################################
180 # Usage: killproc [-p pidfile] pathname [signal] #
182 # Purpose: Send control signals to running processes #
184 # Inputs: -p pidfile, uses the specified pidfile #
185 # pathname, pathname to the specified program #
186 # signal, send this signal to pathname #
188 # Return values (as defined by LSB exit codes): #
189 # 0 - program (pathname) has stopped/is already stopped or a #
190 # running program has been sent specified signal and stopped #
192 # 1 - generic or unspecified error #
193 # 2 - invalid or excessive argument(s) #
194 # 5 - program is not installed #
195 # 7 - program is not running and a signal was supplied #
196 ################################################################################
204 local fallback
="-KILL"
223 if [ -n "${2}" ]; then
230 # Error on additional arguments
231 if [ -n "${3}" ]; then
240 # Check for a valid program
241 if [ ! -e "${program}" ]; then return 5; fi
243 # Check for a valid signal
244 check_signal
"${signal}"
245 if [ "${?}" -ne "0" ]; then return 2; fi
248 if [ -z "${pidfile}" ]; then
249 # determine the pid by discovery
250 pidlist
=`pidofproc "${1}"`
253 # The PID file contains the needed PIDs
254 # Note that by LSB requirement, the path must be given to pidofproc,
255 # however, it is not used by the current implementation or standard.
256 pidlist
=`pidofproc -p "${pidfile}" "${1}"`
260 # Return a value ONLY
261 # It is the init script's (or distribution's functions) responsibilty
266 # Program is running correctly
267 # Do nothing here, let killproc continue.
271 # Program is not running, but an invalid pid file exists
272 # Remove the pid file.
275 # This is only a success if no signal was passed.
276 if [ -n "${nosig}" ]; then
284 # Program is not running and no pidfile exists
285 # This is only a success if no signal was passed.
286 if [ -n "${nosig}" ]; then
294 # Others as returned by status values shall not be interpreted
295 # and returned as an unspecified error.
300 # Perform different actions for exit signals and control signals
301 check_sig_type
"${signal}"
303 if [ "${?}" -eq "0" ]; then # Signal is used to terminate the program
305 # Account for empty pidlist (pid file still exists and no
307 if [ "${pidlist}" != "" ]; then
309 # Kill the list of pids
310 for pid
in ${pidlist}; do
312 kill -0 "${pid}" 2> /dev
/null
314 if [ "${?}" -ne "0" ]; then
315 # Process is dead, continue to next and assume all is well
318 kill "${signal}" "${pid}" 2> /dev
/null
320 # Wait up to ${delay}/10 seconds to for "${pid}" to
321 # terminate in 10ths of a second
323 while [ "${delay}" -ne "0" ]; do
324 kill -0 "${pid}" 2> /dev
/null || piddead
="1"
325 if [ "${piddead}" = "1" ]; then break; fi
327 delay
="$(( ${delay} - 1 ))"
330 # If a fallback is set, and program is still running, then
332 if [ -n "${fallback}" -a "${piddead}" != "1" ]; then
333 kill "${fallback}" "${pid}" 2> /dev
/null
335 # Check again, and fail if still running
336 kill -0 "${pid}" 2> /dev
/null
&& return 1
338 # just check one last time and if still alive, fail
340 kill -0 "${pid}" 2> /dev
/null
&& return 1
346 # Check for and remove stale PID files.
347 if [ -z "${pidfile}" ]; then
348 # Find the basename of $program
349 prefix
=`echo "${program}" | sed 's/[^/]*$//'`
350 progname
=`echo "${program}" | sed "s@${prefix}@@"`
352 if [ -e "/var/run/${progname}.pid" ]; then
353 rm -f "/var/run/${progname}.pid" 2> /dev
/null
356 if [ -e "${pidfile}" ]; then rm -f "${pidfile}" 2> /dev
/null
; fi
359 # For signals that do not expect a program to exit, simply
360 # let kill do it's job, and evaluate kills return for value
362 else # check_sig_type - signal is not used to terminate program
363 for pid
in ${pidlist}; do
364 kill "${signal}" "${pid}"
365 if [ "${?}" -ne "0" ]; then return 1; fi
370 ################################################################################
372 # Usage: pidofproc [-p pidfile] pathname #
374 # Purpose: This function returns one or more pid(s) for a particular daemon #
376 # Inputs: -p pidfile, use the specified pidfile instead of pidof #
377 # pathname, path to the specified program #
379 # Return values (as defined by LSB status codes): #
380 # 0 - Success (PIDs to stdout) #
381 # 1 - Program is dead, PID file still exists (remaining PIDs output) #
382 # 3 - Program is not running (no output) #
383 ################################################################################
405 if [ -n "${2}" ]; then
407 # Since this is status, return unknown
416 # If a PID file is not specified, try and find one.
417 if [ -z "${pidfile}" ]; then
418 # Get the program's basename
419 prefix
=`echo "${program}" | sed 's/[^/]*$//'`
421 if [ -z "${prefix}" ]; then
422 progname
="${program}"
424 progname
=`echo "${program}" | sed "s@${prefix}@@"`
427 # If a PID file exists with that name, assume that is it.
428 if [ -e "/var/run/${progname}.pid" ]; then
429 pidfile
="/var/run/${progname}.pid"
433 # If a PID file is set and exists, use it.
434 if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
436 # Use the value in the first line of the pidfile
437 pidlist
=`/bin/head -n1 "${pidfile}"`
438 # This can optionally be written as 'sed 1q' to repalce 'head -n1'
439 # should LFS move /bin/head to /usr/bin/head
442 pidlist
=`pidof "${program}"`
445 # Figure out if all listed PIDs are running.
446 for pid
in ${pidlist}; do
447 kill -0 ${pid} 2> /dev
/null
449 if [ "${?}" -eq "0" ]; then
450 lpids
="${pids}${pid} "
456 if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
460 return "${exitstatus}"
464 ################################################################################
466 # Usage: statusproc [-p pidfile] pathname #
468 # Purpose: This function prints the status of a particular daemon to stdout #
470 # Inputs: -p pidfile, use the specified pidfile instead of pidof #
471 # pathname, path to the specified program #
474 # 0 - Status printed #
475 # 1 - Input error. The daemon to check was not specified. #
476 ################################################################################
479 if [ "${#}" = "0" ]; then
480 echo "Usage: statusproc {program}"
484 if [ -z "${PIDFILE}" ]; then
485 pidlist
=`pidofproc -p "${PIDFILE}" $@`
487 pidlist
=`pidofproc $@`
490 # Trim trailing blanks
491 pidlist
=`echo "${pidlist}" | sed -r 's/ +$//'`
495 if [ -n "${pidlist}" ]; then
496 echo -e "${INFO}${base} is running with Process" \
497 "ID(s) ${pidlist}.${NORMAL}"
499 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
500 echo -e "${WARNING}${1} is not running but" \
501 "/var/run/${base}.pid exists.${NORMAL}"
503 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
504 echo -e "${WARNING}${1} is not running" \
505 "but ${PIDFILE} exists.${NORMAL}"
507 echo -e "${INFO}${1} is not running.${NORMAL}"
513 ################################################################################
516 # Purpose: An internal utility function to format a timestamp #
517 # a boot log file. Sets the STAMP variable. #
519 # Return value: Not used #
520 ################################################################################
523 STAMP
="$(echo `date +"%b
%d
%T
%:z
"` `hostname`) "
527 ################################################################################
528 # log_success_msg() #
529 # Usage: log_success_msg ["message"] #
531 # Purpose: Print a successful status message to the screen and #
534 # Inputs: $@ - Message #
536 # Return values: Not used #
537 ################################################################################
541 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
544 echo -e "${STAMP} ${@} OK" >> ${BOOTLOG}
551 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
553 echo " OK
" >> ${BOOTLOG}
557 ################################################################################
558 # log_failure_msg() #
559 # Usage: log_failure_msg ["message
"] #
561 # Purpose: Print a failure status message to the screen and #
564 # Inputs: $@ - Message #
566 # Return values: Not used #
567 ################################################################################
571 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
574 echo -e "${STAMP} ${@} FAIL" >> ${BOOTLOG}
581 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
583 echo "FAIL" >> ${BOOTLOG}
587 ################################################################################
588 # log_warning_msg() #
589 # Usage: log_warning_msg ["message"] #
591 # Purpose: Print a warning status message to the screen and #
594 # Return values: Not used #
595 ################################################################################
599 echo -e "${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
602 echo -e "${STAMP} ${@} WARN" >> ${BOOTLOG}
606 ################################################################################
608 # Usage: log_info_msg message #
610 # Purpose: Print an information message to the screen and #
611 # a boot log file. Does not print a trailing newline character. #
613 # Return values: Not used #
614 ################################################################################
620 echo -n -e "${STAMP} ${@}" >> ${BOOTLOG}
628 echo -n -e "${@}" >> ${BOOTLOG}
632 ################################################################################
633 # evaluate_retval() #
634 # Usage: Evaluate a return value and print success or failyure as appropriate #
636 # Purpose: Convenience function to terminate an info message #
638 # Return values: Not used #
639 ################################################################################
642 local error_value
="${?}"
644 if [ ${error_value} = 0 ]; then
651 ################################################################################
653 # Usage: check_signal [ -{signal} | {signal} ] #
655 # Purpose: Check for a valid signal. This is not defined by any LSB draft, #
656 # however, it is required to check the signals to determine if the #
657 # signals chosen are invalid arguments to the other functions. #
659 # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
662 # 0 - Success (signal is valid #
663 # 1 - Signal is not valid #
664 ################################################################################
669 # Add error handling for invalid signals
670 valsig
="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
671 valsig
="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
672 valsig
="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
673 valsig
="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
674 valsig
="${valsig} -11 -13 -14 -15"
676 echo "${valsig}" |
grep -- " ${1} " > /dev
/null
678 if [ "${?}" -eq "0" ]; then
685 ################################################################################
687 # Usage: check_signal [ -{signal} | {signal} ] #
689 # Purpose: Check if signal is a program termination signal or a control signal #
690 # This is not defined by any LSB draft, however, it is required to #
691 # check the signals to determine if they are intended to end a #
692 # program or simply to control it. #
694 # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
697 # 0 - Signal is used for program termination #
698 # 1 - Signal is used for program control #
699 ################################################################################
704 # The list of termination signals (limited to generally used items)
705 valsig
="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"
707 echo "${valsig}" |
grep -- " ${1} " > /dev
/null
709 if [ "${?}" -eq "0" ]; then
716 ################################################################################
719 # Purpose: Wait for the user to respond if not a headless system #
721 ################################################################################
724 # Wait for the user by default
725 [ "${HEADLESS=0}" = "0" ] && read ENTER
729 # End /lib/lsb/init-functions