source: bootscripts/lfs/init.d/functions@ 176b4b6

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