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