]> git.pld-linux.org Git - projects/rc-scripts.git/blame - rc.d/init.d/functions
- disable log_success/log_failed when RC_LOGGING is off
[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{
e81194ca 518 typeset errors="" prog="" waitname="" waittime=""
d553f606 519 typeset -i exit_code=0
42595a81
ER
520 local nice=$SERVICE_RUN_NICE_LEVEL
521
522 while [ $# -gt 0 ]; do
d553f606 523 case $1 in
8a835e11 524 '')
42595a81 525 msg_usage " daemon [--user user] [--fork] [--waitforname procname] [--waitfortime seconds] [+/-nicelevel] {program} <program args>"
8a835e11 526 return 2
d553f606 527 ;;
8a835e11 528 --check)
d553f606
JR
529 # for compatibility with redhat/mandrake
530 nls "warning: --check option is ignored!"
531 shift
d553f606 532 ;;
8a835e11 533 --user)
d553f606 534 shift
7b1a2a70 535 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
d553f606 536 ;;
b486c392
ER
537 --fork)
538 prog="/usr/bin/setsid sh -c \""
539 end='&'
b486c392 540 ;;
683d384e 541 --waitforname)
b486c392 542 shift
683d384e 543 waitname="$1"
683d384e
AM
544 ;;
545 --waitfortime)
b486c392 546 shift
683d384e 547 waittime="$1"
683d384e 548 ;;
42595a81
ER
549 -*|+*)
550 nice=$1
a6e727aa 551 shift
42595a81
ER
552 break
553 ;;
554 *)
555 break
683d384e 556 ;;
d553f606 557 esac
42595a81 558 shift
d553f606 559 done
2eea8492 560 # If command to execute ends with quotation mark, add remaining
561 # arguments and close quotation.
562 if [ "$prog" != "${prog%\"}" ]; then
565736ed 563 prog="$prog $*$end\""
2eea8492 564 else
565736ed 565 prog="$prog $*$end"
2eea8492 566 fi
7742e157 567
e81194ca 568 _daemon_set_ulimits
f054a85e 569
3edf5e48 570 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
42595a81 571 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
3edf5e48 572
7742e157
AF
573 # And start it up.
574 busy
90c59319 575 cd /
42595a81
ER
576 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
577 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
578 if [ -n "$waitname" -a -n "$waittime" ]; then
579 # Save basename.
03e01a49 580 base=${waitname##*/}
683d384e
AM
581 # Find pid.
582 pid=$(pidofproc "$waitname" "$pidfile")
583 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
584 i=0
585 while [ "$i" -lt "$waittime" ]; do
42595a81 586 i=$((i + 1))
683d384e
AM
587 checkpid $pid && sleep 1 || break
588 done
589 fi
d553f606 590 log_success "$1 startup"
1b228409 591 ok
7742e157 592 else
b7299ffc 593 exit_code=1
1b228409 594 fail
d553f606
JR
595 log_failed "$1 startup"
596 [ -n "$errors" ] && echo "$errors"
7742e157 597 fi
b7299ffc 598 return $exit_code
7742e157
AF
599}
600
601# A function to stop a program.
8a835e11 602killproc()
7e04fe0e 603{
8de7f381 604 typeset notset killlevel base pid pidfile result
7742e157
AF
605 # Test syntax.
606 if [ $# = 0 ]; then
d553f606 607 msg_usage " killproc {program} [signal]"
228edf1d 608 return 2
7742e157
AF
609 fi
610
bb49a1c7 611 while [ "$1" != "${1##-}" ]; do
8de7f381 612 case $1 in
5e6dfc29
JR
613 --pidfile)
614 shift
615 pidfile="$1"
616 shift
8de7f381 617 ;;
5e6dfc29
JR
618 --waitforname)
619 shift
620 waitname="$1"
621 shift
683d384e 622 ;;
5e6dfc29
JR
623 --waitfortime)
624 shift
625 waittime="$1"
626 shift
683d384e 627 ;;
8de7f381
AM
628 esac
629 done
630
7742e157 631 busy
8a835e11 632
d553f606 633 typeset -i notset=0
7742e157 634 # check for second arg to be kill level
d553f606 635 if [ -n "$2" ] ; then
7742e157
AF
636 killlevel=$2
637 else
638 notset=1
639 killlevel="-9"
640 fi
641
228edf1d 642 # Save basename.
03e01a49 643 base=${1##*/}
7742e157 644
228edf1d 645 # Find pid.
8de7f381
AM
646 pid=$(pidofproc "$1" "$pidfile")
647 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
7742e157 648
228edf1d 649 # Kill it.
34b6b730 650 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1 ; then
830949ca 651 if [ "$notset" = "1" ] ; then
228edf1d 652 if checkpid $pid 2>&1; then
653 # TERM first, then KILL if not dead
654 kill -TERM $pid
655 usleep 100000
656 if checkpid $pid && sleep 1 &&
657 checkpid $pid && sleep 3 &&
658 checkpid $pid; then
659 kill -KILL $pid
660 usleep 100000
661 fi
7742e157 662 fi
228edf1d 663 checkpid $pid
bbb612de
AM
664 result=$?
665 if [ "$result" -eq 0 ]; then
228edf1d 666 fail
667 log_failed "$1 shutdown"
668 else
669 ok
670 log_success "$1 shutdown"
671 fi
672 result=$(( ! $result ))
673 else
674 # use specified level only
675 if checkpid $pid > /dev/null 2>&1; then
676 kill $killlevel $pid
677 result=$?
678 if [ "$result" -eq 0 ]; then
679 ok
680 log_success "$1 got $killlevel"
681 else
682 result=7
683 fail
684 log_failed "$1 didn't get $killlevel"
685 fi
e016cdae 686 else
228edf1d 687 result=7
688 died
689 log_failed "$1 shutdown"
e016cdae 690 fi
7742e157
AF
691 fi
692 else
228edf1d 693 died
694 log_failed "$1 shutdown"
695 result=7
7742e157 696 fi
5e6dfc29 697
683d384e 698 if [ -n "$waitname" -a -n "$waittime" ]; then
e6f93a80 699 # Save basename.
03e01a49 700 base=${waitname##*/}
e6f93a80
AM
701 # Find pid.
702 pid=$(pidofproc "$waitname" "$pidfile")
703 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
683d384e
AM
704 i=0
705 while [ "$i" -lt "$waittime" ]; do
1101d321 706 i=$(( i + 1 ))
683d384e
AM
707 checkpid $pid && sleep 1 || break
708 done
709 fi
7742e157 710
228edf1d 711 # Remove pid file if any.
e016cdae 712 if [ "$notset" = "1" ]; then
228edf1d 713 rm -f /var/run/${base}.pid
7742e157 714 fi
bbb612de
AM
715
716 return $result
7742e157
AF
717}
718
719# A function to find the pid of a program.
8a835e11 720pidofproc()
7e04fe0e 721{
03e01a49 722 typeset pid pidfile base=${1##*/}
8de7f381
AM
723 pidfile="$base.pid"
724 [ -n "$2" ] && pidfile="$2"
7742e157 725
8a835e11 726 # Test syntax.
727 if [ $# = 0 ] ; then
728 msg_usage " pidofproc {program}"
729 return 2
730 fi
731
8de7f381 732 # First try pidfile or "/var/run/*.pid"
f955cf0f 733 if [[ "$pidfile" = /* ]]; then
1c26fe27
AM
734 pidfile="${pidfile}"
735 else
736 pidfile="/var/run/${pidfile}";
737 fi
738 if [ -f "${pidfile}" ] ; then
7eb94de6 739 typeset p pid
8a835e11 740 pid=
7eb94de6 741 for p in $(< "${pidfile}"); do
3030e60e 742 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
8a835e11 743 done
7742e157 744 fi
8a835e11 745
7742e157 746 # Next try "pidof"
1e5e8177
AM
747 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
748 pid=$(filter_chroot "$pid")
749 echo $pid
7742e157 750}
12de71be 751
8a835e11 752status()
7e04fe0e 753{
03e01a49 754 typeset pid subsys daemon cpuset_msg
8a835e11 755 subsys=$1
756 daemon=${2:-$subsys}
03e01a49 757 local base=${daemon##*/}
bbb612de 758
8a835e11 759 # Test syntax.
760 if [ $# = 0 ] ; then
761 msg_usage " status {subsys} [{daemon}]"
762 return 2
763 fi
bbb612de 764
8a835e11 765 # First try "pidof"
766 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
1e5e8177 767 pid=$(filter_chroot "$pid")
bbb612de 768
b90fbc1e 769 if [ "$pid" ]; then
814e38ce 770 cpuset_msg="..."
b90fbc1e 771 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
6c99c680 772 if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
814e38ce
JR
773 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
774 else
775 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
776 fi
777 fi
778 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
8a835e11 779 return 0
780# else
f74fbf2c 781# pid=$(ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
8a835e11 782# { if ((prog == $5) || (("(" prog ")") == $5) ||
5e6dfc29 783# (("[" prog "]") == $5) ||
f74fbf2c 784# ((prog ":") == $5)) { print $1 ; exit 0 } }' $1)
8a835e11 785# if [ "$pid" != "" ]; then
54e6320e 786# cpuset_msg="..."
b90fbc1e 787# if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" ]; then
6c99c680 788# if $(grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"); then
54e6320e
JR
789# cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
790# else
791# cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
792# fi
793# fi
794# nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
8a835e11 795# return 0
796# fi
797 fi
798
799 # Next try "/var/run/*.pid" files
800 if [ -f /var/run/${base}.pid ]; then
801 read pid < /var/run/${base}.pid
1e5e8177 802 pid=$(filter_chroot "$pid")
8a835e11 803 if [ "$pid" != "" ]; then
804 nls "%s dead but pid file exists" "$subsys"
805 return 1
806 fi
807 fi
808
809 # See if /var/lock/subsys/$subsys exists
810 if [ -f /var/lock/subsys/$subsys ]; then
811 nls "%s dead but subsys locked" "$subsys"
812 return 2
7742e157 813 fi
8a835e11 814 nls "%s is stopped" "$subsys"
815 return 3
7742e157 816}
0524c81f 817
6b4a354c
AM
818# Confirm whether we really want to run this service
819confirm() {
da1fc19d 820 typeset answer
d553f606 821 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
6b4a354c
AM
822 read answer
823 case $answer in
8a835e11 824 y|Y|t|T|j|J|"")
825 return 0
6b4a354c 826 ;;
8a835e11 827 c|C|k|K|w|W)
828 return 2
6b4a354c 829 ;;
8a835e11 830 n|N)
831 return 1
6b4a354c 832 ;;
8a835e11 833 *)
834 confirm $1
835 return $?
6b4a354c
AM
836 ;;
837 esac
838}
839
d553f606
JR
840# module is needed (ie. is requested, is available and isn't loaded already)
841is_module()
842{
843 # module name without .o at end
5778a670
ER
844 if ! lsmod | grep -q "$1"; then
845 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
d553f606
JR
846 # true
847 return 0
848 fi
849 fi
850 # false
851 return 1
0524c81f 852}
f7b2d235
JR
853
854_modprobe()
855{
da1fc19d 856 typeset parsed single die args foo result
f7b2d235 857 parsed=no
d553f606 858 while is_no "$parsed" ; do
f7b2d235 859 case "$1" in
8a835e11 860 "single")
861 single=yes
862 shift
863 ;;
864 "die")
865 die=yes
866 shift
867 ;;
868 -*)
869 args="$args $1"
870 shift
871 ;;
872 *)
873 parsed=yes
874 ;;
f7b2d235
JR
875 esac
876 done
877 if is_yes "${single}" ; then
878 foo="$@"
879 show "Loading %s kernel module(s)" "$foo"
f7b2d235
JR
880 busy
881 fi
882 if [ -x /sbin/modprobe ] ; then
883 /sbin/modprobe -s $args "$@"
884 result=$?
885 else
886 deltext ; fail
887 result=1
888 fi
889 if is_yes "${single}" ; then
890 deltext
891 if [ $result == "0" ] ; then
d553f606 892 is_yes "$single" && ok
f7b2d235
JR
893 else
894 fail
895 if is_yes "$die" ; then
896 nls "Could not load %s kernel module(s)" "$@"
897 exit 1
898 fi
899 fi
900 fi
f7b2d235 901}
d553f606 902
bc94aa53
ER
903if is_no "$RC_LOGGING"; then
904 log_success() {
905 :
906 }
d553f606 907
bc94aa53
ER
908 log_failed () {
909 :
910 }
911else
912 log_success () {
913 initlog -n $0 -s "$1 $2" -e 1
914 }
915
916 log_failed () {
917 initlog -n $0 -s "$1 $2" -e 2
918 }
919fi
d553f606 920
f0c296a0
JR
921# Check if any flavor of portmapper is running
922check_portmapper() {
1cb7535e
JR
923 typeset RPCINFO
924
cbb91a00
ER
925 if [ -x /usr/sbin/rpcinfo ]; then
926 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
fd8737b5 927 return 0
f0c296a0
JR
928 else
929 return 1
930 fi
931 elif [ -x /sbin/pidof ]; then
f74fbf2c
JR
932 [ -z "$(/sbin/pidof portmap)" -a \
933 -z "$(/sbin/pidof rpcbind)" ] && return 1
f0c296a0
JR
934 fi
935 return 0
936}
937
0084dcf3 938rc_cache_init() {
6e311c54
ER
939 # If we have cachefile, use it.
940 # If we don't, create memory variables and try to save silently,
acf05b4f 941 local cachefile='/var/cache/rc-scripts/msg.cache'
0084dcf3 942
4132441e
ER
943 local term
944 if is_yes "$ISATTY"; then
945 term=$TERM
946 else
947 term=dumb
948 fi
949
0084dcf3
ER
950 # We create $check variable which is used to invalidate the cache.
951 # The $check contains user locale and terminal.
4132441e 952 local check="$term.$LC_MESSAGES"
0084dcf3 953
f9df72e7 954 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
0084dcf3
ER
955 if . "$cachefile" 2>/dev/null; then
956 if [ "$check" = "$_check" ]; then
957 return
958 fi
959 fi
f9df72e7
ER
960 fi
961
962 # primitive caching
963 _busy=$(progress "BUSY" "$CBUSY")
964 _ok=$(progress "DONE")
965 _started=$(progress "WORK")
966 _fail=$(progress "FAIL" "$CFAIL")
967 _died=$(progress "DIED" "$CFAIL")
cc54351b
ER
968
969 # we don't use heredoc, as ksh attempts to create tempfile then
3ba0bc12 970 (> "$cachefile" ) 2>/dev/null || return
cc54351b
ER
971 echo "_busy='$_busy';" >> "$cachefile"
972 echo "_ok='$_ok';" >> "$cachefile"
973 echo "_started='$_started';" >> "$cachefile"
974 echo "_fail='$_fail';" >> "$cachefile"
975 echo "_died='$_died';" >> "$cachefile"
976 echo "_check='$check';" >> "$cachefile"
f9df72e7
ER
977}
978
0084dcf3 979rc_gettext_init() {
9a0a016d
ER
980 if [ -z "$GETTEXT" ]; then
981 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
982 GETTEXT=yes
983 else
984 GETTEXT=no
985 fi
986 fi
987
988 if [ -z "$TPUT" ]; then
989 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ] ; then
990 TPUT=yes
991 # check if we are on proper terminal
992 tput longname >/dev/null 2>&1 || TPUT=no
993 else
994 TPUT=no
995 fi
996 fi
997}
998
0084dcf3
ER
999rc_gettext_init
1000rc_cache_init
f9df72e7 1001
bbb612de
AM
1002#/*
1003# * Local variables:
1004# * mode: sh
1005# * indent-tabs-mode: notnil
1006# * End:
1007# *
bbb612de 1008# */
This page took 0.402166 seconds and 4 git commands to generate.