1 # /lib/lsb/init-functions for Debian -*- shell-script -*-
3 #Copyright (c) 2002-08 Chris Lawrence
6 #Redistribution and use in source and binary forms, with or without
7 #modification, are permitted provided that the following conditions
9 #1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #3. Neither the name of the author nor the names of other contributors
15 # may be used to endorse or promote products derived from this software
16 # without specific prior written permission.
18 #THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 #IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 #ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22 #LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 #BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 #OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 #EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 local force nice pidfile exec i args
37 while getopts fn:p: opt ; do
41 p) pidfile="$OPTARG";;
45 shift $(($OPTIND - 1))
46 if [ "$1" = '--' ]; then
52 args="--start --nicelevel $nice --quiet --oknodo"
53 if [ $force = 1 ]; then
54 /sbin/start-stop-daemon $args --chdir "$PWD" --startas $exec --pidfile /dev/null -- "$@"
55 elif [ $pidfile ]; then
56 /sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec --oknodo --pidfile "$pidfile" -- "$@"
58 /sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec -- "$@"
63 local pidfile line i pids= status specified pid
68 while getopts p: opt ; do
70 p) pidfile="$OPTARG"; specified=1;;
73 shift $(($OPTIND - 1))
76 if [ ! "$specified" ]; then
77 pidfile="/var/run/$base.pid"
80 if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
82 if [ -n "${pid:-}" ]; then
83 if $(kill -0 "${pid:-}" 2> /dev/null); then
86 elif ps "${pid:-}" >/dev/null 2>&1; then
88 return 0 # program is running, but not owned by this user
90 return 1 # program is dead and /var/run pid file exists
94 if [ -x /bin/pidof -a ! "$specified" ]; then
96 /bin/pidof -o %PPID -x $1 || status="$?"
97 if [ "$status" = 1 ]; then
98 return 3 # program is not running
102 return 4 # Unable to determine status
105 # start-stop-daemon uses the same algorithm as "pidofproc" above.
107 local pidfile sig status base i name_param is_term_sig
113 while getopts p: opt ; do
115 p) pidfile="$OPTARG";;
118 shift $(($OPTIND - 1))
121 if [ ! $pidfile ]; then
122 name_param="--name $base --pidfile /var/run/$base.pid"
124 name_param="--pidfile $pidfile"
127 sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
128 sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
129 if [ -z "$sig" -o "$sig" = 15 -o "$sig" = TERM ]; then
133 if [ ! "$is_term_sig" = yes ]; then
134 if [ -n "$sig" ]; then
135 /sbin/start-stop-daemon --stop --signal "$sig" --quiet $name_param || status="$?"
137 /sbin/start-stop-daemon --stop --quiet $name_param || status="$?"
140 /sbin/start-stop-daemon --stop --quiet --oknodo $name_param || status="$?"
142 if [ "$status" = 1 ]; then
143 if [ -n "$sig" ]; then
146 return 3 # program is not running
149 if [ "$status" = 0 -a "$is_term_sig" = yes -a "$pidfile" ]; then
150 pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile"
157 local pidfile daemon name status
161 while getopts p: opt ; do
163 p) pidfile="$OPTARG";;
166 shift $(($OPTIND - 1))
168 if [ -n "$pidfile" ]; then
169 pidfile="-p $pidfile"
175 pidofproc $pidfile $daemon >/dev/null || status="$?"
176 if [ "$status" = 0 ]; then
177 log_success_msg "$name is running"
179 elif [ "$status" = 4 ]; then
180 log_failure_msg "could not access PID file for $name"
183 log_failure_msg "$name is not running"
188 log_use_fancy_output () {
191 if [ -t 1 ] && [ "x${TERM:-}" != "x" ] && [ "x${TERM:-}" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
192 [ -z $FANCYTTY ] && FANCYTTY=1 || true
203 if [ -n "${1:-}" ]; then
210 if [ -n "${1:-}" ]; then
211 log_begin_msg $@ "..."
213 log_end_msg 1 || true
217 if [ -n "${1:-}" ]; then
218 log_begin_msg $@ "..."
220 log_end_msg 255 || true
224 # NON-LSB HELPER FUNCTIONS
226 # int get_lsb_header_val (char *scriptpathname, char *key)
227 get_lsb_header_val () {
228 if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
231 LSB_S="### BEGIN INIT INFO"
232 LSB_E="### END INIT INFO"
233 sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
236 # int log_begin_message (char *message)
238 if [ -z "${1:-}" ]; then
245 # log_daemon_msg "Starting GNOME Login Manager" "gdm"
247 # On Debian, would output "Starting GNOME Login Manager: gdm"
248 # On Ubuntu, would output " * Starting GNOME Login Manager..."
250 # If the second argument is omitted, logging suitable for use with
251 # log_progress_msg() is used:
253 # log_daemon_msg "Starting remote filesystem services"
255 # On Debian, would output "Starting remote filesystem services:"
256 # On Ubuntu, would output " * Starting remote filesystem services..."
259 if [ -z "${1:-}" ]; then
262 log_daemon_msg_pre "$@"
264 if [ -z "${2:-}" ]; then
270 log_daemon_msg_post "$@"
277 # log_daemon_msg "Starting remote file system services"
278 # log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
279 # log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
280 # log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
283 # You could also do something fancy with log_end_msg here based on the
284 # return values of start-stop-daemon; this is left as an exercise for
287 # On Ubuntu, one would expect log_progress_msg to be a no-op.
288 log_progress_msg () {
289 if [ -z "${1:-}" ]; then
296 # int log_end_message (int exitstatus)
298 # If no arguments were passed, return
299 if [ -z "${1:-}" ]; then
307 # Only do the fancy stuff if we have an appropriate terminal
308 # and if /usr is already mounted
309 if log_use_fancy_output; then
311 YELLOW=`$TPUT setaf 3`
319 if [ $1 -eq 0 ]; then
321 elif [ $1 -eq 255 ]; then
322 /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
324 /bin/echo -e " ${RED}failed!${NORMAL}"
326 log_end_msg_post "$@"
334 log_action_begin_msg () {
338 log_action_cont_msg () {
342 log_action_end_msg () {
343 log_action_end_msg_pre "$@"
344 if [ -z "${2:-}" ]; then
350 if [ $1 -eq 0 ]; then
353 if log_use_fancy_output; then
356 /bin/echo -e "${RED}failed${end}${NORMAL}"
361 log_action_end_msg_post "$@"
364 # Hooks for /etc/lsb-base-logging.sh
365 log_daemon_msg_pre () { :; }
366 log_daemon_msg_post () { :; }
367 log_end_msg_pre () { :; }
368 log_end_msg_post () { :; }
369 log_action_end_msg_pre () { :; }
370 log_action_end_msg_post () { :; }
373 [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true