source: bootscripts/lfs/init.d/functions@ 4c0c012

10.0 10.0-rc1 10.1 10.1-rc1 11.0 11.0-rc1 11.0-rc2 11.0-rc3 11.1 11.1-rc1 11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 6.5 6.6 6.7 6.8 7.0 7.1 7.2 7.3 7.4 7.5 7.5-systemd 7.6 7.6-systemd 7.7 7.7-systemd 7.8 7.8-systemd 7.9 7.9-systemd 8.0 8.1 8.2 8.3 8.4 9.0 9.1 arm bdubbs/gcc13 ml-11.0 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/lfs-next xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since 4c0c012 was 4c0c012, checked in by Bruce Dubbs <bdubbs@…>, 15 years ago

Reformatted several pages so pdf is properly generated.

git-svn-id: http://svn.linuxfromscratch.org/LFS/trunk/BOOK@8781 4aa44e1e-78dd-0310-a6d2-fbcd4c07a689

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