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