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