source: bootscripts/contrib/lsb-v3/lib/lsb/init-functions@ cb95d5f

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 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 cb95d5f was cb95d5f, checked in by DJ Lucas <dj@…>, 13 years ago

Changed distribution directory structure.

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

  • Property mode set to 100644
File size: 21.0 KB
Line 
1# Begin /lib/lsb/init-funtions
2
3# Provides initialization funtions as defined by the Linux Standard Base
4# specification, version 3.1.0
5
6# Source rc configuration if not inherited from the environment
7if [ "${RC_BASE}" = "" ]; then
8 . /etc/sysconfig/rc
9fi
10
11# Source the distro functions file
12if [ "${DISTRO_MINI}" != "" ]; then
13 . "${RC_BASE}/init.d/${DISTRO_MINI}-functions"
14fi
15
16################################################################################
17# start_daemon() #
18# Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...] #
19# #
20# Purpose: This runs the specified program as a daemon #
21# #
22# Inputs: -f: (force) run the program even if it is already running. #
23# -n nicelevel: specify a nice level. See 'man nice(1)'. #
24# -p pidfile: use the specified file to determine PIDs. #
25# pathname: the complete path to the specified program #
26# args: additional arguments passed to the program (pathname) #
27# #
28# Return values (as defined by LSB exit codes): #
29# 0 - program is running or service is OK #
30# 1 - generic or unspecified error #
31# 2 - invalid or excessive argument(s) #
32# 5 - program is not installed #
33################################################################################
34start_daemon()
35{
36 local force=""
37 local nice="0"
38 local pidfile=""
39 local pidlist=""
40 local retval=""
41
42 # Process arguments
43 while true
44 do
45 case "${1}" in
46
47 -f)
48 force="1"
49 shift 1
50 ;;
51
52 -n)
53 nice="${2}"
54 shift 2
55 ;;
56
57 -p)
58 pidfile="${2}"
59 shift 2
60 ;;
61
62 -*)
63 return 2
64 ;;
65
66 *)
67 program="${1}"
68 break
69 ;;
70 esac
71 done
72
73 # Check for a valid program
74 if [ ! -e "${program}" ]
75 then
76 return 5
77 fi
78
79 # Execute
80 if [ -z "${force}" ]
81 then
82 if [ -z "${pidfile}" ]
83 then
84 # determine the pid by discovery
85 pidlist=`pidofproc "${1}"`
86 retval="${?}"
87 else
88 # The PID file contains the needed PIDs
89 # Note that by LSB requirement, the path must be given to pidofproc,
90 # however, it is not used by the current implementation or standard.
91 pidlist=`pidofproc -p "${pidfile}" "${1}"`
92 retval="${?}"
93 fi
94
95 # return a value ONLY
96 # It is the init script's (or distribution's functions) responsibilty
97 # to log messages!
98 case "${retval}" in
99
100 0)
101 # program is already running correctly, this is a
102 # succesful start.
103 return 0
104 ;;
105
106 1)
107 # program is not running, but an invalid pid file exists
108 # remove the pid file and continue
109 rm -f "${pidfile}"
110 ;;
111
112 3)
113 # program is not running and no pidfile exists
114 # do nothing here, let start_deamon continue.
115 ;;
116
117 *)
118 # Others as returned by status values shall not be interpreted
119 # and returned as an unspecified error.
120 return 1
121 ;;
122 esac
123 fi
124
125 # do the start!
126 nice -n "${nice}" "${@}"
127
128}
129
130################################################################################
131# killproc() #
132# Usage: killproc [-p pidfile] pathname [signal] #
133# #
134# Purpose: Send control signals to running processes #
135# #
136# Inputs: -p pidfile, uses the specified pidfile #
137# pathname, pathname to the specified program #
138# signal, send this signal to pathname #
139# #
140# Return values (as defined by LSB exit codes): #
141# 0 - program (pathname) has stopped/is already stopped or a #
142# running program has been sent specified signal and stopped #
143# successfully #
144# 1 - generic or unspecified error #
145# 2 - invalid or excessive argument(s) #
146# 5 - program is not installed #
147# 7 - program is not running and a signal was supplied #
148################################################################################
149killproc()
150{
151 local pidfile
152 local program
153 local prefix
154 local progname
155 local signal="-TERM"
156 local fallback="-KILL"
157 local nosig
158 local pidlist
159 local retval
160 local pid
161 local delay="30"
162 local piddead
163 local dtime
164
165 # Process arguments
166 while true
167 do
168 case "${1}" in
169
170 -p)
171 pidfile="${2}"
172 shift 2
173 ;;
174
175 *)
176 program="${1}"
177 if [ -n "${2}" ]
178 then
179 signal="${2}"
180 fallback=""
181 else
182 nosig=1
183 fi
184
185 # error on additional arguments
186 if [ -n "${3}" ]
187 then
188 return 2
189 else
190 break
191 fi
192 ;;
193 esac
194 done
195
196 # Check for a valid program
197 if [ ! -e "${program}" ]
198 then
199 return 5
200 fi
201
202 # Check for a valid signal
203 check_signal "${signal}"
204 if [ "${?}" -ne "0" ]
205 then
206 return 2
207 fi
208
209 # Get a list of pids
210 if [ -z "${pidfile}" ]
211 then
212 # determine the pid by discovery
213 pidlist=`pidofproc "${1}"`
214 retval="${?}"
215 else
216 # The PID file contains the needed PIDs
217 # Note that by LSB requirement, the path must be given to pidofproc,
218 # however, it is not used by the current implementation or standard.
219 pidlist=`pidofproc -p "${pidfile}" "${1}"`
220 retval="${?}"
221 fi
222
223 # return a value ONLY
224 # It is the init script's (or distribution's functions) responsibilty
225 # to log messages!
226 case "${retval}" in
227
228 0)
229 # program is running correctly
230 # do nothing here, let killproc continue.
231 ;;
232
233 1)
234 # program is not running, but an invalid pid file exists
235 # remove the pid file.
236 rm -f "${pidfile}"
237 # this is only a success if no signal was passed.
238 if [ -n "${nosig}" ]
239 then
240 return 0
241 else
242 return 7
243 fi
244 ;;
245
246 3)
247 # program is not running and no pidfile exists
248 # this is only a success if no signal was passed.
249 if [ -n "${nosig}" ]
250 then
251 return 0
252 else
253 return 7
254 fi
255 ;;
256
257 *)
258 # Others as returned by status values shall not be interpreted
259 # and returned as an unspecified error.
260 return 1
261 ;;
262 esac
263
264 # perform different actions for exit signals and control signals
265 check_sig_type "${signal}"
266 if [ "${?}" -eq "0" ] # signal is used to terminate the program
267 then
268 # account for empty pidlist (pid file still exists and nosignal was given)
269 if [ "${pidlist}" != "" ]; then
270 #kill the list of pids
271 for pid in ${pidlist}
272 do
273 kill -0 "${pid}" 2> /dev/null
274 if [ "${?}" -ne "0" ]; then
275 # process is dead, continue to next and assume all is well
276 continue
277 else
278 kill "${signal}" "${pid}" 2> /dev/null
279 # Wait up to ${delay}/10 seconds to for "${pid}" to
280 # terminate in 10ths of a second
281 while [ "${delay}" -ne "0" ]
282 do
283 kill -0 "${pid}" 2> /dev/null || piddead="1"
284 if [ "${piddead}" = "1" ]
285 then
286 break
287 fi
288 sleep 0.1
289 delay="$(( ${delay} - 1 ))"
290 done
291 # If a fallback is set, and program is still running, then
292 # use the fallback
293 if [ -n "${fallback}" -a "${piddead}" != "1" ]
294 then
295 kill "${fallback}" "${pid}" 2> /dev/null
296 sleep 1
297 # Check again, and fail if still running
298 kill -0 "${pid}" 2> /dev/null && return 1
299 else
300 # just check one last time and if still alive, fail
301 sleep 1
302 kill -0 "${pid}" 2> /dev/null && return 1
303 fi
304 fi
305 done
306 fi
307
308 # Check for and remove stale PID files.
309 if [ -z "${pidfile}" ]
310 then
311 #find the basename of $program
312 prefix=`echo "${program}" | sed 's/[^/]*$//'`
313 progname=`echo "${program}" | sed "s@${prefix}@@"`
314 if [ -e "/var/run/${progname}.pid" ]
315 then
316 rm -f "/var/run/${progname}.pid" 2> /dev/null
317 fi
318 else
319 if [ -e "${pidfile}" ]
320 then
321 rm -f "${pidfile}" 2> /dev/null
322 fi
323 fi
324
325 # For signals that do not expect a program to exit, simply
326 # let kill do it's job, and evaluate kills return for value
327 else # check_sig_type - signal is not used to terminate program
328 for pid in ${pidlist}
329 do
330 kill "${signal}" "${pid}"
331 if [ "${?}" -ne "0" ]; then
332 return 1
333 fi
334 done
335 fi
336}
337
338################################################################################
339# pidofproc() #
340# Usage: pidofproc [-p pidfile] pathname #
341# #
342# Purpose: This function returns one or more pid(s) for a particular daemon #
343# #
344# Inputs: -p pidfile, use the specified pidfile instead of pidof #
345# pathname, path to the specified program #
346# #
347# Return values (as defined by LSB status codes): #
348# 0 - Success (PIDs to stdout) #
349# 1 - Program is dead, PID file still exists (remaining PIDs output) #
350# 3 - Program is not running (no output) #
351################################################################################
352pidofproc()
353{
354
355local pidfile
356local program
357local prefix
358local progname
359local pidlist
360local lpids
361local exitstatus="0"
362
363 # Process arguments
364 while true
365 do
366 case "${1}" in
367
368 -p)
369 pidfile="${2}"
370 shift 2
371 ;;
372
373 *)
374 program="${1}"
375 if [ -n "${2}" ]
376 then
377 # Too many arguments
378 # Since this is status, return unknown
379 return 4
380 else
381 break
382 fi
383 ;;
384 esac
385 done
386
387 # If a PID file is not specified, try and find one.
388 if [ -z "${pidfile}" ]
389 then
390 # get the program's basename
391 prefix=`echo "${program}" | sed 's/[^/]*$//'`
392 progname=`echo "${program}" | sed "s@${prefix}@@"`
393 # if a PID file exists with that name, assume that is it.
394 if [ -e "/var/run/${progname}.pid" ]
395 then
396 pidfile="/var/run/${progname}.pid"
397 fi
398 fi
399
400 # if a PID file is set and exists, use it.
401 if [ -n "${pidfile}" -a -e "${pidfile}" ]
402 then
403 # use the value in the first line of the pidfile
404 pidlist=`/bin/head -n1 "${pidfile}"`
405 # This can optionally be written as 'sed 1q' to repalce 'head -n1'
406 # should LFS move /bin/head to /usr/bin/head
407 else
408 # use pidof
409 pidlist=`pidof "${program}"`
410 fi
411
412 # Figure out if all listed PIDs are running.
413 for pid in ${pidlist}
414 do
415 kill -0 ${pid} 2> /dev/null
416 if [ "${?}" -eq "0" ]; then
417 lpids="${pids}${pid} "
418 else
419 exitstatus="1"
420 fi
421 done
422
423 if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
424 return 3
425 else
426 echo "${lpids}"
427 return "${exitstatus}"
428 fi
429}
430################################################################################
431# log_success_msg() #
432# Usage: log_success_msg [$MESSAGE | "message"] #
433# #
434# Purpose: Print a successful status message to the screen and optionally #
435# a boot log file. #
436# #
437# Inputs: accepts one string value, either a quoted string or optionally #
438# the value of $MESSAGE if set in the running environment. #
439# #
440# Return values: Not used #
441################################################################################
442log_success_msg()
443{
444 echo -n -e "${PREFIX_SUCCESS}${@}"
445 echo -e "${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
446 if [ "${BOOTLOG_ENAB}" = "yes" ]; then
447 if [ $( hostname ) = "(none)" ]; then
448 BTTIMESPEC=""
449 else
450 BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
451 fi
452 echo "${BTTIMESPEC}bootlog: ${@} Successful" >> "${TEMPFS_MOUNT}/.bootlog"
453 fi
454 return 0
455}
456
457################################################################################
458# log_failure_msg() #
459# Usage: log_failure_msg [$MESSAGE | "message"] #
460# #
461# Purpose: Print a failure status message to the screen and optionally #
462# a boot log file. #
463# #
464# Inputs: accepts one string value, either a quoted string or optionally #
465# the value of $MESSAGE if set in the running environment. #
466# #
467# Return values: Not used #
468################################################################################
469log_failure_msg()
470{
471 echo -n -e "${PREFIX_FAILURE}${@}"
472 echo -e "${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
473 if [ "${BOOTLOG_ENAB}" = "yes" ]; then
474 if [ $( hostname ) = "(none)" ]; then
475 BTTIMESPEC=""
476 else
477 BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
478 fi
479 echo "${BTTIMESPEC}bootlog: ${@} Failed!" >> "${TEMPFS_MOUNT}/.bootlog"
480 fi
481 return 0
482}
483
484################################################################################
485# log_warning_msg() #
486# Usage: log_warning_msg [$MESSAGE | "message"] #
487# #
488# Purpose: Print a warning status message to the screen and optionally #
489# a boot log file. #
490# #
491# Inputs: accepts one string value, either a quoted string or optionally #
492# the value of $MESSAGE if set in the running environment. #
493# #
494# Return values: Not used #
495################################################################################
496log_warning_msg()
497{
498 echo -n -e "${PREFIX_WARNING}${@}"
499 echo -e "${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
500 if [ "${BOOTLOG_ENAB}" = "yes" ]; then
501 if [ $( hostname ) = "(none)" ]; then
502 BTTIMESPEC=""
503 else
504 BTTIMESPEC="$(echo `date -u +"%b %d %T"` `hostname`) "
505 fi
506 echo "${BTTIMESPEC}bootlog: ${@} Warning" >> "${TEMPFS_MOUNT}/.bootlog"
507 fi
508 return 0
509}
510
511################################################################################
512# check_signal() #
513# Usage: check_signal [ -{signal} | {signal} ] #
514# #
515# Purpose: Check for a valid signal. This is not defined by any LSB draft, #
516# however, it is required to check the signals to determine if the #
517# signals chosen are invalid arguments to the other functions. #
518# #
519# Inputs: accepts a single string value in the form or -{signal} or {signal} #
520# #
521# Return values: #
522# 0 - Success (signal is valid #
523# 1 - Signal is not valid #
524################################################################################
525check_signal()
526{
527 local valsig
528
529 # Add error handling for invalid signals
530 valsig="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
531 valsig="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
532 valsig="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
533 valsig="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
534 valsig="${valsig} -11 -13 -14 -15"
535
536 echo "${valsig}" | grep -- " ${1} " > /dev/null
537 if [ "${?}" -eq "0" ]
538 then
539 return 0
540 else
541 return 1
542 fi
543}
544
545
546################################################################################
547# check_sig_type() #
548# Usage: check_signal [ -{signal} | {signal} ] #
549# #
550# Purpose: Check if signal is a program termination signal or a control signal #
551# This is not defined by any LSB draft, however, it is required to #
552# check the signals to determine if they are intended to end a #
553# program or simply to control it. #
554# #
555# Inputs: accepts a single string value in the form or -{signal} or {signal} #
556# #
557# Return values: #
558# 0 - Signal is used for program termination #
559# 1 - Signal is used for program control #
560################################################################################
561check_sig_type()
562{
563 local valsig
564
565 # The list of termination signals (limited to generally used items)
566 valsig="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"
567
568 echo "${valsig}" | grep -- " ${1} " > /dev/null
569 if [ "${?}" -eq "0" ]
570 then
571 return 0
572 else
573 return 1
574 fi
575}
576
577# End /lib/lsb/init-functions
Note: See TracBrowser for help on using the repository browser.