CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by johnraff on Fri 22nd Apr 18:56 (modification of post by view diff)
View followups from johnraff | download | new post

  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.  
  237. # SEND MESSAGES TO PLYMOUTH
  238. if [ -x /bin/plymouth ] # is this true at the time the function is loaded?
  239. then
  240.     plymouth_send() {
  241.     [ "$1" = '-n' ] && { # add a flag '>' for lines that will be extended
  242.         shift
  243.         /bin/plymouth message --text=">$*"
  244.         return
  245.     }
  246.     [ "$1" = '-w' ] && { # add "warning" formatting
  247.         shift
  248.         /bin/plymouth update --status="warning"
  249.         /bin/plymouth message --text="$*"
  250.         /bin/plymouth update --status="normal"
  251.         return
  252.     }
  253.     [ "$1" = '-f' ] && { # add "failed" formatting
  254.         shift
  255.         /bin/plymouth update --status="failed"
  256.         /bin/plymouth message --text="$*"
  257.         /bin/plymouth update --status="normal"
  258.         return
  259.     }
  260.     /bin/plymouth message --text="$*"
  261.     }
  262. else
  263.     plymouth_send() { :; }
  264. fi
  265.  
  266.  
  267. # int log_begin_message (char *message)
  268. log_begin_msg () {
  269.     if [ -z "${1:-}" ]; then
  270.         return 1
  271.     fi
  272.     echo -n "$@"
  273.     plymouth_send -n "$@"
  274. }
  275.  
  276. # Sample usage:
  277. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  278. #
  279. # On Debian, would output "Starting GNOME Login Manager: gdm"
  280. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  281. #
  282. # If the second argument is omitted, logging suitable for use with
  283. # log_progress_msg() is used:
  284. #
  285. # log_daemon_msg "Starting remote filesystem services"
  286. #
  287. # On Debian, would output "Starting remote filesystem services:"
  288. # On Ubuntu, would output " * Starting remote filesystem services..."
  289.  
  290. log_daemon_msg () {
  291.     if [ -z "${1:-}" ]; then
  292.         return 1
  293.     fi
  294.     log_daemon_msg_pre "$@"
  295.  
  296.     if [ -z "${2:-}" ]; then
  297.         echo -n "$1:"
  298.         plymouth_send -n "$1:"
  299.         return
  300.     fi
  301.    
  302.     echo -n "$1: $2"
  303.     plymouth_send -n "$1: $2"
  304.     log_daemon_msg_post "$@"
  305. }
  306.  
  307. # #319739
  308. #
  309. # Per policy docs:
  310. #
  311. #     log_daemon_msg "Starting remote file system services"
  312. #     log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  313. #     log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  314. #     log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  315. #     log_end_msg 0
  316. #
  317. # You could also do something fancy with log_end_msg here based on the
  318. # return values of start-stop-daemon; this is left as an exercise for
  319. # the reader...
  320. #
  321. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  322. log_progress_msg () {
  323.     if [ -z "${1:-}" ]; then
  324.         return 1
  325.     fi
  326.     echo -n " $@"
  327.     plymouth_send -n " $@"
  328. }
  329.  
  330.  
  331. # int log_end_message (int exitstatus)
  332. log_end_msg () {
  333.     # If no arguments were passed, return
  334.     if [ -z "${1:-}" ]; then
  335.         return 1
  336.     fi
  337.  
  338.     retval=$1
  339.  
  340.     log_end_msg_pre "$@"
  341.  
  342.     # Only do the fancy stuff if we have an appropriate terminal
  343.     # and if /usr is already mounted
  344.     if log_use_fancy_output; then
  345.         RED=`$TPUT setaf 1`
  346.         YELLOW=`$TPUT setaf 3`
  347.         NORMAL=`$TPUT op`
  348.     else
  349.         RED=''
  350.         YELLOW=''
  351.         NORMAL=''
  352.     fi
  353.  
  354.     if [ $1 -eq 0 ]; then
  355.         echo "."
  356.         plymouth_send "."
  357.     elif [ $1 -eq 255 ]; then
  358.         /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
  359.         plymouth_send -w " (warning)."
  360.     else
  361.         /bin/echo -e " ${RED}failed!${NORMAL}"
  362.         plymouth_send -f " failed!"
  363.     fi
  364.     log_end_msg_post "$@"
  365.     return $retval
  366. }
  367.  
  368. log_action_msg () {
  369.     echo "$@."
  370.     plymouth_send "$@."
  371. }
  372.  
  373. log_action_begin_msg () {
  374.     echo -n "$@..."
  375.     plymouth_send -n "$@..."
  376. }
  377.  
  378. log_action_cont_msg () {
  379.     echo -n "$@..."
  380.     plymouth_send -n "$@..."
  381. }
  382.  
  383. log_action_end_msg () {
  384.     log_action_end_msg_pre "$@"
  385.     if [ -z "${2:-}" ]; then
  386.         end="."
  387.     else
  388.         end=" ($2)."
  389.     fi
  390.  
  391.     if [ $1 -eq 0 ]; then
  392.         echo "done${end}"
  393.         plymouth_send "done${end}"
  394.     else
  395.         if log_use_fancy_output; then
  396.             RED=`$TPUT setaf 1`
  397.             NORMAL=`$TPUT op`
  398.             /bin/echo -e "${RED}failed${end}${NORMAL}"
  399.             plymouth_send -f "failed${end}"
  400.         else
  401.             echo "failed${end}"
  402.             plymouth_send -f "failed${end}"
  403.         fi
  404.     fi
  405.     log_action_end_msg_post "$@"
  406. }
  407.  
  408. # Hooks for /etc/lsb-base-logging.sh
  409. log_daemon_msg_pre () { :; }
  410. log_daemon_msg_post () { :; }
  411. log_end_msg_pre () { :; }
  412. log_end_msg_post () { :; }
  413. log_action_end_msg_pre () { :; }
  414. log_action_end_msg_post () { :; }
  415.  
  416. FANCYTTY=
  417. [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me