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