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.
6 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by: Greg Galloway and Marc Ewing
8 # Modified for PLD Linux by:
9 # Marek Obuchowicz <elephant@pld-linux.org>
10 # Arkadiusz Miśkiewicz <misiek@pld-linux.org>
11 # Michał Kochanowicz <mkochano@pld-linux.org>
12 # Łukasz Pawelczyk <havner@pld-linux.org>
14 # First set up a default search path.
15 export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
18 if [ -z "$COLUMNS" -o -z "$LINES" ]; then
20 set -- $(stty size 2>/dev/null)
22 COLUMNS=${COLUMNS:-$2}
27 [ -z "$LINES" ] || [ "$LINES" -le 0 ] && LINES=40
28 [ -z "$COLUMNS" ] || [ "$COLUMNS" -le 0 ] && COLUMNS=80
30 INIT_COL=$((COLUMNS - 13))
41 # Bold definition (second parameter to termput setaf)
45 CBRACKETS="$CYAN" # brackets [ ] color
46 CDONE="$GREEN" # DONE and WORK color
47 CBUSY="$MAGENTA" # BUSY color
48 CFAIL="$RED" # FAIL and DIED color
49 CPOWEREDBY="$CYAN" # "Powered by" color
50 CPLD="$GREEN" # "PLD Linux Distribution" color
51 CI="$RED" # Capital I color (press I to enter interactive startup)
52 CRESMAN="$GREEN" # "Resource Manager" color
53 CHARS="" # Characters displayed on the beginning of show line
54 CCHARS="$NORMAL" # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
56 # save from env, set by /sbin/service
57 env_upstart=$USE_UPSTART
59 # Source configuration if available - may override default values
60 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
61 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
62 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
64 # if initscript is invoked via bash, enable RedHat/Fedora compatibility
65 # RC_FEDORA is "set" if enabled and "unset" when not, but it's "value" is always empty
66 # this is useful for inline constructs
67 if [ "${BASH_VERSION+set}" = "set" ]; then
75 [ "$env_upstart" ] && USE_UPSTART=$env_upstart
77 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
79 while read _f _ctx; do
80 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
81 done </proc/self/status
83 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
91 # VSERVER_ISOLATION_NET = isolation only inside of vserver guests
92 if [ -z "$VSERVER_ISOLATION_NET" -o "$VSERVER_ISOLATION_NET" = "detect" ]; then
93 VSERVER_ISOLATION_NET=no
94 if [ "$VSERVER" = "yes" ]; then
95 if [ -f /proc/self/nsproxy ]; then
97 while read _t _data; do
98 [ "$_t" = "net:" ] && break
99 done < /proc/self/nsproxy
102 # assume old kernel mode
103 VSERVER_ISOLATION_NET=yes
105 if [ "${_data##*\(}" = "I)" ]; then
106 VSERVER_ISOLATION_NET=yes
112 # we need to know in functions if we were called from a terminal
113 if [ -z "$ISATTY" ]; then
114 [ -t ] && ISATTY=yes || ISATTY=no
121 msg_usage " is_yes {value}"
127 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
141 msg_usage " is_no {value}"
146 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
157 # checks if file is empty
158 # empty lines and lines beginning with hash are ignored
160 [ -s "$1" ] || return 0
161 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
164 # returns OK if $1 contains $2
167 [ "${1#*$a*}" = "$1" ] && return 1
171 # Apply sysctl settings, including files in /etc/sysctl.d
173 if [ -x /lib/systemd/systemd-sysctl ]; then
174 /lib/systemd/systemd-sysctl
179 for file in /usr/lib/sysctl.d/*.conf; do
180 [ -f /run/sysctl.d/${file##*/} ] && continue
181 [ -f /etc/sysctl.d/${file##*/} ] && continue
182 test -f "$file" && sysctl -q -e -p "$file"
184 for file in /run/sysctl.d/*.conf; do
185 [ -f /etc/sysctl.d/${file##*/} ] && continue
186 test -f "$file" && sysctl -q -e -p "$file"
188 for file in /etc/sysctl.d/*.conf; do
189 test -f "$file" && sysctl -q -e -p "$file"
191 sysctl -q -e -p /etc/sysctl.conf
194 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
198 if is_no "$RC_LOGGING"; then
201 while [ "$1" != "${1##-}" ]; do
219 local _x _y _z v v1 old_IFS ver
223 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
232 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
234 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
236 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
242 local _x _y _z v v1 old_IFS ver
246 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
254 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
256 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
262 local _x _y _z v v1 old_IFS ver
266 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
274 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
281 is_yes "$ISATTY" || return
283 if is_yes "$FASTRC" || is_no "$TPUT"; then
286 echo -ne "\033[$(($2+1))G"
289 echo -ne "\033[${2}A"
301 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
304 termput setaf $NORMAL
313 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
314 is_yes "$COLOR_INIT" && tput setaf "$2"
317 termput setaf $NORMAL
323 if [ ! -x /bin/printf ]; then
325 # FIXME: buggy when single or double quotes in message!
330 if [ $# -gt 0 ]; then
333 while [ $# -gt 0 ]; do
338 awk "BEGIN {printf \"$text\", \"$m\"; }"
342 # National language support function
344 local msg_echo nls_domain text message
346 nls_domain="$NLS_DOMAIN"
347 while [ "$1" != "${1##-}" ]; do
363 # empty message, so we return --misiek
364 if [ -z "$message" ]; then
369 if is_yes "$GETTEXT"; then
370 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
373 printf "$message" "$@"
380 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
381 [ -x /bin/splash ] && /bin/splash "$action"
388 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
392 show "Starting %s service" "$1"
395 msg_already_running() {
396 nls "%s service is already running." "$1"
400 show "Stopping %s service" "$1"
404 nls "%s service is not running." "$1"
408 show "Reloading %s service" "$1"
415 # Some functions to handle PLD Linux-style messages
419 if is_yes "$RC_UPTIME"; then
420 time=$(awk '{printf("[%8.2f] ", $1)}' /proc/uptime)
423 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
424 text=$time$(nls -n "$@")
426 text=$time$(printf "$@")
429 while [ $((len++)) -lt $INIT_COL ]; do
432 if [ -n "$CHARS" ]; then
433 termput setaf $CCHARS
441 termput hpa $INIT_COL
444 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
445 # First is the text to display, second is color number to use (argument to
446 # tput setaf). If second argument is not given, default (2, green) will be
456 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
464 echo -ne "$_ok${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
472 echo -ne "$_fail${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
481 # Check if $pid (could be plural) are running
484 [ -d "/proc/$1" ] && return 0
490 # - outside chroot get only those processes, which are outside chroot.
491 # - inside chroot get only those processes, which are inside chroot.
492 # - don't filter out pids which do not have corresponding running processes (process died etc)
493 # (note: some processes like named are chrooted but run outside chroot)
494 # - do nothing inside vserver
496 # filter by pid namespace if such dir exists for current process
497 # we do filter in containers as stacked containers are possible with LXC
498 if [ -d /proc/$$/ns ]; then
500 pids=$(filter_ns "$@") && set -- "$pids"
503 if is_yes "$VSERVER"; then
508 if [ $# -lt 1 -o ! -d /proc/1 ]; then
513 local root_dir good_pids="" good_add_pid
514 for root_pid in $@; do
515 root_dir=$(resolvesymlink /proc/${root_pid}/root)
516 if [ -n "$root_dir" ]; then
518 if [ -n "${SYSTEM_CHROOTS}" ]; then
519 for r_dir in ${SYSTEM_CHROOTS}; do
520 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
523 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
524 elif [ ! -d "/proc/$root_pid" ]; then
525 good_pids="$good_pids $root_pid"
531 # similar to filter_chroot, but filter based on /proc/PID/ns/pid value
533 local cur_ns=$(resolvesymlink /proc/$$/ns/pid)
534 [ "$cur_ns" ] || return 1
537 # add pids if it matches current pid namespace
538 # we should add pids what do not exist (dead processes),
539 # but not add pids whose namespace does not match
540 # (processes belonging to different NS do exist in /proc)
542 if [ ! -d /proc/$pid ]; then
546 ns=$(resolvesymlink /proc/$pid/ns/pid)
547 if [ "$ns" = "$cur_ns" ]; then
556 # run_cmd Message command_to_run
557 # run_cmd -a Message command_to_run
558 # run_cmd --user "username" "Message" command_to_run
560 local force_err=0 exit_code=0 errors user
561 while [ $# -gt 0 ]; do
576 local message=$1; shift
577 show "$message"; busy
581 export HOME=/tmp TMPDIR=/tmp
582 if is_no "$RC_LOGGING"; then
583 ${user:+setuidgid -s $user} "$@" 2>&1
585 ${user:+setuidgid -s $user} initlog -c "$*" 2>&1
589 log_success "$1 $message"
592 log_failed "$1 $message"
595 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
599 _daemon_set_ulimits() {
600 local opt val ksh=${KSH_VERSION:+1}
601 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
602 while [ $# -gt 0 ]; do
623 # A function to start a program (now it's useful on read-only filesystem too)
625 local errors="" prog="" end="" waitname="" waittime=""
627 local nice=$SERVICE_RUN_NICE_LEVEL
628 local fork user closefds redirfds pidfile makepid chdir=/
630 # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
631 # then the answer is: it totally ignores $prog and uses "$@" itself.
633 while [ $# -gt 0 ]; do
636 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
640 # for compatibility with redhat/mandrake
641 nls "warning: --check option is ignored!"
671 pidfile="${1#--pidfile=}"
672 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
677 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
693 if [ -n "$user" -a "$user" != "root" ]; then
694 prog="/bin/su $user -s /bin/sh -c \""
696 if [ "$fork" = "1" ]; then
697 prog="/usr/bin/setsid ${prog:-sh -c \"}"
699 # If command to execute ends with quotation mark, add remaining
700 # arguments and close quotation.
701 if [ "$prog" != "${prog%\"}" ]; then
702 prog="$prog $*$end\""
709 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
710 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
715 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
717 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
718 export USER=root HOME=/tmp TMPDIR=/tmp
720 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
723 # make nice level absolute, not to be dependant of nice level of shell where service started
724 nice=$(($nice - $(nice)))
726 if [ "$closefds" = 1 ]; then
730 elif [ "$redirfds" = 1 ]; then
739 if is_no "$RC_LOGGING"; then
741 if [ ! -x $prog ]; then
742 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
746 if [ -x $a/$prog ]; then
755 /sbin/start-stop-daemon -q --start \
757 ${pidfile:+--pidfile $pidfile} \
758 ${makepid:+--make-pidfile} \
759 ${user:+--chuid $user} \
760 ${chdir:+--chdir "$chdir"} \
761 ${fork:+--background} \
762 ${waitname:+--name $waitname} \
763 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
767 nice -n $nice initlog -c "$prog" 2>&1 </dev/null
771 if [ -n "$waitname" -a -n "$waittime" ]; then
775 pid=$(pidofproc "$waitname" "$pidfile")
776 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
778 while [ "$i" -lt "$waittime" ]; do
780 checkpid $pid && sleep 1 || break
783 log_success "$1 startup"
788 log_failed "$1 startup"
789 [ -n "$errors" ] && echo >&2 "$errors"
794 # A function to stop a program.
796 local notset killlevel base pid pidfile result delay=3 try
799 msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
803 while [ "$1" != "${1##-}" ]; do
811 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
828 # check for second arg to be kill level
835 # experimental start-stop-daemon based killing.
836 # works only with pidfile
837 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
838 local sig=${killlevel:--TERM} retry
839 # retry only if signal is not specified,
840 # as otherwise impossible to send HUP if process pid stays in pidfile.
841 if [ "${killlevel+set}" = "set" ]; then
842 # if we send HUP it's ok if process does not die
845 retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
847 /sbin/start-stop-daemon -q --stop \
850 ${pidfile:+--pidfile $pidfile}
852 if [ "$result" -eq 0 ]; then
865 pid=$(pidofproc "$1" "$pidfile")
866 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
869 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
870 if [ "$notset" = "1" ]; then
871 if checkpid $pid 2>&1; then
872 # TERM first, then KILL if not dead
877 while [ $try -lt $delay ]; do
878 checkpid $pid || break
882 if checkpid $pid; then
883 # XXX: SIGKILL is sent already on 4th second!
884 # HARMFUL for example to mysqld (which is already workarounded)
891 if [ "$result" -eq 0 ]; then
893 log_failed "$1 shutdown"
896 log_success "$1 shutdown"
898 result=$(( ! $result ))
900 # use specified level only
901 if checkpid $pid > /dev/null 2>&1; then
904 if [ "$result" -eq 0 ]; then
906 log_success "$1 got $killlevel"
910 log_failed "$1 didn't get $killlevel"
915 log_failed "$1 shutdown"
920 log_failed "$1 shutdown"
924 if [ -n "$waitname" -a -n "$waittime" ]; then
928 pid=$(pidofproc "$waitname" "$pidfile")
929 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
931 while [ "$i" -lt "$waittime" ]; do
933 checkpid $pid && sleep 1 || break
937 # Remove pid file if any.
938 if [ "$notset" = "1" ]; then
939 rm -f /var/run/${base}.pid
945 # A function to find the pid of a program.
947 local pid pidfile base=${1##*/}
949 [ -n "$2" ] && pidfile="$2"
953 msg_usage " pidofproc {program}"
957 # First try pidfile or "/var/run/*.pid"
959 /*)pidfile="${pidfile}";;
960 *) pidfile="/var/run/$pidfile";;
962 if [ -f "${pidfile}" ]; then
964 for p in $(< "${pidfile}"); do
965 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
970 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
971 pid=$(filter_chroot $pid)
975 # status [--pidfile PIDFILE] {subsys} [{daemon}]"
977 local pid subsys daemon cpuset_msg pidfile
978 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
980 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
989 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
993 # if pidfile specified, pid must be there
994 if [ "$pidfile" ]; then
995 [ -f "$pidfile" ] && read pid < $pidfile
996 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
997 if [ ! -d "/proc/$pid" ]; then
1001 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
1003 pid=$(filter_chroot $pid)
1007 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
1008 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
1009 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
1011 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
1014 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
1018 # Next try "/var/run/*.pid" files; if pidfile is not set
1019 local base=${daemon##*/}
1020 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
1021 read pid < /var/run/${base}.pid
1022 pid=$(filter_chroot $pid)
1024 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
1029 # See if /var/lock/subsys/$subsys exists
1030 if [ -f /var/lock/subsys/$subsys ]; then
1031 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
1034 nls "%s is stopped" "$subsys"
1038 # Confirm whether we really want to run this service
1041 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
1060 # module is needed (ie. is requested, is available and isn't loaded already)
1062 # module name without .o at end
1063 if ! lsmod | grep -q "$1"; then
1064 if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
1074 local parsed single die args foo result
1076 while is_no "$parsed"; do
1095 if is_yes "${single}"; then
1097 show "Loading %s kernel module(s)" "$foo"
1100 if [ -x /sbin/modprobe ]; then
1101 /sbin/modprobe -s $args "$@"
1107 if is_yes "${single}"; then
1109 if [ $result = "0" ]; then
1110 is_yes "$single" && ok
1113 if is_yes "$die"; then
1114 nls "Could not load %s kernel module(s)" "$@"
1121 if is_no "$RC_LOGGING"; then
1131 initlog -n $0 -s "$1 $2" -e 1
1135 initlog -n $0 -s "$1 $2" -e 2
1139 # Check if any flavor of portmapper is running
1140 check_portmapper() {
1141 if [ -x /usr/sbin/rpcinfo ]; then
1142 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
1147 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
1153 # is_fsmounted fstype mntpoint
1154 # Check if filesystem fstype is mounted on mntpoint
1159 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1161 if [ -r /proc/mounts ]; then
1162 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
1165 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1173 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
1174 # awk_program should process fstab_file and return a list of fstab-encoded
1175 # paths; it doesn't have to handle comments in fstab_file.
1177 local remaining sig=
1180 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1181 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1182 if [ "$retry" -eq 3 ]; then
1183 run_cmd "$3" fstab-decode umount $5 $remaining
1185 run_cmd "$4" fstab-decode umount $5 $remaining
1188 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1189 while [ "$count" -gt 0 ]; do
1190 [ -z "$remaining" ] && break
1193 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1195 [ -z "$remaining" ] && break
1196 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1198 retry=$(($retry -1))
1203 # Similar to __umount loop above, specialized for loopback devices
1204 __umount_loopback_loop() {
1205 local remaining devremaining sig=
1208 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1209 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1210 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1211 if [ "$retry" -eq 3 ]; then
1212 run_cmd "Unmounting loopback filesystems: " \
1213 fstab-decode umount $remaining
1215 run_cmd "Unmounting loopback filesystems (retry):" \
1216 fstab-decode umount $remaining
1218 for dev in $devremaining ; do
1219 losetup $dev > /dev/null 2>&1 && \
1220 run_cmd "Detaching loopback device $dev: " \
1223 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1224 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1225 [ -z "$remaining" ] && break
1226 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1228 retry=$(($retry -1))
1234 # If we have cachefile, use it.
1235 # If we don't, create memory variables and try to save silently,
1236 local cachefile='/var/cache/rc-scripts/msg.cache'
1239 if is_yes "$ISATTY"; then
1245 # We create $check variable which is used to invalidate the cache.
1246 # The $check contains user locale and terminal.
1247 local check="$term.$LC_MESSAGES.$INIT_COL"
1249 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1250 if . "$cachefile" 2>/dev/null; then
1251 if [ "$check" = "$_check" ]; then
1258 _busy=$(progress "BUSY" "$CBUSY")
1259 _ok=$(progress "DONE")
1260 _started=$(progress "WORK")
1261 _fail=$(progress "FAIL" "$CFAIL")
1262 _died=$(progress "DIED" "$CFAIL")
1264 # we don't use heredoc, as ksh attempts to create tempfile then
1265 (> "$cachefile" ) 2>/dev/null || return
1266 echo "_busy='$_busy';" >> "$cachefile"
1267 echo "_ok='$_ok';" >> "$cachefile"
1268 echo "_started='$_started';" >> "$cachefile"
1269 echo "_fail='$_fail';" >> "$cachefile"
1270 echo "_died='$_died';" >> "$cachefile"
1271 echo "_check='$check';" >> "$cachefile"
1275 if [ -z "$GETTEXT" ]; then
1276 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1283 if [ -z "$TPUT" ]; then
1284 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1286 # check if we are on proper terminal
1287 tput longname >/dev/null 2>&1 || TPUT=no
1295 # True when upstart-event-based boot should be used
1296 is_yes "$USE_UPSTART" && return 0
1297 is_no "$USE_UPSTART" && return 1
1298 if [ ! -x /sbin/initctl ] ; then
1302 local cmdline=$(cat /proc/cmdline 2>/dev/null)
1303 if strstr "$cmdline" "pld.no-upstart" ; then
1313 # emit upstart signal
1314 # only when 'upstart' boot is enabled
1315 use_upstart || return 0
1316 /sbin/initctl emit "$@"
1320 # Return 0 if the given service is an upstart task.
1321 grep -q '^task' "/etc/init/$1.conf"
1323 is_upstart_running() {
1324 # Return 0 if the given service is running via upstart
1325 initctl status "$1" 2>/dev/null | grep -q running
1329 if is_upstart_running "${service}"; then
1330 msg_already_running "$service"
1334 msg_starting "${service}"
1335 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1346 if ! is_upstart_running "${service}"; then
1347 msg_not_running "$name"
1350 msg_stopping "${service}"
1351 if errors=$(/sbin/initctl stop ${service}) ; then
1362 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1365 msg_reloading "${service}"
1366 if errors=$(/sbin/initctl reload ${service}) ; then
1376 # get service status
1377 # should be compliant with
1378 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1381 if is_upstart_task "${service}" ; then
1382 # we probably should have a way to handle task status
1385 if ! status=$(/sbin/initctl status "${service}") ; then
1386 # program or service status is not known
1389 if strstr "$status" "running" ; then
1390 # program is running or service is OK
1394 # program is not running
1398 # TODO: other statuses
1401 _upstart_controlled() {
1402 # If the service is to be handled by upstart
1403 # execute the start/stop/etc. commands the upstart way
1404 if ! use_upstart; then
1410 [ $# -gt 0 ] && shift
1411 local name=$(basename "$script")
1412 if [ ! -f /etc/init/${name}.conf ] ; then
1416 local extra_commands
1417 local has_configtest
1418 if [ "$1" = "--except" ] ; then
1421 for cmd in $commands ; do
1422 if [ "$command" = "$cmd" ] ; then
1426 start|stop|status|reload|restart|try-restart|force-reload)
1430 extra_commands="|$cmd"
1433 extra_commands="|$cmd"
1437 elif [ -n "$*" ] ; then
1441 # is there a better way
1442 for cmd in $commands ; do
1443 if [ "$command" = "$cmd" ] ; then
1448 if [ $found = 0 ] ; then
1449 # let the script handle it
1463 upstart_status $name
1467 if is_yes "$has_configtest" ; then
1468 "$script" configtest || exit 1
1475 if ! is_upstart_running "$name" ; then
1478 if is_yes "$has_configtest" ; then
1479 "$script" configtest || exit 1
1486 if is_yes "$has_configtest" ; then
1487 "$script" configtest || exit 1
1489 if is_upstart_task "$name" ; then
1490 nls "$command not implemented for $name"
1493 upstart_reload "$name"
1498 if is_yes "$has_configtest" ; then
1499 "$script" configtest || exit 1
1501 if is_upstart_task "$name" ; then
1502 upstart_stop "$name"
1503 upstart_start "$name"
1506 upstart_reload "$name"
1511 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
1515 return 1 # should not happen
1519 # somewhere at the begining of init script:
1520 # upstart_controlled
1521 # - to pass implement all upstart commands via initctl
1522 # start, stop, status, restart, reload and force_reload
1524 # upstart_controlled command...
1525 # - to pass handle only specific commands the upstart way
1526 # and leave the rest to the script
1528 alias upstart_controlled='_upstart_controlled $0 "$@"'
1534 # * Local variables:
1536 # * indent-tabs-mode: notnil