Add statusproc back to bootscripts.
[linux_from_scratch.git] / bootscripts / lfs / lib / services / init-functions
blob4ba623adce9c95e6c49ce94a7d7181ed1d93b26e
1 #!/bin/sh
2 ########################################################################
3 #
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
12 # Version : LFS 7.0
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
23 umask 022
24 export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
26 ## Screen Dimensions
27 # Find current screen size
28 if [ -z "${COLUMNS}" ]; then
29 COLUMNS=$(stty size)
30 COLUMNS=${COLUMNS##* }
33 # When using remote connections, such as a serial port, stty size returns 0
34 if [ "${COLUMNS}" = "0" ]; then
35 COLUMNS=80
38 ## Measurements for positioning result messages
39 COL=$((${COLUMNS} - 8))
40 WCOL=$((${COL} - 2))
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
64 KILLDELAY=3
66 # Set any user specified environment variables e.g. HEADLESS
67 [ -r /etc/sysconfig/rc.site ] && . /etc/sysconfig/rc.site
69 ################################################################################
70 # start_daemon() #
71 # Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
72 # #
73 # Purpose: This runs the specified program as a daemon #
74 # #
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) #
80 # #
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 ################################################################################
87 start_daemon()
89 local force=""
90 local nice="0"
91 local pidfile=""
92 local pidlist=""
93 local retval=""
95 # Process arguments
96 while true
98 case "${1}" in
101 force="1"
102 shift 1
106 nice="${2}"
107 shift 2
111 pidfile="${2}"
112 shift 2
116 return 2
120 program="${1}"
121 break
123 esac
124 done
126 # Check for a valid program
127 if [ ! -e "${program}" ]; then return 5; fi
129 # Execute
130 if [ -z "${force}" ]; then
131 if [ -z "${pidfile}" ]; then
132 # Determine the pid by discovery
133 pidlist=`pidofproc "${1}"`
134 retval="${?}"
135 else
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}"`
140 retval="${?}"
143 # Return a value ONLY
144 # It is the init script's (or distribution's functions) responsibilty
145 # to log messages!
146 case "${retval}" in
149 # Program is already running correctly, this is a
150 # succesful start.
151 return 0
155 # Program is not running, but an invalid pid file exists
156 # remove the pid file and continue
157 rm -f "${pidfile}"
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.
168 return 1
170 esac
173 # Do the start!
175 nice -n "${nice}" "${@}"
178 ################################################################################
179 # killproc() #
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 #
191 # successfully #
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 ################################################################################
197 killproc()
199 local pidfile
200 local program
201 local prefix
202 local progname
203 local signal="-TERM"
204 local fallback="-KILL"
205 local nosig
206 local pidlist
207 local retval
208 local pid
209 local delay="30"
210 local piddead
211 local dtime
213 # Process arguments
214 while true; do
215 case "${1}" in
217 pidfile="${2}"
218 shift 2
222 program="${1}"
223 if [ -n "${2}" ]; then
224 signal="${2}"
225 fallback=""
226 else
227 nosig=1
230 # Error on additional arguments
231 if [ -n "${3}" ]; then
232 return 2
233 else
234 break
237 esac
238 done
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
247 # Get a list of pids
248 if [ -z "${pidfile}" ]; then
249 # determine the pid by discovery
250 pidlist=`pidofproc "${1}"`
251 retval="${?}"
252 else
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}"`
257 retval="${?}"
260 # Return a value ONLY
261 # It is the init script's (or distribution's functions) responsibilty
262 # to log messages!
263 case "${retval}" in
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.
273 rm -f "${pidfile}"
275 # This is only a success if no signal was passed.
276 if [ -n "${nosig}" ]; then
277 return 0
278 else
279 return 7
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
287 return 0
288 else
289 return 7
294 # Others as returned by status values shall not be interpreted
295 # and returned as an unspecified error.
296 return 1
298 esac
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
306 # signal was given)
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
316 continue
317 else
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
326 sleep 0.1
327 delay="$(( ${delay} - 1 ))"
328 done
330 # If a fallback is set, and program is still running, then
331 # use the fallback
332 if [ -n "${fallback}" -a "${piddead}" != "1" ]; then
333 kill "${fallback}" "${pid}" 2> /dev/null
334 sleep 1
335 # Check again, and fail if still running
336 kill -0 "${pid}" 2> /dev/null && return 1
337 else
338 # just check one last time and if still alive, fail
339 sleep 1
340 kill -0 "${pid}" 2> /dev/null && return 1
343 done
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
355 else
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
366 done
370 ################################################################################
371 # pidofproc() #
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 ################################################################################
384 pidofproc()
386 local pidfile
387 local program
388 local prefix
389 local progname
390 local pidlist
391 local lpids
392 local exitstatus="0"
394 # Process arguments
395 while true; do
396 case "${1}" in
399 pidfile="${2}"
400 shift 2
404 program="${1}"
405 if [ -n "${2}" ]; then
406 # Too many arguments
407 # Since this is status, return unknown
408 return 4
409 else
410 break
413 esac
414 done
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}"
423 else
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
440 else
441 # Use pidof
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} "
451 else
452 exitstatus="1"
454 done
456 if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
457 return 3
458 else
459 echo "${lpids}"
460 return "${exitstatus}"
464 ################################################################################
465 # statusproc() #
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 #
473 # Return values: #
474 # 0 - Status printed #
475 # 1 - Input error. The daemon to check was not specified. #
476 ################################################################################
477 statusproc()
479 if [ "${#}" = "0" ]; then
480 echo "Usage: statusproc {program}"
481 exit 1
484 if [ -z "${PIDFILE}" ]; then
485 pidlist=`pidofproc -p "${PIDFILE}" $@`
486 else
487 pidlist=`pidofproc $@`
490 # Trim trailing blanks
491 pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`
493 base="${1##*/}"
495 if [ -n "${pidlist}" ]; then
496 echo -e "${INFO}${base} is running with Process" \
497 "ID(s) ${pidlist}.${NORMAL}"
498 else
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}"
502 else
503 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
504 echo -e "${WARNING}${1} is not running" \
505 "but ${PIDFILE} exists.${NORMAL}"
506 else
507 echo -e "${INFO}${1} is not running.${NORMAL}"
513 ################################################################################
514 # timespec() #
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 ################################################################################
521 timespec()
523 STAMP="$(echo `date +"%b %d %T %:z"` `hostname`) "
524 return 0
527 ################################################################################
528 # log_success_msg() #
529 # Usage: log_success_msg ["message"] #
531 # Purpose: Print a successful status message to the screen and #
532 # a boot log file. #
534 # Inputs: $@ - Message #
536 # Return values: Not used #
537 ################################################################################
538 log_success_msg()
540 echo -n -e "${@}"
541 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
543 timespec
544 echo -e "${STAMP} ${@} OK" >> ${BOOTLOG}
545 return 0
548 log_success_msg2()
550 echo -n -e "${@}"
551 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
553 echo " OK" >> ${BOOTLOG}
554 return 0
557 ################################################################################
558 # log_failure_msg() #
559 # Usage: log_failure_msg ["message"] #
561 # Purpose: Print a failure status message to the screen and #
562 # a boot log file. #
564 # Inputs: $@ - Message #
566 # Return values: Not used #
567 ################################################################################
568 log_failure_msg()
570 echo -n -e "${@}"
571 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
573 timespec
574 echo -e "${STAMP} ${@} FAIL" >> ${BOOTLOG}
575 return 0
578 log_failure_msg2()
580 echo -n -e "${@}"
581 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
583 echo "FAIL" >> ${BOOTLOG}
584 return 0
587 ################################################################################
588 # log_warning_msg() #
589 # Usage: log_warning_msg ["message"] #
591 # Purpose: Print a warning status message to the screen and #
592 # a boot log file. #
594 # Return values: Not used #
595 ################################################################################
596 log_warning_msg()
598 echo -n -e "${@}"
599 echo -e "${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
601 timespec
602 echo -e "${STAMP} ${@} WARN" >> ${BOOTLOG}
603 return 0
606 ################################################################################
607 # log_info_msg() #
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 ################################################################################
615 log_info_msg()
617 echo -n -e "${@}"
619 timespec
620 echo -n -e "${STAMP} ${@}" >> ${BOOTLOG}
621 return 0
624 log_info_msg2()
626 echo -n -e "${@}"
628 echo -n -e "${@}" >> ${BOOTLOG}
629 return 0
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 ################################################################################
640 evaluate_retval()
642 local error_value="${?}"
644 if [ ${error_value} = 0 ]; then
645 log_success_msg2
646 else
647 log_failure_msg2
651 ################################################################################
652 # check_signal() #
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} #
661 # Return values: #
662 # 0 - Success (signal is valid #
663 # 1 - Signal is not valid #
664 ################################################################################
665 check_signal()
667 local valsig
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
679 return 0
680 else
681 return 1
685 ################################################################################
686 # check_sig_type() #
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} #
696 # Return values: #
697 # 0 - Signal is used for program termination #
698 # 1 - Signal is used for program control #
699 ################################################################################
700 check_sig_type()
702 local valsig
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
710 return 0
711 else
712 return 1
716 ################################################################################
717 # wait_for_user() #
719 # Purpose: Wait for the user to respond if not a headless system #
721 ################################################################################
722 wait_for_user()
724 # Wait for the user by default
725 [ "${HEADLESS=0}" = "0" ] && read ENTER
726 return 0
729 # End /lib/lsb/init-functions