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