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