source: bootscripts/contrib/enhanced/init.d/functions@ e3cda9a

clfs-1.2 clfs-2.1 clfs-3.0.0-systemd clfs-3.0.0-sysvinit systemd sysvinit
Last change on this file since e3cda9a was 163a6701, checked in by Jim Gifford <clfs@…>, 19 years ago

r720@server (orig r718): jim | 2005-11-18 08:06:26 -0800
Added bootscripts to cross-lfs svn

  • Property mode set to 100644
File size: 18.4 KB
Line 
1#!/bin/sh
2########################################################################
3# Begin $rc_base/init.d/functions
4#
5# Description : Run Level Control Functions
6#
7# Authors : Gerard Beekmans - gerard@linuxfromscratch.org
8#
9# Version : 00.00
10#
11# Notes : With code based on Matthias Benkmann's simpleinit-msb
12# http://winterdrache.de/linux/newboot/index.html
13# Updated for Interactive bootscript execution and
14# bootlogging prior to start of syslog
15########################################################################
16
17## Environmental setup
18# Setup default values for environment
19umask 022
20export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
21
22# Signal sent to running processes to refresh their configuration
23RELOADSIG="HUP"
24
25# Number of seconds between STOPSIG and FALLBACK when stopping processes
26KILLDELAY="3"
27
28## Screen Dimensions
29# Find current screen size
30if [ -z "${COLUMNS}" ]; then
31 COLUMNS=$(stty size)
32 COLUMNS=${COLUMNS##* }
33fi
34
35# When using remote connections, such as a serial port, stty size returns 0
36if [ "${COLUMNS}" = "0" ]; then
37 COLUMNS=80
38fi
39
40## Measurements for positioning result messages
41COL=$((${COLUMNS} - 8))
42WCOL=$((${COL} - 2))
43
44## Set Cursor Position Commands, used via echo -e
45SET_COL="\\033[${COL}G" # at the $COL char
46SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
47CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
48
49## Set color commands, used via echo -e
50# Please consult `man console_codes for more information
51# under the "ECMA-48 Set Graphics Rendition" section
52#
53# Warning: when switching from a 8bit to a 9bit font,
54# the linux console will reinterpret the bold (1;) to
55# the top 256 glyphs of the 9bit font. This does
56# not affect framebuffer consoles
57NORMAL="\\033[0;39m" # Standard console grey
58SUCCESS="\\033[1;32m" # Success is green
59WARNING="\\033[1;33m" # Warnings are yellow
60FAILURE="\\033[1;31m" # Failures are red
61INFO="\\033[1;36m" # Information is light cyan
62BRACKET="\\033[1;34m" # Brackets are blue
63
64## Set screen and bootlog message values to zero
65BOOTMESG="" # text meant for the logfile
66BOOTMESGPRN="" # text meant for the console
67CUR_LENGTH="0" # the current line length
68CHARS_LEFT="${WCOL}" # avilible space on the current line
69STRING_LENGTH="0" # the length of the current message
70BMSEP="" # contains neline separated bootmessage
71BMSEPCT="" # used for character counts of each word in BMSEP
72BMWORD="" # last printed word before a newline when wrapping text
73
74#*******************************************************************************
75# Function - boot_mesg()
76#
77# Purpose: Sending information from bootup scripts to the console
78#
79# Inputs: $1 is the message
80# $2 is the colorcode for the console
81#
82# Outputs: Standard Output
83#
84# Dependencies: - sed for parsing strings.
85# - grep, for counting \n's in a variable.
86#
87# Todo: - This function was created for the purpose of storing
88# information to be passed to a bootup log and for better
89# formatting of text sent to the screen. The logging support
90# now pushes messages to the local2 service which is now
91# configured by LFS default in syslog.conf. This also
92# requires that /usr/bin/logger be moved to /bin.
93# Support for parallel bootscripts is not added *yet*...
94#*******************************************************************************
95boot_mesg()
96{
97 local ECHOPARM=""
98 ##### NOTE!!!! A single message cannot span two complete lines
99 ##### (longer than WCOL) or exceed 100 words.
100
101 while true
102 do
103 case "${1}" in
104 -n)
105 ECHOPARM=" -n "
106 shift 1
107 ;;
108 -*)
109 echo "Unknown Option: ${1}"
110 return 1
111 ;;
112 *)
113 break
114 ;;
115 esac
116 done
117
118 ## Figure out the length of what is to be printed.
119 # This is effectivly eqivelent to wc -m.
120 STRING_LENGTH="$(( `echo "${1}" | sed \
121 -e 's,.,.,g' -e 'l 1' | grep -c \$` - 2 ))"
122
123 # Append to BOOTMESG for logging
124 BOOTMESG="${BOOTMESG} ${1}"
125
126 # Find out how much room is left to work with
127 CHARS_LEFT="$(( ${WCOL} - ${CUR_LENGTH} ))"
128
129 # If the message doesn't exceed the remaining columns
130 # of the current line, just print it...
131 if [ "${STRING_LENGTH}" -le "${CHARS_LEFT}" ]; then
132 CUR_LENGTH="$(( ${CUR_LENGTH} + ${STRING_LENGTH} ))"
133 BOOTMESGPRN="${1}"
134
135 # Avoid a null value being passed to sed
136 if [ "${STRING_LENGTH}" -eq "${CHARS_LEFT}" ]; then
137 # set CUR_LENGTH to zero
138 CUR_LENGTH="0"
139 fi
140
141 else
142
143 ## Do some adjustments to the output because it
144 ## it is too long to fit on one line
145
146 # BMSEP is the boot message with each word
147 # on one line of it's own
148 BMSEP=`echo "${1}" | sed 's/ /\\\n/g'`
149
150 # To work proerly with unicode, each character
151 # is replaced with a single dot (.) for counting.
152 ### Note that this is still broken for Chinese,
153 ### Japanese, and Korean because of characters
154 ### that actually use two columns when printed
155 ### to the screen.
156 BMSEPCT=`echo -e "${BMSEP}" | sed 's/././g'`
157
158 # keep adding the character count of each line
159 # untill the count exceeds the value of CHARS_LEFT
160 # There is a ceiling of 100 words!
161 n="1"
162 while [ "${n}" -lt "100" ]; do
163 char_count="$(( `echo -e "${BMSEPCT}" | \
164 sed -n "${n}"'~0p' | sed 'l 1' | \
165 grep -c \$` - 2 ))"
166 if [ "$(( ${CUR_LENGTH} + ${char_count} ))" \
167 -lt "${WCOL}" ]; then
168 let n++
169 CUR_LENGTH="$(( ${CUR_LENGTH} + ${char_count} + 1 ))"
170 else
171 let n--
172 break;
173 fi
174 done
175
176 # The number of words that will fit is now
177 # represented by 'n'. Find the nth word.
178 BMWORD="$( echo -e "${BMSEP}" | sed -n "${n}"'~0p' )"
179
180 # Get rid of the extra space
181 BMWORD="${BMWORD} "
182
183 # And format the message as needed.
184 BOOTMESGPRN=`echo "${1}" | \
185 sed "s/${BMWORD}/${BMWORD}\\\n/"`
186
187 # Now, determine the length of the second line
188 CUR_LENGTH=$(( `echo -e "${BOOTMESGPRN}" | sed -n '2~0p' | \
189 sed -e 's,.,.,g' -e 'l 1' | grep -c \$` - 2 ))
190 fi
191
192 # Print the results to the screen
193 echo ${ECHOPARM} -e "${2}${BOOTMESGPRN}"
194
195 # if CUR_LENGTH was set to zero, then end the line
196 if [ "${CUR_LENGTH}" = "0" ]; then
197 echo ""
198 fi
199}
200
201boot_mesg_flush()
202{
203 # Do not log null messages
204 if [ -z "${BOOTMESG}" ]; then
205 return 0
206 else
207 boot_log "${BOOTMESG}${@}"
208 fi
209
210 # Reset boot message buffers to keep from bleeding over again
211 BOOTMESG=""
212 BOOTMESGPRN=""
213 CUR_LENGTH="0"
214 CHARS_LEFT="${WCOL}"
215 STRING_LENGTH="0"
216 BMSEP=""
217 BMSEPCT=""
218 BMWORD=""
219}
220
221boot_log()
222{
223 if [ -f /bin/logger ]; then
224 if [ "${runlevel}" = "sysinit" ]; then
225 /bin/logger -t bootlog "${@}" -s 2>&1 | \
226 cat >> /media/boottemp/.logcache
227 else
228 /bin/logger -p "${BLSERVICE}".info -t bootlog "${@}"
229 fi
230 else
231 return 0
232 fi
233}
234
235echo_ok()
236{
237 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
238 echo -e "${NORMAL}"
239 boot_mesg_flush "[ OK ]"
240}
241
242echo_failure()
243{
244 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
245 echo -e "${NORMAL}"
246 boot_mesg_flush "[ FAIL ]"
247}
248
249echo_warning()
250{
251 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
252 echo -e "${NORMAL}"
253 boot_mesg_flush "[ WARN ]"
254}
255
256print_error_msg()
257{
258 echo_failure
259 # $i is inherited by the rc script
260 boot_log "\n\n${i} failed and exited with a return value of ${error_value}."
261 boot_mesg_flush
262 boot_mesg -n "FAILURE:\n\nYou should not be reading this error message.\n\n" ${FAILURE}
263 boot_mesg -n " It means that an unforeseen error took"
264 boot_mesg -n " place in ${i}, which exited with a return value of"
265 boot_mesg " ${error_value}.\n"
266 boot_mesg_flush
267 boot_mesg -n "If you're able to track this"
268 boot_mesg -n " error down to a bug in one of the files provided by"
269 boot_mesg -n " the LFS book, please be so kind to inform us at"
270 boot_mesg " lfs-dev@linuxfromscratch.org.\n"
271 boot_mesg_flush
272 boot_mesg -n "Press Enter to continue..." ${INFO}
273 boot_mesg "" ${NORMAL}
274 read ENTER
275}
276
277check_script_status()
278{
279 # $i is inherited by the rc script
280 if [ ! -f ${i} ]; then
281 boot_mesg "${i} is not a valid symlink." ${WARNING}
282 echo_warning
283 continue
284 fi
285
286 if [ ! -x ${i} ]; then
287 boot_mesg "${i} is not executable, skipping." ${WARNING}
288 echo_warning
289 continue
290 fi
291}
292
293evaluate_retval()
294{
295 error_value="${?}"
296
297 if [ ${error_value} = 0 ]; then
298 echo_ok
299 else
300 echo_failure
301 fi
302
303 # This prevents the 'An Unexpected Error Has Occurred' from trivial
304 # errors.
305 return 0
306}
307
308print_status()
309{
310 if [ "${#}" = "0" ]; then
311 echo "Usage: ${0} {success|warning|failure}"
312 return 1
313 fi
314
315 case "${1}" in
316
317 success)
318 echo_ok
319 ;;
320
321 warning)
322 # Leave this extra case in because old scripts
323 # may call it this way.
324 case "${2}" in
325 running)
326 echo -e -n "${CURS_UP}"
327 echo -e -n "\\033[${CUR_LENGTH}G "
328 boot_mesg "Already running." ${WARNING}
329 echo_warning
330 ;;
331 not_running)
332 echo -e -n "${CURS_UP}"
333 echo -e -n "\\033[${CUR_LENGTH}G "
334 boot_mesg "Not running." ${WARNING}
335 echo_warning
336 ;;
337 not_available)
338 echo -e -n "${CURS_UP}"
339 echo -e -n "\\033[${CUR_LENGTH}G "
340 boot_mesg "Not available." ${WARNING}
341 echo_warning
342 ;;
343 *)
344 # This is how it is supposed to
345 # be called
346 echo_warning
347 ;;
348 esac
349 ;;
350
351 failure)
352 echo_failure
353 ;;
354
355 esac
356
357}
358
359reloadproc()
360{
361 if [ "${#}" = "0" ]; then
362 echo "Usage: reloadproc [{program}]"
363 exit 1
364 fi
365
366 getpids "${1}"
367
368 if [ -n "${pidlist}" ]; then
369 failure="0"
370 for pid in ${pidlist}
371 do
372 kill -"${RELOADSIG}" "${pid}" || failure="1"
373 done
374
375 (exit ${failure})
376 evaluate_retval
377
378 else
379 boot_mesg "Process ${1} not running." ${WARNING}
380 echo_warning
381 fi
382}
383
384statusproc()
385{
386 if [ "${#}" = "0" ]
387 then
388 echo "Usage: statusproc {program}"
389 exit 1
390 fi
391
392 getpids "${1}"
393
394 if [ -n "${pidlist}" ]; then
395 echo -e "${INFO}${base} is running with Process"\
396 "ID(s) ${pidlist}.${NORMAL}"
397 else
398 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
399 echo -e "${WARNING}${1} is not running but"\
400 "/var/run/${base}.pid exists.${NORMAL}"
401 else
402 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
403 echo -e "${WARNING}${1} is not running"\
404 "but ${PIDFILE} exists.${NORMAL}"
405 else
406 echo -e "${INFO}${1} is not running.${NORMAL}"
407 fi
408 fi
409 fi
410}
411
412# The below functions are documented in the LSB-generic 2.1.0
413
414#*******************************************************************************
415# Function - pidofproc [-s] [-p pidfile] pathname
416#
417# Purpose: This function returns one or more pid(s) for a particular daemon
418#
419# Inputs: -p pidfile, use the specified pidfile instead of pidof
420# pathname, path to the specified program
421#
422# Outputs: return 0 - Success, pid's in stdout
423# return 1 - Program is dead, pidfile exists
424# return 2 - Invalid or excessive number of arguments,
425# warning in stdout
426# return 3 - Program is not running
427#
428# Dependencies: pidof, echo, head
429#
430# Todo: Remove dependency on head
431# This depreciates getpids
432# Test changes to pidof
433#
434#*******************************************************************************
435pidofproc()
436{
437 local pidfile=""
438 local lpids=""
439 local silent=""
440 pidlist=""
441 while true
442 do
443 case "${1}" in
444 -p)
445 pidfile="${2}"
446 shift 2
447 ;;
448
449 -s)
450 # Added for legacy opperation of getpids
451 # eliminates several '> /dev/null'
452 silent="1"
453 shift 1
454 ;;
455 -*)
456 log_failure_msg "Unknown Option: ${1}"
457 return 2
458 ;;
459 *)
460 break
461 ;;
462 esac
463 done
464
465 if [ "${#}" != "1" ]; then
466 shift 1
467 log_failure_msg "Usage: pidofproc [-s] [-p pidfile] pathname"
468 return 2
469 fi
470
471 if [ -n "${pidfile}" ]; then
472 if [ ! -r "${pidfile}" ]; then
473 return 3 # Program is not running
474 fi
475
476 lpids=`head -n 1 ${pidfile}`
477 for pid in ${lpids}
478 do
479 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
480 kill -0 "${pid}" > /dev/null &&
481 pidlist="${pidlist} ${pid}"
482 fi
483
484 if [ "${silent}" -ne "1" ]; then
485 echo "${pidlist}"
486 fi
487
488 test -z "${pidlist}" &&
489 # Program is dead, pidfile exists
490 return 1
491 # else
492 return 0
493 done
494
495 else
496 pidlist=`pidof -o $$ -o $PPID -x "$1"`
497 if [ "x${silent}" != "x1" ]; then
498 echo "${pidlist}"
499 fi
500
501 # Get provide correct running status
502 if [ -n "${pidlist}" ]; then
503 return 0
504 else
505 return 3
506 fi
507
508 fi
509
510 if [ "$?" != "0" ]; then
511 return 3 # Program is not running
512 fi
513}
514
515# This will ensure compatibility with previous LFS Bootscripts
516getpids()
517{
518 if [ -z "${PIDFILE}" ]; then
519 pidofproc -s -p "${PIDFILE}" $@
520 else
521 pidofproc -s $@
522 fi
523 base="${1##*/}"
524}
525
526#*******************************************************************************
527# Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
528#
529# Purpose: This runs the specified program as a daemon
530#
531# Inputs: -f, run the program even if it is already running
532# -n nicelevel, specifies a nice level. See nice(1).
533# -p pidfile, uses the specified pidfile
534# pathname, pathname to the specified program
535# args, arguments to pass to specified program
536#
537# Outputs: return 0 - Success
538# return 2 - Invalid of excessive number of arguments,
539# warning in stdout
540# return 4 - Program or service status is unknown
541#
542# Dependencies: nice
543#
544# Todo: LSB says this should be called start_daemon
545# LSB does not say that it should call evaluate_retval
546# It checks for PIDFILE, which is deprecated.
547# Will be removed after BLFS 6.0
548# loadproc returns 0 if program is already running, not LSB compliant
549#
550#*******************************************************************************
551loadproc()
552{
553 local pidfile=""
554 local forcestart=""
555 local nicelevel="10"
556
557# This will ensure compatibility with previous LFS Bootscripts
558 if [ -n "${PIDFILE}" ]; then
559 pidfile="${PIDFILE}"
560 fi
561
562 while true
563 do
564 case "${1}" in
565 -f)
566 forcestart="1"
567 shift 1
568 ;;
569 -n)
570 nicelevel="${2}"
571 shift 2
572 ;;
573 -p)
574 pidfile="${2}"
575 shift 2
576 ;;
577 -*)
578 log_failure_msg "Unknown Option: ${1}"
579 return 2 #invalid or excess argument(s)
580 ;;
581 *)
582 break
583 ;;
584 esac
585 done
586
587 if [ "${#}" = "0" ]; then
588 log_failure_msg "Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
589 return 2 #invalid or excess argument(s)
590 fi
591
592 if [ -z "${forcestart}" ]; then
593 if [ -z "${pidfile}" ]; then
594 pidofproc -s "${1}"
595 else
596 pidofproc -s -p "${pidfile}" "${1}"
597 fi
598
599 case "${?}" in
600 0)
601 log_warning_msg "Unable to continue: ${1} is running"
602 return 0 # 4
603 ;;
604 1)
605 log_warning_msg "Unable to continue: ${pidfile} exists"
606 return 0 # 4
607 ;;
608 3)
609 ;;
610 *)
611 log_failure_msg "Unknown error code from pidofproc: ${?}"
612 return 4
613 ;;
614 esac
615 fi
616
617 nice -n "${nicelevel}" "${@}"
618 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
619 return 0
620}
621
622#*******************************************************************************
623# Function - killproc [-p pidfile] pathname [signal]
624#
625# Purpose:
626#
627# Inputs: -p pidfile, uses the specified pidfile
628# pathname, pathname to the specified program
629# signal, send this signal to pathname
630#
631# Outputs: return 0 - Success
632# return 2 - Invalid of excessive number of arguments,
633# warning in stdout
634# return 4 - Unknown Status
635#
636# Dependencies: kill
637#
638# Todo: LSB does not say that it should call evaluate_retval
639# It checks for PIDFILE, which is deprecated.
640# Will be removed after BLFS 6.0
641#
642#*******************************************************************************
643killproc()
644{
645 local pidfile=""
646 local killsig=""
647 pidlist=""
648
649# This will ensure compatibility with previous LFS Bootscripts
650 if [ -n "${PIDFILE}" ]; then
651 pidfile="${PIDFILE}"
652 fi
653
654 while true
655 do
656 case "${1}" in
657 -p)
658 pidfile="${2}"
659 shift 2
660 ;;
661 -*)
662 log_failure_msg "Unknown Option: ${1}"
663 return 2
664 ;;
665 *)
666 break
667 ;;
668 esac
669 done
670
671 if [ "${#}" = "2" ]; then
672 killsig="${2}"
673 elif [ "${#}" != "1" ]; then
674 shift 2
675 log_failure_msg "Usage: killproc [-p pidfile] pathname [signal]"
676 return 2
677 fi
678
679 if [ -z "${pidfile}" ]; then
680 pidofproc -s "${1}"
681 else
682 pidofproc -s -p "${pidfile}" "${1}"
683 fi
684
685 if [ -n "${pidlist}" ]; then
686 for pid in ${pidlist}
687 do
688 kill -${killsig:-TERM} ${pid} 2>/dev/null
689 if [ -z "${killsig}" ]; then
690 # Wait up to 3 seconds, for ${pid} to terminate
691 local dtime=${KILLDELAY}
692 while [ "${dtime}" != "0" ]
693 do
694 kill -0 ${pid} 2>/dev/null || break
695 sleep 1
696 dtime=$(( ${dtime} - 1))
697 done
698 # If ${pid} is still running, kill it
699 kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
700 fi
701 done
702
703 if [ -z "${killsig}" ]; then
704 pidofproc -s "${1}"
705
706 # Program was terminated
707 if [ "$?" != "0" ]; then
708 # Pidfile Exists
709 if [ -f "${pidfile}" ]; then
710 rm -f "${pidfile}"
711 fi
712 echo_ok
713 return 0
714 else # Program is still running
715 echo_failure
716 return 4 # Unknown Status
717 fi
718 else
719 if [ -z "${pidfile}" ]; then
720 pidofproc -s "${1}"
721 else
722 pidofproc -s -p "${pidfile}" "${1}"
723 fi
724 fi
725
726 evaluate_retval
727
728 else
729 print_status warning not_running
730 fi
731}
732
733
734#*******************************************************************************
735# Function - log_success_msg "message"
736#
737# Purpose: Print a success message
738#
739# Inputs: $@ - Message
740#
741# Outputs: Text output to screen
742#
743# Dependencies: echo
744#
745# Todo: logging
746#
747#*******************************************************************************
748log_success_msg()
749{
750 echo -n -e "${BOOTMESG_PREFIX}${@}"
751 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
752 return 0
753}
754
755#*******************************************************************************
756# Function - log_failure_msg "message"
757#
758# Purpose: Print a failure message
759#
760# Inputs: $@ - Message
761#
762# Outputs: Text output to screen
763#
764# Dependencies: echo
765#
766# Todo: logging
767#
768#*******************************************************************************
769log_failure_msg() {
770 echo -n -e "${BOOTMESG_PREFIX}${@}"
771 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
772 return 0
773}
774
775#*******************************************************************************
776# Function - log_warning_msg "message"
777#
778# Purpose: print a warning message
779#
780# Inputs: $@ - Message
781#
782# Outputs: Text output to screen
783#
784# Dependencies: echo
785#
786# Todo: logging
787#
788#*******************************************************************************
789log_warning_msg() {
790 echo -n -e "${BOOTMESG_PREFIX}${@}"
791 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
792 return 0
793}
794
795# End $rc_base/init.d/functions
Note: See TracBrowser for help on using the repository browser.