Add .changes file
[platform/upstream/lsb.git] / init-functions
1 # /lib/lsb/init-functions for Debian -*- shell-script -*-
2 #
3 #Copyright (c) 2002-08 Chris Lawrence
4 #All rights reserved.
5 #
6 #Redistribution and use in source and binary forms, with or without
7 #modification, are permitted provided that the following conditions
8 #are met:
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.
17 #
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.
29
30 start_daemon () {
31     local force nice pidfile exec i args
32     force=0
33     nice=0
34     pidfile=/dev/null
35
36     OPTIND=1
37     while getopts fn:p: opt ; do
38         case "$opt" in
39             f)  force=1;;
40             n)  nice="$OPTARG";;
41             p)  pidfile="$OPTARG";;
42         esac
43     done
44     
45     shift $(($OPTIND - 1))
46     if [ "$1" = '--' ]; then
47         shift
48     fi
49
50     exec="$1"; shift
51
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" -- "$@"
57     else
58         /sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec -- "$@"
59     fi
60 }
61
62 pidofproc () {
63     local pidfile line i pids= status specified pid
64     pidfile=
65     specified=
66     
67     OPTIND=1
68     while getopts p: opt ; do
69         case "$opt" in
70             p)  pidfile="$OPTARG"; specified=1;;
71         esac
72     done
73     shift $(($OPTIND - 1))
74
75     base=${1##*/}
76     if [ ! "$specified" ]; then
77         pidfile="/var/run/$base.pid"
78     fi
79
80     if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
81         read pid < "$pidfile"
82         if [ -n "${pid:-}" ]; then
83             if $(kill -0 "${pid:-}" 2> /dev/null); then
84                 echo "$pid"
85                 return 0
86             elif ps "${pid:-}" >/dev/null 2>&1; then
87                 echo "$pid"
88                 return 0 # program is running, but not owned by this user
89             else
90                 return 1 # program is dead and /var/run pid file exists
91             fi
92         fi
93     fi
94     if [ -x /bin/pidof -a ! "$specified" ]; then
95         status="0"
96         /bin/pidof -o %PPID -x $1 || status="$?"
97         if [ "$status" = 1 ]; then
98             return 3 # program is not running
99         fi
100         return 0
101     fi
102     return 4 # Unable to determine status
103 }
104
105 # start-stop-daemon uses the same algorithm as "pidofproc" above.
106 killproc () {
107     local pidfile sig status base i name_param is_term_sig
108     pidfile=
109     name_param=
110     is_term_sig=no
111
112     OPTIND=1
113     while getopts p: opt ; do
114         case "$opt" in
115             p)  pidfile="$OPTARG";;
116         esac
117     done
118     shift $(($OPTIND - 1))
119
120     base=${1##*/}
121     if [ ! $pidfile ]; then
122         name_param="--name $base --pidfile /var/run/$base.pid"
123     else
124         name_param="--pidfile $pidfile"
125     fi
126
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
130         is_term_sig=yes
131     fi
132     status=0
133     if [ ! "$is_term_sig" = yes ]; then
134         if [ -n "$sig" ]; then
135             /sbin/start-stop-daemon --stop --signal "$sig" --quiet $name_param || status="$?"
136         else
137             /sbin/start-stop-daemon --stop --quiet $name_param || status="$?"
138         fi
139     else
140         /sbin/start-stop-daemon --stop --quiet --oknodo $name_param || status="$?"
141     fi
142     if [ "$status" = 1 ]; then
143         if [ -n "$sig" ]; then
144             return 0
145         fi
146         return 3 # program is not running
147     fi
148
149     if [ "$status" = 0 -a "$is_term_sig" = yes -a "$pidfile" ]; then
150         pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile"
151     fi
152     return 0
153 }
154
155 # Return LSB status
156 status_of_proc () {
157     local pidfile daemon name status
158
159     pidfile=
160     OPTIND=1
161     while getopts p: opt ; do
162         case "$opt" in
163             p)  pidfile="$OPTARG";;
164         esac
165     done
166     shift $(($OPTIND - 1))
167
168     if [ -n "$pidfile" ]; then
169         pidfile="-p $pidfile"
170     fi
171     daemon="$1"
172     name="$2"
173
174     status="0"
175     pidofproc $pidfile $daemon >/dev/null || status="$?"
176     if [ "$status" = 0 ]; then
177         log_success_msg "$name is running"
178         return 0
179     elif [ "$status" = 4 ]; then
180         log_failure_msg "could not access PID file for $name"
181         return $status
182     else
183         log_failure_msg "$name is not running"
184         return $status
185     fi
186 }
187
188 log_use_fancy_output () {
189     TPUT=/usr/bin/tput
190     EXPR=/usr/bin/expr
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
193     else
194         FANCYTTY=0
195     fi
196     case "$FANCYTTY" in
197         1|Y|yes|true)   true;;
198         *)              false;;
199     esac
200 }
201
202 log_success_msg () {
203     if [ -n "${1:-}" ]; then
204         log_begin_msg $@
205     fi
206     log_end_msg 0
207 }
208
209 log_failure_msg () {
210     if [ -n "${1:-}" ]; then
211         log_begin_msg $@ "..."
212     fi
213     log_end_msg 1 || true
214 }
215
216 log_warning_msg () {
217     if [ -n "${1:-}" ]; then
218         log_begin_msg $@ "..."
219     fi
220     log_end_msg 255 || true
221 }
222
223 #
224 # NON-LSB HELPER FUNCTIONS
225 #
226 # int get_lsb_header_val (char *scriptpathname, char *key)
227 get_lsb_header_val () {
228         if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
229                 return 1
230         fi
231         LSB_S="### BEGIN INIT INFO"
232         LSB_E="### END INIT INFO"
233         sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
234 }
235
236 # int log_begin_message (char *message)
237 log_begin_msg () {
238     if [ -z "${1:-}" ]; then
239         return 1
240     fi
241     echo -n "$@"
242 }
243
244 # Sample usage:
245 # log_daemon_msg "Starting GNOME Login Manager" "gdm"
246 #
247 # On Debian, would output "Starting GNOME Login Manager: gdm"
248 # On Ubuntu, would output " * Starting GNOME Login Manager..."
249 #
250 # If the second argument is omitted, logging suitable for use with
251 # log_progress_msg() is used:
252 #
253 # log_daemon_msg "Starting remote filesystem services"
254 #
255 # On Debian, would output "Starting remote filesystem services:"
256 # On Ubuntu, would output " * Starting remote filesystem services..."
257
258 log_daemon_msg () {
259     if [ -z "${1:-}" ]; then
260         return 1
261     fi
262     log_daemon_msg_pre "$@"
263
264     if [ -z "${2:-}" ]; then
265         echo -n "$1:"
266         return
267     fi
268     
269     echo -n "$1: $2"
270     log_daemon_msg_post "$@"
271 }
272
273 # #319739
274 #
275 # Per policy docs:
276 #
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
281 #     log_end_msg 0
282 #
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
285 # the reader...
286 #
287 # On Ubuntu, one would expect log_progress_msg to be a no-op.
288 log_progress_msg () {
289     if [ -z "${1:-}" ]; then
290         return 1
291     fi
292     echo -n " $@"
293 }
294
295
296 # int log_end_message (int exitstatus)
297 log_end_msg () {
298     # If no arguments were passed, return
299     if [ -z "${1:-}" ]; then
300         return 1
301     fi
302
303     retval=$1
304
305     log_end_msg_pre "$@"
306
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
310         RED=`$TPUT setaf 1`
311         YELLOW=`$TPUT setaf 3`
312         NORMAL=`$TPUT op`
313     else
314         RED=''
315         YELLOW=''
316         NORMAL=''
317     fi
318
319     if [ $1 -eq 0 ]; then
320         echo "."
321     elif [ $1 -eq 255 ]; then
322         /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
323     else
324         /bin/echo -e " ${RED}failed!${NORMAL}"
325     fi
326     log_end_msg_post "$@"
327     return $retval
328 }
329
330 log_action_msg () {
331     echo "$@."
332 }
333
334 log_action_begin_msg () {
335     echo -n "$@..."
336 }
337
338 log_action_cont_msg () {
339     echo -n "$@..."
340 }
341
342 log_action_end_msg () {
343     log_action_end_msg_pre "$@"
344     if [ -z "${2:-}" ]; then
345         end="."
346     else
347         end=" ($2)."
348     fi
349
350     if [ $1 -eq 0 ]; then
351         echo "done${end}"
352     else
353         if log_use_fancy_output; then
354             RED=`$TPUT setaf 1`
355             NORMAL=`$TPUT op`
356             /bin/echo -e "${RED}failed${end}${NORMAL}"
357         else
358             echo "failed${end}"
359         fi
360     fi
361     log_action_end_msg_post "$@"
362 }
363
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 () { :; }
371
372 FANCYTTY=
373 [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true