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.
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>
15 # First set up a default search path.
16 export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
19 if [ -z "$COLUMNS" -o -z "$LINES" ]; then
21 set -- $(stty size 2>/dev/null)
23 COLUMNS=${COLUMNS:-$2}
28 [ -z "$LINES" ] || [ "$LINES" -le 0 ] && LINES=40
29 [ -z "$COLUMNS" ] || [ "$COLUMNS" -le 0 ] && COLUMNS=80
31 INIT_COL=$((COLUMNS - 13))
42 # Bold definition (second parameter to termput setaf)
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
54 CHARS="" # Characters displayed on the beginning of show line
55 CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
57 # save from env, set by /sbin/service
58 env_upstart=$USE_UPSTART
60 # Source configuration if available - may override default values
61 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
62 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
63 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
65 # if initscript is invoked via bash, enable RedHat/Fedora compatibility
66 # RC_FEDORA is "set" if enabled and "unset" when not, but it's "value" is always empty
67 # this is useful for inline constructs
68 if [ "${BASH_VERSION+set}" = "set" ]; then
76 [ "$env_upstart" ] && USE_UPSTART=$env_upstart
78 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
80 while read _f _ctx; do
81 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
82 done </proc/self/status
84 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
92 # VSERVER_ISOLATION_NET = isolation only inside of vserver guests
93 if [ -z "$VSERVER_ISOLATION_NET" -o "$VSERVER_ISOLATION_NET" = "detect" ]; then
94 VSERVER_ISOLATION_NET=no
95 if [ "$VSERVER" = "yes" ]; then
96 if [ -f /proc/self/nsproxy ]; then
98 while read _t _data; do
99 [ "$_t" = "net:" ] && break
100 done < /proc/self/nsproxy
103 # assume old kernel mode
104 VSERVER_ISOLATION_NET=yes
106 if [ "${_data##*\(}" = "I)" ]; then
107 VSERVER_ISOLATION_NET=yes
113 # we need to know in functions if we were called from a terminal
114 if [ -z "$ISATTY" ]; then
115 [ -t ] && ISATTY=yes || ISATTY=no
122 msg_usage " is_yes {value}"
128 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
142 msg_usage " is_no {value}"
147 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
158 # checks if file is empty
159 # empty lines and lines beginning with hash are ignored
161 [ -s "$1" ] || return 0
162 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
165 # returns OK if $1 contains $2
168 [ "${1#*$a*}" = "$1" ] && return 1
172 # Apply sysctl settings, including files in /etc/sysctl.d
174 if [ -x /lib/systemd/systemd-sysctl ]; then
175 /lib/systemd/systemd-sysctl
180 for file in /usr/lib/sysctl.d/*.conf; do
181 [ -f /run/sysctl.d/${file##*/} ] && continue
182 [ -f /etc/sysctl.d/${file##*/} ] && continue
183 test -f "$file" && sysctl -q -e -p "$file"
185 for file in /run/sysctl.d/*.conf; do
186 [ -f /etc/sysctl.d/${file##*/} ] && continue
187 test -f "$file" && sysctl -q -e -p "$file"
189 for file in /etc/sysctl.d/*.conf; do
190 test -f "$file" && sysctl -q -e -p "$file"
192 sysctl -q -e -p /etc/sysctl.conf
195 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
199 if is_no "$RC_LOGGING"; then
202 while [ "$1" != "${1##-}" ]; do
220 local _x _y _z v v1 old_IFS ver
224 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
233 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
235 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
237 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
243 local _x _y _z v v1 old_IFS ver
247 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
255 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
257 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
263 local _x _y _z v v1 old_IFS ver
267 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
275 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
282 is_yes "$ISATTY" || return
284 if is_yes "$FASTRC" || is_no "$TPUT"; then
287 echo -ne "\033[$(($2+1))G"
290 echo -ne "\033[${2}A"
302 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
305 termput setaf $NORMAL
314 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
315 is_yes "$COLOR_INIT" && tput setaf "$2"
318 termput setaf $NORMAL
324 if [ ! -x /bin/printf ]; then
326 # FIXME: buggy when single or double quotes in message!
331 if [ $# -gt 0 ]; then
334 while [ $# -gt 0 ]; do
339 awk "BEGIN {printf \"$text\", \"$m\"; }"
343 # National language support function
345 local msg_echo nls_domain text message
347 nls_domain="$NLS_DOMAIN"
348 while [ "$1" != "${1##-}" ]; do
364 # empty message, so we return --misiek
365 if [ -z "$message" ]; then
370 if is_yes "$GETTEXT"; then
371 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
374 printf "$message" "$@"
381 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
382 [ -x /bin/splash ] && /bin/splash "$action"
389 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
393 show "Starting %s service" "$1"
396 msg_already_running() {
397 nls "%s service is already running." "$1"
401 show "Stopping %s service" "$1"
405 nls "%s service is not running." "$1"
409 show "Reloading %s service" "$1"
416 # Some functions to handle PLD Linux-style messages
420 if is_yes "$RC_UPTIME"; then
421 time=$(awk '{printf("[%8.2f] ", $1)}' /proc/uptime)
424 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
425 text=$time$(nls -n "$@")
427 text=$time$(printf "$@")
430 while [ $((len++)) -lt $INIT_COL ]; do
433 if [ -n "$CHARS" ]; then
434 termput setaf $CCHARS
442 termput hpa $INIT_COL
445 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
446 # First is the text to display, second is color number to use (argument to
447 # tput setaf). If second argument is not given, default (2, green) will be
457 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
465 echo -ne "$_ok${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
473 echo -ne "$_fail${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
482 # Check if $pid (could be plural) are running
485 [ -d "/proc/$1" ] && return 0
491 # - outside chroot get only those processes, which are outside chroot.
492 # - inside chroot get only those processes, which are inside chroot.
493 # - don't filter out pids which do not have corresponding running processes (process died etc)
494 # (note: some processes like named are chrooted but run outside chroot)
495 # - do nothing inside vserver
497 if is_yes "$VSERVER"; then
501 if [ $# -lt 1 -o ! -d /proc/1 ]; then
505 local root_dir good_pids="" good_add_pid
506 for root_pid in $@; do
507 root_dir=$(resolvesymlink /proc/${root_pid}/root)
508 if [ -n "$root_dir" ]; then
510 if [ -n "${SYSTEM_CHROOTS}" ]; then
511 for r_dir in ${SYSTEM_CHROOTS}; do
512 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
515 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
516 elif [ ! -d "/proc/$root_pid" ]; then
517 good_pids="$good_pids $root_pid"
524 # run_cmd Message command_to_run
525 # run_cmd -a Message command_to_run
526 # run_cmd --user "username" "Message" command_to_run
528 local force_err=0 exit_code=0 errors user
529 while [ $# -gt 0 ]; do
544 local message=$1; shift
545 show "$message"; busy
549 export HOME=/tmp TMPDIR=/tmp
550 if is_no "$RC_LOGGING"; then
551 ${user:+setuidgid -s $user} "$@" 2>&1
553 ${user:+setuidgid -s $user} initlog -c "$*" 2>&1
557 log_success "$1 $message"
560 log_failed "$1 $message"
563 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
567 _daemon_set_ulimits() {
568 local opt val ksh=${KSH_VERSION:+1}
569 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
570 while [ $# -gt 0 ]; do
591 # A function to start a program (now it's useful on read-only filesystem too)
593 local errors="" prog="" end="" waitname="" waittime=""
595 local nice=$SERVICE_RUN_NICE_LEVEL
596 local fork user closefds redirfds pidfile makepid chdir=/
598 # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
599 # then the answer is: it totally ignores $prog and uses "$@" itself.
601 while [ $# -gt 0 ]; do
604 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
608 # for compatibility with redhat/mandrake
609 nls "warning: --check option is ignored!"
639 pidfile="${1#--pidfile=}"
640 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
645 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
661 if [ -n "$user" -a "$user" != "root" ]; then
662 prog="/bin/su $user -s /bin/sh -c \""
664 if [ "$fork" = "1" ]; then
665 prog="/usr/bin/setsid ${prog:-sh -c \"}"
667 # If command to execute ends with quotation mark, add remaining
668 # arguments and close quotation.
669 if [ "$prog" != "${prog%\"}" ]; then
670 prog="$prog $*$end\""
677 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
678 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
683 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
685 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
686 export USER=root HOME=/tmp TMPDIR=/tmp
688 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
691 # make nice level absolute, not to be dependant of nice level of shell where service started
692 nice=$(($nice - $(nice)))
694 if [ "$closefds" = 1 ]; then
698 elif [ "$redirfds" = 1 ]; then
706 if is_no "$RC_LOGGING"; then
708 if [ ! -x $prog ]; then
709 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
713 if [ -x $a/$prog ]; then
720 /sbin/start-stop-daemon -q --start \
722 ${pidfile:+--pidfile $pidfile} \
723 ${makepid:+--make-pidfile} \
724 ${user:+--chuid $user} \
725 ${chdir:+--chdir "$chdir"} \
726 ${fork:+--background} \
727 ${waitname:+--name $waitname} \
728 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
732 nice -n $nice initlog -c "$prog" 2>&1 </dev/null
736 if [ -n "$waitname" -a -n "$waittime" ]; then
740 pid=$(pidofproc "$waitname" "$pidfile")
741 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
743 while [ "$i" -lt "$waittime" ]; do
745 checkpid $pid && sleep 1 || break
748 log_success "$1 startup"
753 log_failed "$1 startup"
754 [ -n "$errors" ] && echo >&2 "$errors"
759 # A function to stop a program.
761 local notset killlevel base pid pidfile result delay=3 try
764 msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
768 while [ "$1" != "${1##-}" ]; do
776 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
793 # check for second arg to be kill level
800 # experimental start-stop-daemon based killing.
801 # works only with pidfile
802 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
803 local sig=${killlevel:--TERM} retry
804 # retry only if signal is not specified,
805 # as otherwise impossible to send HUP if process pid stays in pidfile.
806 if [ "${killlevel+set}" = "set" ]; then
807 # if we send HUP it's ok if process does not die
810 retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
812 /sbin/start-stop-daemon -q --stop \
815 ${pidfile:+--pidfile $pidfile}
817 if [ "$result" -eq 0 ]; then
830 pid=$(pidofproc "$1" "$pidfile")
831 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
834 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
835 if [ "$notset" = "1" ]; then
836 if checkpid $pid 2>&1; then
837 # TERM first, then KILL if not dead
842 while [ $try -lt $delay ]; do
843 checkpid $pid || break
847 if checkpid $pid; then
848 # XXX: SIGKILL is sent already on 4th second!
849 # HARMFUL for example to mysqld (which is already workarounded)
856 if [ "$result" -eq 0 ]; then
858 log_failed "$1 shutdown"
861 log_success "$1 shutdown"
863 result=$(( ! $result ))
865 # use specified level only
866 if checkpid $pid > /dev/null 2>&1; then
869 if [ "$result" -eq 0 ]; then
871 log_success "$1 got $killlevel"
875 log_failed "$1 didn't get $killlevel"
880 log_failed "$1 shutdown"
885 log_failed "$1 shutdown"
889 if [ -n "$waitname" -a -n "$waittime" ]; then
893 pid=$(pidofproc "$waitname" "$pidfile")
894 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
896 while [ "$i" -lt "$waittime" ]; do
898 checkpid $pid && sleep 1 || break
902 # Remove pid file if any.
903 if [ "$notset" = "1" ]; then
904 rm -f /var/run/${base}.pid
910 # A function to find the pid of a program.
912 local pid pidfile base=${1##*/}
914 [ -n "$2" ] && pidfile="$2"
918 msg_usage " pidofproc {program}"
922 # First try pidfile or "/var/run/*.pid"
924 /*)pidfile="${pidfile}";;
925 *) pidfile="/var/run/$pidfile";;
927 if [ -f "${pidfile}" ]; then
929 for p in $(< "${pidfile}"); do
930 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
935 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
936 pid=$(filter_chroot "$pid")
940 # status [--pidfile PIDFILE] {subsys} [{daemon}]"
942 local pid subsys daemon cpuset_msg pidfile
943 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
945 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
954 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
958 # if pidfile specified, pid must be there
959 if [ "$pidfile" ]; then
960 [ -f "$pidfile" ] && read pid < $pidfile
961 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
962 if [ ! -d "/proc/$pid" ]; then
966 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
968 pid=$(filter_chroot "$pid")
972 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
973 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
974 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
976 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
979 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
983 # Next try "/var/run/*.pid" files; if pidfile is not set
984 local base=${daemon##*/}
985 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
986 read pid < /var/run/${base}.pid
987 pid=$(filter_chroot "$pid")
989 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
994 # See if /var/lock/subsys/$subsys exists
995 if [ -f /var/lock/subsys/$subsys ]; then
996 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
999 nls "%s is stopped" "$subsys"
1003 # Confirm whether we really want to run this service
1006 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
1025 # module is needed (ie. is requested, is available and isn't loaded already)
1027 # module name without .o at end
1028 if ! lsmod | grep -q "$1"; then
1029 if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
1039 local parsed single die args foo result
1041 while is_no "$parsed"; do
1060 if is_yes "${single}"; then
1062 show "Loading %s kernel module(s)" "$foo"
1065 if [ -x /sbin/modprobe ]; then
1066 /sbin/modprobe -s $args "$@"
1072 if is_yes "${single}"; then
1074 if [ $result = "0" ]; then
1075 is_yes "$single" && ok
1078 if is_yes "$die"; then
1079 nls "Could not load %s kernel module(s)" "$@"
1086 if is_no "$RC_LOGGING"; then
1096 initlog -n $0 -s "$1 $2" -e 1
1100 initlog -n $0 -s "$1 $2" -e 2
1104 # Check if any flavor of portmapper is running
1105 check_portmapper() {
1106 if [ -x /usr/sbin/rpcinfo ]; then
1107 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
1112 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
1118 # is_fsmounted fstype mntpoint
1119 # Check if filesystem fstype is mounted on mntpoint
1124 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1126 if [ -r /proc/mounts ]; then
1127 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
1130 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1138 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
1139 # awk_program should process fstab_file and return a list of fstab-encoded
1140 # paths; it doesn't have to handle comments in fstab_file.
1142 local remaining sig=
1145 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1146 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1147 if [ "$retry" -eq 3 ]; then
1148 run_cmd "$3" fstab-decode umount $5 $remaining
1150 run_cmd "$4" fstab-decode umount $5 $remaining
1153 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1154 while [ "$count" -gt 0 ]; do
1155 [ -z "$remaining" ] && break
1158 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1160 [ -z "$remaining" ] && break
1161 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1163 retry=$(($retry -1))
1168 # Similar to __umount loop above, specialized for loopback devices
1169 __umount_loopback_loop() {
1170 local remaining devremaining sig=
1173 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1174 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1175 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1176 if [ "$retry" -eq 3 ]; then
1177 run_cmd "Unmounting loopback filesystems: " \
1178 fstab-decode umount $remaining
1180 run_cmd "Unmounting loopback filesystems (retry):" \
1181 fstab-decode umount $remaining
1183 for dev in $devremaining ; do
1184 losetup $dev > /dev/null 2>&1 && \
1185 run_cmd "Detaching loopback device $dev: " \
1188 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1189 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1190 [ -z "$remaining" ] && break
1191 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1193 retry=$(($retry -1))
1199 # If we have cachefile, use it.
1200 # If we don't, create memory variables and try to save silently,
1201 local cachefile='/var/cache/rc-scripts/msg.cache'
1204 if is_yes "$ISATTY"; then
1210 # We create $check variable which is used to invalidate the cache.
1211 # The $check contains user locale and terminal.
1212 local check="$term.$LC_MESSAGES.$INIT_COL"
1214 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1215 if . "$cachefile" 2>/dev/null; then
1216 if [ "$check" = "$_check" ]; then
1223 _busy=$(progress "BUSY" "$CBUSY")
1224 _ok=$(progress "DONE")
1225 _started=$(progress "WORK")
1226 _fail=$(progress "FAIL" "$CFAIL")
1227 _died=$(progress "DIED" "$CFAIL")
1229 # we don't use heredoc, as ksh attempts to create tempfile then
1230 (> "$cachefile" ) 2>/dev/null || return
1231 echo "_busy='$_busy';" >> "$cachefile"
1232 echo "_ok='$_ok';" >> "$cachefile"
1233 echo "_started='$_started';" >> "$cachefile"
1234 echo "_fail='$_fail';" >> "$cachefile"
1235 echo "_died='$_died';" >> "$cachefile"
1236 echo "_check='$check';" >> "$cachefile"
1240 if [ -z "$GETTEXT" ]; then
1241 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1248 if [ -z "$TPUT" ]; then
1249 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1251 # check if we are on proper terminal
1252 tput longname >/dev/null 2>&1 || TPUT=no
1260 # True when upstart-event-based boot should be used
1261 is_yes "$USE_UPSTART" && return 0
1262 is_no "$USE_UPSTART" && return 1
1263 if [ ! -x /sbin/initctl ] ; then
1267 local cmdline=$(cat /proc/cmdline 2>/dev/null)
1268 if strstr "$cmdline" "pld.no-upstart" ; then
1278 # emit upstart signal
1279 # only when 'upstart' boot is enabled
1280 use_upstart || return 0
1281 /sbin/initctl emit "$@"
1285 # Return 0 if the given service is an upstart task.
1286 grep -q '^task' "/etc/init/$1.conf"
1288 is_upstart_running() {
1289 # Return 0 if the given service is running via upstart
1290 initctl status "$1" 2>/dev/null | grep -q running
1294 if is_upstart_running "${service}"; then
1295 msg_already_running "$service"
1299 msg_starting "${service}"
1300 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1311 if ! is_upstart_running "${service}"; then
1312 msg_not_running "$name"
1315 msg_stopping "${service}"
1316 if errors=$(/sbin/initctl stop ${service}) ; then
1327 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1330 msg_reloading "${service}"
1331 if errors=$(/sbin/initctl reload ${service}) ; then
1341 # get service status
1342 # should be compliant with
1343 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1346 if is_upstart_task "${service}" ; then
1347 # we probably should have a way to handle task status
1350 if ! status=$(/sbin/initctl status "${service}") ; then
1351 # program or service status is not known
1354 if strstr "$status" "running" ; then
1355 # program is running or service is OK
1359 # program is not running
1363 # TODO: other statuses
1366 _upstart_controlled() {
1367 # If the service is to be handled by upstart
1368 # execute the start/stop/etc. commands the upstart way
1369 if ! use_upstart; then
1375 [ $# -gt 0 ] && shift
1376 local name=$(basename "$script")
1377 if [ ! -f /etc/init/${name}.conf ] ; then
1381 local extra_commands
1382 local has_configtest
1383 if [ "$1" = "--except" ] ; then
1386 for cmd in $commands ; do
1387 if [ "$command" = "$cmd" ] ; then
1391 start|stop|status|reload|restart|try-restart|force-reload)
1395 extra_commands="|$cmd"
1398 extra_commands="|$cmd"
1402 elif [ -n "$*" ] ; then
1406 # is there a better way
1407 for cmd in $commands ; do
1408 if [ "$command" = "$cmd" ] ; then
1413 if [ $found = 0 ] ; then
1414 # let the script handle it
1428 upstart_status $name
1432 if is_yes "$has_configtest" ; then
1433 "$script" configtest || exit 1
1440 if ! is_upstart_running "$name" ; then
1443 if is_yes "$has_configtest" ; then
1444 "$script" configtest || exit 1
1451 if is_yes "$has_configtest" ; then
1452 "$script" configtest || exit 1
1454 if is_upstart_task "$name" ; then
1455 nls "$command not implemented for $name"
1458 upstart_reload "$name"
1463 if is_yes "$has_configtest" ; then
1464 "$script" configtest || exit 1
1466 if is_upstart_task "$name" ; then
1467 upstart_stop "$name"
1468 upstart_start "$name"
1471 upstart_reload "$name"
1476 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
1480 return 1 # should not happen
1484 # somewhere at the begining of init script:
1485 # upstart_controlled
1486 # - to pass implement all upstart commands via initctl
1487 # start, stop, status, restart, reload and force_reload
1489 # upstart_controlled command...
1490 # - to pass handle only specific commands the upstart way
1491 # and leave the rest to the script
1493 alias upstart_controlled='_upstart_controlled $0 "$@"'
1499 # * Local variables:
1501 # * indent-tabs-mode: notnil