[1108] | 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 |
---|
[3071] | 441 | if [ "$1" = "-l" ]; then |
---|
| 442 | lock_file=$2 |
---|
| 443 | shift 2 |
---|
| 444 | fi |
---|
[1108] | 445 | base=${1##*/} |
---|
| 446 | |
---|
[3071] | 447 | if [ "$_use_systemctl" = "1" ]; then |
---|
| 448 | systemctl status ${0##*/}.service |
---|
| 449 | return $? |
---|
| 450 | fi |
---|
| 451 | |
---|
[1108] | 452 | # First try "pidof" |
---|
[3071] | 453 | __pids_var_run "$1" "$pid_file" |
---|
| 454 | RC=$? |
---|
| 455 | if [ -z "$pid_file" -a -z "$pid" ]; then |
---|
| 456 | pid="$(__pids_pidof "$1")" |
---|
| 457 | fi |
---|
[1108] | 458 | if [ -n "$pid" ]; then |
---|
| 459 | echo $"${base} (pid $pid) is running..." |
---|
| 460 | return 0 |
---|
| 461 | fi |
---|
| 462 | |
---|
[3071] | 463 | case "$RC" in |
---|
[1108] | 464 | 0) |
---|
| 465 | echo $"${base} (pid $pid) is running..." |
---|
| 466 | return 0 |
---|
| 467 | ;; |
---|
| 468 | 1) |
---|
| 469 | echo $"${base} dead but pid file exists" |
---|
| 470 | return 1 |
---|
| 471 | ;; |
---|
[3071] | 472 | 4) |
---|
| 473 | echo $"${base} status unknown due to insufficient privileges." |
---|
| 474 | return 4 |
---|
| 475 | ;; |
---|
[1108] | 476 | esac |
---|
[3071] | 477 | if [ -z "${lock_file}" ]; then |
---|
| 478 | lock_file=${base} |
---|
| 479 | fi |
---|
| 480 | # See if /var/lock/subsys/${lock_file} exists |
---|
| 481 | if [ -f /var/lock/subsys/${lock_file} ]; then |
---|
[1108] | 482 | echo $"${base} dead but subsys locked" |
---|
| 483 | return 2 |
---|
| 484 | fi |
---|
| 485 | echo $"${base} is stopped" |
---|
| 486 | return 3 |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | echo_success() { |
---|
| 490 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
| 491 | echo -n "[" |
---|
| 492 | [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
---|
| 493 | echo -n $" OK " |
---|
| 494 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
| 495 | echo -n "]" |
---|
| 496 | echo -ne "\r" |
---|
| 497 | return 0 |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | echo_failure() { |
---|
| 501 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
| 502 | echo -n "[" |
---|
| 503 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
---|
| 504 | echo -n $"FAILED" |
---|
| 505 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
| 506 | echo -n "]" |
---|
| 507 | echo -ne "\r" |
---|
| 508 | return 1 |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | echo_passed() { |
---|
| 512 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
| 513 | echo -n "[" |
---|
| 514 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
| 515 | echo -n $"PASSED" |
---|
| 516 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
| 517 | echo -n "]" |
---|
| 518 | echo -ne "\r" |
---|
| 519 | return 1 |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | echo_skipped() { |
---|
| 523 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
| 524 | echo -n "[" |
---|
| 525 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
| 526 | echo -n $"SKIPPED" |
---|
| 527 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
| 528 | echo -n "]" |
---|
| 529 | echo -ne "\r" |
---|
| 530 | return 1 |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | echo_warning() { |
---|
| 534 | [ "$BOOTUP" = "color" ] && $MOVE_TO_COL |
---|
| 535 | echo -n "[" |
---|
| 536 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
---|
| 537 | echo -n $"WARNING" |
---|
| 538 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
---|
| 539 | echo -n "]" |
---|
| 540 | echo -ne "\r" |
---|
| 541 | return 1 |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | # Inform the graphical boot of our current state |
---|
| 545 | update_boot_stage() { |
---|
[2969] | 546 | if [ -x /usr/bin/plymouth ]; then |
---|
| 547 | /usr/bin/plymouth --update="$1" |
---|
| 548 | fi |
---|
| 549 | return 0 |
---|
[1108] | 550 | } |
---|
| 551 | |
---|
| 552 | # Log that something succeeded |
---|
| 553 | success() { |
---|
| 554 | [ "$BOOTUP" != "verbose" ] && echo_success |
---|
| 555 | return 0 |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | # Log that something failed |
---|
| 559 | failure() { |
---|
| 560 | local rc=$? |
---|
| 561 | [ "$BOOTUP" != "verbose" ] && echo_failure |
---|
| 562 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --details |
---|
| 563 | return $rc |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | # Log that something passed, but may have had errors. Useful for fsck |
---|
| 567 | passed() { |
---|
| 568 | local rc=$? |
---|
| 569 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
| 570 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
| 571 | #fi |
---|
| 572 | [ "$BOOTUP" != "verbose" ] && echo_passed |
---|
| 573 | return $rc |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | # Log that something skipped |
---|
| 577 | skipped() { |
---|
| 578 | local rc=$? |
---|
| 579 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
| 580 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
| 581 | #fi |
---|
| 582 | [ "$BOOTUP" != "verbose" ] && echo_skipped |
---|
| 583 | return $rc |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | # Log a warning |
---|
| 587 | warning() { |
---|
| 588 | local rc=$? |
---|
| 589 | #if [ -z "${IN_INITLOG:-}" ]; then |
---|
| 590 | # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1 |
---|
| 591 | #fi |
---|
| 592 | [ "$BOOTUP" != "verbose" ] && echo_warning |
---|
| 593 | return $rc |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | # Run some action. Log its output. |
---|
| 597 | action() { |
---|
| 598 | local STRING rc |
---|
| 599 | |
---|
| 600 | STRING=$1 |
---|
| 601 | echo -n "$STRING " |
---|
| 602 | shift |
---|
| 603 | "$@" && success $"$STRING" || failure $"$STRING" |
---|
| 604 | rc=$? |
---|
| 605 | echo |
---|
| 606 | return $rc |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | # returns OK if $1 contains $2 |
---|
| 610 | strstr() { |
---|
| 611 | [ "${1#*$2*}" = "$1" ] && return 1 |
---|
| 612 | return 0 |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | # Confirm whether we really want to run this service |
---|
| 616 | confirm() { |
---|
| 617 | local YES=$"yY" |
---|
| 618 | local NO=$"nN" |
---|
| 619 | local CONT=$"cC" |
---|
| 620 | |
---|
| 621 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash |
---|
| 622 | while : ; do |
---|
| 623 | echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] " |
---|
| 624 | read answer |
---|
| 625 | if strstr "$YES" "$answer" || [ "$answer" = "" ] ; then |
---|
| 626 | return 0 |
---|
| 627 | elif strstr "$CONT" "$answer" ; then |
---|
| 628 | [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash |
---|
| 629 | return 2 |
---|
| 630 | elif strstr "$NO" "$answer" ; then |
---|
| 631 | return 1 |
---|
| 632 | fi |
---|
| 633 | done |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | # resolve a device node to its major:minor numbers in decimal or hex |
---|
| 637 | get_numeric_dev() { |
---|
| 638 | ( |
---|
| 639 | fmt="%d:%d" |
---|
| 640 | if [ "$1" == "hex" ]; then |
---|
| 641 | fmt="%x:%x" |
---|
| 642 | fi |
---|
| 643 | ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }' |
---|
| 644 | ) 2>/dev/null |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | # find the working name for a running dm device with the same table as one |
---|
| 648 | # that dmraid would create |
---|
| 649 | resolve_dm_name() { |
---|
| 650 | ( |
---|
| 651 | name="$1" |
---|
| 652 | |
---|
| 653 | line=$(/sbin/dmraid -ay -t --ignorelocking | \ |
---|
| 654 | egrep -iv "no block devices found|No RAID disks" | \ |
---|
| 655 | awk -F ':' "{ if (\$1 ~ /^$name$/) { print \$2; }}") |
---|
| 656 | for x in $line ; do |
---|
| 657 | if [[ "$x" =~ ^/dev/ ]] ; then |
---|
| 658 | majmin=$(get_numeric_dev dec $x) |
---|
| 659 | line=$(echo "$line" | sed -e "s,$x\( \|$\),$majmin\1,g") |
---|
| 660 | fi |
---|
| 661 | done |
---|
| 662 | line=$(echo "$line" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' \ |
---|
| 663 | -e 's/ core [12] [[:digit:]]\+ / core [12] [[:digit:]]\\+ /') |
---|
| 664 | # XXX PJFIX -- this method of handling extra parameters is really just |
---|
| 665 | # asking for failure -- the better way would be to make |
---|
| 666 | # dmsetup or dmraid be able to do resolve_dm_name for us. |
---|
| 667 | if [ "$(echo $line | awk '{ print $3 }')" == "mirror" ]; then |
---|
| 668 | line=$(echo "$line" | sed \ |
---|
| 669 | -e 's/ 1 [[:alpha:]][[:alnum:]_]\+$/\\($\\|&$\\)/') |
---|
| 670 | fi |
---|
| 671 | /sbin/dmsetup table | \ |
---|
| 672 | sed -n -e "s/.*\(no block devices found\|No devices found\).*//" \ |
---|
| 673 | -e "s/\(^[^:]\+\): $line\( \+$\|$\)/\1/p" |
---|
| 674 | ) 2>/dev/null |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | # Check whether file $1 is a backup or rpm-generated file and should be ignored |
---|
| 678 | is_ignored_file() { |
---|
| 679 | case "$1" in |
---|
| 680 | *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave) |
---|
| 681 | return 0 |
---|
| 682 | ;; |
---|
| 683 | esac |
---|
| 684 | return 1 |
---|
| 685 | } |
---|
| 686 | # A sed expression to filter out the files that is_ignored_file recognizes |
---|
| 687 | __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d' |
---|
| 688 | |
---|
| 689 | initsplash() { |
---|
| 690 | [[ -f /etc/sysconfig/bootsplash ]] && source /etc/sysconfig/bootsplash |
---|
| 691 | [[ -n $SPLASH ]] && splash_rc=$SPLASH |
---|
| 692 | [[ -x /sbin/plymouthd ]] || splash_rc=no |
---|
| 693 | if [ -x /sbin/plymouthd ]; then |
---|
| 694 | splash_mode=plymouth |
---|
| 695 | if [ -r /proc/cmdline ] && grep -q splash /proc/cmdline && [ "$splash_rc" != "no" ]; then |
---|
| 696 | splash_rc=yes |
---|
| 697 | else |
---|
| 698 | splash_rc=no |
---|
| 699 | /bin/plymouth quit 2>/dev/null |
---|
| 700 | PLYMOUTH= |
---|
| 701 | fi |
---|
| 702 | else |
---|
| 703 | splash_rc=no |
---|
| 704 | fi |
---|
| 705 | |
---|
| 706 | [[ $splash_rc != "no" && $splash_rc != "No" && $splash_rc != "NO" ]] && export splash_rc=yes |
---|
| 707 | [[ $splash_mode = "plymouth" ]] || splash_rc= |
---|
| 708 | if [[ -z "$1" ]]; then |
---|
| 709 | set `/sbin/runlevel` |
---|
| 710 | __runlevel=$2 |
---|
| 711 | __previous=$1 |
---|
| 712 | else |
---|
| 713 | __runlevel=$1 |
---|
| 714 | __previous=N |
---|
| 715 | fi |
---|
| 716 | [ "$splash_mode" = "plymouth" -a "$splash_rc" = "yes" -a -e /sys/class/graphics/fb0 ] && [ "$__runlevel" = 0 -o "$__runlevel" = 6 ] && /sbin/plymouthd --mode=reboot |
---|
| 717 | export splash_mode splash_rc |
---|
| 718 | } |
---|
| 719 | |
---|
[8038] | 720 | # detect container |
---|
| 721 | detect_container() { |
---|
| 722 | cat /proc/1/environ | tr "\000" "\n" | while read envvar ; do |
---|
| 723 | echo $envvar | while IFS='=' read k v ; do |
---|
| 724 | if [ "$k" == "container" ]; then |
---|
| 725 | export CONTAINER_TYPE="$v" |
---|
| 726 | if (echo $v | grep -qi "lxc"); then |
---|
| 727 | export LXC="lxc" |
---|
| 728 | fi |
---|
| 729 | fi |
---|
| 730 | done |
---|
| 731 | done |
---|
| 732 | } |
---|
[8040] | 733 | |
---|
| 734 | detect_container() { |
---|
| 735 | for envvar in $(cat /proc/1/environ | tr "\000" "\n"); do |
---|
| 736 | CONTAINER_TYPE=$(echo $envvar | while IFS='=' read k v ;do |
---|
| 737 | if [ "$k" == "container" ]; then echo $v ;fi;done) |
---|
| 738 | if (echo "$CONTAINER_TYPE" |grep -iq "lxc"); then |
---|
| 739 | LXC="lxc" |
---|
| 740 | fi |
---|
| 741 | done |
---|
| 742 | if [ -z $CONTAINER_TYPE ]; then |
---|
| 743 | export CONTAINER_TYPE |
---|
| 744 | fi |
---|
| 745 | if [ -z $LXC ]; then |
---|
| 746 | export LXC |
---|
| 747 | fi |
---|
| 748 | } |
---|
| 749 | |
---|