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