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