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