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