]> git.pld-linux.org Git - projects/rc-scripts.git/blame - rc.d/init.d/functions
- 'upstart_native' branch, based on current Th release (0.4.2.8)
[projects/rc-scripts.git] / rc.d / init.d / functions
CommitLineData
356d834b 1#!/bin/sh - keep it for file(1) to get bourne shell script result
7742e157 2# functions This file contains functions to be used by most or all
746a2130 3# shell scripts in the /etc/rc.d/init.d directory.
7742e157 4#
ec8b15cb 5# $Id$
7742e157
AF
6#
7# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
8a835e11 8# Hacked by: Greg Galloway and Marc Ewing
ffe19b59 9# Modified for PLD Linux by:
ec8b15cb 10# Marek Obuchowicz <elephant@pld-linux.org>
fdc764ae
ER
11# Arkadiusz Miśkiewicz <misiek@pld-linux.org>
12# Michał Kochanowicz <mkochano@pld-linux.org>
13# Łukasz Pawelczyk <havner@pld-linux.org>
7742e157
AF
14
15# First set up a default search path.
f2dd2d31 16export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
7742e157 17
d7e5835b 18# Set defaults
66f7cfac
ER
19if [ -z "$COLUMNS" -o -z "$LINES" ]; then
20 _setterm() {
21 set -- $(stty size 2>/dev/null)
22 LINES=${LINES:-$1}
23 COLUMNS=${COLUMNS:-$2}
24 }
25 _setterm
26 unset _setterm
27fi
ef72e56c
AM
28[ -z "$LINES" ] || [ "$LINES" -le 0 ] && LINES=40
29[ -z "$COLUMNS" ] || [ "$COLUMNS" -le 0 ] && COLUMNS=80
66f7cfac
ER
30export LINES COLUMNS
31INIT_COL=$((COLUMNS - 13))
9e9fac7d 32
503bfc80
AM
33# Set colors
34RED=1
35GREEN=2
36YELLOW=3
37BLUE=4
38MAGENTA=5
39CYAN=6
40WHITE=7
94b691da 41NORMAL=15
503bfc80
AM
42# Bold definition (second parameter to termput setaf)
43BOLD=1
44NOBOLD=0
45# Default colors
46CBRACKETS="$CYAN" # brackets [ ] color
47CDONE="$GREEN" # DONE and WORK color
48CBUSY="$MAGENTA" # BUSY color
49CFAIL="$RED" # FAIL and DIED color
50CPOWEREDBY="$CYAN" # "Powered by" color
51CPLD="$GREEN" # "PLD Linux Distribution" color
52CI="$RED" # Capital I color (press I to enter interactive startup)
53CRESMAN="$GREEN" # "Resource Manager" color
bca43764 54CHARS="" # Characters displayed on the beginning of show line
503bfc80
AM
55CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
56
d7e5835b 57# Source configuration if available - may override default values
503bfc80 58[ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
de1fc6ce 59[ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
7f229d88 60[ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
7e04fe0e 61
9e22837c 62if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
ce397bd8
JR
63 {
64 while read _f _ctx; do
65 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
66 done </proc/self/status
67 } 2>/dev/null
9e22837c
JR
68 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
69 VSERVER=no
70 else
71 VSERVER=yes
72 fi
55a4bc1b 73 unset _f _ctx
9e22837c
JR
74fi
75
4132441e
ER
76# we need to know in functions if we were called from a terminal
77if [ -z "$ISATTY" ]; then
78 [ -t ] && ISATTY=yes || ISATTY=no
9cdf2de7 79 export ISATTY
4132441e
ER
80fi
81
18bd13ac 82is_yes() {
13a745bb 83 # Test syntax
ac1f65e1 84 if [ $# = 0 ]; then
13a745bb
AM
85 msg_usage " is_yes {value}"
86 return 2
87 fi
88
89 # Check value
90 case "$1" in
739070c4 91 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
13a745bb
AM
92 # true returns zero
93 return 0
94 ;;
739070c4 95 *)
13a745bb
AM
96 # false returns one
97 return 1
98 ;;
99 esac
100}
101
18bd13ac 102is_no() {
13a745bb 103 # Test syntax
ac1f65e1 104 if [ $# = 0 ]; then
13a745bb
AM
105 msg_usage " is_no {value}"
106 return 2
107 fi
108
109 case "$1" in
739070c4 110 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
13a745bb
AM
111 # true returns zero
112 return 0
113 ;;
739070c4 114 *)
13a745bb
AM
115 # false returns one
116 return 1
117 ;;
118 esac
119}
120
45e0ac73
ER
121# checks if file is empty
122# empty lines and lines beginning with hash are ignored
123is_empty_file() {
124 [ -s "$1" ] || return 0
a51d5138 125 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
45e0ac73
ER
126}
127
1626fc86
ER
128# returns OK if $1 contains $2
129strstr() {
4ec13019
ER
130 local a=$2
131 [ "${1#*$a*}" = "$1" ] && return 1
1626fc86
ER
132 return 0
133}
134
bc94aa53 135if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
d84e0c78
JR
136 RC_LOGGING=no
137fi
138
bc94aa53 139if is_no "$RC_LOGGING"; then
18bd13ac 140 initlog() {
6d0838f4 141 RESULT=0
bb49a1c7 142 while [ "$1" != "${1##-}" ]; do
0a35929a 143 case $1 in
739070c4 144 -c)
5e6dfc29
JR
145 shift
146 $1
147 RESULT=$?
148 break
149 ;;
739070c4 150 *)
5e6dfc29
JR
151 shift
152 ;;
0a35929a
AM
153 esac
154 done
6d0838f4 155 return $RESULT
0a35929a
AM
156 }
157fi
158
18bd13ac 159kernelver() {
944d6d80 160 local _x _y _z v old_IFS ver
55a4bc1b
JR
161 {
162 read _x _y v _z
163 old_IFS=$IFS
164 IFS='.'
165 set -- $v
166 IFS=$old_IFS
28d62f54
ER
167
168 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
89ec4154 169 ver=${3%%[-_]*}
28d62f54
ER
170
171 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b 172 ver="$2$ver"
28d62f54 173 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
55a4bc1b 174 ver="$1$ver"
28d62f54 175 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
55a4bc1b 176 echo $ver
28d62f54 177 } < /proc/version
6f2d09b1
AM
178}
179
18bd13ac 180kernelverser() {
944d6d80 181 local _x _y _z v old_IFS ver
55a4bc1b
JR
182 {
183 read _x _y v _z
184 old_IFS=$IFS
185 IFS='.'
186 set -- $v
187 IFS=$old_IFS
188 ver=$2
ac1f65e1 189 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b 190 ver="$1$ver"
ac1f65e1 191 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
55a4bc1b
JR
192 echo $ver
193 } </proc/version
6f2d09b1
AM
194}
195
18bd13ac 196kernelvermser() {
944d6d80 197 local _x _y _z v old_IFS ver
55a4bc1b
JR
198 {
199 read _x _y v _z
200 old_IFS=$IFS
201 IFS='.'
202 set -- $v
203 IFS=$old_IFS
204 ver="$1$ver"
ac1f65e1 205 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b
JR
206 echo $ver
207 } </proc/version
6f2d09b1
AM
208}
209
cee18a41 210# Colors workaround
18bd13ac 211termput() {
4132441e 212 is_yes "$ISATTY" || return
e3aafb2f 213
294b03eb 214 if is_yes "$FASTRC" || is_no "$TPUT"; then
972964bb 215 case "$1" in
739070c4 216 hpa)
8a835e11 217 echo -ne "\033[$(($2+1))G"
972964bb 218 ;;
739070c4 219 cuu*)
8a835e11 220 echo -ne "\033[${2}A"
972964bb 221 ;;
739070c4 222 el)
8a835e11 223 echo -ne "\033[0K"
972964bb 224 ;;
739070c4 225 setaf)
944d6d80 226 local ISBOLD
503bfc80
AM
227 if [ -n "$3" ]; then
228 ISBOLD="$3"
229 else
230 ISBOLD="$NOBOLD";
231 fi
232 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
8a835e11 233 ;;
739070c4 234 op)
503bfc80 235 termput setaf $NORMAL
dcb00bd9 236 ;;
8a835e11 237 esac
c6c1bd0c 238 else
8a835e11 239 case "$1" in
739070c4 240 hpa | cuu* | el)
8a835e11 241 tput "$@"
945790a2 242 ;;
739070c4 243 setaf)
ac1f65e1 244 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
503bfc80 245 is_yes "$COLOR_INIT" && tput setaf "$2"
945790a2 246 ;;
739070c4 247 op)
503bfc80 248 termput setaf $NORMAL
945790a2 249 ;;
8a835e11 250 esac
c6c1bd0c
AF
251 fi
252}
cee18a41 253
0c4f3cf5
ER
254if [ ! -x /bin/printf ]; then
255 # printf equivalent
256 # FIXME: buggy when single or double quotes in message!
18bd13ac 257 printf() {
944d6d80 258 local text m
0c4f3cf5
ER
259 text="$1"
260 shift
261 if [ $# -gt 0 ]; then
262 m="$1"
263 shift
264 while [ $# -gt 0 ]; do
265 m="$m\",\"$1"
266 shift
267 done
268 fi
269 awk "BEGIN {printf \"$text\", \"$m\"; }"
270 }
271fi
8a835e11 272
38198f50 273# National language support function
18bd13ac 274nls() {
944d6d80 275 local msg_echo nls_domain text message
d553f606 276 msg_echo='\n'
50ed8c3f 277 nls_domain="$NLS_DOMAIN"
e2c2c3a6 278 while [ "$1" != "${1##-}" ]; do
d553f606 279 case "$1" in
739070c4 280 --nls-domain)
8a835e11 281 shift
50ed8c3f 282 nls_domain="$1"
8a835e11 283 shift
284 ;;
739070c4 285 -n)
8a835e11 286 msg_echo=''
287 shift
288 ;;
d553f606
JR
289 esac
290 done
291 message="$1"
292 shift
50ed8c3f 293
d553f606
JR
294 # empty message, so we return --misiek
295 if [ -z "$message" ]; then
d553f606
JR
296 echo -en "$msg_echo"
297 return
38198f50 298 fi
8a835e11 299
e2c2c3a6
ER
300 if is_yes "$GETTEXT"; then
301 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
972964bb 302 fi
8a835e11 303
e2c2c3a6 304 printf "$message" "$@"
d553f606 305 echo -en "$msg_echo"
38198f50
AM
306}
307
18bd13ac 308rc_splash() {
944d6d80 309 local action="$1"
862c980c 310
9e22837c 311 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
fae22957
AM
312 [ -x /bin/splash ] && /bin/splash "$action"
313 fi
b6509675
AM
314
315 : $((progress++))
fae22957
AM
316}
317
18bd13ac 318msg_network_down() {
228edf1d 319 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
a916d1c6 320}
321
18bd13ac 322msg_starting() {
a916d1c6 323 show "Starting %s service" "$1"
324}
325
18bd13ac 326msg_already_running() {
599d198a 327 nls "%s service is already running." "$1"
a916d1c6 328}
329
18bd13ac 330msg_stopping() {
a916d1c6 331 show "Stopping %s service" "$1"
332}
333
18bd13ac 334msg_not_running() {
599d198a 335 nls "%s service is not running." "$1"
a916d1c6 336}
337
18bd13ac 338msg_reloading() {
a916d1c6 339 show "Reloading %s service" "$1"
340}
341
18bd13ac 342msg_usage() {
599d198a 343 nls "Usage: %s" "$*"
a916d1c6 344}
345
ffe19b59 346# Some functions to handle PLD Linux-style messages
18bd13ac 347show() {
944d6d80 348 local text len
0a35929a 349
ce410bbc
JR
350 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
351 text=$(nls -n "$@")
352 else
353 text=$(printf "$@")
354 fi
355 len=${#text}
356 while [ $((len++)) -lt $INIT_COL ]; do
357 text="$text."
358 done
359 if [ -n "$CHARS" ]; then
360 termput setaf $CCHARS
361 echo -n "$CHARS"
362 termput op
363 fi
364 echo -n "$text"
7742e157
AF
365}
366
18bd13ac 367deltext() {
a292b175
ER
368 termput hpa $INIT_COL
369}
370
b9cad282 371# Displays message in square brackests ("[ DONE ]"). Takes two arguments.
372# First is the text to display, second is color number to use (argument to
373# tput setaf). If second argument is not given, default (2, green) will be
374# used).
18bd13ac 375progress() {
944d6d80
ER
376 local COLOR
377 if [ -n "$2" ]; then
378 COLOR="$2"
379 else
380 COLOR="$CDONE"
381 fi
cd8dfdf0 382 deltext
503bfc80 383 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
b9cad282 384}
385
18bd13ac 386busy() {
a292b175 387 echo -n "$_busy"
7742e157
AF
388}
389
18bd13ac 390ok() {
a292b175 391 echo "$_ok"
7742e157
AF
392}
393
18bd13ac 394started() {
a292b175 395 echo "$_started"
7e04fe0e 396}
397
18bd13ac 398fail() {
a292b175 399 echo "$_fail"
d553f606 400 return 1
7742e157
AF
401}
402
18bd13ac 403died() {
a292b175 404 echo "$_died"
d553f606 405 return 1
6cac3cb2 406}
407
bbb612de 408# Check if $pid (could be plural) are running
18bd13ac 409checkpid() {
8a835e11 410 while [ "$1" ]; do
411 [ -d "/proc/$1" ] && return 0
412 shift
413 done
414 return 1
bbb612de
AM
415}
416
290eb891
AM
417# - outside chroot get only those processes, which are outside chroot.
418# - inside chroot get only those processes, which are inside chroot.
419# - don't filter out pids which do not have corresponding running processes (process died etc)
1e5e8177 420# (note: some processes like named are chrooted but run outside chroot)
c3403882 421# - do nothing inside vserver
18bd13ac 422filter_chroot() {
c3403882
JR
423 if is_yes "$VSERVER"; then
424 echo $@
425 return
426 fi
ac1f65e1 427 if [ $# -lt 1 -o ! -d /proc/1 ]; then
8e3b6875
AM
428 echo $@
429 return
430 fi
33ca787f 431 local root_dir good_pids="" good_add_pid
1e5e8177
AM
432 for root_pid in $@; do
433 root_dir=$(resolvesymlink /proc/${root_pid}/root)
434 if [ -n "$root_dir" ]; then
5a8ff397
AM
435 good_add_pid=1
436 if [ -n "${SYSTEM_CHROOTS}" ]; then
437 for r_dir in ${SYSTEM_CHROOTS}; do
438 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
439 done
440 fi
441 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
290eb891
AM
442 elif [ ! -d "/proc/$root_pid" ]; then
443 good_pids="$good_pids $root_pid"
1e5e8177 444 fi
5a8ff397
AM
445 done
446 echo $good_pids
1e5e8177
AM
447}
448
cee18a41 449# Usage run_cmd Message command_to_run
18bd13ac 450run_cmd() {
944d6d80
ER
451 local exit_code errors message force_err
452 local force_err=0 exit_code=0
d553f606 453 case "$1" in
739070c4 454 -a)
8a835e11 455 force_err=1
456 shift
457 ;;
d553f606
JR
458 esac
459 message=$1
460 show "$message"; busy
1b228409 461 shift
90c59319 462 cd /
f10f2102
ER
463 if errors=$(
464 export HOME=/tmp TMPDIR=/tmp
465 if is_no "$RC_LOGGING"; then
466 "$@" 2>&1
467 else
468 initlog -c "$*" 2>&1
469 fi
470 ); then
cd8dfdf0 471 ok
d553f606 472 log_success "$1 $message"
1b228409 473 else
d553f606
JR
474 fail
475 log_failed "$1 $message"
b7299ffc 476 exit_code=1
1b228409 477 fi
d553f606 478 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
1b228409 479 return $exit_code
cee18a41
AM
480}
481
e81194ca 482_daemon_set_ulimits() {
0fba6fe4
ER
483 local opt val ksh=${KSH_VERSION:+1}
484 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
485 while [ $# -gt 0 ]; do
486 opt=$1
487 val=$2
488 if [ "$ksh" ]; then
489 case "$opt" in
490 -Hu)
491 opt=-Hp
492 ;;
493 -Su)
494 opt=-Sp
495 ;;
496 -u)
497 opt=-p
498 ;;
499 esac
500 fi
501 ulimit $opt $val
502 shift 2
503 done
e81194ca
ER
504}
505
e2e24f5a 506# A function to start a program (now it's useful on read-only filesystem too)
18bd13ac 507daemon() {
944d6d80
ER
508 local errors="" prog="" end="" waitname="" waittime=""
509 local exit_code=0
42595a81 510 local nice=$SERVICE_RUN_NICE_LEVEL
64c571c5 511 local fork user closefds redirfds pidfile makepid chdir=/
42595a81
ER
512
513 while [ $# -gt 0 ]; do
d553f606 514 case $1 in
739070c4 515 '')
64c571c5 516 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
8a835e11 517 return 2
d553f606 518 ;;
739070c4 519 --check)
d553f606
JR
520 # for compatibility with redhat/mandrake
521 nls "warning: --check option is ignored!"
522 shift
d553f606 523 ;;
739070c4 524 --user)
d553f606 525 shift
7b1a2a70 526 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
f10f2102 527 user=$1
d553f606 528 ;;
739070c4
ER
529 --fork)
530 fork=1
b486c392
ER
531 prog="/usr/bin/setsid sh -c \""
532 end='&'
b486c392 533 ;;
6b1fd029
ER
534 --chdir)
535 shift
536 chdir=$1
537 ;;
739070c4
ER
538 --closefds)
539 closefds=1
f10f2102 540 ;;
64c571c5
ER
541 --redirfds)
542 redirfds=1
543 ;;
739070c4 544 --waitforname)
b486c392 545 shift
683d384e 546 waitname="$1"
683d384e 547 ;;
739070c4 548 --waitfortime)
b486c392 549 shift
683d384e 550 waittime="$1"
683d384e 551 ;;
739070c4 552 --pidfile)
0a177614
ER
553 shift
554 pidfile="$1"
5bf237b7 555 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
0a177614 556 ;;
a160c01b
PG
557 --makepid)
558 makepid=1
559 ;;
739070c4 560 -*|+*)
42595a81 561 nice=$1
a6e727aa 562 shift
42595a81
ER
563 break
564 ;;
739070c4 565 *)
42595a81 566 break
683d384e 567 ;;
d553f606 568 esac
42595a81 569 shift
d553f606 570 done
2eea8492 571 # If command to execute ends with quotation mark, add remaining
572 # arguments and close quotation.
573 if [ "$prog" != "${prog%\"}" ]; then
565736ed 574 prog="$prog $*$end\""
2eea8492 575 else
565736ed 576 prog="$prog $*$end"
2eea8492 577 fi
7742e157 578
e81194ca 579 _daemon_set_ulimits
f054a85e 580
3edf5e48 581 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
42595a81 582 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
3edf5e48 583
7742e157
AF
584 # And start it up.
585 busy
6b1fd029 586 cd $chdir
42595a81 587 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
18bd13ac 588 if errors=$(
f10f2102 589 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
739070c4 590 export USER=root HOME=/tmp TMPDIR=/tmp
f10f2102
ER
591 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
592 nice=${nice:-0}
593
594 if [ "$closefds" = 1 ]; then
595 exec 1>&-
596 exec 2>&-
597 exec 0>&-
64c571c5
ER
598 elif [ "$redirfds" = 1 ]; then
599 exec 1>/dev/null
600 exec 2>/dev/null
601 exec 0>/dev/null
f10f2102
ER
602 else
603 exec 2>&1
604 fi
605
ac1f65e1 606 if is_no "$RC_LOGGING"; then
f10f2102
ER
607 prog=$1; shift
608 if [ ! -x $prog ]; then
609 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
610 local a o=$IFS
611 IFS=:
612 for a in $PATH; do
613 if [ -x $a/$prog ]; then
614 prog=$a/$prog
615 break
616 fi
617 done
618 IFS=$o
619 fi
620 /sbin/start-stop-daemon -q --start \
621 --nicelevel $nice \
622 ${pidfile:+--pidfile $pidfile} \
86ed0b8f 623 ${makepid:+--make-pidfile} \
8a5b611f 624 ${user:+--chuid $user} \
6b1fd029 625 ${chdir:+--chdir "$chdir"} \
86ed0b8f
TP
626 ${fork:+--background} \
627 ${waitname:+--name $waitname} \
f10f2102
ER
628 --exec "$prog" \
629 -- ${1:+"$@"}
630 else
631 nice -n $nice initlog -c "$prog" 2>&1
632 fi
18bd13ac 633 ); then
f10f2102 634
683d384e
AM
635 if [ -n "$waitname" -a -n "$waittime" ]; then
636 # Save basename.
03e01a49 637 base=${waitname##*/}
683d384e
AM
638 # Find pid.
639 pid=$(pidofproc "$waitname" "$pidfile")
640 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
641 i=0
642 while [ "$i" -lt "$waittime" ]; do
42595a81 643 i=$((i + 1))
683d384e
AM
644 checkpid $pid && sleep 1 || break
645 done
646 fi
d553f606 647 log_success "$1 startup"
1b228409 648 ok
7742e157 649 else
b7299ffc 650 exit_code=1
1b228409 651 fail
d553f606 652 log_failed "$1 startup"
4fbe82fd 653 [ -n "$errors" ] && echo >&2 "$errors"
7742e157 654 fi
b7299ffc 655 return $exit_code
7742e157
AF
656}
657
658# A function to stop a program.
18bd13ac 659killproc() {
944d6d80 660 local notset killlevel base pid pidfile result
7742e157
AF
661 # Test syntax.
662 if [ $# = 0 ]; then
3fa0b8fe 663 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
228edf1d 664 return 2
7742e157
AF
665 fi
666
bb49a1c7 667 while [ "$1" != "${1##-}" ]; do
8de7f381 668 case $1 in
739070c4 669 --pidfile)
53ae90e8 670 pidfile="$2"
5bf237b7 671 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
53ae90e8 672 shift 2
8de7f381 673 ;;
739070c4 674 --waitforname)
53ae90e8
ER
675 waitname="$2"
676 shift 2
683d384e 677 ;;
739070c4 678 --waitfortime)
53ae90e8
ER
679 waittime="$2"
680 shift 2
683d384e 681 ;;
8de7f381
AM
682 esac
683 done
684
7742e157 685 busy
8a835e11 686
944d6d80 687 local notset=0
7742e157 688 # check for second arg to be kill level
ac1f65e1 689 if [ -n "$2" ]; then
7742e157
AF
690 killlevel=$2
691 else
692 notset=1
7742e157
AF
693 fi
694
2f269bdf
ER
695 # experimental start-stop-daemon based killing.
696 # works only with pidfile
697 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
5ec58ec2 698 local sig=${killlevel:--TERM}
2f269bdf
ER
699 /sbin/start-stop-daemon --stop \
700 --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
701 -s ${sig#-} \
702 ${pidfile:+--pidfile $pidfile}
703 result=$?
704 if [ "$result" -eq 0 ]; then
705 ok
706 else
707 fail
708 fi
709 return $result
710 fi
711
712
228edf1d 713 # Save basename.
03e01a49 714 base=${1##*/}
7742e157 715
228edf1d 716 # Find pid.
8de7f381
AM
717 pid=$(pidofproc "$1" "$pidfile")
718 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
7742e157 719
228edf1d 720 # Kill it.
ac1f65e1
TP
721 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
722 if [ "$notset" = "1" ]; then
228edf1d 723 if checkpid $pid 2>&1; then
724 # TERM first, then KILL if not dead
725 kill -TERM $pid
726 usleep 100000
727 if checkpid $pid && sleep 1 &&
728 checkpid $pid && sleep 3 &&
729 checkpid $pid; then
53ae90e8
ER
730 # XXX: SIGKILL is sent already on 4th second!
731 # HARMFUL for example to mysqld (which is already workarounded)
228edf1d 732 kill -KILL $pid
733 usleep 100000
734 fi
7742e157 735 fi
228edf1d 736 checkpid $pid
bbb612de
AM
737 result=$?
738 if [ "$result" -eq 0 ]; then
228edf1d 739 fail
740 log_failed "$1 shutdown"
741 else
742 ok
743 log_success "$1 shutdown"
744 fi
745 result=$(( ! $result ))
746 else
747 # use specified level only
748 if checkpid $pid > /dev/null 2>&1; then
749 kill $killlevel $pid
750 result=$?
751 if [ "$result" -eq 0 ]; then
752 ok
753 log_success "$1 got $killlevel"
754 else
755 result=7
756 fail
757 log_failed "$1 didn't get $killlevel"
758 fi
e016cdae 759 else
228edf1d 760 result=7
761 died
762 log_failed "$1 shutdown"
e016cdae 763 fi
7742e157
AF
764 fi
765 else
228edf1d 766 died
767 log_failed "$1 shutdown"
768 result=7
7742e157 769 fi
5e6dfc29 770
683d384e 771 if [ -n "$waitname" -a -n "$waittime" ]; then
e6f93a80 772 # Save basename.
03e01a49 773 base=${waitname##*/}
e6f93a80
AM
774 # Find pid.
775 pid=$(pidofproc "$waitname" "$pidfile")
776 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
683d384e
AM
777 i=0
778 while [ "$i" -lt "$waittime" ]; do
1101d321 779 i=$(( i + 1 ))
683d384e
AM
780 checkpid $pid && sleep 1 || break
781 done
782 fi
7742e157 783
228edf1d 784 # Remove pid file if any.
e016cdae 785 if [ "$notset" = "1" ]; then
228edf1d 786 rm -f /var/run/${base}.pid
7742e157 787 fi
bbb612de
AM
788
789 return $result
7742e157
AF
790}
791
792# A function to find the pid of a program.
18bd13ac 793pidofproc() {
944d6d80 794 local pid pidfile base=${1##*/}
8de7f381
AM
795 pidfile="$base.pid"
796 [ -n "$2" ] && pidfile="$2"
7742e157 797
8a835e11 798 # Test syntax.
ac1f65e1 799 if [ $# = 0 ]; then
8a835e11 800 msg_usage " pidofproc {program}"
801 return 2
802 fi
803
8de7f381 804 # First try pidfile or "/var/run/*.pid"
5bf237b7
ER
805 case "$pidfile" in
806 /*)pidfile="${pidfile}";;
807 *) pidfile="/var/run/$pidfile";;
808 esac
ac1f65e1 809 if [ -f "${pidfile}" ]; then
944d6d80 810 local p pid=""
7eb94de6 811 for p in $(< "${pidfile}"); do
3030e60e 812 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
8a835e11 813 done
7742e157 814 fi
8a835e11 815
7742e157 816 # Next try "pidof"
1e5e8177
AM
817 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
818 pid=$(filter_chroot "$pid")
819 echo $pid
7742e157 820}
12de71be 821
1b59b41b 822# status [--pidfile PIDFILE] {subsys} [{daemon}]"
18bd13ac 823status() {
944d6d80 824 local pid subsys daemon cpuset_msg pidfile
3232ec7b 825 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
4e901aa0 826 pidfile=$2
5bf237b7 827 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
4e901aa0
ER
828 shift 2
829 fi
830
8a835e11 831 subsys=$1
832 daemon=${2:-$subsys}
bbb612de 833
8a835e11 834 # Test syntax.
ac1f65e1 835 if [ $# = 0 ]; then
4e901aa0 836 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
8a835e11 837 return 2
838 fi
bbb612de 839
4e901aa0 840 # if pidfile specified, pid must be there
24ab860c
ER
841 if [ "$pidfile" ]; then
842 [ -f "$pidfile" ] && read pid < $pidfile
305c3281
ER
843 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
844 if [ ! -d "/proc/$pid" ]; then
845 pid=
846 fi
4e901aa0
ER
847 else
848 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
849 fi
1e5e8177 850 pid=$(filter_chroot "$pid")
bbb612de 851
b90fbc1e 852 if [ "$pid" ]; then
814e38ce 853 cpuset_msg="..."
c51af89c
ER
854 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
855 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
814e38ce
JR
856 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
857 else
858 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
859 fi
860 fi
861 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
8a835e11 862 return 0
8a835e11 863 fi
864
1b59b41b 865 # Next try "/var/run/*.pid" files; if pidfile is not set
4e901aa0 866 local base=${daemon##*/}
1b59b41b 867 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
8a835e11 868 read pid < /var/run/${base}.pid
1e5e8177 869 pid=$(filter_chroot "$pid")
1b59b41b
ER
870 if [ "$pid" ]; then
871 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
8a835e11 872 return 1
873 fi
874 fi
875
876 # See if /var/lock/subsys/$subsys exists
877 if [ -f /var/lock/subsys/$subsys ]; then
1b59b41b 878 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
8a835e11 879 return 2
7742e157 880 fi
8a835e11 881 nls "%s is stopped" "$subsys"
882 return 3
7742e157 883}
0524c81f 884
6b4a354c
AM
885# Confirm whether we really want to run this service
886confirm() {
944d6d80 887 local answer
d553f606 888 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
6b4a354c
AM
889 read answer
890 case $answer in
739070c4 891 y|Y|t|T|j|J|"")
8a835e11 892 return 0
6b4a354c 893 ;;
739070c4 894 c|C|k|K|w|W)
8a835e11 895 return 2
6b4a354c 896 ;;
739070c4 897 n|N)
8a835e11 898 return 1
6b4a354c 899 ;;
739070c4 900 *)
8a835e11 901 confirm $1
902 return $?
6b4a354c
AM
903 ;;
904 esac
905}
906
d553f606 907# module is needed (ie. is requested, is available and isn't loaded already)
18bd13ac 908is_module() {
d553f606 909 # module name without .o at end
5778a670
ER
910 if ! lsmod | grep -q "$1"; then
911 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
d553f606
JR
912 # true
913 return 0
914 fi
915 fi
916 # false
917 return 1
0524c81f 918}
f7b2d235 919
18bd13ac 920_modprobe() {
944d6d80 921 local parsed single die args foo result
f7b2d235 922 parsed=no
ac1f65e1 923 while is_no "$parsed"; do
f7b2d235 924 case "$1" in
739070c4 925 "single")
8a835e11 926 single=yes
927 shift
928 ;;
739070c4 929 "die")
8a835e11 930 die=yes
931 shift
932 ;;
739070c4 933 -*)
8a835e11 934 args="$args $1"
935 shift
936 ;;
739070c4 937 *)
8a835e11 938 parsed=yes
939 ;;
f7b2d235
JR
940 esac
941 done
ac1f65e1 942 if is_yes "${single}"; then
f7b2d235
JR
943 foo="$@"
944 show "Loading %s kernel module(s)" "$foo"
f7b2d235
JR
945 busy
946 fi
ac1f65e1 947 if [ -x /sbin/modprobe ]; then
f7b2d235
JR
948 /sbin/modprobe -s $args "$@"
949 result=$?
950 else
ac1f65e1 951 deltext; fail
f7b2d235
JR
952 result=1
953 fi
ac1f65e1 954 if is_yes "${single}"; then
f7b2d235 955 deltext
ac1f65e1 956 if [ $result = "0" ]; then
d553f606 957 is_yes "$single" && ok
f7b2d235
JR
958 else
959 fail
ac1f65e1 960 if is_yes "$die"; then
f7b2d235
JR
961 nls "Could not load %s kernel module(s)" "$@"
962 exit 1
963 fi
964 fi
965 fi
f7b2d235 966}
d553f606 967
bc94aa53
ER
968if is_no "$RC_LOGGING"; then
969 log_success() {
970 :
971 }
d553f606 972
18bd13ac 973 log_failed() {
bc94aa53
ER
974 :
975 }
976else
18bd13ac 977 log_success() {
bc94aa53
ER
978 initlog -n $0 -s "$1 $2" -e 1
979 }
980
18bd13ac 981 log_failed() {
bc94aa53
ER
982 initlog -n $0 -s "$1 $2" -e 2
983 }
984fi
d553f606 985
f0c296a0
JR
986# Check if any flavor of portmapper is running
987check_portmapper() {
cbb91a00
ER
988 if [ -x /usr/sbin/rpcinfo ]; then
989 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
fd8737b5 990 return 0
f0c296a0
JR
991 else
992 return 1
993 fi
5bf237b7
ER
994 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
995 return 1
f0c296a0
JR
996 fi
997 return 0
998}
999
fab8c1a1
JR
1000# is_fsmounted fstype mntpoint
1001# Check if filesystem fstype is mounted on mntpoint
1002is_fsmounted() {
944d6d80
ER
1003 local fstype=$1
1004 local mntpoint=$2
fab8c1a1
JR
1005
1006 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1007
81ebaa15 1008 if [ -r /proc/mounts ]; then
bfd0227f 1009 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
81ebaa15 1010 return $?
fab8c1a1 1011 else
5bf237b7 1012 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
81ebaa15
JR
1013 return 0
1014 else
1015 return 1
1016 fi
fab8c1a1
JR
1017 fi
1018}
1019
0084dcf3 1020rc_cache_init() {
6e311c54
ER
1021 # If we have cachefile, use it.
1022 # If we don't, create memory variables and try to save silently,
acf05b4f 1023 local cachefile='/var/cache/rc-scripts/msg.cache'
0084dcf3 1024
4132441e
ER
1025 local term
1026 if is_yes "$ISATTY"; then
1027 term=$TERM
1028 else
1029 term=dumb
1030 fi
1031
0084dcf3
ER
1032 # We create $check variable which is used to invalidate the cache.
1033 # The $check contains user locale and terminal.
66f7cfac 1034 local check="$term.$LC_MESSAGES.$INIT_COL"
0084dcf3 1035
f9df72e7 1036 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
0084dcf3
ER
1037 if . "$cachefile" 2>/dev/null; then
1038 if [ "$check" = "$_check" ]; then
1039 return
1040 fi
1041 fi
f9df72e7
ER
1042 fi
1043
1044 # primitive caching
1045 _busy=$(progress "BUSY" "$CBUSY")
1046 _ok=$(progress "DONE")
1047 _started=$(progress "WORK")
1048 _fail=$(progress "FAIL" "$CFAIL")
1049 _died=$(progress "DIED" "$CFAIL")
cc54351b
ER
1050
1051 # we don't use heredoc, as ksh attempts to create tempfile then
3ba0bc12 1052 (> "$cachefile" ) 2>/dev/null || return
cc54351b
ER
1053 echo "_busy='$_busy';" >> "$cachefile"
1054 echo "_ok='$_ok';" >> "$cachefile"
1055 echo "_started='$_started';" >> "$cachefile"
1056 echo "_fail='$_fail';" >> "$cachefile"
1057 echo "_died='$_died';" >> "$cachefile"
1058 echo "_check='$check';" >> "$cachefile"
f9df72e7
ER
1059}
1060
0084dcf3 1061rc_gettext_init() {
9a0a016d
ER
1062 if [ -z "$GETTEXT" ]; then
1063 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1064 GETTEXT=yes
1065 else
1066 GETTEXT=no
1067 fi
1068 fi
1069
1070 if [ -z "$TPUT" ]; then
ac1f65e1 1071 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
9a0a016d
ER
1072 TPUT=yes
1073 # check if we are on proper terminal
1074 tput longname >/dev/null 2>&1 || TPUT=no
1075 else
1076 TPUT=no
1077 fi
1078 fi
1079}
1080
0084dcf3
ER
1081rc_gettext_init
1082rc_cache_init
f9df72e7 1083
bbb612de
AM
1084#/*
1085# * Local variables:
1086# * mode: sh
1087# * indent-tabs-mode: notnil
1088# * End:
1089# *
bbb612de 1090# */
This page took 0.289339 seconds and 4 git commands to generate.