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