1 | # -*-Shell-script-*- |
---|
2 | # |
---|
3 | # functions This file contains functions to be used by most or all |
---|
4 | # shell scripts in the /etc/init.d directory. |
---|
5 | # |
---|
6 | |
---|
7 | TEXTDOMAIN=initscripts |
---|
8 | |
---|
9 | # Make sure umask is sane |
---|
10 | umask 022 |
---|
11 | |
---|
12 | # First set up a default search path. |
---|
13 | export PATH="/sbin:/usr/sbin:/bin:/usr/bin" |
---|
14 | |
---|
15 | # Get a sane screen width, and default to 80 when exact info not available |
---|
16 | [ -z "${COLUMNS:-}" ] && COLUMNS=`stty -a 2>/dev/null | sed -n 's/.*columns \([0-9]*\);.*/\1/p'` |
---|
17 | [ -z "${COLUMNS:-}" ] && COLUMNS=80 |
---|
18 | |
---|
19 | # Get current console type |
---|
20 | [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="`/sbin/consoletype`" |
---|
21 | |
---|
22 | if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then |
---|
23 | . /etc/sysconfig/i18n |
---|
24 | if [ "${LANG::5}" = "ja_JP" -a "${CONSOLETYPE}" != "pty" ]; then |
---|
25 | export LANG |
---|
26 | export LC_MESSAGES=C |
---|
27 | export LC_TIME=C |
---|
28 | elif [ "${LANG:-}" = "ko_KR.eucKR" -a "${CONSOLETYPE}" != "pty" ]; then |
---|
29 | unset LANG |
---|
30 | elif [ "${LANG:-}" = "zh_CN.GB2312" -a "${CONSOLETYPE}" != "pty" ]; then |
---|
31 | unset LANG |
---|
32 | elif [ "${LANG:-}" = "zh_TW.Big5" -a "${CONSOLETYPE}" != "pty" ]; then |
---|
33 | unset LANG |
---|
34 | else |
---|
35 | export LANG |
---|
36 | fi |
---|
37 | fi |
---|
38 | |
---|
39 | # Read in our configuration |
---|
40 | if [ -z "${BOOTUP:-}" ]; then |
---|
41 | if [ -f /etc/sysconfig/init ]; then |
---|
42 | . /etc/sysconfig/init |
---|
43 | else |
---|
44 | # This all seem confusing? Look in /etc/sysconfig/init, |
---|
45 | # or in /usr/doc/initscripts-*/sysconfig.txt |
---|
46 | BOOTUP=color |
---|
47 | RES_COL=$((COLUMNS - 15)) |
---|
48 | MOVE_TO_COL="echo -en \\033[${RES_COL}G" |
---|
49 | SETCOLOR_SUCCESS="echo -en \\033[1;32m" |
---|
50 | SETCOLOR_FAILURE="echo -en \\033[1;31m" |
---|
51 | SETCOLOR_WARNING="echo -en \\033[1;33m" |
---|
52 | SETCOLOR_NORMAL="echo -en \\033[0;39m" |
---|
53 | LOGLEVEL=1 |
---|
54 | fi |
---|
55 | if [ "${CONSOLETYPE}" = "serial" ]; then |
---|
56 | BOOTUP=serial |
---|
57 | MOVE_TO_COL= |
---|
58 | SETCOLOR_SUCCESS= |
---|
59 | SETCOLOR_FAILURE= |
---|
60 | SETCOLOR_WARNING= |
---|
61 | SETCOLOR_NORMAL= |
---|
62 | fi |
---|
63 | fi |
---|
64 | |
---|
65 | if [ "${BOOTUP:-}" != "verbose" ]; then |
---|
66 | INITLOG_ARGS="-q" |
---|
67 | else |
---|
68 | INITLOG_ARGS= |
---|
69 | fi |
---|
70 | |
---|
71 | # gprintf from mandriva |
---|
72 | gprintf() { |
---|
73 | if [ -x /bin/gettext -a -n "$1" ]; then |
---|
74 | if [ -n "$GP_LANG" ]; then |
---|
75 | local TEXT=`LC_ALL=$GP_LANG LANGUAGE=$GP_LANGUAGE gettext -e --domain=$TEXTDOMAIN -- "$1"` |
---|
76 | else |
---|
77 | local TEXT=`gettext -e --domain=$TEXTDOMAIN -- "$1"` |
---|
78 | fi |
---|
79 | else |
---|
80 | local TEXT=$1 |
---|
81 | fi |
---|
82 | [ "${1#*\\n}" ] || TEXT="$TEXT\n" |
---|
83 | |
---|
84 | shift |
---|
85 | printf -- "$TEXT" "$@" |
---|
86 | } |
---|
87 | |
---|
88 | # Frontend to gprintf (support up to 4 %s in format string) |
---|
89 | # returns the message transleted in GPRINTF_MSG and |
---|
90 | # the resting parms in GPRINTF_REST |
---|
91 | # This simplifies a lot the call of functions like action, |
---|
92 | # now with i18n support |
---|
93 | gprintf_msg_rest() { |
---|
94 | case "$1" in |
---|
95 | *%s*%s*%s*%s*) |
---|
96 | GPRINTF_MSG=$(gprintf "$1" "$2" "$3" "$4" "$5") |
---|
97 | shift 5;; |
---|
98 | *%s*%s*%s*) |
---|
99 | GPRINTF_MSG=$(gprintf "$1" "$2" "$3" "$4") |
---|
100 | shift 4;; |
---|
101 | *%s*%s*) |
---|
102 | GPRINTF_MSG=$(gprintf "$1" "$2" "$3") |
---|
103 | shift 3;; |
---|
104 | *%s*) |
---|
105 | GPRINTF_MSG=$(gprintf "$1" "$2") |
---|
106 | shift 2;; |
---|
107 | *) |
---|
108 | GPRINTF_MSG=$(gprintf "$1") |
---|
109 | shift;; |
---|
110 | esac |
---|
111 | GPRINTF_REST="$@" |
---|
112 | } |
---|
113 | |
---|
114 | # Interpret escape sequences in an fstab entry |
---|
115 | fstab_decode_str() { |
---|
116 | fstab-decode echo "$1" |
---|
117 | } |
---|
118 | |
---|
119 | # Check if $pid (could be plural) are running |
---|
120 | checkpid() { |
---|
121 | local i |
---|
122 | for i in $* ; do |
---|
123 | [ -d "/proc/$1" ] && return 0 |
---|
124 | done |
---|
125 | return 1 |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | __readlink() { |
---|
130 | ls -bl "$@" 2>/dev/null| awk '{ print $NF }' |
---|
131 | } |
---|
132 | |
---|
133 | __fgrep() { |
---|
134 | s=$1 |
---|
135 | f=$2 |
---|
136 | while read line; do |
---|
137 | if strstr "$line" "$s"; then |
---|
138 | echo $line |
---|
139 | return 0 |
---|
140 | fi |
---|
141 | done < $f |
---|
142 | return 1 |
---|
143 | } |
---|
144 | |
---|
145 | # __umount_loop awk_program fstab_file first_msg retry_msg umount_args |
---|
146 | # awk_program should process fstab_file and return a list of fstab-encoded |
---|
147 | # paths; it doesn't have to handle comments in fstab_file. |
---|
148 | __umount_loop() { |
---|
149 | local remaining sig= |
---|
150 | local retry=3 count |
---|
151 | |
---|
152 | remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) |
---|
153 | while [ -n "$remaining" -a "$retry" -gt 0 ]; do |
---|
154 | if [ "$retry" -eq 3 ]; then |
---|
155 | action "$3" fstab-decode umount $5 $remaining |
---|
156 | else |
---|
157 | action "$4" fstab-decode umount $5 $remaining |
---|
158 | fi |
---|
159 | count=4 |
---|
160 | remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) |
---|
161 | while [ "$count" -gt 0 ]; do |
---|
162 | [ -z "$remaining" ] && break |
---|
163 | count=$(($count-1)) |
---|
164 | usleep 500000 |
---|
165 | remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) |
---|
166 | done |
---|
167 | [ -z "$remaining" ] && break |
---|
168 | fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null |
---|
169 | sleep 3 |
---|
170 | retry=$(($retry -1)) |
---|
171 | sig=-9 |
---|
172 | done |
---|
173 | } |
---|
174 | |
---|
175 | # Similar to __umount loop above, specialized for loopback devices |
---|
176 | __umount_loopback_loop() { |
---|
177 | local remaining devremaining sig= |
---|
178 | local retry=3 |
---|
179 | |
---|
180 | remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts) |
---|
181 | devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts) |
---|
182 | while [ -n "$remaining" -a "$retry" -gt 0 ]; do |
---|
183 | if [ "$retry" -eq 3 ]; then |
---|
184 | action $"Unmounting loopback filesystems: " \ |
---|
185 | fstab-decode umount $remaining |
---|
186 | else |
---|
187 | action $"Unmounting loopback filesystems (retry):" \ |
---|
188 | fstab-decode umount $remaining |
---|
189 | fi |
---|
190 | for dev in $devremaining ; do |
---|
191 | losetup $dev > /dev/null 2>&1 && \ |
---|
192 | action $"Detaching loopback device $dev: " \ |
---|
193 | losetup -d $dev |
---|
194 | done |
---|
195 | remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts) |
---|
196 | devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts) |
---|
197 | [ -z "$remaining" ] && break |
---|
198 | fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null |
---|
199 | sleep 3 |
---|
200 | retry=$(($retry -1)) |
---|
201 | sig=-9 |
---|
202 | done |
---|
203 | } |
---|
204 | |
---|
205 | # __proc_pids {program} [pidfile] |
---|
206 | # Set $pid to pids from /var/run* for {program}. $pid should be declared |
---|
207 | # local in the caller. |
---|
208 | # Returns LSB exit code for the 'status' action. |
---|
209 | __pids_var_run() { |
---|
210 | local base=${1##*/} |
---|
211 | local pid_file=${2:-/var/run/$base.pid} |
---|
212 | |
---|
213 | pid= |
---|
214 | if [ -f "$pid_file" ] ; then |
---|
215 | local line p |
---|
216 | read line < "$pid_file" |
---|
217 | for p in $line ; do |
---|
218 | [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" |
---|
219 | done |
---|
220 | if [ -n "$pid" ]; then |
---|
221 | return 0 |
---|
222 | fi |
---|
223 | return 1 # "Program is dead and /var/run pid file exists" |
---|
224 | fi |
---|
225 | return 3 # "Program is not running" |
---|
226 | } |
---|
227 | |
---|
228 | # Output PIDs of matching processes, found using pidof |
---|
229 | __pids_pidof() { |
---|
230 | pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \ |
---|
231 | pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}" |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | # A function to start a program. |
---|
236 | daemon() { |
---|
237 | # Test syntax. |
---|
238 | local gotbase= force= nicelevel corelimit |
---|
239 | local pid base= user= nice= bg= pid_file= |
---|
240 | nicelevel=0 |
---|
241 | while [ "$1" != "${1##[-+]}" ]; do |
---|
242 | case $1 in |
---|
243 | '') echo $"$0: Usage: daemon [+/-nicelevel] {program}" |
---|
244 | return 1;; |
---|
245 | --check) |
---|
246 | base=$2 |
---|
247 | gotbase="yes" |
---|
248 | shift 2 |
---|
249 | ;; |
---|
250 | --check=?*) |
---|
251 | base=${1#--check=} |
---|
252 | gotbase="yes" |
---|
253 | shift |
---|
254 | ;; |
---|
255 | --user) |
---|
256 | user=$2 |
---|
257 | shift 2 |
---|
258 | ;; |
---|
259 | --user=?*) |
---|
260 | user=${1#--user=} |
---|
261 | shift |
---|
262 | ;; |
---|
263 | --pidfile) |
---|
264 | pid_file=$2 |
---|
265 | shift 2 |
---|
266 | ;; |
---|
267 | --pidfile=?*) |
---|
268 | pid_file=${1#--pidfile=} |
---|
269 | shift |
---|
270 | ;; |
---|
271 | --force) |
---|
272 | force="force" |
---|
273 | shift |
---|
274 | ;; |
---|
275 | [-+][0-9]*) |
---|
276 | nice="nice -n $1" |
---|
277 | shift |
---|
278 | ;; |
---|
279 | *) echo $"$0: Usage: daemon [+/-nicelevel] {program}" |
---|
280 | return 1;; |
---|
281 | esac |
---|
282 | done |
---|
283 | |
---|
284 | # Save basename. |
---|
285 | [ -z "$gotbase" ] && base=${1##*/} |
---|
286 | |
---|
287 | # See if it's already running. Look *only* at the pid file. |
---|
288 | __pids_var_run "$base" "$pid_file" |
---|
289 | |
---|
290 | [ -n "$pid" -a -z "$force" ] && return |
---|
291 | |
---|
292 | # make sure it doesn't core dump anywhere unless requested |
---|
293 | corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}" |
---|
294 | |
---|
295 | # if they set NICELEVEL in /etc/sysconfig/foo, honor it |
---|
296 | [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL" |
---|
297 | |
---|
298 | # Echo daemon |
---|
299 | [ "${BOOTUP:-}" = "verbose" ] && echo -n " $base" |
---|
300 | |
---|
301 | # And start it up. |
---|
302 | if [ -z "$user" ]; then |
---|
303 | $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*" |
---|
304 | else |
---|
305 | $nice runuser -s /bin/bash - $user -c "$corelimit >/dev/null 2>&1 ; $*" |
---|
306 | fi |
---|
307 | [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup" |
---|
308 | } |
---|
309 | |
---|
310 | # A function to stop a program. |
---|
311 | killproc() { |
---|
312 | local RC killlevel= base pid pid_file= delay |
---|
313 | |
---|
314 | RC=0; delay=3 |
---|
315 | # Test syntax. |
---|
316 | if [ "$#" -eq 0 ]; then |
---|
317 | echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]" |
---|
318 | return 1 |
---|
319 | fi |
---|
320 | if [ "$1" = "-p" ]; then |
---|
321 | pid_file=$2 |
---|
322 | shift 2 |
---|
323 | fi |
---|
324 | if [ "$1" = "-d" ]; then |
---|
325 | delay=$2 |
---|
326 | shift 2 |
---|
327 | fi |
---|
328 | |
---|
329 | |
---|
330 | # check for second arg to be kill level |
---|
331 | [ -n "${2:-}" ] && killlevel=$2 |
---|
332 | |
---|
333 | # Save basename. |
---|
334 | base=${1##*/} |
---|
335 | |
---|
336 | # Find pid. |
---|
337 | __pids_var_run "$1" "$pid_file" |
---|
338 | if [ -z "$pid_file" -a -z "$pid" ]; then |
---|
339 | pid="$(__pids_pidof "$1")" |
---|
340 | fi |
---|
341 | |
---|
342 | # Kill it. |
---|
343 | if [ -n "$pid" ] ; then |
---|
344 | [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base " |
---|
345 | if [ -z "$killlevel" ] ; then |
---|
346 | if checkpid $pid 2>&1; then |
---|
347 | # TERM first, then KILL if not dead |
---|
348 | kill -TERM $pid >/dev/null 2>&1 |
---|
349 | usleep 100000 |
---|
350 | if checkpid $pid && sleep 1 && |
---|
351 | checkpid $pid && sleep $delay && |
---|
352 | checkpid $pid ; then |
---|
353 | kill -KILL $pid >/dev/null 2>&1 |
---|
354 | usleep 100000 |
---|
355 | fi |
---|
356 | fi |
---|
357 | checkpid $pid |
---|
358 | RC=$? |
---|
359 | [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown" |
---|
360 | RC=$((! $RC)) |
---|
361 | # use specified level only |
---|
362 | else |
---|
363 | if checkpid $pid; then |
---|
364 | kill $killlevel $pid >/dev/null 2>&1 |
---|
365 | RC=$? |
---|
366 | [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel" |
---|
367 | elif [ -n "${LSB:-}" ]; then |
---|
368 | RC=7 # Program is not running |
---|
369 | fi |
---|
370 | fi |
---|
371 | else |
---|
372 | if [ -n "${LSB:-}" -a -n "$killlevel" ]; then |
---|
373 | RC=7 # Program is not running |
---|
374 | else |
---|
375 | failure $"$base shutdown" |
---|
376 | RC=0 |
---|
377 | fi |
---|
378 | fi |
---|
379 | |
---|
380 | # Remove pid file if any. |
---|
381 | if [ -z "$killlevel" ]; then |
---|
382 | rm -f "${pid_file:-/var/run/$base.pid}" |
---|
383 | fi |
---|
384 | return $RC |
---|
385 | } |
---|
386 | |
---|
387 | # A function to find the pid of a program. Looks *only* at the pidfile |
---|
388 | pidfileofproc() { |
---|
389 | local pid |
---|
390 | |
---|
391 | # Test syntax. |
---|
392 | if [ "$#" = 0 ] ; then |
---|
393 | echo $"Usage: pidfileofproc {program}" |
---|
394 | return 1 |
---|
395 | fi |
---|
396 | |
---|
397 | __pids_var_run "$1" |
---|
398 | [ -n "$pid" ] && echo $pid |
---|
399 | return 0 |
---|
400 | } |
---|
401 | |
---|
402 | # A function to find the pid of a program. |
---|
403 | pidofproc() { |
---|
404 | local RC pid pid_file= |
---|
405 | |
---|
406 | # Test syntax. |
---|
407 | if [ "$#" = 0 ]; then |
---|
408 | echo $"Usage: pidofproc [-p pidfile] {program}" |
---|
409 | return 1 |
---|
410 | fi |
---|
411 | if [ "$1" = "-p" ]; then |
---|
412 | pid_file=$2 |
---|
413 | shift 2 |
---|
414 | fi |
---|
415 | fail_code=3 # "Program is not running" |
---|
416 | |
---|
417 | # First try "/var/run/*.pid" files |
---|
418 | __pids_var_run "$1" "$pid_file" |
---|
419 | RC=$? |
---|
420 | if [ -n "$pid" ]; then |
---|
421 | echo $pid |
---|
422 | return 0 |
---|
423 | fi |
---|
424 | |
---|
425 | [ -n "$pid_file" ] && return $RC |
---|
426 | __pids_pidof "$1" || return $RC |
---|
427 | } |
---|
428 | |
---|
429 | status() { |
---|
430 | local base pid pid_file= |
---|
431 | |
---|
432 | # Test syntax. |
---|
433 | if [ "$#" = 0 ] ; then |
---|
434 | echo $"Usage: status [-p pidfile] {program}" |
---|
435 | return 1 |
---|
436 | fi |
---|
437 | if [ "$1" = "-p" ]; then |
---|
438 | pid_file=$2 |
---|
439 | shift 2 |
---|
440 | fi |
---|
441 | base=${1##*/} |
---|
442 | |
---|
443 | # First try "pidof" |
---|
444 | pid="$(__pids_pidof "$1")" |
---|
445 | if [ -n "$pid" ]; then |
---|
446 | echo $"${base} (pid $pid) is running..." |
---|
447 | return 0 |
---|
448 | fi |
---|
449 | |
---|
450 | # Next try "/var/run/*.pid" files |
---|
451 | __pids_var_run "$1" "$pid_file" |
---|
452 | case "$?" in |
---|
453 | 0) |
---|
454 | echo $"${base} (pid $pid) is running..." |
---|
455 | return 0 |
---|
456 | ;; |
---|
457 | 1) |
---|
458 | echo $"${base} dead but pid file exists" |
---|
459 | return 1 |
---|
460 | ;; |
---|
461 | esac |
---|
462 | # See if /var/lock/subsys/${base} exists |
---|
463 | if [ -f /var/lock/subsys/${base} ]; then |
---|
464 | echo $"${base} dead but subsys locked" |
---|
465 | return 2 |
---|
466 | fi |
---|
467 | echo $"${base} is stopped" |
---|
468 | return 3 |
---|
469 | } |
---|
470 | |
---|
471 | echo_success() { |
---|
472 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
473 | echo -n "[" |
---|
474 | [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
---|
475 | echo -n $" OK " |
---|
476 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
477 | echo -n "]" |
---|
478 | echo -ne "\r" |
---|
479 | return 0 |
---|
480 | } |
---|
481 | |
---|
482 | echo_failure() { |
---|
483 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
484 | echo -n "[" |
---|
485 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
---|
486 | echo -n $"FAILED" |
---|
487 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
488 | echo -n "]" |
---|
489 | echo -ne "\r" |
---|
490 | return 1 |
---|
491 | } |
---|
492 | |
---|
493 | echo_passed() { |
---|
494 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
495 | echo -n "[" |
---|
496 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
497 | echo -n $"PASSED" |
---|
498 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
499 | echo -n "]" |
---|
500 | echo -ne "\r" |
---|
501 | return 1 |
---|
502 | } |
---|
503 | |
---|
504 | echo_skipped() { |
---|
505 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
506 | echo -n "[" |
---|
507 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
508 | echo -n $"SKIPPED" |
---|
509 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
510 | echo -n "]" |
---|
511 | echo -ne "\r" |
---|
512 | return 1 |
---|
513 | } |
---|
514 | |
---|
515 | echo_warning() { |
---|
516 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
517 | echo -n "[" |
---|
518 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
519 | echo -n $"WARNING" |
---|
520 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
521 | echo -n "]" |
---|
522 | echo -ne "\r" |
---|
523 | return 1 |
---|
524 | } |
---|
525 | |
---|
526 | # Inform the graphical boot of our current state |
---|
527 | update_boot_stage() { |
---|
528 | rc_splash "$1" |
---|
529 | } |
---|
530 | |
---|
531 | # Log that something succeeded |
---|
532 | success() { |
---|
533 | [ "$BOOTUP" != "verbose" ] && echo_success |
---|
534 | return 0 |
---|
535 | } |
---|
536 | |
---|
537 | # Log that something failed |
---|
538 | failure() { |
---|
539 | local rc=$? |
---|
540 | [ "$BOOTUP" != "verbose" ] && echo_failure |
---|
541 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --details |
---|
542 | return $rc |
---|
543 | } |
---|
544 | |
---|
545 | # Log that something passed, but may have had errors. Useful for fsck |
---|
546 | passed() { |
---|
547 | local rc=$? |
---|
548 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
549 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
550 | #fi |
---|
551 | [ "$BOOTUP" != "verbose" ] && echo_passed |
---|
552 | return $rc |
---|
553 | } |
---|
554 | |
---|
555 | # Log that something skipped |
---|
556 | skipped() { |
---|
557 | local rc=$? |
---|
558 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
559 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
560 | #fi |
---|
561 | [ "$BOOTUP" != "verbose" ] && echo_skipped |
---|
562 | return $rc |
---|
563 | } |
---|
564 | |
---|
565 | # Log a warning |
---|
566 | warning() { |
---|
567 | local rc=$? |
---|
568 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
569 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
570 | #fi |
---|
571 | [ "$BOOTUP" != "verbose" ] && echo_warning |
---|
572 | return $rc |
---|
573 | } |
---|
574 | |
---|
575 | # Run some action. Log its output. |
---|
576 | action() { |
---|
577 | local STRING rc |
---|
578 | |
---|
579 | STRING=$1 |
---|
580 | echo -n "$STRING " |
---|
581 | shift |
---|
582 | "$@" && success $"$STRING" || failure $"$STRING" |
---|
583 | rc=$? |
---|
584 | echo |
---|
585 | return $rc |
---|
586 | } |
---|
587 | |
---|
588 | # returns OK if $1 contains $2 |
---|
589 | strstr() { |
---|
590 | [ "${1#*$2*}" = "$1" ] && return 1 |
---|
591 | return 0 |
---|
592 | } |
---|
593 | |
---|
594 | # Confirm whether we really want to run this service |
---|
595 | confirm() { |
---|
596 | local YES=$"yY" |
---|
597 | local NO=$"nN" |
---|
598 | local CONT=$"cC" |
---|
599 | |
---|
600 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash |
---|
601 | while : ; do |
---|
602 | echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] " |
---|
603 | read answer |
---|
604 | if strstr "$YES" "$answer" || [ "$answer" = "" ] ; then |
---|
605 | return 0 |
---|
606 | elif strstr "$CONT" "$answer" ; then |
---|
607 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash |
---|
608 | return 2 |
---|
609 | elif strstr "$NO" "$answer" ; then |
---|
610 | return 1 |
---|
611 | fi |
---|
612 | done |
---|
613 | } |
---|
614 | |
---|
615 | # resolve a device node to its major:minor numbers in decimal or hex |
---|
616 | get_numeric_dev() { |
---|
617 | ( |
---|
618 | fmt="%d:%d" |
---|
619 | if [ "$1" == "hex" ]; then |
---|
620 | fmt="%x:%x" |
---|
621 | fi |
---|
622 | ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }' |
---|
623 | ) 2>/dev/null |
---|
624 | } |
---|
625 | |
---|
626 | # find the working name for a running dm device with the same table as one |
---|
627 | # that dmraid would create |
---|
628 | resolve_dm_name() { |
---|
629 | ( |
---|
630 | name="$1" |
---|
631 | |
---|
632 | line=$(/sbin/dmraid -ay -t --ignorelocking | \ |
---|
633 | egrep -iv "no block devices found|No RAID disks" | \ |
---|
634 | awk -F ':' "{ if (\$1 ~ /^$name$/) { print \$2; }}") |
---|
635 | for x in $line ; do |
---|
636 | if [[ "$x" =~ ^/dev/ ]] ; then |
---|
637 | majmin=$(get_numeric_dev dec $x) |
---|
638 | line=$(echo "$line" | sed -e "s,$x\( \|$\),$majmin\1,g") |
---|
639 | fi |
---|
640 | done |
---|
641 | line=$(echo "$line" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' \ |
---|
642 | -e 's/ core [12] [[:digit:]]\+ / core [12] [[:digit:]]\\+ /') |
---|
643 | # XXX PJFIX -- this method of handling extra parameters is really just |
---|
644 | # asking for failure -- the better way would be to make |
---|
645 | # dmsetup or dmraid be able to do resolve_dm_name for us. |
---|
646 | if [ "$(echo $line | awk '{ print $3 }')" == "mirror" ]; then |
---|
647 | line=$(echo "$line" | sed \ |
---|
648 | -e 's/ 1 [[:alpha:]][[:alnum:]_]\+$/\\($\\|&$\\)/') |
---|
649 | fi |
---|
650 | /sbin/dmsetup table | \ |
---|
651 | sed -n -e "s/.*\(no block devices found\|No devices found\).*//" \ |
---|
652 | -e "s/\(^[^:]\+\): $line\( \+$\|$\)/\1/p" |
---|
653 | ) 2>/dev/null |
---|
654 | } |
---|
655 | |
---|
656 | # Check whether file $1 is a backup or rpm-generated file and should be ignored |
---|
657 | is_ignored_file() { |
---|
658 | case "$1" in |
---|
659 | *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave) |
---|
660 | return 0 |
---|
661 | ;; |
---|
662 | esac |
---|
663 | return 1 |
---|
664 | } |
---|
665 | # A sed expression to filter out the files that is_ignored_file recognizes |
---|
666 | __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d' |
---|
667 | |
---|
668 | initsplash() { |
---|
669 | [[ -f /etc/sysconfig/bootsplash ]] && source /etc/sysconfig/bootsplash |
---|
670 | [[ -n $SPLASH ]] && splash_rc=$SPLASH |
---|
671 | [[ -x /sbin/plymouthd ]] || splash_rc=no |
---|
672 | if [ -x /sbin/plymouthd ]; then |
---|
673 | splash_mode=plymouth |
---|
674 | if [ -r /proc/cmdline ] && grep -q splash /proc/cmdline && [ "$splash_rc" != "no" ]; then |
---|
675 | splash_rc=yes |
---|
676 | else |
---|
677 | splash_rc=no |
---|
678 | /bin/plymouth quit 2>/dev/null |
---|
679 | PLYMOUTH= |
---|
680 | fi |
---|
681 | else |
---|
682 | splash_rc=no |
---|
683 | fi |
---|
684 | |
---|
685 | [[ $splash_rc != "no" && $splash_rc != "No" && $splash_rc != "NO" ]] && export splash_rc=yes |
---|
686 | [[ $splash_mode = "plymouth" ]] || splash_rc= |
---|
687 | if [[ -z "$1" ]]; then |
---|
688 | set `/sbin/runlevel` |
---|
689 | __runlevel=$2 |
---|
690 | __previous=$1 |
---|
691 | else |
---|
692 | __runlevel=$1 |
---|
693 | __previous=N |
---|
694 | fi |
---|
695 | [ "$splash_mode" = "plymouth" -a "$splash_rc" = "yes" -a -e /sys/class/graphics/fb0 ] && [ "$__runlevel" = 0 -o "$__runlevel" = 6 ] && /sbin/plymouthd --mode=reboot |
---|
696 | export splash_mode splash_rc |
---|
697 | } |
---|
698 | |
---|
699 | |
---|
700 | rc_splash() { |
---|
701 | [ -f /etc/sysconfig/plymouth ] && source /etc/sysconfig/plymouth |
---|
702 | [ "$SPLASH" != "no" ] || return |
---|
703 | if [ "$1" = "start" ]; then |
---|
704 | /bin/plymouth --show-splash 2>/dev/null |
---|
705 | if [ "$2" = "1" ]; then |
---|
706 | /bin/plymouth message --text="Booting the system... Press Esc for verbose mode." 2> /dev/null |
---|
707 | elif [ "$2" = "0" ]; then |
---|
708 | /bin/plymouth message --text="Shutting down the system... Press Esc for verbose mode." 2> /dev/null |
---|
709 | else |
---|
710 | /bin/plymouth message --text="Restarting the system... Press Esc for verbose mode." 2> /dev/null |
---|
711 | fi |
---|
712 | elif [ "$1" = "stop" -o "$1" = "exit" ]; then |
---|
713 | /bin/plymouth quit 2>/dev/null |
---|
714 | elif [ "$1" = "umount" ]; then |
---|
715 | /bin/plymouth quit --retain-splash 2>/dev/null |
---|
716 | elif [ "$1" = "verbose" ]; then |
---|
717 | /bin/plymouth --hide-splash 2>/dev/null |
---|
718 | else |
---|
719 | /bin/plymouth --update "$1" 2>/dev/null |
---|
720 | fi |
---|
721 | } |
---|
722 | |
---|