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