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