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

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.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 eb18f5a was eb18f5a, checked in by Matthew Burgess <matthew@…>, 15 years ago

Remove the buggy legacy getpids() function. Fixes #2472.

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

  • Property mode set to 100644
File size: 16.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
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 replaces 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#*******************************************************************************
487# Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
488#
489# Purpose: This runs the specified program as a daemon
490#
491# Inputs: -f, run the program even if it is already running
492# -n nicelevel, specifies a nice level. See nice(1).
493# -p pidfile, uses the specified pidfile
494# pathname, pathname to the specified program
495# args, arguments to pass to specified program
496#
497# Outputs: return 0 - Success
498# return 2 - Invalid of excessive number of arguments,
499# warning in stdout
500# return 4 - Program or service status is unknown
501#
502# Dependencies: nice, rm
503#
504# Todo: LSB says this should be called start_daemon
505# LSB does not say that it should call evaluate_retval
506# It checks for PIDFILE, which is deprecated.
507# Will be removed after BLFS 6.0
508# loadproc returns 0 if program is already running, not LSB compliant
509#
510#*******************************************************************************
511loadproc()
512{
513 local pidfile=""
514 local forcestart=""
515 local nicelevel="10"
516
517# This will ensure compatibility with previous LFS Bootscripts
518 if [ -n "${PIDFILE}" ]; then
519 pidfile="${PIDFILE}"
520 fi
521
522 while true
523 do
524 case "${1}" in
525 -f)
526 forcestart="1"
527 shift 1
528 ;;
529 -n)
530 nicelevel="${2}"
531 shift 2
532 ;;
533 -p)
534 pidfile="${2}"
535 shift 2
536 ;;
537 -*)
538 log_failure_msg "Unknown Option: ${1}"
539 return 2 #invalid or excess argument(s)
540 ;;
541 *)
542 break
543 ;;
544 esac
545 done
546
547 if [ "${#}" = "0" ]; then
548 log_failure_msg "Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
549 return 2 #invalid or excess argument(s)
550 fi
551
552 if [ -z "${forcestart}" ]; then
553 if [ -z "${pidfile}" ]; then
554 pidofproc -s "${1}"
555 else
556 pidofproc -s -p "${pidfile}" "${1}"
557 fi
558
559 case "${?}" in
560 0)
561 log_warning_msg "Unable to continue: ${1} is running"
562 return 0 # 4
563 ;;
564 1)
565 boot_mesg "Removing stale pid file: ${pidfile}" ${WARNING}
566 rm -f "${pidfile}"
567 ;;
568 3)
569 ;;
570 *)
571 log_failure_msg "Unknown error code from pidofproc: ${?}"
572 return 4
573 ;;
574 esac
575 fi
576
577 nice -n "${nicelevel}" "${@}"
578 evaluate_retval # This is "Probably" not LSB compliant,
579# but required to be compatible with older bootscripts
580 return 0
581}
582
583#*******************************************************************************
584# Function - killproc [-p pidfile] pathname [signal]
585#
586# Purpose:
587#
588# Inputs: -p pidfile, uses the specified pidfile
589# pathname, pathname to the specified program
590# signal, send this signal to pathname
591#
592# Outputs: return 0 - Success
593# return 2 - Invalid of excessive number of arguments,
594# warning in stdout
595# return 4 - Unknown Status
596#
597# Dependencies: kill, rm
598#
599# Todo: LSB does not say that it should call evaluate_retval
600# It checks for PIDFILE, which is deprecated.
601# Will be removed after BLFS 6.0
602#
603#*******************************************************************************
604killproc()
605{
606 local pidfile=""
607 local killsig=TERM # default signal is SIGTERM
608 pidlist=""
609
610 # This will ensure compatibility with previous LFS Bootscripts
611 if [ -n "${PIDFILE}" ]; then
612 pidfile="${PIDFILE}"
613 fi
614
615 while true
616 do
617 case "${1}" in
618 -p)
619 pidfile="${2}"
620 shift 2
621 ;;
622 -*)
623 log_failure_msg "Unknown Option: ${1}"
624 return 2
625 ;;
626 *)
627 break
628 ;;
629 esac
630 done
631
632 if [ "${#}" = "2" ]; then
633 killsig="${2}"
634 elif [ "${#}" != "1" ]; then
635 shift 2
636 log_failure_msg "Usage: killproc [-p pidfile] pathname [signal]"
637 return 2
638 fi
639
640 # Is the process running?
641 if [ -z "${pidfile}" ]; then
642 pidofproc -s "${1}"
643 else
644 pidofproc -s -p "${pidfile}" "${1}"
645 fi
646
647 # Remove stale pidfile
648 if [ "$?" = 1 ]; then
649 boot_mesg "Removing stale pid file: ${pidfile}." ${WARNING}
650 rm -f "${pidfile}"
651 fi
652
653 # If running, send the signal
654 if [ -n "${pidlist}" ]; then
655 for pid in ${pidlist}
656 do
657 kill -${killsig} ${pid} 2>/dev/null
658
659 # Wait up to 3 seconds, for ${pid} to terminate
660 case "${killsig}" in
661 TERM|SIGTERM|KILL|SIGKILL)
662 # sleep in 1/10ths of seconds and
663 # multiply KILLDELAY by 10
664 local dtime="${KILLDELAY}0"
665 while [ "${dtime}" != "0" ]
666 do
667 kill -0 ${pid} 2>/dev/null || break
668 sleep 0.1
669 dtime=$(( ${dtime} - 1))
670 done
671 # If ${pid} is still running, kill it
672 kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
673 ;;
674 esac
675 done
676
677 # Check if the process is still running if we tried to stop it
678 case "${killsig}" in
679 TERM|SIGTERM|KILL|SIGKILL)
680 if [ -z "${pidfile}" ]; then
681 pidofproc -s "${1}"
682 else
683 pidofproc -s -p "${pidfile}" "${1}"
684 fi
685
686 # Program was terminated
687 if [ "$?" != "0" ]; then
688 # Remove the pidfile if necessary
689 if [ -f "${pidfile}" ]; then
690 rm -f "${pidfile}"
691 fi
692 echo_ok
693 return 0
694 else # Program is still running
695 echo_failure
696 return 4 # Unknown Status
697 fi
698 ;;
699 *)
700 # Just see if the kill returned successfully
701 evaluate_retval
702 ;;
703 esac
704 else # process not running
705 print_status warning not_running
706 fi
707}
708
709
710#*******************************************************************************
711# Function - log_success_msg "message"
712#
713# Purpose: Print a success message
714#
715# Inputs: $@ - Message
716#
717# Outputs: Text output to screen
718#
719# Dependencies: echo
720#
721# Todo: logging
722#
723#*******************************************************************************
724log_success_msg()
725{
726 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
727 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
728 return 0
729}
730
731#*******************************************************************************
732# Function - log_failure_msg "message"
733#
734# Purpose: Print a failure message
735#
736# Inputs: $@ - Message
737#
738# Outputs: Text output to screen
739#
740# Dependencies: echo
741#
742# Todo: logging
743#
744#*******************************************************************************
745log_failure_msg() {
746 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
747 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
748 return 0
749}
750
751#*******************************************************************************
752# Function - log_warning_msg "message"
753#
754# Purpose: print a warning message
755#
756# Inputs: $@ - Message
757#
758# Outputs: Text output to screen
759#
760# Dependencies: echo
761#
762# Todo: logging
763#
764#*******************************************************************************
765log_warning_msg() {
766 ${ECHO} -n -e "${BOOTMESG_PREFIX}${@}"
767 ${ECHO} -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
768 return 0
769}
770
771# End $rc_base/init.d/functions
Note: See TracBrowser for help on using the repository browser.