home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / lib / lsb / init-functions
Encoding:
Text File  |  2006-07-03  |  7.6 KB  |  291 lines

  1. # /lib/lsb/init-functions for Debian -*- shell-script -*-
  2. #
  3. #Copyright (c) 2002-05 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. #ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. #IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. #ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. #FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. #DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. #OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. #HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. #LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. #OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. #SUCH DAMAGE.
  29.  
  30. start_daemon () {
  31.     local force nice pidfile exec i
  32.     set -- `POSIXLY_CORRECT=1 getopt "fn:p:" $*`
  33.     force=0
  34.     nice=0
  35.     pidfile=/dev/null
  36.  
  37.     for i in $*; do
  38.         case $i in
  39.             -f)  force=1; shift;;
  40.             -n)  nice=$2; shift 2;;
  41.             -p)  pidfile=$2; shift 2;;
  42.             --)  shift; break;;
  43.         esac
  44.     done
  45.  
  46.     exec=$1; shift
  47.  
  48.     if [ $force = 1 ]; then
  49.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --startas $exec --pidfile /dev/null --oknodo -- $*
  50.     elif [ $pidfile ]; then
  51.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo --pidfile "$pidfile" -- $*
  52.     else
  53.         /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo -- $*
  54.     fi
  55. }
  56.  
  57. pidofproc () {
  58.     local pidfile line i pids= status specified pid
  59.     set -- `POSIXLY_CORRECT=1 getopt "p:" $*`
  60.     pidfile=
  61.     specified=
  62.  
  63.     for i in $*; do
  64.         case $i in
  65.             -p)  pidfile=$2; specified=1; shift 2;;
  66.             --)  shift; break;;
  67.         esac
  68.     done
  69.  
  70.     if [ -z "${pidfile:-}" ]; then
  71.         pidfile=/var/run/$(basename "$1").pid
  72.     fi
  73.  
  74.     if [ -f "$pidfile" ]; then
  75.         read pid < "$pidfile"
  76.         if [ -n "${pid:-}" ]; then
  77.             echo "$pid"
  78.             return 0
  79.         else
  80.             return 2 # program is dead and /var/run pid file exists
  81.         fi
  82.     elif [ -x /bin/pidof -a ! "$specified" ]; then
  83.         /bin/pidof -o %PPID $1
  84.         status="$?"
  85.         [ "$status" = 1 ] && return 3 # program is not running
  86.         return 0
  87.     else
  88.         return 4 # program or service is unknown
  89.     fi
  90. }
  91.  
  92. # start-stop-daemon uses the same algorithm as "pidofproc" above.
  93. killproc () {
  94.     local pidfile sig status base i specified
  95.     set -- `POSIXLY_CORRECT=1 getopt "p:" $*`
  96.     pidfile=
  97.     specified=
  98.  
  99.     for i in $*; do
  100.         case $i in
  101.             -p)  pidfile=$2; specified=1; shift 2;;
  102.             --)  shift; break;;
  103.         esac
  104.     done
  105.  
  106.     base=$(basename "$1")
  107.     if [ ! $pidfile ]; then
  108.         pidfile=/var/run/$base.pid
  109.     fi
  110.  
  111.     if [ "$specified" ]; then
  112.         sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
  113.         sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
  114.         sig=${sig:-TERM}
  115.         /sbin/start-stop-daemon --stop --pidfile "$pidfile" --signal $sig --quiet --name "$base"
  116.         status="$?"
  117.         [ "$status" = 1 ] && return 3 # program is not running
  118.         return 0
  119.     else
  120.         /sbin/start-stop-daemon --stop --pidfile "$pidfile" --retry 5 --quiet --oknodo --name "$base"
  121.     fi
  122.  
  123.     rm -f "$pidfile"
  124. }
  125.  
  126. log_use_fancy_output () {
  127.     TPUT=/usr/bin/tput
  128.     EXPR=/usr/bin/expr
  129.     if [ "x$TERM" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1; then
  130.         FANCYTTY=1
  131.         true
  132.     else
  133.         FANCYTTY=0
  134.         false
  135.     fi
  136. }
  137.  
  138. log_success_msg () {
  139.     echo "$@"
  140. }
  141.  
  142. log_failure_msg () {
  143.     if log_use_fancy_output; then
  144.         RED=`$TPUT setaf 1`
  145.         NORMAL=`$TPUT op`
  146.         echo "$RED*$NORMAL $@"
  147.     else
  148.         echo "$@"
  149.     fi
  150. }
  151.  
  152. log_warning_msg () {
  153.     if log_use_fancy_output; then
  154.         YELLOW=`$TPUT setaf 3`
  155.         NORMAL=`$TPUT op`
  156.         echo "$YELLOW*$NORMAL $@"
  157.     else
  158.         echo "$@"
  159.     fi
  160. }
  161.  
  162. #
  163. # NON-LSB HELPER FUNCTIONS
  164. #
  165. # int get_lsb_header_val (char *scriptpathname, char *key)
  166. get_lsb_header_val () {
  167.         if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  168.                 return 1
  169.         fi
  170.         LSB_S="### BEGIN INIT INFO"
  171.         LSB_E="### END INIT INFO"
  172.         sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  173. }
  174.  
  175. # int log_begin_message (char *message)
  176. log_begin_msg () {
  177.     if [ -z "${1:-}" ]; then
  178.         return 1
  179.     fi
  180.     echo -n "$@"
  181. }
  182.  
  183. # Sample usage:
  184. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  185. #
  186. # On Debian, would output "Starting GNOME Login Manager: gdm"
  187. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  188. #
  189. # If the second argument is omitted, logging suitable for use with
  190. # log_progress_msg() is used:
  191. #
  192. # log_daemon_msg "Starting remote filesystem services"
  193. #
  194. # On Debian, would output "Starting remote filesystem services:"
  195. # On Ubuntu, would output " * Starting remote filesystem services..."
  196.  
  197. log_daemon_msg () {
  198.     if [ -z "${1:-}" ]; then
  199.         return 1
  200.     fi
  201.  
  202.     if [ -z "${2:-}" ]; then
  203.         echo -n "$1:"
  204.         return
  205.     fi
  206.     
  207.     echo -n "$1: $2"
  208. }
  209.  
  210. # #319739
  211. #
  212. # Per policy docs:
  213. #
  214. #     log_daemon_msg "Starting remote file system services"
  215. #     log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  216. #     log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  217. #     log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  218. #     log_end_msg 0
  219. #
  220. # You could also do something fancy with log_end_msg here based on the
  221. # return values of start-stop-daemon; this is left as an exercise for
  222. # the reader...
  223. #
  224. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  225. log_progress_msg () {
  226.     if [ -z "${1:-}" ]; then
  227.         return 1
  228.     fi
  229.     echo -n " $@"
  230. }
  231.  
  232.  
  233. # int log_end_message (int exitstatus)
  234. log_end_msg () {
  235.     # If no arguments were passed, return
  236.     [ -z "${1:-}" ] && return 1
  237.  
  238.     # Only do the fancy stuff if we have an appropriate terminal
  239.     # and if /usr is already mounted
  240.     if log_use_fancy_output; then
  241.         RED=`$TPUT setaf 1`
  242.         NORMAL=`$TPUT op`
  243.         if [ $1 -eq 0 ]; then
  244.             echo "."
  245.         else
  246.             /bin/echo -e " ${RED}failed!${NORMAL}"
  247.         fi
  248.     else
  249.     if [ $1 -eq 0 ]; then
  250.             echo "."
  251.         else
  252.             echo " failed!"
  253.         fi
  254.     fi
  255.     return $1
  256. }
  257.  
  258. log_action_msg () {
  259.     echo "$@."
  260. }
  261.  
  262. log_action_begin_msg () {
  263.     echo -n "$@..."
  264. }
  265.  
  266. log_action_cont_msg () {
  267.     echo -n "$@..."
  268. }
  269.  
  270. log_action_end_msg () {
  271.     if [ -z "${2:-}" ]; then
  272.         end="."
  273.     else
  274.         end=" ($2)."
  275.     fi
  276.  
  277.     if [ $1 -eq 0 ]; then
  278.         echo "done${end}"
  279.     else
  280.         if log_use_fancy_output; then
  281.             RED=`$TPUT setaf 1`
  282.             NORMAL=`$TPUT op`
  283.             /bin/echo -e "${RED}failed${end}${NORMAL}"
  284.         else
  285.             echo "failed${end}"
  286.         fi
  287.     fi
  288. }
  289.  
  290. [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true
  291.