source: bootscripts/lfs/init.d/functions@ fdb7196

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

r3512@server: jim | 2006-05-15 16:13:26 -0700
Updated LCD Support

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