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

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

Put the correct change in for the LCD Fix.

  • Property mode set to 100644
File size: 15.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#
14########################################################################
15
16if [ -e /etc/sysconfig/lcd ]; then
17 if [ -e /dev/lcd ]; then
18 source /etc/sysconfig/lcd
19 fi
20fi
21
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
90 if [ "$LCD_PROG" ]; then
91 LCD_OUT1="${1:0:$LCD_CHAR}"
92 $LCD_PROG "$LCD_OUT1"
93 fi
94
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 ]"
139 if [ "$LCD_PROG" ]; then
140 LCD_OUT2="[ OK ]"
141 $LCD_PROG "$LCD_OUT1" "$LCD_OUT2"
142 fi
143}
144
145echo_failure()
146{
147 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
148 echo -e "${NORMAL}"
149 boot_mesg_flush "[ FAIL ]"
150 if [ "$LCD_PROG" ]; then
151 LCD_OUT2="[ FAIL ]"
152 $LCD_PROG "$LCD_OUT1" "$LCD_OUT2"
153 fi
154}
155
156echo_warning()
157{
158 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
159 echo -e "${NORMAL}"
160 if [ "$LCD_PROG" ]; then
161 LCD_OUT2="[ WARN ]"
162 $LCD_PROG "$LCD_OUT1" "$LCD_OUT2"
163 fi
164 boot_mesg_flush "[ WARN ]"
165}
166
167print_error_msg()
168{
169 echo_failure
170 # $i is inherited by the rc script
171 boot_log "\n\n${i} failed and exited with a return value of ${error_value}."
172 boot_mesg_flush
173 boot_mesg -n "FAILURE:\n\nYou should not be reading this error message.\n\n" ${FAILURE}
174 boot_mesg -n " It means that an unforeseen error took"
175 boot_mesg -n " place in ${i}, which exited with a return value of"
176 boot_mesg " ${error_value}.\n"
177 boot_mesg_flush
178 boot_mesg -n "If you're able to track this"
179 boot_mesg -n " error down to a bug in one of the files provided by"
180 boot_mesg -n " the LFS book, please be so kind to inform us at"
181 boot_mesg " lfs-dev@linuxfromscratch.org.\n"
182 boot_mesg_flush
183 boot_mesg -n "Press Enter to continue..." ${INFO}
184 boot_mesg "" ${NORMAL}
185 if [ "$LCD_PROG" ]; then
186 sleep 10
187 else
188 read ENTER
189 fi
190}
191
192check_script_status()
193{
194 # $i is inherited by the rc script
195 if [ ! -f ${i} ]; then
196 boot_mesg "${i} is not a valid symlink." ${WARNING}
197 echo_warning
198 continue
199 fi
200
201 if [ ! -x ${i} ]; then
202 boot_mesg "${i} is not executable, skipping." ${WARNING}
203 echo_warning
204 continue
205 fi
206}
207
208evaluate_retval()
209{
210 error_value="${?}"
211
212 if [ ${error_value} = 0 ]; then
213 echo_ok
214 else
215 echo_failure
216 fi
217
218 # This prevents the 'An Unexpected Error Has Occurred' from trivial
219 # errors.
220 return 0
221}
222
223print_status()
224{
225 if [ "${#}" = "0" ]; then
226 echo "Usage: ${0} {success|warning|failure}"
227 return 1
228 fi
229
230# boot_mesg_flush
231# echo_warning
232
233 case "${1}" in
234
235 success)
236 echo_ok
237 ;;
238
239 warning)
240 # Leave this extra case in because old scripts
241 # may call it this way.
242 case "${2}" in
243 running)
244 echo -e -n "${CURS_UP}"
245 echo -e -n "\\033[${STRING_LENGTH}G "
246 boot_mesg "Already running." ${WARNING}
247 echo_warning
248 ;;
249 not_running)
250 echo -e -n "${CURS_UP}"
251 echo -e -n "\\033[${STRING_LENGTH}G "
252 boot_mesg "Not running." ${WARNING}
253 echo_warning
254 ;;
255 not_available)
256 echo -e -n "${CURS_UP}"
257 echo -e -n "\\033[${STRING_LENGTH}G "
258 boot_mesg "Not available." ${WARNING}
259 echo_warning
260 ;;
261 *)
262 # This is how it is supposed to
263 # be called
264 echo_warning
265 ;;
266 esac
267 ;;
268
269 failure)
270 echo_failure
271 ;;
272
273 esac
274
275}
276
277reloadproc()
278{
279 if [ "${#}" = "0" ]; then
280 echo "Usage: reloadproc [{program}]"
281 exit 1
282 fi
283
284 getpids "${1}"
285
286 if [ -n "${pidlist}" ]; then
287 failure="0"
288 for pid in ${pidlist}
289 do
290 kill -"${RELOADSIG}" "${pid}" || failure="1"
291 done
292
293 (exit ${failure})
294 evaluate_retval
295
296 else
297 boot_mesg "Process ${1} not running." ${WARNING}
298 echo_warning
299 fi
300}
301
302statusproc()
303{
304 if [ "${#}" = "0" ]
305 then
306 echo "Usage: statusproc {program}"
307 exit 1
308 fi
309
310 getpids "${1}"
311
312 if [ -n "${pidlist}" ]; then
313 echo -e "${INFO}${base} is running with Process"\
314 "ID(s) ${pidlist}.${NORMAL}"
315 else
316 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
317 echo -e "${WARNING}${1} is not running but"\
318 "/var/run/${base}.pid exists.${NORMAL}"
319 else
320 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
321 echo -e "${WARNING}${1} is not running"\
322 "but ${PIDFILE} exists.${NORMAL}"
323 else
324 echo -e "${INFO}${1} is not running.${NORMAL}"
325 fi
326 fi
327 fi
328}
329
330# The below functions are documented in the LSB-generic 2.1.0
331
332#*******************************************************************************
333# Function - pidofproc [-s] [-p pidfile] pathname
334#
335# Purpose: This function returns one or more pid(s) for a particular daemon
336#
337# Inputs: -p pidfile, use the specified pidfile instead of pidof
338# pathname, path to the specified program
339#
340# Outputs: return 0 - Success, pid's in stdout
341# return 1 - Program is dead, pidfile exists
342# return 2 - Invalid or excessive number of arguments,
343# warning in stdout
344# return 3 - Program is not running
345#
346# Dependencies: pidof, echo, head
347#
348# Todo: Remove dependency on head
349# This depreciates getpids
350# Test changes to pidof
351#
352#*******************************************************************************
353pidofproc()
354{
355 local pidfile=""
356 local lpids=""
357 local silent=""
358 pidlist=""
359 while true
360 do
361 case "${1}" in
362 -p)
363 pidfile="${2}"
364 shift 2
365 ;;
366
367 -s)
368 # Added for legacy opperation of getpids
369 # eliminates several '> /dev/null'
370 silent="1"
371 shift 1
372 ;;
373 -*)
374 log_failure_msg "Unknown Option: ${1}"
375 return 2
376 ;;
377 *)
378 break
379 ;;
380 esac
381 done
382
383 if [ "${#}" != "1" ]; then
384 shift 1
385 log_failure_msg "Usage: pidofproc [-s] [-p pidfile] pathname"
386 return 2
387 fi
388
389 if [ -n "${pidfile}" ]; then
390 if [ ! -r "${pidfile}" ]; then
391 return 3 # Program is not running
392 fi
393
394 lpids=`head -n 1 ${pidfile}`
395 for pid in ${lpids}
396 do
397 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
398 kill -0 "${pid}" > /dev/null &&
399 pidlist="${pidlist} ${pid}"
400 fi
401
402 if [ "${silent}" -ne "1" ]; then
403 echo "${pidlist}"
404 fi
405
406 test -z "${pidlist}" &&
407 # Program is dead, pidfile exists
408 return 1
409 # else
410 return 0
411 done
412
413 else
414 pidlist=`pidof -o $$ -o $PPID -x "$1"`
415 if [ "x${silent}" != "x1" ]; then
416 echo "${pidlist}"
417 fi
418
419 # Get provide correct running status
420 if [ -n "${pidlist}" ]; then
421 return 0
422 else
423 return 3
424 fi
425
426 fi
427
428 if [ "$?" != "0" ]; then
429 return 3 # Program is not running
430 fi
431}
432
433# This will ensure compatibility with previous LFS Bootscripts
434getpids()
435{
436 if [ -z "${PIDFILE}" ]; then
437 pidofproc -s -p "${PIDFILE}" $@
438 else
439 pidofproc -s $@
440 fi
441 base="${1##*/}"
442}
443
444#*******************************************************************************
445# Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
446#
447# Purpose: This runs the specified program as a daemon
448#
449# Inputs: -f, run the program even if it is already running
450# -n nicelevel, specifies a nice level. See nice(1).
451# -p pidfile, uses the specified pidfile
452# pathname, pathname to the specified program
453# args, arguments to pass to specified program
454#
455# Outputs: return 0 - Success
456# return 2 - Invalid of excessive number of arguments,
457# warning in stdout
458# return 4 - Program or service status is unknown
459#
460# Dependencies: nice
461#
462# Todo: LSB says this should be called start_daemon
463# LSB does not say that it should call evaluate_retval
464# It checks for PIDFILE, which is deprecated.
465# Will be removed after BLFS 6.0
466# loadproc returns 0 if program is already running, not LSB compliant
467#
468#*******************************************************************************
469loadproc()
470{
471 local pidfile=""
472 local forcestart=""
473 local nicelevel="10"
474
475# This will ensure compatibility with previous LFS Bootscripts
476 if [ -n "${PIDFILE}" ]; then
477 pidfile="${PIDFILE}"
478 fi
479
480 while true
481 do
482 case "${1}" in
483 -f)
484 forcestart="1"
485 shift 1
486 ;;
487 -n)
488 nicelevel="${2}"
489 shift 2
490 ;;
491 -p)
492 pidfile="${2}"
493 shift 2
494 ;;
495 -*)
496 log_failure_msg "Unknown Option: ${1}"
497 return 2 #invalid or excess argument(s)
498 ;;
499 *)
500 break
501 ;;
502 esac
503 done
504
505 if [ "${#}" = "0" ]; then
506 log_failure_msg "Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
507 return 2 #invalid or excess argument(s)
508 fi
509
510 if [ -z "${forcestart}" ]; then
511 if [ -z "${pidfile}" ]; then
512 pidofproc -s "${1}"
513 else
514 pidofproc -s -p "${pidfile}" "${1}"
515 fi
516
517 case "${?}" in
518 0)
519 log_warning_msg "Unable to continue: ${1} is running"
520 return 0 # 4
521 ;;
522 1)
523 log_warning_msg "Unable to continue: ${pidfile} exists"
524 return 0 # 4
525 ;;
526 3)
527 ;;
528 *)
529 log_failure_msg "Unknown error code from pidofproc: ${?}"
530 return 4
531 ;;
532 esac
533 fi
534
535 nice -n "${nicelevel}" "${@}"
536 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
537 return 0
538}
539
540#*******************************************************************************
541# Function - killproc [-p pidfile] pathname [signal]
542#
543# Purpose:
544#
545# Inputs: -p pidfile, uses the specified pidfile
546# pathname, pathname to the specified program
547# signal, send this signal to pathname
548#
549# Outputs: return 0 - Success
550# return 2 - Invalid of excessive number of arguments,
551# warning in stdout
552# return 4 - Unknown Status
553#
554# Dependencies: kill
555#
556# Todo: LSB does not say that it should call evaluate_retval
557# It checks for PIDFILE, which is deprecated.
558# Will be removed after BLFS 6.0
559#
560#*******************************************************************************
561killproc()
562{
563 local pidfile=""
564 local killsig=""
565 pidlist=""
566
567# This will ensure compatibility with previous LFS Bootscripts
568 if [ -n "${PIDFILE}" ]; then
569 pidfile="${PIDFILE}"
570 fi
571
572 while true
573 do
574 case "${1}" in
575 -p)
576 pidfile="${2}"
577 shift 2
578 ;;
579 -*)
580 log_failure_msg "Unknown Option: ${1}"
581 return 2
582 ;;
583 *)
584 break
585 ;;
586 esac
587 done
588
589 if [ "${#}" = "2" ]; then
590 killsig="${2}"
591 elif [ "${#}" != "1" ]; then
592 shift 2
593 log_failure_msg "Usage: killproc [-p pidfile] pathname [signal]"
594 return 2
595 fi
596
597 if [ -z "${pidfile}" ]; then
598 pidofproc -s "${1}"
599 else
600 pidofproc -s -p "${pidfile}" "${1}"
601 fi
602
603 # Change....
604 if [ -n "${pidlist}" ]; then
605 for pid in ${pidlist}
606 do
607 kill -${killsig:-TERM} ${pid} 2>/dev/null
608 if [ -z "${killsig}" ]; then
609 # Wait up to 3 seconds, for ${pid} to terminate
610 local dtime=${KILLDELAY}
611 while [ "${dtime}" != "0" ]
612 do
613 kill -0 ${pid} 2>/dev/null || break
614 sleep 1
615 dtime=$(( ${dtime} - 1))
616 done
617 # If ${pid} is still running, kill it
618 kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
619 fi
620 done
621
622 if [ -z "${killsig}" ]; then
623 pidofproc -s "${1}"
624
625 # Program was terminated
626 if [ "$?" != "0" ]; then
627 # Pidfile Exists
628 if [ -f "${pidfile}" ]; then
629 rm -f "${pidfile}"
630 fi
631 echo_ok
632 return 0
633 else # Program is still running
634 echo_failure
635 return 4 # Unknown Status
636 fi
637 else
638 if [ -z "${pidfile}" ]; then
639 pidofproc -s "${1}"
640 else
641 pidofproc -s -p "${pidfile}" "${1}"
642 fi
643 fi
644
645 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
646
647 else
648 print_status warning not_running
649 fi
650}
651
652
653#*******************************************************************************
654# Function - log_success_msg "message"
655#
656# Purpose: Print a success message
657#
658# Inputs: $@ - Message
659#
660# Outputs: Text output to screen
661#
662# Dependencies: echo
663#
664# Todo: logging
665#
666#*******************************************************************************
667log_success_msg()
668{
669 echo -n -e "${BOOTMESG_PREFIX}${@}"
670 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
671 return 0
672}
673
674#*******************************************************************************
675# Function - log_failure_msg "message"
676#
677# Purpose: Print a failure message
678#
679# Inputs: $@ - Message
680#
681# Outputs: Text output to screen
682#
683# Dependencies: echo
684#
685# Todo: logging
686#
687#*******************************************************************************
688log_failure_msg() {
689 echo -n -e "${BOOTMESG_PREFIX}${@}"
690 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
691 return 0
692}
693
694#*******************************************************************************
695# Function - log_warning_msg "message"
696#
697# Purpose: print a warning message
698#
699# Inputs: $@ - Message
700#
701# Outputs: Text output to screen
702#
703# Dependencies: echo
704#
705# Todo: logging
706#
707#*******************************************************************************
708log_warning_msg() {
709 echo -n -e "${BOOTMESG_PREFIX}${@}"
710 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
711 return 0
712}
713
714# End $rc_base/init.d/functions
Note: See TracBrowser for help on using the repository browser.