2 ########################################################################
5 # Description : Run Level Control Functions
7 # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
8 # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
12 # Notes : With code based on Matthias Benkmann's simpleinit-msb
13 # http://winterdrache.de/linux/newboot/index.html
15 # The file should be located in /lib/boot
17 ########################################################################
19 # Set any needed environment variables e.g. HEADLESS
20 [ -r /etc
/sysconfig
/init_params
] && .
/etc
/sysconfig
/init_params
22 ## Environmental setup
23 # Setup default values for environment
25 export PATH
="/bin:/usr/bin:/sbin:/usr/sbin"
27 # Signal sent to running processes to refresh their configuration
30 # Number of seconds between STOPSIG and FALLBACK when stopping processes
34 # Find current screen size
35 if [ -z "${COLUMNS}" ]; then
37 COLUMNS
=${COLUMNS##* }
40 # When using remote connections, such as a serial port, stty size returns 0
41 if [ "${COLUMNS}" = "0" ]; then
45 ## Measurements for positioning result messages
46 COL
=$
((${COLUMNS} - 8))
49 ## Provide an echo that supports -e and -n
50 # If formatting is needed, $ECHO should be used
51 case "`echo -e -n test`" in
60 ## Set Cursor Position Commands, used via $ECHO
61 SET_COL
="\\033[${COL}G" # at the $COL char
62 SET_WCOL
="\\033[${WCOL}G" # at the $WCOL char
63 CURS_UP
="\\033[1A\\033[0G" # Up one line, at the 0'th char
65 ## Set color commands, used via $ECHO
66 # Please consult `man console_codes for more information
67 # under the "ECMA-48 Set Graphics Rendition" section
69 # Warning: when switching from a 8bit to a 9bit font,
70 # the linux console will reinterpret the bold (1;) to
71 # the top 256 glyphs of the 9bit font. This does
72 # not affect framebuffer consoles
73 NORMAL
="\\033[0;39m" # Standard console grey
74 SUCCESS
="\\033[1;32m" # Success is green
75 WARNING
="\\033[1;33m" # Warnings are yellow
76 FAILURE
="\\033[1;31m" # Failures are red
77 INFO
="\\033[1;36m" # Information is light cyan
78 BRACKET
="\\033[1;34m" # Brackets are blue
80 STRING_LENGTH
="0" # the length of the current message
82 #*******************************************************************************
83 # Function - boot_mesg()
85 # Purpose: Sending information from bootup scripts to the console
87 # Inputs: $1 is the message
88 # $2 is the colorcode for the console
90 # Outputs: Standard Output
92 # Dependencies: - sed for parsing strings.
93 # - grep for counting string length.
96 #*******************************************************************************
109 echo "Unknown Option: ${1}"
118 ## Figure out the length of what is to be printed to be used
119 ## for warning messages.
120 STRING_LENGTH
=$
((${#1} + 1))
122 # Print the message to the screen
123 ${ECHO} ${ECHOPARM} -e "${2}${1}"
126 ${ECHO} ${ECHOPARM} -e "${2}${1}" >> /run
/var
/bootlog
131 # Reset STRING_LENGTH for next message
137 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
138 ${ECHO} -e "${NORMAL}"
140 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]" >> /run
/var
/bootlog
141 ${ECHO} -e "${NORMAL}" >> /run
/var
/bootlog
146 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
147 ${ECHO} -e "${NORMAL}"
149 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]" >> /run
/var
/bootlog
150 ${ECHO} -e "${NORMAL}" >> /run
/var
/bootlog
155 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
156 ${ECHO} -e "${NORMAL}"
158 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]" >> /run
/var
/bootlog
159 ${ECHO} -e "${NORMAL}" >> /run
/var
/bootlog
164 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} SKIP ${BRACKET}]"
165 ${ECHO} -e "${NORMAL}"
167 ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} SKIP ${BRACKET}]" >> /run
/var
/bootlog
168 ${ECHO} -e "${NORMAL}" >> /run
/var
/bootlog
173 # Wait for the user by default
174 [ "${HEADLESS=0}" = "0" ] && read ENTER
181 if [ ${error_value} = 0 ]; then
187 # This prevents the 'An Unexpected Error Has Occurred' from trivial
194 if [ "${#}" = "0" ]; then
195 echo "Usage: ${0} {success|warning|failure}"
206 # Leave this extra case in because old scripts
207 # may call it this way.
210 ${ECHO} -e -n "${CURS_UP}"
211 ${ECHO} -e -n "\\033[${STRING_LENGTH}G "
212 boot_mesg
"Already running." ${WARNING}
216 ${ECHO} -e -n "${CURS_UP}"
217 ${ECHO} -e -n "\\033[${STRING_LENGTH}G "
218 boot_mesg
"Not running." ${WARNING}
222 ${ECHO} -e -n "${CURS_UP}"
223 ${ECHO} -e -n "\\033[${STRING_LENGTH}G "
224 boot_mesg
"Not available." ${WARNING}
228 # This is how it is supposed to
256 log_failure_msg
"Unknown Option: ${1}"
265 if [ "${#}" -lt "1" ]; then
266 log_failure_msg
"Usage: reloadproc [-p pidfile] pathname"
270 # This will ensure compatibility with previous LFS Bootscripts
271 if [ -n "${PIDFILE}" ]; then
275 # Is the process running?
276 if [ -z "${pidfile}" ]; then
279 pidofproc
-s -p "${pidfile}" "${1}"
282 # Warn about stale pid file
283 if [ "$?" = 1 ]; then
284 boot_mesg
-n "Removing stale pid file: ${pidfile}. " ${WARNING}
288 if [ -n "${pidlist}" ]; then
289 for pid
in ${pidlist}
291 kill -"${RELOADSIG}" "${pid}" || failure
="1"
298 boot_mesg
"Process ${1} not running." ${WARNING}
317 log_failure_msg
"Unknown Option: ${1}"
326 if [ "${#}" != "1" ]; then
328 log_failure_msg
"Usage: statusproc [-p pidfile] pathname"
332 # Get the process basename
335 # This will ensure compatibility with previous LFS Bootscripts
336 if [ -n "${PIDFILE}" ]; then
340 # Is the process running?
341 if [ -z "${pidfile}" ]; then
344 pidofproc
-s -p "${pidfile}" "${1}"
347 # Store the return status
350 if [ -n "${pidlist}" ]; then
351 ${ECHO} -e "${INFO}${base} is running with Process
"\
352 "ID
(s
) ${pidlist}.
${NORMAL}"
354 if [ -n "${base}" -a -e "/var
/run
/${base}.pid
" ]; then
355 ${ECHO} -e "${WARNING}${1} is not running but"\
356 "/var/run/${base}.pid exists.${NORMAL}"
358 if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
359 ${ECHO} -e "${WARNING}${1} is not running
"\
360 "but
${pidfile} exists.
${NORMAL}"
362 ${ECHO} -e "${INFO}${1} is not running.${NORMAL}"
367 # Return the status from pidofproc
371 # The below functions are documented in the LSB-generic 2.1.0
373 #*******************************************************************************
374 # Function - pidofproc [-s] [-p pidfile] pathname
376 # Purpose: This function returns one or more pid(s) for a particular daemon
378 # Inputs: -p pidfile, use the specified pidfile instead of pidof
379 # pathname, path to the specified program
381 # Outputs: return 0 - Success, pid's in stdout
382 # return 1 - Program is dead, pidfile exists
383 # return 2 - Invalid or excessive number of arguments,
385 # return 3 - Program is not running
387 # Dependencies: pidof, echo, head
389 # Todo: Remove dependency on head
390 # This replaces getpids
391 # Test changes to pidof
393 #*******************************************************************************
409 # Added for legacy opperation of getpids
410 # eliminates several '> /dev/null'
415 log_failure_msg
"Unknown Option: ${1}"
424 if [ "${#}" != "1" ]; then
426 log_failure_msg
"Usage: pidofproc [-s] [-p pidfile] pathname"
430 if [ -n "${pidfile}" ]; then
431 if [ ! -r "${pidfile}" ]; then
432 return 3 # Program is not running
435 lpids
=`head -n 1 ${pidfile}`
438 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
439 kill -0 "${pid}" 2>/dev
/null
&&
440 pidlist
="${pidlist} ${pid}"
443 if [ "${silent}" != "1" ]; then
447 test -z "${pidlist}" &&
448 # Program is dead, pidfile exists
455 pidlist
=`pidof -o $$ -o $PPID -x "$1"`
456 if [ "${silent}" != "1" ]; then
460 # Get provide correct running status
461 if [ -n "${pidlist}" ]; then
469 if [ "$?" != "0" ]; then
470 return 3 # Program is not running
474 #*******************************************************************************
475 # Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
477 # Purpose: This runs the specified program as a daemon
479 # Inputs: -f, run the program even if it is already running
480 # -n nicelevel, specifies a nice level. See nice(1).
481 # -p pidfile, uses the specified pidfile
482 # pathname, pathname to the specified program
483 # args, arguments to pass to specified program
485 # Outputs: return 0 - Success
486 # return 2 - Invalid of excessive number of arguments,
488 # return 4 - Program or service status is unknown
490 # Dependencies: nice, rm
492 # Todo: LSB says this should be called start_daemon
493 # LSB does not say that it should call evaluate_retval
494 # It checks for PIDFILE, which is deprecated.
495 # Will be removed after BLFS 6.0
496 # loadproc returns 0 if program is already running, not LSB compliant
498 #*******************************************************************************
505 # This will ensure compatibility with previous LFS Bootscripts
506 if [ -n "${PIDFILE}" ]; then
526 log_failure_msg
"Unknown Option: ${1}"
527 return 2 #invalid or excess argument(s)
535 if [ "${#}" = "0" ]; then
536 log_failure_msg
"Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
537 return 2 #invalid or excess argument(s)
540 if [ -z "${forcestart}" ]; then
541 if [ -z "${pidfile}" ]; then
544 pidofproc
-s -p "${pidfile}" "${1}"
549 log_warning_msg
"Unable to continue: ${1} is running"
553 boot_mesg
"Removing stale pid file: ${pidfile}" ${WARNING}
559 log_failure_msg
"Unknown error code from pidofproc: ${?}"
565 nice
-n "${nicelevel}" "${@}"
566 evaluate_retval
# This is "Probably" not LSB compliant,
567 # but required to be compatible with older bootscripts
571 #*******************************************************************************
572 # Function - killproc [-p pidfile] pathname [signal]
576 # Inputs: -p pidfile, uses the specified pidfile
577 # pathname, pathname to the specified program
578 # signal, send this signal to pathname
580 # Outputs: return 0 - Success
581 # return 2 - Invalid of excessive number of arguments,
583 # return 4 - Unknown Status
585 # Dependencies: kill, rm
587 # Todo: LSB does not say that it should call evaluate_retval
588 # It checks for PIDFILE, which is deprecated.
589 # Will be removed after BLFS 6.0
591 #*******************************************************************************
595 local killsig
=TERM
# default signal is SIGTERM
598 # This will ensure compatibility with previous LFS Bootscripts
599 if [ -n "${PIDFILE}" ]; then
611 log_failure_msg
"Unknown Option: ${1}"
620 if [ "${#}" = "2" ]; then
622 elif [ "${#}" != "1" ]; then
624 log_failure_msg
"Usage: killproc [-p pidfile] pathname [signal]"
628 # Is the process running?
629 if [ -z "${pidfile}" ]; then
632 pidofproc
-s -p "${pidfile}" "${1}"
635 # Remove stale pidfile
636 if [ "$?" = 1 ]; then
637 boot_mesg
"Removing stale pid file: ${pidfile}." ${WARNING}
641 # If running, send the signal
642 if [ -n "${pidlist}" ]; then
643 for pid
in ${pidlist}
645 kill -${killsig} ${pid} 2>/dev
/null
647 # Wait up to 3 seconds, for ${pid} to terminate
649 TERM|SIGTERM|KILL|SIGKILL
)
650 # sleep in 1/10ths of seconds and
651 # multiply KILLDELAY by 10
652 local dtime
="${KILLDELAY}0"
653 while [ "${dtime}" != "0" ]
655 kill -0 ${pid} 2>/dev
/null ||
break
657 dtime
=$
(( ${dtime} - 1))
659 # If ${pid} is still running, kill it
660 kill -0 ${pid} 2>/dev
/null
&& kill -KILL ${pid} 2>/dev
/null
665 # Check if the process is still running if we tried to stop it
667 TERM|SIGTERM|KILL|SIGKILL
)
668 if [ -z "${pidfile}" ]; then
671 pidofproc
-s -p "${pidfile}" "${1}"
674 # Program was terminated
675 if [ "$?" != "0" ]; then
676 # Remove the pidfile if necessary
677 if [ -f "${pidfile}" ]; then
682 else # Program is still running
684 return 4 # Unknown Status
688 # Just see if the kill returned successfully
692 else # process not running
693 print_status warning not_running
698 #*******************************************************************************
699 # Function - log_success_msg "message"
701 # Purpose: Print a success message
703 # Inputs: $@ - Message
705 # Outputs: Text output to screen
711 #*******************************************************************************
714 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
715 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
716 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}" >> /run/var/bootlog
717 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}" \
722 #*******************************************************************************
723 # Function - log_failure_msg "message"
725 # Purpose: Print a failure message
727 # Inputs: $@ - Message
729 # Outputs: Text output to screen
735 #*******************************************************************************
737 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
738 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
739 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}" >> /run/var/bootlog
740 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}" \
745 #*******************************************************************************
746 # Function - log_warning_msg "message"
748 # Purpose: print a warning message
750 # Inputs: $@ - Message
752 # Outputs: Text output to screen
758 #*******************************************************************************
760 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
761 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
762 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}" >> /run/var/bootlog
763 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}" \
768 #*******************************************************************************
769 # Function - log_skipped_msg "message"
771 # Purpose: print a message that the script was skipped
773 # Inputs: $@ - Message
775 # Outputs: Text output to screen
781 #*******************************************************************************
783 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
784 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${WARNING}"" SKIP ""${BRACKET}""]""${NORMAL}"
785 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}" >> /run/var/bootlog
786 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${WARNING}"" SKIP ""${BRACKET}""]""${NORMAL}" \