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