| 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 args OPTIND |
| 32 |
force="" |
| 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="force";; |
| 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" ]; then |
| 54 |
/sbin/start-stop-daemon $args \ |
| 55 |
--chdir "$PWD" --startas $exec --pidfile /dev/null -- "$@" |
| 56 |
elif [ $pidfile ]; then |
| 57 |
/sbin/start-stop-daemon $args \ |
| 58 |
--chdir "$PWD" --exec $exec --oknodo --pidfile "$pidfile" -- "$@" |
| 59 |
else |
| 60 |
/sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec -- "$@" |
| 61 |
fi |
| 62 |
} |
| 63 |
|
| 64 |
pidofproc () { |
| 65 |
local pidfile base status specified pid OPTIND |
| 66 |
pidfile= |
| 67 |
specified= |
| 68 |
|
| 69 |
OPTIND=1 |
| 70 |
while getopts p: opt ; do |
| 71 |
case "$opt" in |
| 72 |
p) pidfile="$OPTARG" |
| 73 |
specified="specified" |
| 74 |
;; |
| 75 |
esac |
| 76 |
done |
| 77 |
shift $(($OPTIND - 1)) |
| 78 |
if [ $# -ne 1 ]; then |
| 79 |
echo "$0: invalid arguments" >&2 |
| 80 |
return 4 |
| 81 |
fi |
| 82 |
|
| 83 |
base=${1##*/} |
| 84 |
if [ ! "$specified" ]; then |
| 85 |
pidfile="/var/run/$base.pid" |
| 86 |
fi |
| 87 |
|
| 88 |
if [ -n "${pidfile:-}" ]; then |
| 89 |
if [ -e "$pidfile" ]; then |
| 90 |
if [ -r "$pidfile" ]; then |
| 91 |
read pid < "$pidfile" |
| 92 |
if [ -n "${pid:-}" ]; then |
| 93 |
if $(kill -0 "${pid:-}" 2> /dev/null); then |
| 94 |
echo "$pid" || true |
| 95 |
return 0 |
| 96 |
elif ps "${pid:-}" >/dev/null 2>&1; then |
| 97 |
echo "$pid" || true |
| 98 |
return 0 # program is running, but not owned by this user |
| 99 |
else |
| 100 |
return 1 # program is dead and /var/run pid file exists |
| 101 |
fi |
| 102 |
fi |
| 103 |
else |
| 104 |
return 4 # pid file not readable, hence status is unknown. |
| 105 |
fi |
| 106 |
else |
| 107 |
# pid file doesn't exist, try to find the pid nevertheless |
| 108 |
if [ -x /bin/pidof ] && [ ! "$specified" ]; then |
| 109 |
status="0" |
| 110 |
/bin/pidof -c -o %PPID -x $1 || status="$?" |
| 111 |
if [ "$status" = 1 ]; then |
| 112 |
return 3 # program is not running |
| 113 |
fi |
| 114 |
return 0 |
| 115 |
fi |
| 116 |
return 3 # specified pid file doesn't exist, program probably stopped |
| 117 |
fi |
| 118 |
fi |
| 119 |
if [ "$specified" ]; then |
| 120 |
return 3 # almost certain it's not running |
| 121 |
fi |
| 122 |
return 4 # Unable to determine status |
| 123 |
} |
| 124 |
|
| 125 |
# start-stop-daemon uses the same algorithm as "pidofproc" above. |
| 126 |
killproc () { |
| 127 |
local pidfile sig status base name_param is_term_sig OPTIND |
| 128 |
pidfile= |
| 129 |
name_param= |
| 130 |
is_term_sig= |
| 131 |
|
| 132 |
OPTIND=1 |
| 133 |
while getopts p: opt ; do |
| 134 |
case "$opt" in |
| 135 |
p) pidfile="$OPTARG";; |
| 136 |
esac |
| 137 |
done |
| 138 |
shift $(($OPTIND - 1)) |
| 139 |
|
| 140 |
base=${1##*/} |
| 141 |
if [ ! $pidfile ]; then |
| 142 |
name_param="--name $base --pidfile /var/run/$base.pid" |
| 143 |
else |
| 144 |
name_param="--name $base --pidfile $pidfile" |
| 145 |
fi |
| 146 |
|
| 147 |
sig=${2:-} sig=${sig#-} sig=${sig#SIG} |
| 148 |
if [ "$sig" = 15 ] || [ "$sig" = TERM ]; then |
| 149 |
is_term_sig="terminate_signal" |
| 150 |
fi |
| 151 |
status=0 |
| 152 |
if [ ! "$is_term_sig" ]; then |
| 153 |
if [ -n "$sig" ]; then |
| 154 |
/sbin/start-stop-daemon --stop --signal "$sig" \ |
| 155 |
--quiet $name_param || status="$?" |
| 156 |
else |
| 157 |
/sbin/start-stop-daemon --stop \ |
| 158 |
--retry 5 \ |
| 159 |
--quiet $name_param || status="$?" |
| 160 |
fi |
| 161 |
else |
| 162 |
/sbin/start-stop-daemon --stop --quiet \ |
| 163 |
--oknodo $name_param || status="$?" |
| 164 |
fi |
| 165 |
if [ "$status" = 1 ]; then |
| 166 |
if [ -z "$sig" ]; then |
| 167 |
return 0 |
| 168 |
fi |
| 169 |
return 3 # program is not running |
| 170 |
fi |
| 171 |
|
| 172 |
if [ "$status" = 0 ] && [ "$is_term_sig" ] && [ "$pidfile" ]; then |
| 173 |
pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile" |
| 174 |
fi |
| 175 |
return 0 |
| 176 |
} |
| 177 |
|
| 178 |
# Return LSB status |
| 179 |
status_of_proc () { |
| 180 |
local pidfile daemon name status OPTIND |
| 181 |
|
| 182 |
pidfile= |
| 183 |
OPTIND=1 |
| 184 |
while getopts p: opt ; do |
| 185 |
case "$opt" in |
| 186 |
p) pidfile="$OPTARG";; |
| 187 |
esac |
| 188 |
done |
| 189 |
shift $(($OPTIND - 1)) |
| 190 |
|
| 191 |
if [ -n "$pidfile" ]; then |
| 192 |
pidfile="-p $pidfile" |
| 193 |
fi |
| 194 |
daemon="$1" |
| 195 |
name="$2" |
| 196 |
|
| 197 |
status="0" |
| 198 |
pidofproc $pidfile $daemon >/dev/null || status="$?" |
| 199 |
if [ "$status" = 0 ]; then |
| 200 |
log_success_msg "$name is running" |
| 201 |
return 0 |
| 202 |
elif [ "$status" = 4 ]; then |
| 203 |
log_failure_msg "could not access PID file for $name" |
| 204 |
return $status |
| 205 |
else |
| 206 |
log_failure_msg "$name is not running" |
| 207 |
return $status |
| 208 |
fi |
| 209 |
} |
| 210 |
|
| 211 |
log_use_fancy_output () { |
| 212 |
if [ -t 1 ] && |
| 213 |
[ "${TERM:-}" ] && |
| 214 |
[ "${TERM:-}" != "dumb" ] |
| 215 |
then |
| 216 |
[ -z $FANCYTTY ] && return 0 || true |
| 217 |
else |
| 218 |
return 1 |
| 219 |
fi |
| 220 |
case "$FANCYTTY" in |
| 221 |
1|Y|yes|true) true;; |
| 222 |
*) false;; |
| 223 |
esac |
| 224 |
} |
| 225 |
|
| 226 |
log_success_msg () { |
| 227 |
if [ -n "${1:-}" ]; then |
| 228 |
log_begin_msg "$@" |
| 229 |
fi |
| 230 |
log_end_msg 0 |
| 231 |
} |
| 232 |
|
| 233 |
log_failure_msg () { |
| 234 |
if [ -n "${1:-}" ]; then |
| 235 |
log_begin_msg "$@" "..." |
| 236 |
fi |
| 237 |
log_end_msg 1 || true |
| 238 |
} |
| 239 |
|
| 240 |
log_warning_msg () { |
| 241 |
if [ -n "${1:-}" ]; then |
| 242 |
log_begin_msg "$@" "..." |
| 243 |
fi |
| 244 |
log_end_msg 255 || true |
| 245 |
} |
| 246 |
|
| 247 |
# |
| 248 |
# NON-LSB HELPER FUNCTIONS |
| 249 |
# |
| 250 |
# int get_lsb_header_val (char *scriptpathname, char *key) |
| 251 |
get_lsb_header_val () { |
| 252 |
if [ ! -f "$1" ] || [ -z "${2:-}" ]; then |
| 253 |
return 1 |
| 254 |
fi |
| 255 |
LSB_S="### BEGIN INIT INFO" |
| 256 |
LSB_E="### END INIT INFO" |
| 257 |
sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \+\(.*\)/\1/p" "$1" |
| 258 |
} |
| 259 |
|
| 260 |
# If the currently running init daemon is upstart, return zero; if the |
| 261 |
# calling init script belongs to a package which also provides a native |
| 262 |
# upstart job, it should generally exit non-zero in this case. |
| 263 |
init_is_upstart() |
| 264 |
{ |
| 265 |
if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | /bin/grep -q upstart; then |
| 266 |
return 0 |
| 267 |
fi |
| 268 |
return 1 |
| 269 |
} |
| 270 |
|
| 271 |
# int log_begin_message (char *message) |
| 272 |
log_begin_msg () { |
| 273 |
log_begin_msg_pre "$@" |
| 274 |
if [ -z "${1:-}" ]; then |
| 275 |
return 1 |
| 276 |
fi |
| 277 |
echo -n "$@" || true |
| 278 |
log_begin_msg_post "$@" |
| 279 |
} |
| 280 |
|
| 281 |
# Sample usage: |
| 282 |
# log_daemon_msg "Starting GNOME Login Manager" "gdm" |
| 283 |
# |
| 284 |
# On Debian, would output "Starting GNOME Login Manager: gdm" |
| 285 |
# On Ubuntu, would output " * Starting GNOME Login Manager..." |
| 286 |
# |
| 287 |
# If the second argument is omitted, logging suitable for use with |
| 288 |
# log_progress_msg() is used: |
| 289 |
# |
| 290 |
# log_daemon_msg "Starting remote filesystem services" |
| 291 |
# |
| 292 |
# On Debian, would output "Starting remote filesystem services:" |
| 293 |
# On Ubuntu, would output " * Starting remote filesystem services..." |
| 294 |
|
| 295 |
log_daemon_msg () { |
| 296 |
if [ -z "${1:-}" ]; then |
| 297 |
return 1 |
| 298 |
fi |
| 299 |
log_daemon_msg_pre "$@" |
| 300 |
|
| 301 |
if [ -z "${2:-}" ]; then |
| 302 |
echo -n "$1:" || true |
| 303 |
return |
| 304 |
fi |
| 305 |
|
| 306 |
echo -n "$1: $2" || true |
| 307 |
log_daemon_msg_post "$@" |
| 308 |
} |
| 309 |
|
| 310 |
# #319739 |
| 311 |
# |
| 312 |
# Per policy docs: |
| 313 |
# |
| 314 |
# log_daemon_msg "Starting remote file system services" |
| 315 |
# log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd |
| 316 |
# log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd |
| 317 |
# log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd |
| 318 |
# log_end_msg 0 |
| 319 |
# |
| 320 |
# You could also do something fancy with log_end_msg here based on the |
| 321 |
# return values of start-stop-daemon; this is left as an exercise for |
| 322 |
# the reader... |
| 323 |
# |
| 324 |
# On Ubuntu, one would expect log_progress_msg to be a no-op. |
| 325 |
log_progress_msg () { |
| 326 |
if [ -z "${1:-}" ]; then |
| 327 |
return 1 |
| 328 |
fi |
| 329 |
echo -n " $@" || true |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
# int log_end_message (int exitstatus) |
| 334 |
log_end_msg () { |
| 335 |
# If no arguments were passed, return |
| 336 |
if [ -z "${1:-}" ]; then |
| 337 |
return 1 |
| 338 |
fi |
| 339 |
|
| 340 |
log_end_msg_pre "$@" |
| 341 |
|
| 342 |
# Only do the fancy stuff if we have an appropriate terminal |
| 343 |
if log_use_fancy_output; then |
| 344 |
esc='' # escape character, printf '\033' |
| 345 |
red="$esc[31m" # ANSI color escapes |
| 346 |
yellow="$esc[33m" |
| 347 |
normal="$esc[0m" |
| 348 |
else |
| 349 |
red= yellow= normal= |
| 350 |
fi |
| 351 |
|
| 352 |
if [ $1 -eq 0 ]; then |
| 353 |
echo "." || true |
| 354 |
elif [ $1 -eq 255 ]; then |
| 355 |
echo " ${yellow}(warning).${normal}" || true |
| 356 |
else |
| 357 |
echo " ${red}failed!${normal}" || true |
| 358 |
fi |
| 359 |
log_end_msg_post "$@" |
| 360 |
return $1 |
| 361 |
} |
| 362 |
|
| 363 |
log_action_msg () { |
| 364 |
log_action_msg_pre "$@" |
| 365 |
echo "$@." || true |
| 366 |
log_action_msg_post "$@" |
| 367 |
} |
| 368 |
|
| 369 |
log_action_begin_msg () { |
| 370 |
log_action_begin_msg_pre "$@" |
| 371 |
echo -n "$@..." || true |
| 372 |
log_action_begin_msg_post "$@" |
| 373 |
} |
| 374 |
|
| 375 |
log_action_cont_msg () { |
| 376 |
echo -n "$@..." || true |
| 377 |
} |
| 378 |
|
| 379 |
log_action_end_msg () { |
| 380 |
local end |
| 381 |
log_action_end_msg_pre "$@" |
| 382 |
if [ -z "${2:-}" ]; then |
| 383 |
end="." |
| 384 |
else |
| 385 |
end=" ($2)." |
| 386 |
fi |
| 387 |
|
| 388 |
if [ $1 -eq 0 ]; then |
| 389 |
echo "done${end}" || true |
| 390 |
else |
| 391 |
if log_use_fancy_output; then |
| 392 |
esc='' # escape character, printf '\033' |
| 393 |
red="$esc[31m" # ANSI color escapes |
| 394 |
normal="$esc[0m" |
| 395 |
echo "${red}failed${end}${normal}" || true |
| 396 |
else |
| 397 |
echo "failed${end}" || true |
| 398 |
fi |
| 399 |
fi |
| 400 |
log_action_end_msg_post "$@" |
| 401 |
} |
| 402 |
|
| 403 |
# Pre&Post empty function declaration, to be overriden from /lib/lsb/init-functions.d/* |
| 404 |
log_daemon_msg_pre () { :; } |
| 405 |
log_daemon_msg_post () { :; } |
| 406 |
log_begin_msg_pre () { :; } |
| 407 |
log_begin_msg_post () { :; } |
| 408 |
log_end_msg_pre () { :; } |
| 409 |
log_end_msg_post () { :; } |
| 410 |
log_action_msg_pre () { :; } |
| 411 |
log_action_msg_post () { :; } |
| 412 |
log_action_begin_msg_pre () { :; } |
| 413 |
log_action_begin_msg_post () { :; } |
| 414 |
log_action_end_msg_pre () { :; } |
| 415 |
log_action_end_msg_post () { :; } |
| 416 |
|
| 417 |
# Include hooks from other packages in /lib/lsb/init-functions.d |
| 418 |
for hook in $(run-parts --lsbsysinit --list /lib/lsb/init-functions.d 2>/dev/null); do |
| 419 |
[ -r $hook ] && . $hook || true |
| 420 |
done |
| 421 |
|
| 422 |
FANCYTTY= |
| 423 |
[ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true |