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