]> git.pld-linux.org Git - projects/rc-scripts.git/blame - rc.d/init.d/functions
- tabs
[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
a160c01b 511 local fork user closefds pidfile makepid chdir=/
42595a81
ER
512
513 while [ $# -gt 0 ]; do
d553f606 514 case $1 in
739070c4 515 '')
a160c01b 516 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--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 ;;
739070c4 541 --waitforname)
b486c392 542 shift
683d384e 543 waitname="$1"
683d384e 544 ;;
739070c4 545 --waitfortime)
b486c392 546 shift
683d384e 547 waittime="$1"
683d384e 548 ;;
739070c4 549 --pidfile)
0a177614
ER
550 shift
551 pidfile="$1"
5bf237b7 552 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
0a177614 553 ;;
a160c01b
PG
554 --makepid)
555 makepid=1
556 ;;
739070c4 557 -*|+*)
42595a81 558 nice=$1
a6e727aa 559 shift
42595a81
ER
560 break
561 ;;
739070c4 562 *)
42595a81 563 break
683d384e 564 ;;
d553f606 565 esac
42595a81 566 shift
d553f606 567 done
2eea8492 568 # If command to execute ends with quotation mark, add remaining
569 # arguments and close quotation.
570 if [ "$prog" != "${prog%\"}" ]; then
565736ed 571 prog="$prog $*$end\""
2eea8492 572 else
565736ed 573 prog="$prog $*$end"
2eea8492 574 fi
7742e157 575
e81194ca 576 _daemon_set_ulimits
f054a85e 577
3edf5e48 578 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
42595a81 579 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
3edf5e48 580
7742e157
AF
581 # And start it up.
582 busy
6b1fd029 583 cd $chdir
42595a81 584 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
18bd13ac 585 if errors=$(
f10f2102 586 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
739070c4 587 export USER=root HOME=/tmp TMPDIR=/tmp
f10f2102
ER
588 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
589 nice=${nice:-0}
590
591 if [ "$closefds" = 1 ]; then
592 exec 1>&-
593 exec 2>&-
594 exec 0>&-
595 else
596 exec 2>&1
597 fi
598
ac1f65e1 599 if is_no "$RC_LOGGING"; then
f10f2102
ER
600 prog=$1; shift
601 if [ ! -x $prog ]; then
602 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
603 local a o=$IFS
604 IFS=:
605 for a in $PATH; do
606 if [ -x $a/$prog ]; then
607 prog=$a/$prog
608 break
609 fi
610 done
611 IFS=$o
612 fi
613 /sbin/start-stop-daemon -q --start \
614 --nicelevel $nice \
615 ${pidfile:+--pidfile $pidfile} \
86ed0b8f 616 ${makepid:+--make-pidfile} \
8a5b611f 617 ${user:+--chuid $user} \
6b1fd029 618 ${chdir:+--chdir "$chdir"} \
86ed0b8f
TP
619 ${fork:+--background} \
620 ${waitname:+--name $waitname} \
f10f2102
ER
621 --exec "$prog" \
622 -- ${1:+"$@"}
623 else
624 nice -n $nice initlog -c "$prog" 2>&1
625 fi
18bd13ac 626 ); then
f10f2102 627
683d384e
AM
628 if [ -n "$waitname" -a -n "$waittime" ]; then
629 # Save basename.
03e01a49 630 base=${waitname##*/}
683d384e
AM
631 # Find pid.
632 pid=$(pidofproc "$waitname" "$pidfile")
633 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
634 i=0
635 while [ "$i" -lt "$waittime" ]; do
42595a81 636 i=$((i + 1))
683d384e
AM
637 checkpid $pid && sleep 1 || break
638 done
639 fi
d553f606 640 log_success "$1 startup"
1b228409 641 ok
7742e157 642 else
b7299ffc 643 exit_code=1
1b228409 644 fail
d553f606 645 log_failed "$1 startup"
4fbe82fd 646 [ -n "$errors" ] && echo >&2 "$errors"
7742e157 647 fi
b7299ffc 648 return $exit_code
7742e157
AF
649}
650
651# A function to stop a program.
18bd13ac 652killproc() {
944d6d80 653 local notset killlevel base pid pidfile result
7742e157
AF
654 # Test syntax.
655 if [ $# = 0 ]; then
3fa0b8fe 656 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
228edf1d 657 return 2
7742e157
AF
658 fi
659
bb49a1c7 660 while [ "$1" != "${1##-}" ]; do
8de7f381 661 case $1 in
739070c4 662 --pidfile)
53ae90e8 663 pidfile="$2"
5bf237b7 664 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
53ae90e8 665 shift 2
8de7f381 666 ;;
739070c4 667 --waitforname)
53ae90e8
ER
668 waitname="$2"
669 shift 2
683d384e 670 ;;
739070c4 671 --waitfortime)
53ae90e8
ER
672 waittime="$2"
673 shift 2
683d384e 674 ;;
8de7f381
AM
675 esac
676 done
677
7742e157 678 busy
8a835e11 679
944d6d80 680 local notset=0
7742e157 681 # check for second arg to be kill level
ac1f65e1 682 if [ -n "$2" ]; then
7742e157
AF
683 killlevel=$2
684 else
685 notset=1
7742e157
AF
686 fi
687
2f269bdf
ER
688 # experimental start-stop-daemon based killing.
689 # works only with pidfile
690 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
5ec58ec2 691 local sig=${killlevel:--TERM}
2f269bdf
ER
692 /sbin/start-stop-daemon --stop \
693 --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
694 -s ${sig#-} \
695 ${pidfile:+--pidfile $pidfile}
696 result=$?
697 if [ "$result" -eq 0 ]; then
698 ok
699 else
700 fail
701 fi
702 return $result
703 fi
704
705
228edf1d 706 # Save basename.
03e01a49 707 base=${1##*/}
7742e157 708
228edf1d 709 # Find pid.
8de7f381
AM
710 pid=$(pidofproc "$1" "$pidfile")
711 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
7742e157 712
228edf1d 713 # Kill it.
ac1f65e1
TP
714 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
715 if [ "$notset" = "1" ]; then
228edf1d 716 if checkpid $pid 2>&1; then
717 # TERM first, then KILL if not dead
718 kill -TERM $pid
719 usleep 100000
720 if checkpid $pid && sleep 1 &&
721 checkpid $pid && sleep 3 &&
722 checkpid $pid; then
53ae90e8
ER
723 # XXX: SIGKILL is sent already on 4th second!
724 # HARMFUL for example to mysqld (which is already workarounded)
228edf1d 725 kill -KILL $pid
726 usleep 100000
727 fi
7742e157 728 fi
228edf1d 729 checkpid $pid
bbb612de
AM
730 result=$?
731 if [ "$result" -eq 0 ]; then
228edf1d 732 fail
733 log_failed "$1 shutdown"
734 else
735 ok
736 log_success "$1 shutdown"
737 fi
738 result=$(( ! $result ))
739 else
740 # use specified level only
741 if checkpid $pid > /dev/null 2>&1; then
742 kill $killlevel $pid
743 result=$?
744 if [ "$result" -eq 0 ]; then
745 ok
746 log_success "$1 got $killlevel"
747 else
748 result=7
749 fail
750 log_failed "$1 didn't get $killlevel"
751 fi
e016cdae 752 else
228edf1d 753 result=7
754 died
755 log_failed "$1 shutdown"
e016cdae 756 fi
7742e157
AF
757 fi
758 else
228edf1d 759 died
760 log_failed "$1 shutdown"
761 result=7
7742e157 762 fi
5e6dfc29 763
683d384e 764 if [ -n "$waitname" -a -n "$waittime" ]; then
e6f93a80 765 # Save basename.
03e01a49 766 base=${waitname##*/}
e6f93a80
AM
767 # Find pid.
768 pid=$(pidofproc "$waitname" "$pidfile")
769 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
683d384e
AM
770 i=0
771 while [ "$i" -lt "$waittime" ]; do
1101d321 772 i=$(( i + 1 ))
683d384e
AM
773 checkpid $pid && sleep 1 || break
774 done
775 fi
7742e157 776
228edf1d 777 # Remove pid file if any.
e016cdae 778 if [ "$notset" = "1" ]; then
228edf1d 779 rm -f /var/run/${base}.pid
7742e157 780 fi
bbb612de
AM
781
782 return $result
7742e157
AF
783}
784
785# A function to find the pid of a program.
18bd13ac 786pidofproc() {
944d6d80 787 local pid pidfile base=${1##*/}
8de7f381
AM
788 pidfile="$base.pid"
789 [ -n "$2" ] && pidfile="$2"
7742e157 790
8a835e11 791 # Test syntax.
ac1f65e1 792 if [ $# = 0 ]; then
8a835e11 793 msg_usage " pidofproc {program}"
794 return 2
795 fi
796
8de7f381 797 # First try pidfile or "/var/run/*.pid"
5bf237b7
ER
798 case "$pidfile" in
799 /*)pidfile="${pidfile}";;
800 *) pidfile="/var/run/$pidfile";;
801 esac
ac1f65e1 802 if [ -f "${pidfile}" ]; then
944d6d80 803 local p pid=""
7eb94de6 804 for p in $(< "${pidfile}"); do
3030e60e 805 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
8a835e11 806 done
7742e157 807 fi
8a835e11 808
7742e157 809 # Next try "pidof"
1e5e8177
AM
810 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
811 pid=$(filter_chroot "$pid")
812 echo $pid
7742e157 813}
12de71be 814
1b59b41b 815# status [--pidfile PIDFILE] {subsys} [{daemon}]"
18bd13ac 816status() {
944d6d80 817 local pid subsys daemon cpuset_msg pidfile
3232ec7b 818 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
4e901aa0 819 pidfile=$2
5bf237b7 820 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
4e901aa0
ER
821 shift 2
822 fi
823
8a835e11 824 subsys=$1
825 daemon=${2:-$subsys}
bbb612de 826
8a835e11 827 # Test syntax.
ac1f65e1 828 if [ $# = 0 ]; then
4e901aa0 829 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
8a835e11 830 return 2
831 fi
bbb612de 832
4e901aa0 833 # if pidfile specified, pid must be there
24ab860c
ER
834 if [ "$pidfile" ]; then
835 [ -f "$pidfile" ] && read pid < $pidfile
305c3281
ER
836 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
837 if [ ! -d "/proc/$pid" ]; then
838 pid=
839 fi
4e901aa0
ER
840 else
841 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
842 fi
1e5e8177 843 pid=$(filter_chroot "$pid")
bbb612de 844
b90fbc1e 845 if [ "$pid" ]; then
814e38ce 846 cpuset_msg="..."
c51af89c
ER
847 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
848 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
814e38ce
JR
849 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
850 else
851 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
852 fi
853 fi
854 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
8a835e11 855 return 0
8a835e11 856 fi
857
1b59b41b 858 # Next try "/var/run/*.pid" files; if pidfile is not set
4e901aa0 859 local base=${daemon##*/}
1b59b41b 860 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
8a835e11 861 read pid < /var/run/${base}.pid
1e5e8177 862 pid=$(filter_chroot "$pid")
1b59b41b
ER
863 if [ "$pid" ]; then
864 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
8a835e11 865 return 1
866 fi
867 fi
868
869 # See if /var/lock/subsys/$subsys exists
870 if [ -f /var/lock/subsys/$subsys ]; then
1b59b41b 871 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
8a835e11 872 return 2
7742e157 873 fi
8a835e11 874 nls "%s is stopped" "$subsys"
875 return 3
7742e157 876}
0524c81f 877
6b4a354c
AM
878# Confirm whether we really want to run this service
879confirm() {
944d6d80 880 local answer
d553f606 881 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
6b4a354c
AM
882 read answer
883 case $answer in
739070c4 884 y|Y|t|T|j|J|"")
8a835e11 885 return 0
6b4a354c 886 ;;
739070c4 887 c|C|k|K|w|W)
8a835e11 888 return 2
6b4a354c 889 ;;
739070c4 890 n|N)
8a835e11 891 return 1
6b4a354c 892 ;;
739070c4 893 *)
8a835e11 894 confirm $1
895 return $?
6b4a354c
AM
896 ;;
897 esac
898}
899
d553f606 900# module is needed (ie. is requested, is available and isn't loaded already)
18bd13ac 901is_module() {
d553f606 902 # module name without .o at end
5778a670
ER
903 if ! lsmod | grep -q "$1"; then
904 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
d553f606
JR
905 # true
906 return 0
907 fi
908 fi
909 # false
910 return 1
0524c81f 911}
f7b2d235 912
18bd13ac 913_modprobe() {
944d6d80 914 local parsed single die args foo result
f7b2d235 915 parsed=no
ac1f65e1 916 while is_no "$parsed"; do
f7b2d235 917 case "$1" in
739070c4 918 "single")
8a835e11 919 single=yes
920 shift
921 ;;
739070c4 922 "die")
8a835e11 923 die=yes
924 shift
925 ;;
739070c4 926 -*)
8a835e11 927 args="$args $1"
928 shift
929 ;;
739070c4 930 *)
8a835e11 931 parsed=yes
932 ;;
f7b2d235
JR
933 esac
934 done
ac1f65e1 935 if is_yes "${single}"; then
f7b2d235
JR
936 foo="$@"
937 show "Loading %s kernel module(s)" "$foo"
f7b2d235
JR
938 busy
939 fi
ac1f65e1 940 if [ -x /sbin/modprobe ]; then
f7b2d235
JR
941 /sbin/modprobe -s $args "$@"
942 result=$?
943 else
ac1f65e1 944 deltext; fail
f7b2d235
JR
945 result=1
946 fi
ac1f65e1 947 if is_yes "${single}"; then
f7b2d235 948 deltext
ac1f65e1 949 if [ $result = "0" ]; then
d553f606 950 is_yes "$single" && ok
f7b2d235
JR
951 else
952 fail
ac1f65e1 953 if is_yes "$die"; then
f7b2d235
JR
954 nls "Could not load %s kernel module(s)" "$@"
955 exit 1
956 fi
957 fi
958 fi
f7b2d235 959}
d553f606 960
bc94aa53
ER
961if is_no "$RC_LOGGING"; then
962 log_success() {
963 :
964 }
d553f606 965
18bd13ac 966 log_failed() {
bc94aa53
ER
967 :
968 }
969else
18bd13ac 970 log_success() {
bc94aa53
ER
971 initlog -n $0 -s "$1 $2" -e 1
972 }
973
18bd13ac 974 log_failed() {
bc94aa53
ER
975 initlog -n $0 -s "$1 $2" -e 2
976 }
977fi
d553f606 978
f0c296a0
JR
979# Check if any flavor of portmapper is running
980check_portmapper() {
cbb91a00
ER
981 if [ -x /usr/sbin/rpcinfo ]; then
982 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
fd8737b5 983 return 0
f0c296a0
JR
984 else
985 return 1
986 fi
5bf237b7
ER
987 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
988 return 1
f0c296a0
JR
989 fi
990 return 0
991}
992
fab8c1a1
JR
993# is_fsmounted fstype mntpoint
994# Check if filesystem fstype is mounted on mntpoint
995is_fsmounted() {
944d6d80
ER
996 local fstype=$1
997 local mntpoint=$2
fab8c1a1
JR
998
999 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1000
81ebaa15 1001 if [ -r /proc/mounts ]; then
bfd0227f 1002 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
81ebaa15 1003 return $?
fab8c1a1 1004 else
5bf237b7 1005 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
81ebaa15
JR
1006 return 0
1007 else
1008 return 1
1009 fi
fab8c1a1
JR
1010 fi
1011}
1012
0084dcf3 1013rc_cache_init() {
6e311c54
ER
1014 # If we have cachefile, use it.
1015 # If we don't, create memory variables and try to save silently,
acf05b4f 1016 local cachefile='/var/cache/rc-scripts/msg.cache'
0084dcf3 1017
4132441e
ER
1018 local term
1019 if is_yes "$ISATTY"; then
1020 term=$TERM
1021 else
1022 term=dumb
1023 fi
1024
0084dcf3
ER
1025 # We create $check variable which is used to invalidate the cache.
1026 # The $check contains user locale and terminal.
66f7cfac 1027 local check="$term.$LC_MESSAGES.$INIT_COL"
0084dcf3 1028
f9df72e7 1029 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
0084dcf3
ER
1030 if . "$cachefile" 2>/dev/null; then
1031 if [ "$check" = "$_check" ]; then
1032 return
1033 fi
1034 fi
f9df72e7
ER
1035 fi
1036
1037 # primitive caching
1038 _busy=$(progress "BUSY" "$CBUSY")
1039 _ok=$(progress "DONE")
1040 _started=$(progress "WORK")
1041 _fail=$(progress "FAIL" "$CFAIL")
1042 _died=$(progress "DIED" "$CFAIL")
cc54351b
ER
1043
1044 # we don't use heredoc, as ksh attempts to create tempfile then
3ba0bc12 1045 (> "$cachefile" ) 2>/dev/null || return
cc54351b
ER
1046 echo "_busy='$_busy';" >> "$cachefile"
1047 echo "_ok='$_ok';" >> "$cachefile"
1048 echo "_started='$_started';" >> "$cachefile"
1049 echo "_fail='$_fail';" >> "$cachefile"
1050 echo "_died='$_died';" >> "$cachefile"
1051 echo "_check='$check';" >> "$cachefile"
f9df72e7
ER
1052}
1053
0084dcf3 1054rc_gettext_init() {
9a0a016d
ER
1055 if [ -z "$GETTEXT" ]; then
1056 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1057 GETTEXT=yes
1058 else
1059 GETTEXT=no
1060 fi
1061 fi
1062
1063 if [ -z "$TPUT" ]; then
ac1f65e1 1064 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
9a0a016d
ER
1065 TPUT=yes
1066 # check if we are on proper terminal
1067 tput longname >/dev/null 2>&1 || TPUT=no
1068 else
1069 TPUT=no
1070 fi
1071 fi
1072}
1073
0084dcf3
ER
1074rc_gettext_init
1075rc_cache_init
f9df72e7 1076
bbb612de
AM
1077#/*
1078# * Local variables:
1079# * mode: sh
1080# * indent-tabs-mode: notnil
1081# * End:
1082# *
bbb612de 1083# */
This page took 0.365753 seconds and 4 git commands to generate.