]> git.pld-linux.org Git - projects/rc-scripts.git/blame - rc.d/init.d/functions
- Version: 0.4.3.1
[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 2# functions This file contains functions to be used by most or all
746a2130 3# shell scripts in the /etc/rc.d/init.d directory.
7742e157 4#
ec8b15cb 5# $Id$
7742e157
AF
6#
7# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
8a835e11 8# Hacked by: Greg Galloway and Marc Ewing
ffe19b59 9# Modified for PLD Linux by:
ec8b15cb 10# Marek Obuchowicz <elephant@pld-linux.org>
fdc764ae
ER
11# Arkadiusz Miśkiewicz <misiek@pld-linux.org>
12# Michał Kochanowicz <mkochano@pld-linux.org>
13# Łukasz Pawelczyk <havner@pld-linux.org>
7742e157
AF
14
15# First set up a default search path.
f2dd2d31 16export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
7742e157 17
d7e5835b 18# Set defaults
66f7cfac
ER
19if [ -z "$COLUMNS" -o -z "$LINES" ]; then
20 _setterm() {
21 set -- $(stty size 2>/dev/null)
22 LINES=${LINES:-$1}
23 COLUMNS=${COLUMNS:-$2}
24 }
25 _setterm
26 unset _setterm
27fi
ef72e56c
AM
28[ -z "$LINES" ] || [ "$LINES" -le 0 ] && LINES=40
29[ -z "$COLUMNS" ] || [ "$COLUMNS" -le 0 ] && COLUMNS=80
66f7cfac
ER
30export LINES COLUMNS
31INIT_COL=$((COLUMNS - 13))
9e9fac7d 32
503bfc80
AM
33# Set colors
34RED=1
35GREEN=2
36YELLOW=3
37BLUE=4
38MAGENTA=5
39CYAN=6
40WHITE=7
94b691da 41NORMAL=15
503bfc80
AM
42# Bold definition (second parameter to termput setaf)
43BOLD=1
44NOBOLD=0
45# Default colors
46CBRACKETS="$CYAN" # brackets [ ] color
47CDONE="$GREEN" # DONE and WORK color
48CBUSY="$MAGENTA" # BUSY color
49CFAIL="$RED" # FAIL and DIED color
50CPOWEREDBY="$CYAN" # "Powered by" color
51CPLD="$GREEN" # "PLD Linux Distribution" color
52CI="$RED" # Capital I color (press I to enter interactive startup)
53CRESMAN="$GREEN" # "Resource Manager" color
bca43764 54CHARS="" # Characters displayed on the beginning of show line
503bfc80
AM
55CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
56
d7e5835b 57# Source configuration if available - may override default values
503bfc80 58[ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
de1fc6ce 59[ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
7f229d88 60[ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
7e04fe0e 61
9e22837c 62if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
ce397bd8
JR
63 {
64 while read _f _ctx; do
65 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
66 done </proc/self/status
67 } 2>/dev/null
9e22837c
JR
68 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
69 VSERVER=no
70 else
71 VSERVER=yes
72 fi
55a4bc1b 73 unset _f _ctx
9e22837c
JR
74fi
75
4132441e
ER
76# we need to know in functions if we were called from a terminal
77if [ -z "$ISATTY" ]; then
78 [ -t ] && ISATTY=yes || ISATTY=no
9cdf2de7 79 export ISATTY
4132441e
ER
80fi
81
18bd13ac 82is_yes() {
13a745bb 83 # Test syntax
ac1f65e1 84 if [ $# = 0 ]; then
13a745bb
AM
85 msg_usage " is_yes {value}"
86 return 2
87 fi
88
89 # Check value
90 case "$1" in
739070c4 91 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
13a745bb
AM
92 # true returns zero
93 return 0
94 ;;
739070c4 95 *)
13a745bb
AM
96 # false returns one
97 return 1
98 ;;
99 esac
100}
101
18bd13ac 102is_no() {
13a745bb 103 # Test syntax
ac1f65e1 104 if [ $# = 0 ]; then
13a745bb
AM
105 msg_usage " is_no {value}"
106 return 2
107 fi
108
109 case "$1" in
739070c4 110 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
13a745bb
AM
111 # true returns zero
112 return 0
113 ;;
739070c4 114 *)
13a745bb
AM
115 # false returns one
116 return 1
117 ;;
118 esac
119}
120
45e0ac73
ER
121# checks if file is empty
122# empty lines and lines beginning with hash are ignored
123is_empty_file() {
124 [ -s "$1" ] || return 0
a51d5138 125 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
45e0ac73
ER
126}
127
1626fc86
ER
128# returns OK if $1 contains $2
129strstr() {
4ec13019
ER
130 local a=$2
131 [ "${1#*$a*}" = "$1" ] && return 1
1626fc86
ER
132 return 0
133}
134
bc94aa53 135if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
d84e0c78
JR
136 RC_LOGGING=no
137fi
138
bc94aa53 139if is_no "$RC_LOGGING"; then
18bd13ac 140 initlog() {
6d0838f4 141 RESULT=0
bb49a1c7 142 while [ "$1" != "${1##-}" ]; do
0a35929a 143 case $1 in
739070c4 144 -c)
5e6dfc29
JR
145 shift
146 $1
147 RESULT=$?
148 break
149 ;;
739070c4 150 *)
5e6dfc29
JR
151 shift
152 ;;
0a35929a
AM
153 esac
154 done
6d0838f4 155 return $RESULT
0a35929a
AM
156 }
157fi
158
18bd13ac 159kernelver() {
944d6d80 160 local _x _y _z v old_IFS ver
55a4bc1b
JR
161 {
162 read _x _y v _z
163 old_IFS=$IFS
164 IFS='.'
165 set -- $v
166 IFS=$old_IFS
28d62f54
ER
167
168 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
89ec4154 169 ver=${3%%[-_]*}
28d62f54
ER
170
171 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b 172 ver="$2$ver"
28d62f54 173 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
55a4bc1b 174 ver="$1$ver"
28d62f54 175 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
55a4bc1b 176 echo $ver
28d62f54 177 } < /proc/version
6f2d09b1
AM
178}
179
18bd13ac 180kernelverser() {
944d6d80 181 local _x _y _z v old_IFS ver
55a4bc1b
JR
182 {
183 read _x _y v _z
184 old_IFS=$IFS
185 IFS='.'
186 set -- $v
187 IFS=$old_IFS
188 ver=$2
ac1f65e1 189 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b 190 ver="$1$ver"
ac1f65e1 191 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
55a4bc1b
JR
192 echo $ver
193 } </proc/version
6f2d09b1
AM
194}
195
18bd13ac 196kernelvermser() {
944d6d80 197 local _x _y _z v old_IFS ver
55a4bc1b
JR
198 {
199 read _x _y v _z
200 old_IFS=$IFS
201 IFS='.'
202 set -- $v
203 IFS=$old_IFS
204 ver="$1$ver"
ac1f65e1 205 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
55a4bc1b
JR
206 echo $ver
207 } </proc/version
6f2d09b1
AM
208}
209
cee18a41 210# Colors workaround
18bd13ac 211termput() {
4132441e 212 is_yes "$ISATTY" || return
e3aafb2f 213
294b03eb 214 if is_yes "$FASTRC" || is_no "$TPUT"; then
972964bb 215 case "$1" in
739070c4 216 hpa)
8a835e11 217 echo -ne "\033[$(($2+1))G"
972964bb 218 ;;
739070c4 219 cuu*)
8a835e11 220 echo -ne "\033[${2}A"
972964bb 221 ;;
739070c4 222 el)
8a835e11 223 echo -ne "\033[0K"
972964bb 224 ;;
739070c4 225 setaf)
944d6d80 226 local ISBOLD
503bfc80
AM
227 if [ -n "$3" ]; then
228 ISBOLD="$3"
229 else
230 ISBOLD="$NOBOLD";
231 fi
232 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
8a835e11 233 ;;
739070c4 234 op)
503bfc80 235 termput setaf $NORMAL
dcb00bd9 236 ;;
8a835e11 237 esac
c6c1bd0c 238 else
8a835e11 239 case "$1" in
739070c4 240 hpa | cuu* | el)
8a835e11 241 tput "$@"
945790a2 242 ;;
739070c4 243 setaf)
ac1f65e1 244 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
503bfc80 245 is_yes "$COLOR_INIT" && tput setaf "$2"
945790a2 246 ;;
739070c4 247 op)
503bfc80 248 termput setaf $NORMAL
945790a2 249 ;;
8a835e11 250 esac
c6c1bd0c
AF
251 fi
252}
cee18a41 253
0c4f3cf5
ER
254if [ ! -x /bin/printf ]; then
255 # printf equivalent
256 # FIXME: buggy when single or double quotes in message!
18bd13ac 257 printf() {
944d6d80 258 local text m
0c4f3cf5
ER
259 text="$1"
260 shift
261 if [ $# -gt 0 ]; then
262 m="$1"
263 shift
264 while [ $# -gt 0 ]; do
265 m="$m\",\"$1"
266 shift
267 done
268 fi
269 awk "BEGIN {printf \"$text\", \"$m\"; }"
270 }
271fi
8a835e11 272
38198f50 273# National language support function
18bd13ac 274nls() {
944d6d80 275 local msg_echo nls_domain text message
d553f606 276 msg_echo='\n'
50ed8c3f 277 nls_domain="$NLS_DOMAIN"
e2c2c3a6 278 while [ "$1" != "${1##-}" ]; do
d553f606 279 case "$1" in
739070c4 280 --nls-domain)
8a835e11 281 shift
50ed8c3f 282 nls_domain="$1"
8a835e11 283 shift
284 ;;
739070c4 285 -n)
8a835e11 286 msg_echo=''
287 shift
288 ;;
d553f606
JR
289 esac
290 done
291 message="$1"
292 shift
50ed8c3f 293
d553f606
JR
294 # empty message, so we return --misiek
295 if [ -z "$message" ]; then
d553f606
JR
296 echo -en "$msg_echo"
297 return
38198f50 298 fi
8a835e11 299
e2c2c3a6
ER
300 if is_yes "$GETTEXT"; then
301 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
972964bb 302 fi
8a835e11 303
e2c2c3a6 304 printf "$message" "$@"
d553f606 305 echo -en "$msg_echo"
38198f50
AM
306}
307
18bd13ac 308rc_splash() {
944d6d80 309 local action="$1"
862c980c 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
18bd13ac 318msg_network_down() {
228edf1d 319 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
a916d1c6 320}
321
18bd13ac 322msg_starting() {
a916d1c6 323 show "Starting %s service" "$1"
324}
325
18bd13ac 326msg_already_running() {
599d198a 327 nls "%s service is already running." "$1"
a916d1c6 328}
329
18bd13ac 330msg_stopping() {
a916d1c6 331 show "Stopping %s service" "$1"
332}
333
18bd13ac 334msg_not_running() {
599d198a 335 nls "%s service is not running." "$1"
a916d1c6 336}
337
18bd13ac 338msg_reloading() {
a916d1c6 339 show "Reloading %s service" "$1"
340}
341
18bd13ac 342msg_usage() {
599d198a 343 nls "Usage: %s" "$*"
a916d1c6 344}
345
ffe19b59 346# Some functions to handle PLD Linux-style messages
18bd13ac 347show() {
944d6d80 348 local text len
0a35929a 349
ce410bbc
JR
350 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
351 text=$(nls -n "$@")
352 else
353 text=$(printf "$@")
354 fi
355 len=${#text}
356 while [ $((len++)) -lt $INIT_COL ]; do
357 text="$text."
358 done
359 if [ -n "$CHARS" ]; then
360 termput setaf $CCHARS
361 echo -n "$CHARS"
362 termput op
363 fi
364 echo -n "$text"
7742e157
AF
365}
366
18bd13ac 367deltext() {
a292b175
ER
368 termput hpa $INIT_COL
369}
370
b9cad282 371# Displays message in square brackests ("[ DONE ]"). Takes two arguments.
372# First is the text to display, second is color number to use (argument to
373# tput setaf). If second argument is not given, default (2, green) will be
374# used).
18bd13ac 375progress() {
944d6d80
ER
376 local COLOR
377 if [ -n "$2" ]; then
378 COLOR="$2"
379 else
380 COLOR="$CDONE"
381 fi
cd8dfdf0 382 deltext
503bfc80 383 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
b9cad282 384}
385
18bd13ac 386busy() {
a292b175 387 echo -n "$_busy"
7742e157
AF
388}
389
18bd13ac 390ok() {
a292b175 391 echo "$_ok"
7742e157
AF
392}
393
18bd13ac 394started() {
a292b175 395 echo "$_started"
7e04fe0e 396}
397
18bd13ac 398fail() {
a292b175 399 echo "$_fail"
d553f606 400 return 1
7742e157
AF
401}
402
18bd13ac 403died() {
a292b175 404 echo "$_died"
d553f606 405 return 1
6cac3cb2 406}
407
bbb612de 408# Check if $pid (could be plural) are running
18bd13ac 409checkpid() {
8a835e11 410 while [ "$1" ]; do
411 [ -d "/proc/$1" ] && return 0
412 shift
413 done
414 return 1
bbb612de
AM
415}
416
290eb891
AM
417# - outside chroot get only those processes, which are outside chroot.
418# - inside chroot get only those processes, which are inside chroot.
419# - don't filter out pids which do not have corresponding running processes (process died etc)
1e5e8177 420# (note: some processes like named are chrooted but run outside chroot)
c3403882 421# - do nothing inside vserver
18bd13ac 422filter_chroot() {
c3403882
JR
423 if is_yes "$VSERVER"; then
424 echo $@
425 return
426 fi
ac1f65e1 427 if [ $# -lt 1 -o ! -d /proc/1 ]; then
8e3b6875
AM
428 echo $@
429 return
430 fi
33ca787f 431 local root_dir good_pids="" good_add_pid
1e5e8177
AM
432 for root_pid in $@; do
433 root_dir=$(resolvesymlink /proc/${root_pid}/root)
434 if [ -n "$root_dir" ]; then
5a8ff397
AM
435 good_add_pid=1
436 if [ -n "${SYSTEM_CHROOTS}" ]; then
437 for r_dir in ${SYSTEM_CHROOTS}; do
438 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
439 done
440 fi
441 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
290eb891
AM
442 elif [ ! -d "/proc/$root_pid" ]; then
443 good_pids="$good_pids $root_pid"
1e5e8177 444 fi
5a8ff397
AM
445 done
446 echo $good_pids
1e5e8177
AM
447}
448
cee18a41 449# Usage run_cmd Message command_to_run
18bd13ac 450run_cmd() {
944d6d80
ER
451 local exit_code errors message force_err
452 local force_err=0 exit_code=0
d553f606 453 case "$1" in
739070c4 454 -a)
8a835e11 455 force_err=1
456 shift
457 ;;
d553f606
JR
458 esac
459 message=$1
460 show "$message"; busy
1b228409 461 shift
90c59319 462 cd /
f10f2102
ER
463 if errors=$(
464 export HOME=/tmp TMPDIR=/tmp
465 if is_no "$RC_LOGGING"; then
466 "$@" 2>&1
467 else
468 initlog -c "$*" 2>&1
469 fi
470 ); then
cd8dfdf0 471 ok
d553f606 472 log_success "$1 $message"
1b228409 473 else
d553f606
JR
474 fail
475 log_failed "$1 $message"
b7299ffc 476 exit_code=1
1b228409 477 fi
d553f606 478 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
1b228409 479 return $exit_code
cee18a41
AM
480}
481
e81194ca 482_daemon_set_ulimits() {
0fba6fe4
ER
483 local opt val ksh=${KSH_VERSION:+1}
484 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
485 while [ $# -gt 0 ]; do
486 opt=$1
487 val=$2
488 if [ "$ksh" ]; then
489 case "$opt" in
490 -Hu)
491 opt=-Hp
492 ;;
493 -Su)
494 opt=-Sp
495 ;;
496 -u)
497 opt=-p
498 ;;
499 esac
500 fi
501 ulimit $opt $val
502 shift 2
503 done
e81194ca
ER
504}
505
e2e24f5a 506# A function to start a program (now it's useful on read-only filesystem too)
18bd13ac 507daemon() {
944d6d80
ER
508 local errors="" prog="" end="" waitname="" waittime=""
509 local exit_code=0
42595a81 510 local nice=$SERVICE_RUN_NICE_LEVEL
64c571c5 511 local fork user closefds redirfds pidfile makepid chdir=/
42595a81
ER
512
513 while [ $# -gt 0 ]; do
d553f606 514 case $1 in
739070c4 515 '')
64c571c5 516 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
8a835e11 517 return 2
d553f606 518 ;;
739070c4 519 --check)
d553f606
JR
520 # for compatibility with redhat/mandrake
521 nls "warning: --check option is ignored!"
522 shift
d553f606 523 ;;
739070c4 524 --user)
d553f606 525 shift
7b1a2a70 526 [ "$1" != "root" ] && prog="/bin/su $1 -s /bin/sh -c \""
f10f2102 527 user=$1
d553f606 528 ;;
739070c4
ER
529 --fork)
530 fork=1
b486c392
ER
531 prog="/usr/bin/setsid sh -c \""
532 end='&'
b486c392 533 ;;
6b1fd029
ER
534 --chdir)
535 shift
536 chdir=$1
537 ;;
739070c4
ER
538 --closefds)
539 closefds=1
f10f2102 540 ;;
64c571c5
ER
541 --redirfds)
542 redirfds=1
543 ;;
739070c4 544 --waitforname)
b486c392 545 shift
683d384e 546 waitname="$1"
683d384e 547 ;;
739070c4 548 --waitfortime)
b486c392 549 shift
683d384e 550 waittime="$1"
683d384e 551 ;;
739070c4 552 --pidfile)
0a177614
ER
553 shift
554 pidfile="$1"
5bf237b7 555 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
0a177614 556 ;;
a160c01b
PG
557 --makepid)
558 makepid=1
559 ;;
739070c4 560 -*|+*)
42595a81 561 nice=$1
a6e727aa 562 shift
42595a81
ER
563 break
564 ;;
739070c4 565 *)
42595a81 566 break
683d384e 567 ;;
d553f606 568 esac
42595a81 569 shift
d553f606 570 done
2eea8492 571 # If command to execute ends with quotation mark, add remaining
572 # arguments and close quotation.
573 if [ "$prog" != "${prog%\"}" ]; then
565736ed 574 prog="$prog $*$end\""
2eea8492 575 else
565736ed 576 prog="$prog $*$end"
2eea8492 577 fi
7742e157 578
e81194ca 579 _daemon_set_ulimits
f054a85e 580
3edf5e48 581 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
42595a81 582 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
3edf5e48 583
7742e157
AF
584 # And start it up.
585 busy
6b1fd029 586 cd $chdir
42595a81 587 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
18bd13ac 588 if errors=$(
f10f2102 589 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
739070c4 590 export USER=root HOME=/tmp TMPDIR=/tmp
f10f2102
ER
591 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
592 nice=${nice:-0}
593
594 if [ "$closefds" = 1 ]; then
595 exec 1>&-
596 exec 2>&-
597 exec 0>&-
64c571c5
ER
598 elif [ "$redirfds" = 1 ]; then
599 exec 1>/dev/null
600 exec 2>/dev/null
601 exec 0>/dev/null
f10f2102
ER
602 else
603 exec 2>&1
604 fi
605
ac1f65e1 606 if is_no "$RC_LOGGING"; then
f10f2102
ER
607 prog=$1; shift
608 if [ ! -x $prog ]; then
609 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
610 local a o=$IFS
611 IFS=:
612 for a in $PATH; do
613 if [ -x $a/$prog ]; then
614 prog=$a/$prog
615 break
616 fi
617 done
618 IFS=$o
619 fi
620 /sbin/start-stop-daemon -q --start \
621 --nicelevel $nice \
622 ${pidfile:+--pidfile $pidfile} \
86ed0b8f 623 ${makepid:+--make-pidfile} \
8a5b611f 624 ${user:+--chuid $user} \
6b1fd029 625 ${chdir:+--chdir "$chdir"} \
86ed0b8f
TP
626 ${fork:+--background} \
627 ${waitname:+--name $waitname} \
69bdc5ba 628 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
f10f2102
ER
629 --exec "$prog" \
630 -- ${1:+"$@"}
631 else
632 nice -n $nice initlog -c "$prog" 2>&1
633 fi
18bd13ac 634 ); then
f10f2102 635
683d384e
AM
636 if [ -n "$waitname" -a -n "$waittime" ]; then
637 # Save basename.
03e01a49 638 base=${waitname##*/}
683d384e
AM
639 # Find pid.
640 pid=$(pidofproc "$waitname" "$pidfile")
641 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
642 i=0
643 while [ "$i" -lt "$waittime" ]; do
42595a81 644 i=$((i + 1))
683d384e
AM
645 checkpid $pid && sleep 1 || break
646 done
647 fi
d553f606 648 log_success "$1 startup"
1b228409 649 ok
7742e157 650 else
b7299ffc 651 exit_code=1
1b228409 652 fail
d553f606 653 log_failed "$1 startup"
4fbe82fd 654 [ -n "$errors" ] && echo >&2 "$errors"
7742e157 655 fi
b7299ffc 656 return $exit_code
7742e157
AF
657}
658
659# A function to stop a program.
18bd13ac 660killproc() {
944d6d80 661 local notset killlevel base pid pidfile result
7742e157
AF
662 # Test syntax.
663 if [ $# = 0 ]; then
3fa0b8fe 664 msg_usage " killproc [--pidfile PIDFILE] {program} [-SIGNAME]"
228edf1d 665 return 2
7742e157
AF
666 fi
667
bb49a1c7 668 while [ "$1" != "${1##-}" ]; do
8de7f381 669 case $1 in
739070c4 670 --pidfile)
53ae90e8 671 pidfile="$2"
5bf237b7 672 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
53ae90e8 673 shift 2
8de7f381 674 ;;
739070c4 675 --waitforname)
53ae90e8
ER
676 waitname="$2"
677 shift 2
683d384e 678 ;;
739070c4 679 --waitfortime)
53ae90e8
ER
680 waittime="$2"
681 shift 2
683d384e 682 ;;
8de7f381
AM
683 esac
684 done
685
7742e157 686 busy
8a835e11 687
944d6d80 688 local notset=0
7742e157 689 # check for second arg to be kill level
ac1f65e1 690 if [ -n "$2" ]; then
7742e157
AF
691 killlevel=$2
692 else
693 notset=1
7742e157
AF
694 fi
695
2f269bdf
ER
696 # experimental start-stop-daemon based killing.
697 # works only with pidfile
698 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
5ec58ec2 699 local sig=${killlevel:--TERM}
2f269bdf
ER
700 /sbin/start-stop-daemon --stop \
701 --retry ${sig#-}/10/${sig#-}/60/KILL/10 \
702 -s ${sig#-} \
703 ${pidfile:+--pidfile $pidfile}
704 result=$?
705 if [ "$result" -eq 0 ]; then
706 ok
707 else
708 fail
709 fi
710 return $result
711 fi
712
713
228edf1d 714 # Save basename.
03e01a49 715 base=${1##*/}
7742e157 716
228edf1d 717 # Find pid.
8de7f381
AM
718 pid=$(pidofproc "$1" "$pidfile")
719 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
7742e157 720
228edf1d 721 # Kill it.
ac1f65e1
TP
722 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
723 if [ "$notset" = "1" ]; then
228edf1d 724 if checkpid $pid 2>&1; then
725 # TERM first, then KILL if not dead
726 kill -TERM $pid
727 usleep 100000
728 if checkpid $pid && sleep 1 &&
729 checkpid $pid && sleep 3 &&
730 checkpid $pid; then
53ae90e8
ER
731 # XXX: SIGKILL is sent already on 4th second!
732 # HARMFUL for example to mysqld (which is already workarounded)
228edf1d 733 kill -KILL $pid
734 usleep 100000
735 fi
7742e157 736 fi
228edf1d 737 checkpid $pid
bbb612de
AM
738 result=$?
739 if [ "$result" -eq 0 ]; then
228edf1d 740 fail
741 log_failed "$1 shutdown"
742 else
743 ok
744 log_success "$1 shutdown"
745 fi
746 result=$(( ! $result ))
747 else
748 # use specified level only
749 if checkpid $pid > /dev/null 2>&1; then
750 kill $killlevel $pid
751 result=$?
752 if [ "$result" -eq 0 ]; then
753 ok
754 log_success "$1 got $killlevel"
755 else
756 result=7
757 fail
758 log_failed "$1 didn't get $killlevel"
759 fi
e016cdae 760 else
228edf1d 761 result=7
762 died
763 log_failed "$1 shutdown"
e016cdae 764 fi
7742e157
AF
765 fi
766 else
228edf1d 767 died
768 log_failed "$1 shutdown"
769 result=7
7742e157 770 fi
5e6dfc29 771
683d384e 772 if [ -n "$waitname" -a -n "$waittime" ]; then
e6f93a80 773 # Save basename.
03e01a49 774 base=${waitname##*/}
e6f93a80
AM
775 # Find pid.
776 pid=$(pidofproc "$waitname" "$pidfile")
777 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
683d384e
AM
778 i=0
779 while [ "$i" -lt "$waittime" ]; do
1101d321 780 i=$(( i + 1 ))
683d384e
AM
781 checkpid $pid && sleep 1 || break
782 done
783 fi
7742e157 784
228edf1d 785 # Remove pid file if any.
e016cdae 786 if [ "$notset" = "1" ]; then
228edf1d 787 rm -f /var/run/${base}.pid
7742e157 788 fi
bbb612de
AM
789
790 return $result
7742e157
AF
791}
792
793# A function to find the pid of a program.
18bd13ac 794pidofproc() {
944d6d80 795 local pid pidfile base=${1##*/}
8de7f381
AM
796 pidfile="$base.pid"
797 [ -n "$2" ] && pidfile="$2"
7742e157 798
8a835e11 799 # Test syntax.
ac1f65e1 800 if [ $# = 0 ]; then
8a835e11 801 msg_usage " pidofproc {program}"
802 return 2
803 fi
804
8de7f381 805 # First try pidfile or "/var/run/*.pid"
5bf237b7
ER
806 case "$pidfile" in
807 /*)pidfile="${pidfile}";;
808 *) pidfile="/var/run/$pidfile";;
809 esac
ac1f65e1 810 if [ -f "${pidfile}" ]; then
944d6d80 811 local p pid=""
7eb94de6 812 for p in $(< "${pidfile}"); do
3030e60e 813 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
8a835e11 814 done
7742e157 815 fi
8a835e11 816
7742e157 817 # Next try "pidof"
1e5e8177
AM
818 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
819 pid=$(filter_chroot "$pid")
820 echo $pid
7742e157 821}
12de71be 822
1b59b41b 823# status [--pidfile PIDFILE] {subsys} [{daemon}]"
18bd13ac 824status() {
944d6d80 825 local pid subsys daemon cpuset_msg pidfile
3232ec7b 826 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
4e901aa0 827 pidfile=$2
5bf237b7 828 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
4e901aa0
ER
829 shift 2
830 fi
831
8a835e11 832 subsys=$1
833 daemon=${2:-$subsys}
bbb612de 834
8a835e11 835 # Test syntax.
ac1f65e1 836 if [ $# = 0 ]; then
4e901aa0 837 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
8a835e11 838 return 2
839 fi
bbb612de 840
4e901aa0 841 # if pidfile specified, pid must be there
24ab860c
ER
842 if [ "$pidfile" ]; then
843 [ -f "$pidfile" ] && read pid < $pidfile
305c3281
ER
844 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
845 if [ ! -d "/proc/$pid" ]; then
846 pid=
847 fi
4e901aa0
ER
848 else
849 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
850 fi
1e5e8177 851 pid=$(filter_chroot "$pid")
bbb612de 852
b90fbc1e 853 if [ "$pid" ]; then
814e38ce 854 cpuset_msg="..."
c51af89c
ER
855 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
856 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
814e38ce
JR
857 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
858 else
859 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
860 fi
861 fi
862 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
8a835e11 863 return 0
8a835e11 864 fi
865
1b59b41b 866 # Next try "/var/run/*.pid" files; if pidfile is not set
4e901aa0 867 local base=${daemon##*/}
1b59b41b 868 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
8a835e11 869 read pid < /var/run/${base}.pid
1e5e8177 870 pid=$(filter_chroot "$pid")
1b59b41b
ER
871 if [ "$pid" ]; then
872 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
8a835e11 873 return 1
874 fi
875 fi
876
877 # See if /var/lock/subsys/$subsys exists
878 if [ -f /var/lock/subsys/$subsys ]; then
1b59b41b 879 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
8a835e11 880 return 2
7742e157 881 fi
8a835e11 882 nls "%s is stopped" "$subsys"
883 return 3
7742e157 884}
0524c81f 885
6b4a354c
AM
886# Confirm whether we really want to run this service
887confirm() {
944d6d80 888 local answer
d553f606 889 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
6b4a354c
AM
890 read answer
891 case $answer in
739070c4 892 y|Y|t|T|j|J|"")
8a835e11 893 return 0
6b4a354c 894 ;;
739070c4 895 c|C|k|K|w|W)
8a835e11 896 return 2
6b4a354c 897 ;;
739070c4 898 n|N)
8a835e11 899 return 1
6b4a354c 900 ;;
739070c4 901 *)
8a835e11 902 confirm $1
903 return $?
6b4a354c
AM
904 ;;
905 esac
906}
907
d553f606 908# module is needed (ie. is requested, is available and isn't loaded already)
18bd13ac 909is_module() {
d553f606 910 # module name without .o at end
5778a670
ER
911 if ! lsmod | grep -q "$1"; then
912 if ls -R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "${1}.\(\|k\)o\(\|.gz\)"; then
d553f606
JR
913 # true
914 return 0
915 fi
916 fi
917 # false
918 return 1
0524c81f 919}
f7b2d235 920
18bd13ac 921_modprobe() {
944d6d80 922 local parsed single die args foo result
f7b2d235 923 parsed=no
ac1f65e1 924 while is_no "$parsed"; do
f7b2d235 925 case "$1" in
739070c4 926 "single")
8a835e11 927 single=yes
928 shift
929 ;;
739070c4 930 "die")
8a835e11 931 die=yes
932 shift
933 ;;
739070c4 934 -*)
8a835e11 935 args="$args $1"
936 shift
937 ;;
739070c4 938 *)
8a835e11 939 parsed=yes
940 ;;
f7b2d235
JR
941 esac
942 done
ac1f65e1 943 if is_yes "${single}"; then
f7b2d235
JR
944 foo="$@"
945 show "Loading %s kernel module(s)" "$foo"
f7b2d235
JR
946 busy
947 fi
ac1f65e1 948 if [ -x /sbin/modprobe ]; then
f7b2d235
JR
949 /sbin/modprobe -s $args "$@"
950 result=$?
951 else
ac1f65e1 952 deltext; fail
f7b2d235
JR
953 result=1
954 fi
ac1f65e1 955 if is_yes "${single}"; then
f7b2d235 956 deltext
ac1f65e1 957 if [ $result = "0" ]; then
d553f606 958 is_yes "$single" && ok
f7b2d235
JR
959 else
960 fail
ac1f65e1 961 if is_yes "$die"; then
f7b2d235
JR
962 nls "Could not load %s kernel module(s)" "$@"
963 exit 1
964 fi
965 fi
966 fi
f7b2d235 967}
d553f606 968
bc94aa53
ER
969if is_no "$RC_LOGGING"; then
970 log_success() {
971 :
972 }
d553f606 973
18bd13ac 974 log_failed() {
bc94aa53
ER
975 :
976 }
977else
18bd13ac 978 log_success() {
bc94aa53
ER
979 initlog -n $0 -s "$1 $2" -e 1
980 }
981
18bd13ac 982 log_failed() {
bc94aa53
ER
983 initlog -n $0 -s "$1 $2" -e 2
984 }
985fi
d553f606 986
f0c296a0
JR
987# Check if any flavor of portmapper is running
988check_portmapper() {
cbb91a00
ER
989 if [ -x /usr/sbin/rpcinfo ]; then
990 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
fd8737b5 991 return 0
f0c296a0
JR
992 else
993 return 1
994 fi
5bf237b7
ER
995 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
996 return 1
f0c296a0
JR
997 fi
998 return 0
999}
1000
fab8c1a1
JR
1001# is_fsmounted fstype mntpoint
1002# Check if filesystem fstype is mounted on mntpoint
1003is_fsmounted() {
944d6d80
ER
1004 local fstype=$1
1005 local mntpoint=$2
fab8c1a1
JR
1006
1007 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1008
81ebaa15 1009 if [ -r /proc/mounts ]; then
bfd0227f 1010 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
81ebaa15 1011 return $?
fab8c1a1 1012 else
5bf237b7 1013 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
81ebaa15
JR
1014 return 0
1015 else
1016 return 1
1017 fi
fab8c1a1
JR
1018 fi
1019}
1020
0084dcf3 1021rc_cache_init() {
6e311c54
ER
1022 # If we have cachefile, use it.
1023 # If we don't, create memory variables and try to save silently,
acf05b4f 1024 local cachefile='/var/cache/rc-scripts/msg.cache'
0084dcf3 1025
4132441e
ER
1026 local term
1027 if is_yes "$ISATTY"; then
1028 term=$TERM
1029 else
1030 term=dumb
1031 fi
1032
0084dcf3
ER
1033 # We create $check variable which is used to invalidate the cache.
1034 # The $check contains user locale and terminal.
66f7cfac 1035 local check="$term.$LC_MESSAGES.$INIT_COL"
0084dcf3 1036
f9df72e7 1037 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
0084dcf3
ER
1038 if . "$cachefile" 2>/dev/null; then
1039 if [ "$check" = "$_check" ]; then
1040 return
1041 fi
1042 fi
f9df72e7
ER
1043 fi
1044
1045 # primitive caching
1046 _busy=$(progress "BUSY" "$CBUSY")
1047 _ok=$(progress "DONE")
1048 _started=$(progress "WORK")
1049 _fail=$(progress "FAIL" "$CFAIL")
1050 _died=$(progress "DIED" "$CFAIL")
cc54351b
ER
1051
1052 # we don't use heredoc, as ksh attempts to create tempfile then
3ba0bc12 1053 (> "$cachefile" ) 2>/dev/null || return
cc54351b
ER
1054 echo "_busy='$_busy';" >> "$cachefile"
1055 echo "_ok='$_ok';" >> "$cachefile"
1056 echo "_started='$_started';" >> "$cachefile"
1057 echo "_fail='$_fail';" >> "$cachefile"
1058 echo "_died='$_died';" >> "$cachefile"
1059 echo "_check='$check';" >> "$cachefile"
f9df72e7
ER
1060}
1061
0084dcf3 1062rc_gettext_init() {
9a0a016d
ER
1063 if [ -z "$GETTEXT" ]; then
1064 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1065 GETTEXT=yes
1066 else
1067 GETTEXT=no
1068 fi
1069 fi
1070
1071 if [ -z "$TPUT" ]; then
ac1f65e1 1072 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
9a0a016d
ER
1073 TPUT=yes
1074 # check if we are on proper terminal
1075 tput longname >/dev/null 2>&1 || TPUT=no
1076 else
1077 TPUT=no
1078 fi
1079 fi
1080}
1081
439b6d80
JK
1082use_upstart () {
1083 # True when upstart-event-based boot should be used
1084 is_yes "$USE_UPSTART" && return 0
1085 is_no "$USE_UPSTART" && return 1
1086 if [ ! -x /sbin/initctl ] ; then
1087 USE_UPSTART="no"
1088 return 1
1089 fi
1090 local cmdline=$(cat /proc/cmdline)
1091 if strstr "$cmdline" "pld.no-upstart" ; then
1092 USE_UPSTART="no"
1093 return 1
1094 else
1095 USE_UPSTART="yes"
1096 return 0
1097 fi
1098}
1099
1100emit () {
1101 # emit upstart signal
1102 # only when 'upstart' boot is enabled
1103 use_upstart || return 0
1104 /sbin/initctl emit "$@"
1105}
1106
9ee04f3b
JK
1107is_upstart_task() {
1108 # Return 0 if the given service is an upstart task.
1109 grep -q '^task' "/etc/init/$1.conf"
1110}
1111is_upstart_running() {
1112 # Return 0 if the given service is running via upstart
1113 initctl status "$1" 2>/dev/null | grep -q running
1114}
1115upstart_start() {
1116 local service=$1
1117 is_upstart_running "${service}" && return 0
1118 msg_starting "${service}"
1119 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1120 ok
439b6d80 1121 return 0
9ee04f3b
JK
1122 else
1123 fail
1124 echo "$errors" >&2
1125 return 1
1126 fi
1127}
1128upstart_stop() {
1129 local service=$1
1130 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1131 return 0
1132 fi
1133 msg_stopping "${service}"
1134 if errors=$(/sbin/initctl stop ${service}) ; then
1135 ok
1136 return 0
1137 else
1138 fail
1139 echo "$errors" >&2
1140 return 1
1141 fi
1142}
1143upstart_reload() {
1144 local service=$1
1145 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1146 return 0
1147 fi
1148 msg_reloading "${service}"
1149 if errors=$(/sbin/initctl reload ${service}) ; then
1150 ok
1151 return 0
1152 else
1153 fail
1154 echo "$errors" >&2
1155 return 1
1156 fi
1157}
1158upstart_status() {
1159 # get service status
1160 # should be compliant with
1161 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1162 local service=$1
1163 local status
1164 if is_upstart_task "${service}" ; then
1165 # we probably should have a way to handle task status
439b6d80
JK
1166 return 0
1167 fi
9ee04f3b
JK
1168 if ! status=$(/sbin/initctl status "${service}") ; then
1169 # program or service status is not known
1170 return 4
1171 fi
1172 if strstr "$status" "running" ; then
1173 # program is running or service is OK
945d7cfd 1174 echo "$status"
9ee04f3b
JK
1175 return 0
1176 else
1177 # program is not running
945d7cfd 1178 echo "$status"
9ee04f3b
JK
1179 return 3
1180 fi
1181 # TODO: other statuses
1182}
1183
1184_upstart_controlled () {
1185 # If the service is to be handled by upstart
1186 # execute the start/stop/etc. commands the upstart way
1187 if ! use_upstart ; then
1188 return 0
1189 fi
1190 local script=$1
1191 shift
1192 local command=$1
1193 shift
9ee04f3b 1194 local name=$(basename "$script")
439b6d80
JK
1195 if [ ! -f /etc/init/${name}.conf ] ; then
1196 return 0
1197 fi
9b1f5468
JK
1198 local commands
1199 local extra_commands
02271277 1200 local has_configtest
9b1f5468
JK
1201 if [ "$1" = "--except" ] ; then
1202 shift
1203 commands="$*"
1204 for cmd in $commands ; do
1205 if [ "$command" = "$cmd" ] ; then
1206 return 0
1207 fi
1208 case "$cmd" in
1209 start|stop|status|reload|restart|try-restart|force-reload)
1210 ;;
02271277
JK
1211 configtest)
1212 has_configtest=yes
1213 extra_commands="|$cmd"
1214 ;;
9b1f5468
JK
1215 *)
1216 extra_commands="|$cmd"
1217 ;;
1218 esac
1219 done
1220 elif [ -n "$*" ] ; then
1221 commands="$*"
9ee04f3b
JK
1222 local cmd
1223 local found=0
1224 # is there a better way
1225 for cmd in $commands ; do
1226 if [ "$command" = "$cmd" ] ; then
1227 found=1
1228 break;
1229 fi
1230 done
1231 if [ $found = 0 ] ; then
1232 # let the script handle it
1233 return 0
1234 fi
1235 fi
439b6d80 1236 case "$command" in
9ee04f3b
JK
1237 start)
1238 upstart_start $name
1239 exit $?
1240 ;;
1241 stop)
1242 upstart_stop $name
1243 exit $?
1244 ;;
1245 status)
1246 upstart_status $name
1247 exit $?
1248 ;;
1249 restart)
023f83d5 1250 if is_yes "$has_configtest" ; then
02271277
JK
1251 "$script" configtest || exit 1
1252 fi
9ee04f3b
JK
1253 upstart_stop $name
1254 upstart_start $name
1255 exit $?
1256 ;;
d5a0dae5
JK
1257 try-restart)
1258 if ! is_upstart_running "$name" ; then
1259 exit 0
1260 fi
023f83d5 1261 if is_yes "$has_configtest" ; then
02271277
JK
1262 "$script" configtest || exit 1
1263 fi
d5a0dae5
JK
1264 upstart_stop $name
1265 upstart_start $name
1266 exit $?
1267 ;;
9ee04f3b 1268 reload)
023f83d5 1269 if is_yes "$has_configtest" ; then
02271277
JK
1270 "$script" configtest || exit 1
1271 fi
9ee04f3b
JK
1272 if is_upstart_task "$name" ; then
1273 nls "$command not implemented for $name"
1274 exit 3
1275 else
1276 upstart_reload "$name"
1277 exit $?
1278 fi
1279 ;;
1280 force-reload)
023f83d5 1281 if is_yes "$has_configtest" ; then
02271277
JK
1282 "$script" configtest || exit 1
1283 fi
9ee04f3b
JK
1284 if is_upstart_task "$name" ; then
1285 upstart_stop "$name"
1286 upstart_start "$name"
1287 exit $?
1288 else
1289 upstart_reload "$name"
1290 exit $?
1291 fi
439b6d80
JK
1292 ;;
1293 *)
9b1f5468 1294 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
9ee04f3b 1295 exit 3
439b6d80
JK
1296 ;;
1297 esac
9ee04f3b 1298 return 1 # should not happen
439b6d80 1299}
9ee04f3b
JK
1300
1301# Usage:
1302# somewhere at the begining of init script:
1303# upstart_controlled
1304# - to pass implement all upstart commands via initctl
1305# start, stop, status, restart, reload and force_reload
1306# are implemented
1307# upstart_controlled command...
1308# - to pass handle only specific commands the upstart way
1309# and leave the rest to the script
1310#
945d7cfd 1311alias upstart_controlled='_upstart_controlled $0 "$@"'
439b6d80 1312
0084dcf3
ER
1313rc_gettext_init
1314rc_cache_init
f9df72e7 1315
bbb612de
AM
1316#/*
1317# * Local variables:
1318# * mode: sh
1319# * indent-tabs-mode: notnil
1320# * End:
1321# *
bbb612de 1322# */
This page took 2.544331 seconds and 4 git commands to generate.