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
98 while read _t _data; do
99 [ "$_t" = "net:" ] && break
100 done < /proc/self/nsproxy
102 if [ "${_data##*\(}" = "I)" ]; then
103 VSERVER_ISOLATION_NET=yes
105 elif [ -f /proc/self/ninfo ]; then
108 while read _t _data; do
109 [ "$_t" = "NCaps:" ] && break
110 done < /proc/self/ninfo
112 if [ "${_t}" = "NCaps:" ]; then
113 VSERVER_ISOLATION_NET=yes
116 # assume (very?) old kernel mode
117 VSERVER_ISOLATION_NET=yes
123 # we need to know in functions if we were called from a terminal
124 if [ -z "$ISATTY" ]; then
125 [ -t ] && ISATTY=yes || ISATTY=no
132 msg_usage " is_yes {value}"
138 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
152 msg_usage " is_no {value}"
157 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
168 # checks if file is empty
169 # empty lines and lines beginning with hash are ignored
171 [ -s "$1" ] || return 0
172 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
175 # returns OK if $1 contains $2
177 [ "${1#*$2*}" = "$1" ] && return 1
181 # Apply sysctl settings, including files in /etc/sysctl.d
183 if [ -x /lib/systemd/systemd-sysctl ]; then
184 /lib/systemd/systemd-sysctl
189 for file in /usr/lib/sysctl.d/*.conf; do
190 [ -f /run/sysctl.d/${file##*/} ] && continue
191 [ -f /etc/sysctl.d/${file##*/} ] && continue
192 test -f "$file" && sysctl -q -e -p "$file"
194 for file in /run/sysctl.d/*.conf; do
195 [ -f /etc/sysctl.d/${file##*/} ] && continue
196 test -f "$file" && sysctl -q -e -p "$file"
198 for file in /etc/sysctl.d/*.conf; do
199 test -f "$file" && sysctl -q -e -p "$file"
201 sysctl -q -e -p /etc/sysctl.conf
204 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
208 if is_no "$RC_LOGGING"; then
211 while [ "$1" != "${1##-}" ]; do
229 local _x _y _z v v1 old_IFS ver
233 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
242 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
244 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
246 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
252 local _x _y _z v v1 old_IFS ver
256 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
264 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
266 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
272 local _x _y _z v v1 old_IFS ver
276 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
284 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
291 is_yes "$ISATTY" || return
293 if is_yes "$FASTRC" || is_no "$TPUT"; then
296 echo -ne "\033[$(($2+1))G"
299 echo -ne "\033[${2}A"
311 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
314 termput setaf $NORMAL
323 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
324 is_yes "$COLOR_INIT" && tput setaf "$2"
327 termput setaf $NORMAL
333 if [ ! -x /bin/printf ]; then
335 # FIXME: buggy when single or double quotes in message!
340 if [ $# -gt 0 ]; then
343 while [ $# -gt 0 ]; do
348 awk "BEGIN {printf \"$text\", \"$m\"; }"
352 # National language support function
354 local msg_echo nls_domain text message
356 nls_domain="$NLS_DOMAIN"
357 while [ "$1" != "${1##-}" ]; do
373 # empty message, so we return --misiek
374 if [ -z "$message" ]; then
379 if is_yes "$GETTEXT"; then
380 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
383 printf "$message" "$@"
390 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
391 [ -x /bin/splash ] && /bin/splash "$action"
398 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
402 show "Starting %s service" "$1"
405 msg_already_running() {
406 nls "%s service is already running." "$1"
410 show "Stopping %s service" "$1"
414 nls "%s service is not running." "$1"
418 show "Reloading %s service" "$1"
425 # Some functions to handle PLD Linux-style messages
429 if is_yes "$RC_UPTIME"; then
430 time=$(awk '{printf("[%8.2f] ", $1)}' /proc/uptime)
433 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
434 text=$time$(nls -n "$@")
436 text=$time$(printf "$@")
439 while [ $((len++)) -lt $INIT_COL ]; do
442 if [ -n "$CHARS" ]; then
443 termput setaf $CCHARS
451 termput hpa $INIT_COL
454 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
455 # First is the text to display, second is color number to use (argument to
456 # tput setaf). If second argument is not given, default (2, green) will be
466 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
474 echo -ne "$_ok${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
482 echo -ne "$_fail${RC_FEDORA+\\r}${RC_FEDORA-\\n}"
491 # Check if $pid (could be plural) are running
494 [ -d "/proc/$1" ] && return 0
500 # - outside chroot get only those processes, which are outside chroot.
501 # - inside chroot get only those processes, which are inside chroot.
502 # - don't filter out pids which do not have corresponding running processes (process died etc)
503 # (note: some processes like named are chrooted but run outside chroot)
504 # - do nothing inside vserver
506 # filter by pid namespace if such dir exists for current process
507 # we do filter in containers as stacked containers are possible with LXC
508 if [ -d /proc/$$/ns ]; then
510 pids=$(filter_ns "$@") && set -- "$pids"
513 if is_yes "$VSERVER"; then
518 if [ $# -lt 1 -o ! -d /proc/1 ]; then
523 local root_dir good_pids="" good_add_pid
524 for root_pid in $@; do
525 root_dir=$(resolvesymlink /proc/${root_pid}/root)
526 if [ -n "$root_dir" ]; then
528 if [ -n "${SYSTEM_CHROOTS}" ]; then
529 for r_dir in ${SYSTEM_CHROOTS}; do
530 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
533 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
534 elif [ ! -d "/proc/$root_pid" ]; then
535 good_pids="$good_pids $root_pid"
541 # similar to filter_chroot, but filter based on /proc/PID/ns/pid value
543 local cur_ns=$(resolvesymlink /proc/$$/ns/pid)
544 [ "$cur_ns" ] || return 1
547 # add pids if it matches current pid namespace
548 # we should add pids what do not exist (dead processes),
549 # but not add pids whose namespace does not match
550 # (processes belonging to different NS do exist in /proc)
552 if [ ! -d /proc/$pid ]; then
556 ns=$(resolvesymlink /proc/$pid/ns/pid)
557 if [ "$ns" = "$cur_ns" ]; then
566 # run_cmd Message command_to_run
567 # run_cmd -a Message command_to_run
568 # run_cmd --user "username" "Message" command_to_run
570 local force_err=0 exit_code=0 errors user
571 while [ $# -gt 0 ]; do
586 local message=$1; shift
587 show "$message"; busy
591 export HOME=/tmp TMPDIR=/tmp
592 if is_no "$RC_LOGGING"; then
593 ${user:+setuidgid -s $user} "$@" 2>&1
595 ${user:+setuidgid -s $user} initlog -c "$*" 2>&1
599 log_success "$1 $message"
602 log_failed "$1 $message"
605 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
609 _daemon_set_ulimits() {
610 local opt val ksh=${KSH_VERSION:+1}
611 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
612 while [ $# -gt 0 ]; do
633 # A function to start a program (now it's useful on read-only filesystem too)
635 local errors="" prog="" end="" waitname="" waittime=""
637 local nice=$SERVICE_RUN_NICE_LEVEL
638 local fork user closefds redirfds pidfile makepid chdir=/
640 # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
641 # then the answer is: it totally ignores $prog and uses "$@" itself.
643 while [ $# -gt 0 ]; do
646 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
650 # for compatibility with redhat/mandrake
651 nls "warning: --check option is ignored!"
681 pidfile="${1#--pidfile=}"
682 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
687 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
703 if [ -n "$user" -a "$user" != "root" ]; then
704 prog="/bin/su $user -s /bin/sh -c \""
706 if [ "$fork" = "1" ]; then
707 prog="/usr/bin/setsid ${prog:-sh -c \"}"
709 # If command to execute ends with quotation mark, add remaining
710 # arguments and close quotation.
711 if [ "$prog" != "${prog%\"}" ]; then
712 prog="$prog $*$end\""
719 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
720 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
725 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
727 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
728 export USER=root HOME=/tmp TMPDIR=/tmp
730 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
733 # make nice level absolute, not to be dependant of nice level of shell where service started
734 nice=$(($nice - $(nice)))
736 if [ "$closefds" = 1 ]; then
740 elif [ "$redirfds" = 1 ]; then
749 if is_no "$RC_LOGGING"; then
751 if [ ! -x $prog ]; then
752 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
756 if [ -x $a/$prog ]; then
765 # use setsid to detach from terminal,
766 # needs pidfile or ssd would check setsid program instead of real program
767 if [ "$pidfile" ]; then
768 set -- /usr/bin/setsid "$@"
772 /sbin/start-stop-daemon -q --start \
774 ${pidfile:+--pidfile $pidfile} \
775 ${makepid:+--make-pidfile} \
776 ${user:+--chuid $user} \
777 ${chdir:+--chdir "$chdir"} \
778 ${fork:+--background} \
779 ${waitname:+--name $waitname} \
780 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
784 nice -n $nice initlog -c "$prog" 2>&1 </dev/null
788 if [ -n "$waitname" -a -n "$waittime" ]; then
792 pid=$(pidofproc "$waitname" "$pidfile")
793 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
795 while [ "$i" -lt "$waittime" ]; do
797 checkpid $pid && sleep 1 || break
800 log_success "$1 startup"
805 log_failed "$1 startup"
806 [ -n "$errors" ] && echo >&2 "$errors"
811 # A function to stop a program.
813 local notset killlevel base pid pidfile result delay=3 try
816 msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
820 while [ "$1" != "${1##-}" ]; do
828 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
845 # check for second arg to be kill level
852 # experimental start-stop-daemon based killing.
853 # works only with pidfile
854 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
855 local sig=${killlevel:--TERM} retry
856 # retry only if signal is not specified,
857 # as otherwise impossible to send HUP if process pid stays in pidfile.
858 if [ "${killlevel+set}" = "set" ]; then
859 # if we send HUP it's ok if process does not die
862 retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
864 /sbin/start-stop-daemon -q --stop \
867 ${pidfile:+--pidfile $pidfile}
869 if [ "$result" -eq 0 ]; then
882 pid=$(pidofproc "$1" "$pidfile")
883 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
886 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
887 if [ "$notset" = "1" ]; then
888 if checkpid $pid 2>&1; then
889 # TERM first, then KILL if not dead
894 while [ $try -lt $delay ]; do
895 checkpid $pid || break
899 if checkpid $pid; then
900 # XXX: SIGKILL is sent already on 4th second!
901 # HARMFUL for example to mysqld (which is already workarounded)
908 if [ "$result" -eq 0 ]; then
910 log_failed "$1 shutdown"
913 log_success "$1 shutdown"
915 result=$(( ! $result ))
917 # use specified level only
918 if checkpid $pid > /dev/null 2>&1; then
921 if [ "$result" -eq 0 ]; then
923 log_success "$1 got $killlevel"
927 log_failed "$1 didn't get $killlevel"
932 log_failed "$1 shutdown"
937 log_failed "$1 shutdown"
941 if [ -n "$waitname" -a -n "$waittime" ]; then
945 pid=$(pidofproc "$waitname" "$pidfile")
946 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
948 while [ "$i" -lt "$waittime" ]; do
950 checkpid $pid && sleep 1 || break
954 # Remove pid file if any.
955 if [ "$notset" = "1" ]; then
956 rm -f /var/run/${base}.pid
962 # A function to find the pid of a program.
964 local pid pidfile base=${1##*/}
966 [ -n "$2" ] && pidfile="$2"
970 msg_usage " pidofproc {program}"
974 # First try pidfile or "/var/run/*.pid"
976 /*)pidfile="${pidfile}";;
977 *) pidfile="/var/run/$pidfile";;
979 if [ -f "${pidfile}" ]; then
981 for p in $(< "${pidfile}"); do
982 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
987 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
988 pid=$(filter_chroot $pid)
992 # status [--pidfile PIDFILE] {subsys} [{daemon}]"
994 local pid subsys daemon cpuset_msg pidfile
995 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
997 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
1002 daemon=${2:-$subsys}
1006 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
1010 # if pidfile specified, pid must be there
1011 if [ "$pidfile" ]; then
1012 [ -f "$pidfile" ] && read pid < $pidfile
1013 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
1014 if [ ! -d "/proc/$pid" ]; then
1018 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
1020 pid=$(filter_chroot $pid)
1024 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
1025 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
1026 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
1028 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
1031 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
1035 # Next try "/var/run/*.pid" files; if pidfile is not set
1036 local base=${daemon##*/}
1037 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
1038 read pid < /var/run/${base}.pid
1039 pid=$(filter_chroot $pid)
1041 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
1046 # See if /var/lock/subsys/$subsys exists
1047 if [ -f /var/lock/subsys/$subsys ]; then
1048 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
1051 nls "%s is stopped" "$subsys"
1055 # Confirm whether we really want to run this service
1058 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
1077 # module is needed (ie. is requested, is available and isn't loaded already)
1079 # module name without .o at end
1080 if ! lsmod | grep -q "$1"; then
1081 if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
1091 local parsed single die args foo result
1093 while is_no "$parsed"; do
1112 if is_yes "${single}"; then
1114 show "Loading %s kernel module(s)" "$foo"
1117 if [ -x /sbin/modprobe ]; then
1118 /sbin/modprobe -s $args "$@"
1124 if is_yes "${single}"; then
1126 if [ $result = "0" ]; then
1127 is_yes "$single" && ok
1130 if is_yes "$die"; then
1131 nls "Could not load %s kernel module(s)" "$@"
1138 if is_no "$RC_LOGGING"; then
1148 initlog -n $0 -s "$1 $2" -e 1
1152 initlog -n $0 -s "$1 $2" -e 2
1156 # Check if any flavor of portmapper is running
1157 check_portmapper() {
1158 if [ -x /usr/sbin/rpcinfo ]; then
1159 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
1164 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
1170 # is_fsmounted fstype mntpoint
1171 # Check if filesystem fstype is mounted on mntpoint
1176 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1178 if [ -r /proc/mounts ]; then
1179 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
1182 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1190 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
1191 # awk_program should process fstab_file and return a list of fstab-encoded
1192 # paths; it doesn't have to handle comments in fstab_file.
1194 local remaining sig=
1197 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1198 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1199 if [ "$retry" -eq 3 ]; then
1200 run_cmd "$3" fstab-decode umount $5 $remaining
1202 run_cmd "$4" fstab-decode umount $5 $remaining
1205 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1206 while [ "$count" -gt 0 ]; do
1207 [ -z "$remaining" ] && break
1210 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1212 [ -z "$remaining" ] && break
1213 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1215 retry=$(($retry -1))
1220 # Similar to __umount loop above, specialized for loopback devices
1221 __umount_loopback_loop() {
1222 local remaining devremaining sig=
1225 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1226 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1227 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1228 if [ "$retry" -eq 3 ]; then
1229 run_cmd "Unmounting loopback filesystems: " \
1230 fstab-decode umount $remaining
1232 run_cmd "Unmounting loopback filesystems (retry):" \
1233 fstab-decode umount $remaining
1235 for dev in $devremaining ; do
1236 losetup $dev > /dev/null 2>&1 && \
1237 run_cmd "Detaching loopback device $dev: " \
1240 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1241 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1242 [ -z "$remaining" ] && break
1243 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1245 retry=$(($retry -1))
1251 # If we have cachefile, use it.
1252 # If we don't, create memory variables and try to save silently,
1253 local cachefile='/var/cache/rc-scripts/msg.cache'
1256 if is_yes "$ISATTY"; then
1262 # We create $check variable which is used to invalidate the cache.
1263 # The $check contains user locale and terminal.
1264 local check="$term.$LC_MESSAGES.$INIT_COL"
1266 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1267 if . "$cachefile" 2>/dev/null; then
1268 if [ "$check" = "$_check" ]; then
1275 _busy=$(progress "BUSY" "$CBUSY")
1276 _ok=$(progress "DONE")
1277 _started=$(progress "WORK")
1278 _fail=$(progress "FAIL" "$CFAIL")
1279 _died=$(progress "DIED" "$CFAIL")
1281 # we don't use heredoc, as ksh attempts to create tempfile then
1282 (> "$cachefile" ) 2>/dev/null || return
1283 echo "_busy='$_busy';" >> "$cachefile"
1284 echo "_ok='$_ok';" >> "$cachefile"
1285 echo "_started='$_started';" >> "$cachefile"
1286 echo "_fail='$_fail';" >> "$cachefile"
1287 echo "_died='$_died';" >> "$cachefile"
1288 echo "_check='$check';" >> "$cachefile"
1292 if [ -z "$GETTEXT" ]; then
1293 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1300 if [ -z "$TPUT" ]; then
1301 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1303 # check if we are on proper terminal
1304 tput longname >/dev/null 2>&1 || TPUT=no
1312 # True when upstart-event-based boot should be used
1313 is_yes "$USE_UPSTART" && return 0
1314 is_no "$USE_UPSTART" && return 1
1315 if [ ! -x /sbin/initctl ] ; then
1319 local cmdline=$(cat /proc/cmdline 2>/dev/null)
1320 if strstr "$cmdline" "pld.no-upstart" ; then
1330 # emit upstart signal
1331 # only when 'upstart' boot is enabled
1332 use_upstart || return 0
1333 /sbin/initctl emit "$@"
1337 # Return 0 if the given service is an upstart task.
1338 grep -q '^task' "/etc/init/$1.conf"
1340 is_upstart_running() {
1341 # Return 0 if the given service is running via upstart
1342 initctl status "$1" 2>/dev/null | grep -q running
1346 if is_upstart_running "${service}"; then
1347 msg_already_running "$service"
1351 msg_starting "${service}"
1352 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1363 if ! is_upstart_running "${service}"; then
1364 msg_not_running "$name"
1367 msg_stopping "${service}"
1368 if errors=$(/sbin/initctl stop ${service}) ; then
1379 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1382 msg_reloading "${service}"
1383 if errors=$(/sbin/initctl reload ${service}) ; then
1393 # get service status
1394 # should be compliant with
1395 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1398 if is_upstart_task "${service}" ; then
1399 # we probably should have a way to handle task status
1402 if ! status=$(/sbin/initctl status "${service}") ; then
1403 # program or service status is not known
1406 if strstr "$status" "running" ; then
1407 # program is running or service is OK
1411 # program is not running
1415 # TODO: other statuses
1418 _upstart_controlled() {
1419 # If the service is to be handled by upstart
1420 # execute the start/stop/etc. commands the upstart way
1421 if ! use_upstart; then
1427 [ $# -gt 0 ] && shift
1428 local name=$(basename "$script")
1429 if [ ! -f /etc/init/${name}.conf ] ; then
1433 local extra_commands
1434 local has_configtest
1435 if [ "$1" = "--except" ] ; then
1438 for cmd in $commands ; do
1439 if [ "$command" = "$cmd" ] ; then
1443 start|stop|status|reload|restart|try-restart|force-reload)
1447 extra_commands="|$cmd"
1450 extra_commands="|$cmd"
1454 elif [ -n "$*" ] ; then
1458 # is there a better way
1459 for cmd in $commands ; do
1460 if [ "$command" = "$cmd" ] ; then
1465 if [ $found = 0 ] ; then
1466 # let the script handle it
1480 upstart_status $name
1484 if is_yes "$has_configtest" ; then
1485 "$script" configtest || exit 1
1492 if ! is_upstart_running "$name" ; then
1495 if is_yes "$has_configtest" ; then
1496 "$script" configtest || exit 1
1503 if is_yes "$has_configtest" ; then
1504 "$script" configtest || exit 1
1506 if is_upstart_task "$name" ; then
1507 nls "$command not implemented for $name"
1510 upstart_reload "$name"
1515 if is_yes "$has_configtest" ; then
1516 "$script" configtest || exit 1
1518 if is_upstart_task "$name" ; then
1519 upstart_stop "$name"
1520 upstart_start "$name"
1523 upstart_reload "$name"
1528 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
1532 return 1 # should not happen
1536 # somewhere at the begining of init script:
1537 # upstart_controlled
1538 # - to pass implement all upstart commands via initctl
1539 # start, stop, status, restart, reload and force_reload
1541 # upstart_controlled command...
1542 # - to pass handle only specific commands the upstart way
1543 # and leave the rest to the script
1545 alias upstart_controlled='_upstart_controlled $0 "$@"'
1551 # * Local variables:
1553 # * indent-tabs-mode: notnil