2 ########################################################################
3 # Begin $rc_base/init.d/functions
5 # Description : Run Level Control Functions
7 # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
11 # Notes : With code based on Matthias Benkmann's simpleinit-msb
12 # http://winterdrache.de/linux/newboot/index.html
14 ########################################################################
16 ## Environmental setup
17 # Setup default values for environment
19 export PATH
="/bin:/usr/bin:/sbin:/usr/sbin"
21 # Signal sent to running processes to refresh their configuration
24 # Number of seconds between STOPSIG and FALLBACK when stopping processes
28 # Find current screen size
29 if [ -z "${COLUMNS}" ]; then
31 COLUMNS
=${COLUMNS##* }
34 # When using remote connections, such as a serial port, stty size returns 0
35 if [ "${COLUMNS}" = "0" ]; then
39 ## Measurements for positioning result messages
40 COL
=$
((${COLUMNS} - 8))
43 ## Set Cursor Position Commands, used via echo -e
44 SET_COL
="\\033[${COL}G" # at the $COL char
45 SET_WCOL
="\\033[${WCOL}G" # at the $WCOL char
46 CURS_UP
="\\033[1A\\033[0G" # Up one line, at the 0'th char
48 ## Set color commands, used via echo -e
49 # Please consult `man console_codes for more information
50 # under the "ECMA-48 Set Graphics Rendition" section
52 # Warning: when switching from a 8bit to a 9bit font,
53 # the linux console will reinterpret the bold (1;) to
54 # the top 256 glyphs of the 9bit font. This does
55 # 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 STRING_LENGTH
="0" # the length of the current message
65 #*******************************************************************************
66 # Function - boot_mesg()
68 # Purpose: Sending information from bootup scripts to the console
70 # Inputs: $1 is the message
71 # $2 is the colorcode for the console
73 # Outputs: Standard Output
75 # Dependencies: - sed for parsing strings.
76 # - grep for counting string length.
79 #*******************************************************************************
92 echo "Unknown Option: ${1}"
101 ## Figure out the length of what is to be printed to be used
102 ## for warning messges.
103 STRING_LENGTH
="`echo "${1}" | sed \
104 -e 's,.,.,g' -e 'l 1' | grep -c \$`"
106 # Print the message to the screen
107 echo ${ECHOPARM} -e "${2}${1}"
113 # Reset STRING_LENGTH for next message
119 # Left in for backwards compatibility
125 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
132 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
139 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
147 # $i is inherited by the rc script
148 boot_mesg -n "FAILURE
:\n\nYou should not be reading this error message.
\n\n" ${FAILURE}
149 boot_mesg -n " It means that an unforeseen error took
"
150 boot_mesg -n " place
in ${i}, which exited with a
return value of
"
151 boot_mesg " ${error_value}.
\n"
153 boot_mesg -n "If you
're able to track this"
154 boot_mesg -n " error down to a bug in one of the files provided by"
155 boot_mesg -n " ipfire, please be so kind to inform us at"
156 boot_mesg " info@ipfire.org.\n"
158 boot_mesg -n "Press Enter to continue or wait 3 minutes..." ${INFO}
159 boot_mesg "" ${NORMAL}
166 check_script_status()
168 # $i is inherited by the rc script
169 if [ ! -f ${i} ]; then
170 boot_mesg "${i} is not a valid symlink." ${WARNING}
175 if [ ! -x ${i} ]; then
176 boot_mesg "${i} is not executable, skipping." ${WARNING}
186 if [ ${error_value} = 0 ]; then
192 # This prevents the 'An Unexpected Error Has Occurred
' from trivial
199 if [ "${#}" = "0" ]; then
200 echo "Usage: ${0} {success|warning|failure}"
211 # Leave this extra case in because old scripts
212 # may call it this way.
215 echo -e -n "${CURS_UP}"
216 echo -e -n "\\033[${STRING_LENGTH}G "
217 boot_mesg "Already running." ${WARNING}
221 echo -e -n "${CURS_UP}"
222 echo -e -n "\\033[${STRING_LENGTH}G "
223 boot_mesg "Not running." ${WARNING}
227 echo -e -n "${CURS_UP}"
228 echo -e -n "\\033[${STRING_LENGTH}G "
229 boot_mesg "Not available." ${WARNING}
233 # This is how it is supposed to
250 if [ "${#}" = "0" ]; then
251 echo "Usage: reloadproc [{program}]"
257 if [ -n "${pidlist}" ]; then
259 for pid in ${pidlist}
261 kill -"${RELOADSIG}" "${pid}" || failure="1"
268 boot_mesg "Process ${1} not running." ${WARNING}
277 echo "Usage: statusproc {program}"
283 if [ -n "${pidlist}" ]; then
284 echo -e "${INFO}${base} is running with Process"\
285 "ID(s) ${pidlist}.${NORMAL}"
287 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
288 echo -e "${WARNING}${1} is not running but"\
289 "/var/run/${base}.pid exists.${NORMAL}"
291 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
292 echo -e "${WARNING}${1} is not running"\
293 "but ${PIDFILE} exists.${NORMAL}"
295 echo -e "${INFO}${1} is not running.${NORMAL}"
301 # The below functions are documented in the LSB-generic 2.1.0
303 #*******************************************************************************
304 # Function - pidofproc [-s] [-p pidfile] pathname
306 # Purpose: This function returns one or more pid(s) for a particular daemon
308 # Inputs: -p pidfile, use the specified pidfile instead of pidof
309 # pathname, path to the specified program
311 # Outputs: return 0 - Success, pid's
in stdout
312 # return 1 - Program is dead, pidfile exists
313 # return 2 - Invalid or excessive number of arguments,
315 # return 3 - Program is not running
317 # Dependencies: pidof, echo, head
319 # Todo: Remove dependency on head
320 # This depreciates getpids
321 # Test changes to pidof
323 #*******************************************************************************
339 # Added for legacy opperation of getpids
340 # eliminates several '> /dev/null'
345 log_failure_msg
"Unknown Option: ${1}"
354 if [ "${#}" != "1" ]; then
356 log_failure_msg
"Usage: pidofproc [-s] [-p pidfile] pathname"
360 if [ -n "${pidfile}" ]; then
361 if [ ! -r "${pidfile}" ]; then
362 return 3 # Program is not running
365 lpids
=`head -n 1 ${pidfile}`
368 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
369 kill -0 "${pid}" > /dev
/null
&&
370 pidlist
="${pidlist} ${pid}"
373 if [ "${silent}" -ne "1" ]; then
377 test -z "${pidlist}" &&
378 # Program is dead, pidfile exists
385 pidlist
=`pidof -o $$ -o $PPID -x "$1"`
386 if [ "x${silent}" != "x1" ]; then
390 # Get provide correct running status
391 if [ -n "${pidlist}" ]; then
399 if [ "$?" != "0" ]; then
400 return 3 # Program is not running
404 # This will ensure compatibility with previous LFS Bootscripts
407 if [ -z "${PIDFILE}" ]; then
408 pidofproc
-s -p "${PIDFILE}" $@
415 #*******************************************************************************
416 # Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
418 # Purpose: This runs the specified program as a daemon
420 # Inputs: -f, run the program even if it is already running
421 # -n nicelevel, specifies a nice level. See nice(1).
422 # -p pidfile, uses the specified pidfile
423 # pathname, pathname to the specified program
424 # args, arguments to pass to specified program
426 # Outputs: return 0 - Success
427 # return 2 - Invalid of excessive number of arguments,
429 # return 4 - Program or service status is unknown
433 # Todo: LSB says this should be called start_daemon
434 # LSB does not say that it should call evaluate_retval
435 # It checks for PIDFILE, which is deprecated.
436 # Will be removed after BLFS 6.0
437 # loadproc returns 0 if program is already running, not LSB compliant
439 #*******************************************************************************
446 # This will ensure compatibility with previous LFS Bootscripts
447 if [ -n "${PIDFILE}" ]; then
467 log_failure_msg
"Unknown Option: ${1}"
468 return 2 #invalid or excess argument(s)
476 if [ "${#}" = "0" ]; then
477 log_failure_msg
"Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
478 return 2 #invalid or excess argument(s)
481 if [ -z "${forcestart}" ]; then
482 if [ -z "${pidfile}" ]; then
485 pidofproc
-s -p "${pidfile}" "${1}"
490 log_warning_msg
"Unable to continue: ${1} is running"
494 log_warning_msg
"Unable to continue: ${pidfile} exists"
500 log_failure_msg
"Unknown error code from pidofproc: ${?}"
506 nice
-n "${nicelevel}" "${@}"
507 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
511 #*******************************************************************************
512 # Function - killproc [-p pidfile] pathname [signal]
516 # Inputs: -p pidfile, uses the specified pidfile
517 # pathname, pathname to the specified program
518 # signal, send this signal to pathname
520 # Outputs: return 0 - Success
521 # return 2 - Invalid of excessive number of arguments,
523 # return 4 - Unknown Status
527 # Todo: LSB does not say that it should call evaluate_retval
528 # It checks for PIDFILE, which is deprecated.
529 # Will be removed after BLFS 6.0
531 #*******************************************************************************
538 # This will ensure compatibility with previous LFS Bootscripts
539 if [ -n "${PIDFILE}" ]; then
551 log_failure_msg
"Unknown Option: ${1}"
560 if [ "${#}" = "2" ]; then
562 elif [ "${#}" != "1" ]; then
564 log_failure_msg
"Usage: killproc [-p pidfile] pathname [signal]"
568 if [ -z "${pidfile}" ]; then
571 pidofproc
-s -p "${pidfile}" "${1}"
575 if [ -n "${pidlist}" ]; then
576 for pid
in ${pidlist}
578 kill -${killsig:-TERM} ${pid} 2>/dev
/null
579 if [ -z "${killsig}" ]; then
580 # Wait up to 3 seconds, for ${pid} to terminate
581 local dtime
=${KILLDELAY}
582 while [ "${dtime}" != "0" ]
584 kill -0 ${pid} 2>/dev
/null ||
break
586 dtime
=$
(( ${dtime} - 1))
588 # If ${pid} is still running, kill it
589 kill -0 ${pid} 2>/dev
/null
&& kill -KILL ${pid} 2>/dev
/null
593 if [ -z "${killsig}" ]; then
596 # Program was terminated
597 if [ "$?" != "0" ]; then
599 if [ -f "${pidfile}" ]; then
604 else # Program is still running
606 return 4 # Unknown Status
609 if [ -z "${pidfile}" ]; then
612 pidofproc
-s -p "${pidfile}" "${1}"
616 evaluate_retval
# This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
619 print_status warning not_running
624 #*******************************************************************************
625 # Function - log_success_msg "message"
627 # Purpose: Print a success message
629 # Inputs: $@ - Message
631 # Outputs: Text output to screen
637 #*******************************************************************************
640 echo -n -e "${BOOTMESG_PREFIX}${@}"
641 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
645 #*******************************************************************************
646 # Function - log_failure_msg "message"
648 # Purpose: Print a failure message
650 # Inputs: $@ - Message
652 # Outputs: Text output to screen
658 #*******************************************************************************
660 echo -n -e "${BOOTMESG_PREFIX}${@}"
661 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
665 #*******************************************************************************
666 # Function - log_warning_msg "message"
668 # Purpose: print a warning message
670 # Inputs: $@ - Message
672 # Outputs: Text output to screen
678 #*******************************************************************************
680 echo -n -e "${BOOTMESG_PREFIX}${@}"
681 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
689 echo "Usage: animate {hook}"
696 # End $rc_base/init.d/functions