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