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 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
176 if is_no "$RC_LOGGING"; then
179 while [ "$1" != "${1##-}" ]; do
197 local _x _y _z v v1 old_IFS ver
201 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
210 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
212 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
214 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
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"
232 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
234 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
240 local _x _y _z v v1 old_IFS ver
244 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
252 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
259 is_yes "$ISATTY" || return
261 if is_yes "$FASTRC" || is_no "$TPUT"; then
264 echo -ne "\033[$(($2+1))G"
267 echo -ne "\033[${2}A"
279 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
282 termput setaf $NORMAL
291 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
292 is_yes "$COLOR_INIT" && tput setaf "$2"
295 termput setaf $NORMAL
301 if [ ! -x /bin/printf ]; then
303 # FIXME: buggy when single or double quotes in message!
308 if [ $# -gt 0 ]; then
311 while [ $# -gt 0 ]; do
316 awk "BEGIN {printf \"$text\", \"$m\"; }"
320 # National language support function
322 local msg_echo nls_domain text message
324 nls_domain="$NLS_DOMAIN"
325 while [ "$1" != "${1##-}" ]; do
341 # empty message, so we return --misiek
342 if [ -z "$message" ]; then
347 if is_yes "$GETTEXT"; then
348 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
351 printf "$message" "$@"
358 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
359 [ -x /bin/splash ] && /bin/splash "$action"
366 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
370 show "Starting %s service" "$1"
373 msg_already_running() {
374 nls "%s service is already running." "$1"
378 show "Stopping %s service" "$1"
382 nls "%s service is not running." "$1"
386 show "Reloading %s service" "$1"
393 # Some functions to handle PLD Linux-style messages
397 if is_yes "$RC_UPTIME"; then
398 time=$(awk '{printf("[%8.2f] ", $1)}' /proc/uptime)
401 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
402 text=$time$(nls -n "$@")
404 text=$time$(printf "$@")
407 while [ $((len++)) -lt $INIT_COL ]; do
410 if [ -n "$CHARS" ]; then
411 termput setaf $CCHARS
419 termput hpa $INIT_COL
422 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
423 # First is the text to display, second is color number to use (argument to
424 # tput setaf). If second argument is not given, default (2, green) will be
434 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
442 echo -ne "$_ok${RC_FEDORA+\r}${RC_FEDORA-\n}"
450 echo -ne "$_fail${RC_FEDORA+\r}${RC_FEDORA-\n}"
459 # Check if $pid (could be plural) are running
462 [ -d "/proc/$1" ] && return 0
468 # - outside chroot get only those processes, which are outside chroot.
469 # - inside chroot get only those processes, which are inside chroot.
470 # - don't filter out pids which do not have corresponding running processes (process died etc)
471 # (note: some processes like named are chrooted but run outside chroot)
472 # - do nothing inside vserver
474 if is_yes "$VSERVER"; then
478 if [ $# -lt 1 -o ! -d /proc/1 ]; then
482 local root_dir good_pids="" good_add_pid
483 for root_pid in $@; do
484 root_dir=$(resolvesymlink /proc/${root_pid}/root)
485 if [ -n "$root_dir" ]; then
487 if [ -n "${SYSTEM_CHROOTS}" ]; then
488 for r_dir in ${SYSTEM_CHROOTS}; do
489 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
492 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
493 elif [ ! -d "/proc/$root_pid" ]; then
494 good_pids="$good_pids $root_pid"
501 # run_cmd Message command_to_run
502 # run_cmd -a Message command_to_run
503 # run_cmd --user "username" "Message" command_to_run
505 local force_err=0 exit_code=0 errors user
506 while [ $# -gt 0 ]; do
521 local message=$1; shift
522 show "$message"; busy
526 export HOME=/tmp TMPDIR=/tmp
527 if is_no "$RC_LOGGING"; then
528 ${user:+setuidgid -s $user} "$@" 2>&1
530 ${user:+setuidgid -s $user} initlog -c "$*" 2>&1
534 log_success "$1 $message"
537 log_failed "$1 $message"
540 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
544 _daemon_set_ulimits() {
545 local opt val ksh=${KSH_VERSION:+1}
546 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
547 while [ $# -gt 0 ]; do
568 # A function to start a program (now it's useful on read-only filesystem too)
570 local errors="" prog="" end="" waitname="" waittime=""
572 local nice=$SERVICE_RUN_NICE_LEVEL
573 local fork user closefds redirfds pidfile makepid chdir=/
575 # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
576 # then the answer is: it totally ignores $prog and uses "$@" itself.
578 while [ $# -gt 0 ]; do
581 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
585 # for compatibility with redhat/mandrake
586 nls "warning: --check option is ignored!"
616 pidfile="${1#--pidfile=}"
617 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
622 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
638 if [ -n "$user" -a "$user" != "root" ]; then
639 prog="/bin/su $user -s /bin/sh -c \""
641 if [ "$fork" = "1" ]; then
642 prog="/usr/bin/setsid ${prog:-sh -c \"}"
644 # If command to execute ends with quotation mark, add remaining
645 # arguments and close quotation.
646 if [ "$prog" != "${prog%\"}" ]; then
647 prog="$prog $*$end\""
654 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
655 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
660 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
662 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
663 export USER=root HOME=/tmp TMPDIR=/tmp
665 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
668 # make nice level absolute, not to be dependant of nice level of shell where service started
669 nice=$(($nice - $(nice)))
671 if [ "$closefds" = 1 ]; then
675 elif [ "$redirfds" = 1 ]; then
683 if is_no "$RC_LOGGING"; then
685 if [ ! -x $prog ]; then
686 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
690 if [ -x $a/$prog ]; then
697 /sbin/start-stop-daemon -q --start \
699 ${pidfile:+--pidfile $pidfile} \
700 ${makepid:+--make-pidfile} \
701 ${user:+--chuid $user} \
702 ${chdir:+--chdir "$chdir"} \
703 ${fork:+--background} \
704 ${waitname:+--name $waitname} \
705 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
709 nice -n $nice initlog -c "$prog" 2>&1 </dev/null
713 if [ -n "$waitname" -a -n "$waittime" ]; then
717 pid=$(pidofproc "$waitname" "$pidfile")
718 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
720 while [ "$i" -lt "$waittime" ]; do
722 checkpid $pid && sleep 1 || break
725 log_success "$1 startup"
730 log_failed "$1 startup"
731 [ -n "$errors" ] && echo >&2 "$errors"
736 # A function to stop a program.
738 local notset killlevel base pid pidfile result delay=3 try
741 msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
745 while [ "$1" != "${1##-}" ]; do
753 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
770 # check for second arg to be kill level
777 # experimental start-stop-daemon based killing.
778 # works only with pidfile
779 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
780 local sig=${killlevel:--TERM} retry
781 # retry only if signal is not specified,
782 # as otherwise impossible to send HUP if process pid stays in pidfile.
783 if [ "${killlevel+set}" = "set" ]; then
784 # if we send HUP it's ok if process does not die
787 retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
789 /sbin/start-stop-daemon -q --stop \
792 ${pidfile:+--pidfile $pidfile}
794 if [ "$result" -eq 0 ]; then
807 pid=$(pidofproc "$1" "$pidfile")
808 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
811 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
812 if [ "$notset" = "1" ]; then
813 if checkpid $pid 2>&1; then
814 # TERM first, then KILL if not dead
819 while [ $try -lt $delay ]; do
820 checkpid $pid || break
824 if checkpid $pid; then
825 # XXX: SIGKILL is sent already on 4th second!
826 # HARMFUL for example to mysqld (which is already workarounded)
833 if [ "$result" -eq 0 ]; then
835 log_failed "$1 shutdown"
838 log_success "$1 shutdown"
840 result=$(( ! $result ))
842 # use specified level only
843 if checkpid $pid > /dev/null 2>&1; then
846 if [ "$result" -eq 0 ]; then
848 log_success "$1 got $killlevel"
852 log_failed "$1 didn't get $killlevel"
857 log_failed "$1 shutdown"
862 log_failed "$1 shutdown"
866 if [ -n "$waitname" -a -n "$waittime" ]; then
870 pid=$(pidofproc "$waitname" "$pidfile")
871 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
873 while [ "$i" -lt "$waittime" ]; do
875 checkpid $pid && sleep 1 || break
879 # Remove pid file if any.
880 if [ "$notset" = "1" ]; then
881 rm -f /var/run/${base}.pid
887 # A function to find the pid of a program.
889 local pid pidfile base=${1##*/}
891 [ -n "$2" ] && pidfile="$2"
895 msg_usage " pidofproc {program}"
899 # First try pidfile or "/var/run/*.pid"
901 /*)pidfile="${pidfile}";;
902 *) pidfile="/var/run/$pidfile";;
904 if [ -f "${pidfile}" ]; then
906 for p in $(< "${pidfile}"); do
907 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
912 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
913 pid=$(filter_chroot "$pid")
917 # status [--pidfile PIDFILE] {subsys} [{daemon}]"
919 local pid subsys daemon cpuset_msg pidfile
920 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
922 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
931 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
935 # if pidfile specified, pid must be there
936 if [ "$pidfile" ]; then
937 [ -f "$pidfile" ] && read pid < $pidfile
938 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
939 if [ ! -d "/proc/$pid" ]; then
943 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
945 pid=$(filter_chroot "$pid")
949 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
950 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
951 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
953 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
956 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
960 # Next try "/var/run/*.pid" files; if pidfile is not set
961 local base=${daemon##*/}
962 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
963 read pid < /var/run/${base}.pid
964 pid=$(filter_chroot "$pid")
966 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
971 # See if /var/lock/subsys/$subsys exists
972 if [ -f /var/lock/subsys/$subsys ]; then
973 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
976 nls "%s is stopped" "$subsys"
980 # Confirm whether we really want to run this service
983 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
1002 # module is needed (ie. is requested, is available and isn't loaded already)
1004 # module name without .o at end
1005 if ! lsmod | grep -q "$1"; then
1006 if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
1016 local parsed single die args foo result
1018 while is_no "$parsed"; do
1037 if is_yes "${single}"; then
1039 show "Loading %s kernel module(s)" "$foo"
1042 if [ -x /sbin/modprobe ]; then
1043 /sbin/modprobe -s $args "$@"
1049 if is_yes "${single}"; then
1051 if [ $result = "0" ]; then
1052 is_yes "$single" && ok
1055 if is_yes "$die"; then
1056 nls "Could not load %s kernel module(s)" "$@"
1063 if is_no "$RC_LOGGING"; then
1073 initlog -n $0 -s "$1 $2" -e 1
1077 initlog -n $0 -s "$1 $2" -e 2
1081 # Check if any flavor of portmapper is running
1082 check_portmapper() {
1083 if [ -x /usr/sbin/rpcinfo ]; then
1084 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
1089 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
1095 # is_fsmounted fstype mntpoint
1096 # Check if filesystem fstype is mounted on mntpoint
1101 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1103 if [ -r /proc/mounts ]; then
1104 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
1107 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1115 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
1116 # awk_program should process fstab_file and return a list of fstab-encoded
1117 # paths; it doesn't have to handle comments in fstab_file.
1119 local remaining sig=
1122 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1123 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1124 if [ "$retry" -eq 3 ]; then
1125 run_cmd "$3" fstab-decode umount $5 $remaining
1127 run_cmd "$4" fstab-decode umount $5 $remaining
1130 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1131 while [ "$count" -gt 0 ]; do
1132 [ -z "$remaining" ] && break
1135 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1137 [ -z "$remaining" ] && break
1138 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1140 retry=$(($retry -1))
1145 # Similar to __umount loop above, specialized for loopback devices
1146 __umount_loopback_loop() {
1147 local remaining devremaining sig=
1150 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1151 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1152 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1153 if [ "$retry" -eq 3 ]; then
1154 run_cmd "Unmounting loopback filesystems: " \
1155 fstab-decode umount $remaining
1157 run_cmd "Unmounting loopback filesystems (retry):" \
1158 fstab-decode umount $remaining
1160 for dev in $devremaining ; do
1161 losetup $dev > /dev/null 2>&1 && \
1162 run_cmd "Detaching loopback device $dev: " \
1165 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1166 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1167 [ -z "$remaining" ] && break
1168 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1170 retry=$(($retry -1))
1176 # If we have cachefile, use it.
1177 # If we don't, create memory variables and try to save silently,
1178 local cachefile='/var/cache/rc-scripts/msg.cache'
1181 if is_yes "$ISATTY"; then
1187 # We create $check variable which is used to invalidate the cache.
1188 # The $check contains user locale and terminal.
1189 local check="$term.$LC_MESSAGES.$INIT_COL"
1191 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1192 if . "$cachefile" 2>/dev/null; then
1193 if [ "$check" = "$_check" ]; then
1200 _busy=$(progress "BUSY" "$CBUSY")
1201 _ok=$(progress "DONE")
1202 _started=$(progress "WORK")
1203 _fail=$(progress "FAIL" "$CFAIL")
1204 _died=$(progress "DIED" "$CFAIL")
1206 # we don't use heredoc, as ksh attempts to create tempfile then
1207 (> "$cachefile" ) 2>/dev/null || return
1208 echo "_busy='$_busy';" >> "$cachefile"
1209 echo "_ok='$_ok';" >> "$cachefile"
1210 echo "_started='$_started';" >> "$cachefile"
1211 echo "_fail='$_fail';" >> "$cachefile"
1212 echo "_died='$_died';" >> "$cachefile"
1213 echo "_check='$check';" >> "$cachefile"
1217 if [ -z "$GETTEXT" ]; then
1218 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1225 if [ -z "$TPUT" ]; then
1226 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1228 # check if we are on proper terminal
1229 tput longname >/dev/null 2>&1 || TPUT=no
1237 # True when upstart-event-based boot should be used
1238 is_yes "$USE_UPSTART" && return 0
1239 is_no "$USE_UPSTART" && return 1
1240 if [ ! -x /sbin/initctl ] ; then
1244 local cmdline=$(cat /proc/cmdline 2>/dev/null)
1245 if strstr "$cmdline" "pld.no-upstart" ; then
1255 # emit upstart signal
1256 # only when 'upstart' boot is enabled
1257 use_upstart || return 0
1258 /sbin/initctl emit "$@"
1262 # Return 0 if the given service is an upstart task.
1263 grep -q '^task' "/etc/init/$1.conf"
1265 is_upstart_running() {
1266 # Return 0 if the given service is running via upstart
1267 initctl status "$1" 2>/dev/null | grep -q running
1271 if is_upstart_running "${service}"; then
1272 msg_already_running "$service"
1276 msg_starting "${service}"
1277 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1288 if ! is_upstart_running "${service}"; then
1289 msg_not_running "$name"
1292 msg_stopping "${service}"
1293 if errors=$(/sbin/initctl stop ${service}) ; then
1304 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1307 msg_reloading "${service}"
1308 if errors=$(/sbin/initctl reload ${service}) ; then
1318 # get service status
1319 # should be compliant with
1320 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1323 if is_upstart_task "${service}" ; then
1324 # we probably should have a way to handle task status
1327 if ! status=$(/sbin/initctl status "${service}") ; then
1328 # program or service status is not known
1331 if strstr "$status" "running" ; then
1332 # program is running or service is OK
1336 # program is not running
1340 # TODO: other statuses
1343 _upstart_controlled() {
1344 # If the service is to be handled by upstart
1345 # execute the start/stop/etc. commands the upstart way
1346 if ! use_upstart; then
1352 [ $# -gt 0 ] && shift
1353 local name=$(basename "$script")
1354 if [ ! -f /etc/init/${name}.conf ] ; then
1358 local extra_commands
1359 local has_configtest
1360 if [ "$1" = "--except" ] ; then
1363 for cmd in $commands ; do
1364 if [ "$command" = "$cmd" ] ; then
1368 start|stop|status|reload|restart|try-restart|force-reload)
1372 extra_commands="|$cmd"
1375 extra_commands="|$cmd"
1379 elif [ -n "$*" ] ; then
1383 # is there a better way
1384 for cmd in $commands ; do
1385 if [ "$command" = "$cmd" ] ; then
1390 if [ $found = 0 ] ; then
1391 # let the script handle it
1405 upstart_status $name
1409 if is_yes "$has_configtest" ; then
1410 "$script" configtest || exit 1
1417 if ! is_upstart_running "$name" ; then
1420 if is_yes "$has_configtest" ; then
1421 "$script" configtest || exit 1
1428 if is_yes "$has_configtest" ; then
1429 "$script" configtest || exit 1
1431 if is_upstart_task "$name" ; then
1432 nls "$command not implemented for $name"
1435 upstart_reload "$name"
1440 if is_yes "$has_configtest" ; then
1441 "$script" configtest || exit 1
1443 if is_upstart_task "$name" ; then
1444 upstart_stop "$name"
1445 upstart_start "$name"
1448 upstart_reload "$name"
1453 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
1457 return 1 # should not happen
1461 # somewhere at the begining of init script:
1462 # upstart_controlled
1463 # - to pass implement all upstart commands via initctl
1464 # start, stop, status, restart, reload and force_reload
1466 # upstart_controlled command...
1467 # - to pass handle only specific commands the upstart way
1468 # and leave the rest to the script
1470 alias upstart_controlled='_upstart_controlled $0 "$@"'
1476 # * Local variables:
1478 # * indent-tabs-mode: notnil