1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
5 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
7 D.2. /lib/lsb/init-functions
9 <link rel="stylesheet" href="../stylesheets/lfs.css" type="text/css" />
10 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
11 <link rel="stylesheet" href="../stylesheets/lfs-print.css" type=
12 "text/css" media="print" />
14 <body class="lfs" id="lfs-7.2">
15 <div class="navheader">
17 Linux From Scratch - Version 7.2
20 付録 D. ブートスクリプトと sysconfig スクリプト version-20120901
24 <a accesskey="p" href="apds01.html" title=
25 "/etc/rc.d/init.d/rc">前のページ</a>
31 <a accesskey="n" href="apds03.html" title=
32 "/etc/rc.d/init.d/functions">次のページ</a>
34 /etc/rc.d/init.d/functions
38 <a accesskey="u" href="scripts.html" title=
39 "付録 D. ブートスクリプトと sysconfig スクリプト version-20120901">上に戻る</a>
42 <a accesskey="h" href="../index.html" title=
43 "Linux From Scratch - Version 7.2">ホーム</a>
47 <div class="wrap" lang="ja" xml:lang="ja">
49 <a id="init-functions" name="init-functions"></a>D.2.
50 /lib/lsb/init-functions
54 ########################################################################
56 # Begin /lib/lsb/init-funtions
58 # Description : Run Level Control Functions
60 # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
61 # : DJ Lucas - dj@linuxfromscratch.org
62 # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
66 # Notes : With code based on Matthias Benkmann's simpleinit-msb
67 # http://winterdrache.de/linux/newboot/index.html
69 # The file should be located in /lib/lsb
71 ########################################################################
73 ## Environmental setup
74 # Setup default values for environment
76 export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
79 # Find current screen size
80 if [ -z "${COLUMNS}" ]; then
82 COLUMNS=${COLUMNS##* }
85 # When using remote connections, such as a serial port, stty size returns 0
86 if [ "${COLUMNS}" = "0" ]; then
90 ## Measurements for positioning result messages
91 COL=$((${COLUMNS} - 8))
94 ## Set Cursor Position Commands, used via echo
95 SET_COL="\\033[${COL}G" # at the $COL char
96 SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
97 CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
100 ## Set color commands, used via echo
101 # Please consult `man console_codes for more information
102 # under the "ECMA-48 Set Graphics Rendition" section
104 # Warning: when switching from a 8bit to a 9bit font,
105 # the linux console will reinterpret the bold (1;) to
106 # the top 256 glyphs of the 9bit font. This does
107 # not affect framebuffer consoles
109 NORMAL="\\033[0;39m" # Standard console grey
110 SUCCESS="\\033[1;32m" # Success is green
111 WARNING="\\033[1;33m" # Warnings are yellow
112 FAILURE="\\033[1;31m" # Failures are red
113 INFO="\\033[1;36m" # Information is light cyan
114 BRACKET="\\033[1;34m" # Brackets are blue
116 # Use a colored prefix
118 SUCCESS_PREFIX="${SUCCESS} * ${NORMAL}"
119 FAILURE_PREFIX="${FAILURE}*****${NORMAL}"
120 WARNING_PREFIX="${WARNING} *** ${NORMAL}"
122 SUCCESS_SUFFIX="${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
123 FAILURE_SUFFIX="${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
124 WARNING_SUFFIX="${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
126 BOOTLOG=/run/var/bootlog
129 # Set any user specified environment variables e.g. HEADLESS
130 [ -r /etc/sysconfig/rc.site ] && . /etc/sysconfig/rc.site
132 ################################################################################
134 # Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
136 # Purpose: This runs the specified program as a daemon #
138 # Inputs: -f: (force) run the program even if it is already running. #
139 # -n nicelevel: specify a nice level. See 'man nice(1)'. #
140 # -p pidfile: use the specified file to determine PIDs. #
141 # pathname: the complete path to the specified program #
142 # args: additional arguments passed to the program (pathname) #
144 # Return values (as defined by LSB exit codes): #
145 # 0 - program is running or service is OK #
146 # 1 - generic or unspecified error #
147 # 2 - invalid or excessive argument(s) #
148 # 5 - program is not installed #
149 ################################################################################
189 # Check for a valid program
190 if [ ! -e "${program}" ]; then return 5; fi
193 if [ -z "${force}" ]; then
194 if [ -z "${pidfile}" ]; then
195 # Determine the pid by discovery
196 pidlist=`pidofproc "${1}"`
199 # The PID file contains the needed PIDs
200 # Note that by LSB requirement, the path must be given to pidofproc,
201 # however, it is not used by the current implementation or standard.
202 pidlist=`pidofproc -p "${pidfile}" "${1}"`
206 # Return a value ONLY
207 # It is the init script's (or distribution's functions) responsibilty
212 # Program is already running correctly, this is a
218 # Program is not running, but an invalid pid file exists
219 # remove the pid file and continue
224 # Program is not running and no pidfile exists
225 # do nothing here, let start_deamon continue.
229 # Others as returned by status values shall not be interpreted
230 # and returned as an unspecified error.
237 nice -n "${nice}" "${@}"
240 ################################################################################
242 # Usage: killproc [-p pidfile] pathname [signal] #
244 # Purpose: Send control signals to running processes #
246 # Inputs: -p pidfile, uses the specified pidfile #
247 # pathname, pathname to the specified program #
248 # signal, send this signal to pathname #
250 # Return values (as defined by LSB exit codes): #
251 # 0 - program (pathname) has stopped/is already stopped or a #
252 # running program has been sent specified signal and stopped #
254 # 1 - generic or unspecified error #
255 # 2 - invalid or excessive argument(s) #
256 # 5 - program is not installed #
257 # 7 - program is not running and a signal was supplied #
258 ################################################################################
266 local fallback="-KILL"
285 if [ -n "${2}" ]; then
292 # Error on additional arguments
293 if [ -n "${3}" ]; then
302 # Check for a valid program
303 if [ ! -e "${program}" ]; then return 5; fi
305 # Check for a valid signal
306 check_signal "${signal}"
307 if [ "${?}" -ne "0" ]; then return 2; fi
310 if [ -z "${pidfile}" ]; then
311 # determine the pid by discovery
312 pidlist=`pidofproc "${1}"`
315 # The PID file contains the needed PIDs
316 # Note that by LSB requirement, the path must be given to pidofproc,
317 # however, it is not used by the current implementation or standard.
318 pidlist=`pidofproc -p "${pidfile}" "${1}"`
322 # Return a value ONLY
323 # It is the init script's (or distribution's functions) responsibilty
328 # Program is running correctly
329 # Do nothing here, let killproc continue.
333 # Program is not running, but an invalid pid file exists
334 # Remove the pid file.
337 # This is only a success if no signal was passed.
338 if [ -n "${nosig}" ]; then
346 # Program is not running and no pidfile exists
347 # This is only a success if no signal was passed.
348 if [ -n "${nosig}" ]; then
356 # Others as returned by status values shall not be interpreted
357 # and returned as an unspecified error.
362 # Perform different actions for exit signals and control signals
363 check_sig_type "${signal}"
365 if [ "${?}" -eq "0" ]; then # Signal is used to terminate the program
367 # Account for empty pidlist (pid file still exists and no
369 if [ "${pidlist}" != "" ]; then
371 # Kill the list of pids
372 for pid in ${pidlist}; do
374 kill -0 "${pid}" 2> /dev/null
376 if [ "${?}" -ne "0" ]; then
377 # Process is dead, continue to next and assume all is well
380 kill "${signal}" "${pid}" 2> /dev/null
382 # Wait up to ${delay}/10 seconds to for "${pid}" to
383 # terminate in 10ths of a second
385 while [ "${delay}" -ne "0" ]; do
386 kill -0 "${pid}" 2> /dev/null || piddead="1"
387 if [ "${piddead}" = "1" ]; then break; fi
389 delay="$(( ${delay} - 1 ))"
392 # If a fallback is set, and program is still running, then
394 if [ -n "${fallback}" -a "${piddead}" != "1" ]; then
395 kill "${fallback}" "${pid}" 2> /dev/null
397 # Check again, and fail if still running
398 kill -0 "${pid}" 2> /dev/null && return 1
400 # just check one last time and if still alive, fail
402 kill -0 "${pid}" 2> /dev/null && return 1
408 # Check for and remove stale PID files.
409 if [ -z "${pidfile}" ]; then
410 # Find the basename of $program
411 prefix=`echo "${program}" | sed 's/[^/]*$//'`
412 progname=`echo "${program}" | sed "s@${prefix}@@"`
414 if [ -e "/var/run/${progname}.pid" ]; then
415 rm -f "/var/run/${progname}.pid" 2> /dev/null
418 if [ -e "${pidfile}" ]; then rm -f "${pidfile}" 2> /dev/null; fi
421 # For signals that do not expect a program to exit, simply
422 # let kill do it's job, and evaluate kills return for value
424 else # check_sig_type - signal is not used to terminate program
425 for pid in ${pidlist}; do
426 kill "${signal}" "${pid}"
427 if [ "${?}" -ne "0" ]; then return 1; fi
432 ################################################################################
434 # Usage: pidofproc [-p pidfile] pathname #
436 # Purpose: This function returns one or more pid(s) for a particular daemon #
438 # Inputs: -p pidfile, use the specified pidfile instead of pidof #
439 # pathname, path to the specified program #
441 # Return values (as defined by LSB status codes): #
442 # 0 - Success (PIDs to stdout) #
443 # 1 - Program is dead, PID file still exists (remaining PIDs output) #
444 # 3 - Program is not running (no output) #
445 ################################################################################
467 if [ -n "${2}" ]; then
469 # Since this is status, return unknown
478 # If a PID file is not specified, try and find one.
479 if [ -z "${pidfile}" ]; then
480 # Get the program's basename
481 prefix=`echo "${program}" | sed 's/[^/]*$//'`
483 if [ -z "${prefix}" ]; then
484 progname="${program}"
486 progname=`echo "${program}" | sed "s@${prefix}@@"`
489 # If a PID file exists with that name, assume that is it.
490 if [ -e "/var/run/${progname}.pid" ]; then
491 pidfile="/var/run/${progname}.pid"
495 # If a PID file is set and exists, use it.
496 if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
498 # Use the value in the first line of the pidfile
499 pidlist=`/bin/head -n1 "${pidfile}"`
500 # This can optionally be written as 'sed 1q' to repalce 'head -n1'
501 # should LFS move /bin/head to /usr/bin/head
504 pidlist=`pidof "${program}"`
507 # Figure out if all listed PIDs are running.
508 for pid in ${pidlist}; do
509 kill -0 ${pid} 2> /dev/null
511 if [ "${?}" -eq "0" ]; then
512 lpids="${lpids}${pid} "
518 if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
522 return "${exitstatus}"
526 ################################################################################
528 # Usage: statusproc [-p pidfile] pathname #
530 # Purpose: This function prints the status of a particular daemon to stdout #
532 # Inputs: -p pidfile, use the specified pidfile instead of pidof #
533 # pathname, path to the specified program #
536 # 0 - Status printed #
537 # 1 - Input error. The daemon to check was not specified. #
538 ################################################################################
544 if [ "${#}" = "0" ]; then
545 echo "Usage: statusproc [-p pidfle] {program}"
559 if [ -n "${2}" ]; then
560 echo "Too many arguments"
569 if [ -n "${pidfile}" ]; then
570 pidlist=`pidofproc -p "${pidfile}" $@`
572 pidlist=`pidofproc $@`
575 # Trim trailing blanks
576 pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`
580 if [ -n "${pidlist}" ]; then
581 echo -e "${INFO}${base} is running with Process" \
582 "ID(s) ${pidlist}.${NORMAL}"
584 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
585 echo -e "${WARNING}${1} is not running but" \
586 "/var/run/${base}.pid exists.${NORMAL}"
588 if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
589 echo -e "${WARNING}${1} is not running" \
590 "but ${pidfile} exists.${NORMAL}"
592 echo -e "${INFO}${1} is not running.${NORMAL}"
598 ################################################################################
601 # Purpose: An internal utility function to format a timestamp #
602 # a boot log file. Sets the STAMP variable. #
604 # Return value: Not used #
605 ################################################################################
608 STAMP="$(echo `date +"%b %d %T %:z"` `hostname`) "
612 ################################################################################
613 # log_success_msg() #
614 # Usage: log_success_msg ["message"] #
616 # Purpose: Print a successful status message to the screen and #
619 # Inputs: $@ - Message #
621 # Return values: Not used #
622 ################################################################################
625 echo -n -e "${BMPREFIX}${@}"
626 echo -e "${CURS_ZERO}${SUCCESS_PREFIX}${SET_COL}${SUCCESS_SUFFIX}"
628 # Strip non-printable characters from log file
629 local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
632 echo -e "${STAMP} ${logmessage} OK" >> ${BOOTLOG}
639 echo -n -e "${BMPREFIX}${@}"
640 echo -e "${CURS_ZERO}${SUCCESS_PREFIX}${SET_COL}${SUCCESS_SUFFIX}"
642 echo " OK" >> ${BOOTLOG}
647 ################################################################################
648 # log_failure_msg() #
649 # Usage: log_failure_msg ["message"] #
651 # Purpose: Print a failure status message to the screen and #
654 # Inputs: $@ - Message #
656 # Return values: Not used #
657 ################################################################################
660 echo -n -e "${BMPREFIX}${@}"
661 echo -e "${CURS_ZERO}${FAILURE_PREFIX}${SET_COL}${FAILURE_SUFFIX}"
663 # Strip non-printable characters from log file
666 local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
667 echo -e "${STAMP} ${logmessage} FAIL" >> ${BOOTLOG}
674 echo -n -e "${BMPREFIX}${@}"
675 echo -e "${CURS_ZERO}${FAILURE_PREFIX}${SET_COL}${FAILURE_SUFFIX}"
677 echo "FAIL" >> ${BOOTLOG}
682 ################################################################################
683 # log_warning_msg() #
684 # Usage: log_warning_msg ["message"] #
686 # Purpose: Print a warning status message to the screen and #
689 # Return values: Not used #
690 ################################################################################
693 echo -n -e "${BMPREFIX}${@}"
694 echo -e "${CURS_ZERO}${WARNING_PREFIX}${SET_COL}${WARNING_SUFFIX}"
696 # Strip non-printable characters from log file
697 local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
699 echo -e "${STAMP} ${logmessage} WARN" >> ${BOOTLOG}
704 ################################################################################
706 # Usage: log_info_msg message #
708 # Purpose: Print an information message to the screen and #
709 # a boot log file. Does not print a trailing newline character. #
711 # Return values: Not used #
712 ################################################################################
715 echo -n -e "${BMPREFIX}${@}"
717 # Strip non-printable characters from log file
718 local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
720 echo -n -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
729 # Strip non-printable characters from log file
730 local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
731 echo -n -e "${logmessage}" >> ${BOOTLOG}
736 ################################################################################
737 # evaluate_retval() #
738 # Usage: Evaluate a return value and print success or failyure as appropriate #
740 # Purpose: Convenience function to terminate an info message #
742 # Return values: Not used #
743 ################################################################################
746 local error_value="${?}"
748 if [ ${error_value} = 0 ]; then
755 ################################################################################
757 # Usage: check_signal [ -{signal} | {signal} ] #
759 # Purpose: Check for a valid signal. This is not defined by any LSB draft, #
760 # however, it is required to check the signals to determine if the #
761 # signals chosen are invalid arguments to the other functions. #
763 # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
766 # 0 - Success (signal is valid #
767 # 1 - Signal is not valid #
768 ################################################################################
773 # Add error handling for invalid signals
774 valsig="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
775 valsig="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
776 valsig="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
777 valsig="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
778 valsig="${valsig} -11 -13 -14 -15"
780 echo "${valsig}" | grep -- " ${1} " > /dev/null
782 if [ "${?}" -eq "0" ]; then
789 ################################################################################
791 # Usage: check_signal [ -{signal} | {signal} ] #
793 # Purpose: Check if signal is a program termination signal or a control signal #
794 # This is not defined by any LSB draft, however, it is required to #
795 # check the signals to determine if they are intended to end a #
796 # program or simply to control it. #
798 # Inputs: Accepts a single string value in the form or -{signal} or {signal} #
801 # 0 - Signal is used for program termination #
802 # 1 - Signal is used for program control #
803 ################################################################################
808 # The list of termination signals (limited to generally used items)
809 valsig="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"
811 echo "${valsig}" | grep -- " ${1} " > /dev/null
813 if [ "${?}" -eq "0" ]; then
820 ################################################################################
823 # Purpose: Wait for the user to respond if not a headless system #
825 ################################################################################
828 # Wait for the user by default
829 [ "${HEADLESS=0}" = "0" ] && read ENTER
833 ################################################################################
836 # Purpose: Utility to test if a variable is true | yes | 1 #
838 ################################################################################
841 [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] || [ "$1" = "y" ] ||
845 # End /lib/lsb/init-functions
848 <div class="navfooter">
851 <a accesskey="p" href="apds01.html" title=
852 "/etc/rc.d/init.d/rc">前のページ</a>
858 <a accesskey="n" href="apds03.html" title=
859 "/etc/rc.d/init.d/functions">次のページ</a>
861 /etc/rc.d/init.d/functions
865 <a accesskey="u" href="scripts.html" title=
866 "付録 D. ブートスクリプトと sysconfig スクリプト version-20120901">上に戻る</a>
869 <a accesskey="h" href="../index.html" title=
870 "Linux From Scratch - Version 7.2">ホーム</a>