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 [ "$env_upstart" ] && USE_UPSTART=$env_upstart
67 if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
69 while read _f _ctx; do
70 [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
71 done </proc/self/status
73 if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
81 # VSERVER_ISOLATION_NET = isolation only inside of vserver guests
82 if [ -z "$VSERVER_ISOLATION_NET" -o "$VSERVER_ISOLATION_NET" = "detect" ]; then
83 VSERVER_ISOLATION_NET=no
84 if [ "$VSERVER" = "yes" ]; then
85 if [ -f /proc/self/nsproxy ]; then
87 while read _t _data; do
88 [ "$_t" = "net:" ] && break
89 done < /proc/self/nsproxy
92 # assume old kernel mode
93 VSERVER_ISOLATION_NET=yes
95 if [ "${_data##*\(}" = "I)" ]; then
96 VSERVER_ISOLATION_NET=yes
102 # we need to know in functions if we were called from a terminal
103 if [ -z "$ISATTY" ]; then
104 [ -t ] && ISATTY=yes || ISATTY=no
111 msg_usage " is_yes {value}"
117 yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
131 msg_usage " is_no {value}"
136 no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
147 # checks if file is empty
148 # empty lines and lines beginning with hash are ignored
150 [ -s "$1" ] || return 0
151 grep -vqE "^(#|[[:blank:]]*$)" "$1" && return 1 || return 0
154 # returns OK if $1 contains $2
157 [ "${1#*$a*}" = "$1" ] && return 1
161 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
165 if is_no "$RC_LOGGING"; then
168 while [ "$1" != "${1##-}" ]; do
186 local _x _y _z v v1 old_IFS ver
190 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
199 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
201 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
203 while [ ${#ver} -lt 9 ]; do ver="0$ver"; done
209 local _x _y _z v v1 old_IFS ver
213 # strip _* or -* from versions like: "2.6.25_vanilla-1", "2.6.25-1"
221 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
223 while [ ${#ver} -lt 6 ]; do ver="0$ver"; done
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"
241 while [ ${#ver} -lt 3 ]; do ver="0$ver"; done
248 is_yes "$ISATTY" || return
250 if is_yes "$FASTRC" || is_no "$TPUT"; then
253 echo -ne "\033[$(($2+1))G"
256 echo -ne "\033[${2}A"
268 is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
271 termput setaf $NORMAL
280 if [ "$3" = "1" ]; then tput bold; else tput sgr0; fi
281 is_yes "$COLOR_INIT" && tput setaf "$2"
284 termput setaf $NORMAL
290 if [ ! -x /bin/printf ]; then
292 # FIXME: buggy when single or double quotes in message!
297 if [ $# -gt 0 ]; then
300 while [ $# -gt 0 ]; do
305 awk "BEGIN {printf \"$text\", \"$m\"; }"
309 # National language support function
311 local msg_echo nls_domain text message
313 nls_domain="$NLS_DOMAIN"
314 while [ "$1" != "${1##-}" ]; do
330 # empty message, so we return --misiek
331 if [ -z "$message" ]; then
336 if is_yes "$GETTEXT"; then
337 message=$(TEXTDOMAINDIR="/etc/sysconfig/locale" gettext -e --domain="${nls_domain:-rc-scripts}" "$message")
340 printf "$message" "$@"
347 if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
348 [ -x /bin/splash ] && /bin/splash "$action"
355 nls "ERROR: Networking is down. %s can't be run." "$1" >&2
359 show "Starting %s service" "$1"
362 msg_already_running() {
363 nls "%s service is already running." "$1"
367 show "Stopping %s service" "$1"
371 nls "%s service is not running." "$1"
375 show "Reloading %s service" "$1"
382 # Some functions to handle PLD Linux-style messages
386 if is_no "$FASTRC" && is_yes "$GETTEXT"; then
392 while [ $((len++)) -lt $INIT_COL ]; do
395 if [ -n "$CHARS" ]; then
396 termput setaf $CCHARS
404 termput hpa $INIT_COL
407 # Displays message in square brackests ("[ DONE ]"). Takes two arguments.
408 # First is the text to display, second is color number to use (argument to
409 # tput setaf). If second argument is not given, default (2, green) will be
419 echo -n "$(termput setaf $CBRACKETS)[$(termput setaf $COLOR) $(nls --nls-domain rc-scripts "$1") $(termput setaf $CBRACKETS)]$(termput op)"
444 # Check if $pid (could be plural) are running
447 [ -d "/proc/$1" ] && return 0
453 # - outside chroot get only those processes, which are outside chroot.
454 # - inside chroot get only those processes, which are inside chroot.
455 # - don't filter out pids which do not have corresponding running processes (process died etc)
456 # (note: some processes like named are chrooted but run outside chroot)
457 # - do nothing inside vserver
459 if is_yes "$VSERVER"; then
463 if [ $# -lt 1 -o ! -d /proc/1 ]; then
467 local root_dir good_pids="" good_add_pid
468 for root_pid in $@; do
469 root_dir=$(resolvesymlink /proc/${root_pid}/root)
470 if [ -n "$root_dir" ]; then
472 if [ -n "${SYSTEM_CHROOTS}" ]; then
473 for r_dir in ${SYSTEM_CHROOTS}; do
474 echo "$root_dir" | grep -q "^${r_dir}" && good_add_pid=0
477 [ "$good_add_pid" -eq 1 ] && good_pids="$good_pids $root_pid"
478 elif [ ! -d "/proc/$root_pid" ]; then
479 good_pids="$good_pids $root_pid"
486 # run_cmd Message command_to_run
487 # run_cmd -a Message command_to_run
488 # run_cmd --user "username" "Message" command_to_run
490 local force_err=0 exit_code=0 errors user
491 while [ $# -gt 0 ]; do
506 local message=$1; shift
507 show "$message"; busy
511 export HOME=/tmp TMPDIR=/tmp
512 if is_no "$RC_LOGGING"; then
513 ${user:+setuidgid -s $user} "$@" 2>&1
515 ${user:+setuidgid -s $user} initlog -c "$*" 2>&1
519 log_success "$1 $message"
522 log_failed "$1 $message"
525 [ -n "$errors" ] && [ $exit_code -eq 1 -o $force_err -eq 1 ] && echo "$errors"
529 _daemon_set_ulimits() {
530 local opt val ksh=${KSH_VERSION:+1}
531 set -- ${SERVICE_LIMITS:-$DEFAULT_SERVICE_LIMITS}
532 while [ $# -gt 0 ]; do
553 # A function to start a program (now it's useful on read-only filesystem too)
555 local errors="" prog="" end="" waitname="" waittime=""
557 local nice=$SERVICE_RUN_NICE_LEVEL
558 local fork user closefds redirfds pidfile makepid chdir=/
560 # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
561 # then the answer is: it totally ignores $prog and uses "$@" itself.
563 while [ $# -gt 0 ]; do
566 msg_usage " daemon [--check] [--user user] [--fork] [--chdir directory] [--closefds] [--redirfds] [--waitforname procname] [--waitfortime seconds] [--pidfile file] [--makepid] [+/-nicelevel] {program} <program args>"
570 # for compatibility with redhat/mandrake
571 nls "warning: --check option is ignored!"
601 pidfile="${1#--pidfile=}"
602 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
607 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
623 if [ -n "$user" -a "$user" != "root" ]; then
624 prog="/bin/su $user -s /bin/sh -c \""
626 if [ "$fork" = "1" ]; then
627 prog="/usr/bin/setsid ${prog:-sh -c \"}"
629 # If command to execute ends with quotation mark, add remaining
630 # arguments and close quotation.
631 if [ "$prog" != "${prog%\"}" ]; then
632 prog="$prog $*$end\""
639 [ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
640 [ -z "$DEFAULT_SERVICE_RUN_NICE_LEVEL" ] && DEFAULT_SERVICE_RUN_NICE_LEVEL=0
645 [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
647 umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
648 export USER=root HOME=/tmp TMPDIR=/tmp
650 nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
653 # make nice level absolute, not to be dependant of nice level of shell where service started
654 nice=$(($nice - $(nice)))
656 if [ "$closefds" = 1 ]; then
660 elif [ "$redirfds" = 1 ]; then
668 if is_no "$RC_LOGGING"; then
670 if [ ! -x $prog ]; then
671 logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
675 if [ -x $a/$prog ]; then
682 /sbin/start-stop-daemon -q --start \
684 ${pidfile:+--pidfile $pidfile} \
685 ${makepid:+--make-pidfile} \
686 ${user:+--chuid $user} \
687 ${chdir:+--chdir "$chdir"} \
688 ${fork:+--background} \
689 ${waitname:+--name $waitname} \
690 ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
694 nice -n $nice initlog -c "$prog" 2>&1 </dev/null
698 if [ -n "$waitname" -a -n "$waittime" ]; then
702 pid=$(pidofproc "$waitname" "$pidfile")
703 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
705 while [ "$i" -lt "$waittime" ]; do
707 checkpid $pid && sleep 1 || break
710 log_success "$1 startup"
715 log_failed "$1 startup"
716 [ -n "$errors" ] && echo >&2 "$errors"
721 # A function to stop a program.
723 local notset killlevel base pid pidfile result delay=3 try
726 msg_usage " killproc [--pidfile|-p PIDFILE] [-d DELAY] {program} [-SIGNAME]"
730 while [ "$1" != "${1##-}" ]; do
738 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
755 # check for second arg to be kill level
762 # experimental start-stop-daemon based killing.
763 # works only with pidfile
764 if is_no "$RC_LOGGING" && [ "$pidfile" ]; then
765 local sig=${killlevel:--TERM} retry
766 # retry only if signal is not specified,
767 # as otherwise impossible to send HUP if process pid stays in pidfile.
768 if [ "${killlevel+set}" = "set" ]; then
769 # if we send HUP it's ok if process does not die
772 retry="--retry ${sig#-}/10/${sig#-}/60/KILL/10"
774 /sbin/start-stop-daemon -q --stop \
777 ${pidfile:+--pidfile $pidfile}
779 if [ "$result" -eq 0 ]; then
792 pid=$(pidofproc "$1" "$pidfile")
793 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
796 if [ -n "$pid" -a "$pid" != "$$" ] && checkpid $pid 2>&1; then
797 if [ "$notset" = "1" ]; then
798 if checkpid $pid 2>&1; then
799 # TERM first, then KILL if not dead
804 while [ $try -lt $delay ]; do
805 checkpid $pid || break
809 if checkpid $pid; then
810 # XXX: SIGKILL is sent already on 4th second!
811 # HARMFUL for example to mysqld (which is already workarounded)
818 if [ "$result" -eq 0 ]; then
820 log_failed "$1 shutdown"
823 log_success "$1 shutdown"
825 result=$(( ! $result ))
827 # use specified level only
828 if checkpid $pid > /dev/null 2>&1; then
831 if [ "$result" -eq 0 ]; then
833 log_success "$1 got $killlevel"
837 log_failed "$1 didn't get $killlevel"
842 log_failed "$1 shutdown"
847 log_failed "$1 shutdown"
851 if [ -n "$waitname" -a -n "$waittime" ]; then
855 pid=$(pidofproc "$waitname" "$pidfile")
856 [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
858 while [ "$i" -lt "$waittime" ]; do
860 checkpid $pid && sleep 1 || break
864 # Remove pid file if any.
865 if [ "$notset" = "1" ]; then
866 rm -f /var/run/${base}.pid
872 # A function to find the pid of a program.
874 local pid pidfile base=${1##*/}
876 [ -n "$2" ] && pidfile="$2"
880 msg_usage " pidofproc {program}"
884 # First try pidfile or "/var/run/*.pid"
886 /*)pidfile="${pidfile}";;
887 *) pidfile="/var/run/$pidfile";;
889 if [ -f "${pidfile}" ]; then
891 for p in $(< "${pidfile}"); do
892 [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
897 [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
898 pid=$(filter_chroot "$pid")
902 # status [--pidfile PIDFILE] {subsys} [{daemon}]"
904 local pid subsys daemon cpuset_msg pidfile
905 if [ "$1" = "--pidfile" -o "$1" = "-p" ]; then
907 case "$pidfile" in /*);; *) pidfile="/var/run/$pidfile";; esac
916 msg_usage " status [--pidfile PIDFILE] {subsys} [{daemon}]"
920 # if pidfile specified, pid must be there
921 if [ "$pidfile" ]; then
922 [ -f "$pidfile" ] && read pid < $pidfile
923 # filter_chroot does not filter out dead pids, so this extra check, see t/status-pidfile.sh
924 if [ ! -d "/proc/$pid" ]; then
928 pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
930 pid=$(filter_chroot "$pid")
934 if [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS"; then
935 if grep -q "$pid" "/dev/cpuset/${SERVICE_CPUSET}/tasks"; then
936 cpuset_msg=$(nls " in cpuset %s..." "$SERVICE_CPUSET")
938 cpuset_msg=$(nls " outside of configured cpuset %s..." "$SERVICE_CPUSET")
941 nls "%s (pid %s) is running%s" "$daemon" "$pid" "$cpuset_msg"
945 # Next try "/var/run/*.pid" files; if pidfile is not set
946 local base=${daemon##*/}
947 if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
948 read pid < /var/run/${base}.pid
949 pid=$(filter_chroot "$pid")
951 nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
956 # See if /var/lock/subsys/$subsys exists
957 if [ -f /var/lock/subsys/$subsys ]; then
958 nls "daemon %s dead but subsys (%s) locked" "$daemon" "$subsys"
961 nls "%s is stopped" "$subsys"
965 # Confirm whether we really want to run this service
968 nls -n "Start service %s (Y)es/(N)o/(C)ontinue? [Y] " "$1"
987 # module is needed (ie. is requested, is available and isn't loaded already)
989 # module name without .o at end
990 if ! lsmod | grep -q "$1"; then
991 if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
1001 local parsed single die args foo result
1003 while is_no "$parsed"; do
1022 if is_yes "${single}"; then
1024 show "Loading %s kernel module(s)" "$foo"
1027 if [ -x /sbin/modprobe ]; then
1028 /sbin/modprobe -s $args "$@"
1034 if is_yes "${single}"; then
1036 if [ $result = "0" ]; then
1037 is_yes "$single" && ok
1040 if is_yes "$die"; then
1041 nls "Could not load %s kernel module(s)" "$@"
1048 if is_no "$RC_LOGGING"; then
1058 initlog -n $0 -s "$1 $2" -e 1
1062 initlog -n $0 -s "$1 $2" -e 2
1066 # Check if any flavor of portmapper is running
1067 check_portmapper() {
1068 if [ -x /usr/sbin/rpcinfo ]; then
1069 if /usr/sbin/rpcinfo -p localhost >/dev/null 2>/dev/null; then
1074 elif [ -z "$(pidof portmap)" -a -z "$(pidof rpcbind)" ]; then
1080 # is_fsmounted fstype mntpoint
1081 # Check if filesystem fstype is mounted on mntpoint
1086 [ -n "$fstype" -a -n "$mntpoint" ] || return 1
1088 if [ -r /proc/mounts ]; then
1089 grep -qE "[[:blank:]]$mntpoint[[:blank:]]+$fstype[[:blank:]]" /proc/mounts
1092 if [ "$(stat -L -f -c %T $mntpoint 2>/dev/null)" = "$fstype" ]; then
1100 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
1101 # awk_program should process fstab_file and return a list of fstab-encoded
1102 # paths; it doesn't have to handle comments in fstab_file.
1104 local remaining sig=
1107 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1108 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1109 if [ "$retry" -eq 3 ]; then
1110 run_cmd "$3" fstab-decode umount $5 $remaining
1112 run_cmd "$4" fstab-decode umount $5 $remaining
1115 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1116 while [ "$count" -gt 0 ]; do
1117 [ -z "$remaining" ] && break
1120 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
1122 [ -z "$remaining" ] && break
1123 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1125 retry=$(($retry -1))
1130 # Similar to __umount loop above, specialized for loopback devices
1131 __umount_loopback_loop() {
1132 local remaining devremaining sig=
1135 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1136 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1137 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
1138 if [ "$retry" -eq 3 ]; then
1139 run_cmd "Unmounting loopback filesystems: " \
1140 fstab-decode umount $remaining
1142 run_cmd "Unmounting loopback filesystems (retry):" \
1143 fstab-decode umount $remaining
1145 for dev in $devremaining ; do
1146 losetup $dev > /dev/null 2>&1 && \
1147 run_cmd "Detaching loopback device $dev: " \
1150 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
1151 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
1152 [ -z "$remaining" ] && break
1153 fstab-decode /bin/fuser -k -m $sig $remaining >/dev/null
1155 retry=$(($retry -1))
1161 # If we have cachefile, use it.
1162 # If we don't, create memory variables and try to save silently,
1163 local cachefile='/var/cache/rc-scripts/msg.cache'
1166 if is_yes "$ISATTY"; then
1172 # We create $check variable which is used to invalidate the cache.
1173 # The $check contains user locale and terminal.
1174 local check="$term.$LC_MESSAGES.$INIT_COL"
1176 if [ -f "$cachefile" -a "$cachefile" -nt /etc/sysconfig/system -a "$cachefile" -nt /etc/sysconfig/init-colors ]; then
1177 if . "$cachefile" 2>/dev/null; then
1178 if [ "$check" = "$_check" ]; then
1185 _busy=$(progress "BUSY" "$CBUSY")
1186 _ok=$(progress "DONE")
1187 _started=$(progress "WORK")
1188 _fail=$(progress "FAIL" "$CFAIL")
1189 _died=$(progress "DIED" "$CFAIL")
1191 # we don't use heredoc, as ksh attempts to create tempfile then
1192 (> "$cachefile" ) 2>/dev/null || return
1193 echo "_busy='$_busy';" >> "$cachefile"
1194 echo "_ok='$_ok';" >> "$cachefile"
1195 echo "_started='$_started';" >> "$cachefile"
1196 echo "_fail='$_fail';" >> "$cachefile"
1197 echo "_died='$_died';" >> "$cachefile"
1198 echo "_check='$check';" >> "$cachefile"
1202 if [ -z "$GETTEXT" ]; then
1203 if [ -x /bin/gettext -o -x /usr/bin/gettext ]; then
1210 if [ -z "$TPUT" ]; then
1211 if [ -d /usr/share/terminfo ] && [ -x /usr/bin/tput -o -x /bin/tput ]; then
1213 # check if we are on proper terminal
1214 tput longname >/dev/null 2>&1 || TPUT=no
1222 # True when upstart-event-based boot should be used
1223 is_yes "$USE_UPSTART" && return 0
1224 is_no "$USE_UPSTART" && return 1
1225 if [ ! -x /sbin/initctl ] ; then
1229 local cmdline=$(cat /proc/cmdline 2>/dev/null)
1230 if strstr "$cmdline" "pld.no-upstart" ; then
1240 # emit upstart signal
1241 # only when 'upstart' boot is enabled
1242 use_upstart || return 0
1243 /sbin/initctl emit "$@"
1247 # Return 0 if the given service is an upstart task.
1248 grep -q '^task' "/etc/init/$1.conf"
1250 is_upstart_running() {
1251 # Return 0 if the given service is running via upstart
1252 initctl status "$1" 2>/dev/null | grep -q running
1256 if is_upstart_running "${service}"; then
1257 msg_already_running "$service"
1261 msg_starting "${service}"
1262 if errors=$(/sbin/initctl start ${service} 2>&1) ; then
1273 if ! is_upstart_running "${service}"; then
1274 msg_not_running "$name"
1277 msg_stopping "${service}"
1278 if errors=$(/sbin/initctl stop ${service}) ; then
1289 if ! is_upstart_running "${service}" && ! is_upstart_task "${service}" ; then
1292 msg_reloading "${service}"
1293 if errors=$(/sbin/initctl reload ${service}) ; then
1303 # get service status
1304 # should be compliant with
1305 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1308 if is_upstart_task "${service}" ; then
1309 # we probably should have a way to handle task status
1312 if ! status=$(/sbin/initctl status "${service}") ; then
1313 # program or service status is not known
1316 if strstr "$status" "running" ; then
1317 # program is running or service is OK
1321 # program is not running
1325 # TODO: other statuses
1328 _upstart_controlled() {
1329 # If the service is to be handled by upstart
1330 # execute the start/stop/etc. commands the upstart way
1331 if ! use_upstart; then
1337 [ $# -gt 0 ] && shift
1338 local name=$(basename "$script")
1339 if [ ! -f /etc/init/${name}.conf ] ; then
1343 local extra_commands
1344 local has_configtest
1345 if [ "$1" = "--except" ] ; then
1348 for cmd in $commands ; do
1349 if [ "$command" = "$cmd" ] ; then
1353 start|stop|status|reload|restart|try-restart|force-reload)
1357 extra_commands="|$cmd"
1360 extra_commands="|$cmd"
1364 elif [ -n "$*" ] ; then
1368 # is there a better way
1369 for cmd in $commands ; do
1370 if [ "$command" = "$cmd" ] ; then
1375 if [ $found = 0 ] ; then
1376 # let the script handle it
1390 upstart_status $name
1394 if is_yes "$has_configtest" ; then
1395 "$script" configtest || exit 1
1402 if ! is_upstart_running "$name" ; then
1405 if is_yes "$has_configtest" ; then
1406 "$script" configtest || exit 1
1413 if is_yes "$has_configtest" ; then
1414 "$script" configtest || exit 1
1416 if is_upstart_task "$name" ; then
1417 nls "$command not implemented for $name"
1420 upstart_reload "$name"
1425 if is_yes "$has_configtest" ; then
1426 "$script" configtest || exit 1
1428 if is_upstart_task "$name" ; then
1429 upstart_stop "$name"
1430 upstart_start "$name"
1433 upstart_reload "$name"
1438 msg_usage "$0 {start|stop|restart|reload|force-reload|status$extra_commands}"
1442 return 1 # should not happen
1446 # somewhere at the begining of init script:
1447 # upstart_controlled
1448 # - to pass implement all upstart commands via initctl
1449 # start, stop, status, restart, reload and force_reload
1451 # upstart_controlled command...
1452 # - to pass handle only specific commands the upstart way
1453 # and leave the rest to the script
1455 alias upstart_controlled='_upstart_controlled $0 "$@"'
1461 # * Local variables:
1463 # * indent-tabs-mode: notnil