source: bootscripts/lfs/init.d/functions@ 1bf7b61

clfs-1.2 clfs-2.1 clfs-3.0.0-systemd clfs-3.0.0-sysvinit systemd sysvinit
Last change on this file since 1bf7b61 was 6c32364, checked in by Jim Gifford <clfs@…>, 18 years ago

r4177@server: jim | 2006-06-29 16:00:12 -0700
Disable interactive Prompts. Added ccheck for /etc/sysconfig/createfiles.

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