| | | |
| 1 | 1 | | # /lib/lsb/init-functions for Debian -*- shell-script -*- |
| 2 | 2 | | # |
| 3 | 3 | | #Copyright (c) 2002-08 Chris Lawrence |
| 4 | 4 | | #All rights reserved. |
| 5 | 5 | | # |
| 6 | 6 | | #Redistribution and use in source and binary forms, with or without |
| 7 | 7 | | #modification, are permitted provided that the following conditions |
| 8 | 8 | | #are met: |
| 9 | 9 | | #1. Redistributions of source code must retain the above copyright |
| 10 | 10 | | # notice, this list of conditions and the following disclaimer. |
| 11 | 11 | | #2. Redistributions in binary form must reproduce the above copyright |
| 12 | 12 | | # notice, this list of conditions and the following disclaimer in the |
| 13 | 13 | | # documentation and/or other materials provided with the distribution. |
| 14 | 14 | | #3. Neither the name of the author nor the names of other contributors |
| 15 | 15 | | # may be used to endorse or promote products derived from this software |
| 16 | 16 | | # without specific prior written permission. |
| 17 | 17 | | # |
| 18 | 18 | | #THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | 19 | | #IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | 20 | | #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | 21 | | #ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE |
| 22 | 22 | | #LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | 23 | | #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | 24 | | #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | 25 | | #BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | 26 | | #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | 27 | | #OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | 28 | | #EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 | | start_daemon () { |
| 31 | 31 | | local force nice pidfile exec i args |
| 32 | 32 | | force=0 |
| 33 | 33 | | nice=0 |
| 34 | 34 | | pidfile=/dev/null |
| 36 | 36 | | OPTIND=1 |
| 37 | 37 | | while getopts fn:p: opt ; do |
| 38 | 38 | | case "$opt" in |
| 39 | 39 | | f) force=1;; |
| 40 | 40 | | n) nice="$OPTARG";; |
| 41 | 41 | | p) pidfile="$OPTARG";; |
| 42 | 42 | | esac |
| 43 | 43 | | done |
| 45 | 45 | | shift $(($OPTIND - 1)) |
| 46 | 46 | | if [ "$1" = '--' ]; then |
| 47 | 47 | | shift |
| 48 | 48 | | fi |
| 50 | 50 | | exec="$1"; shift |
| 52 | 52 | | args="--start --nicelevel $nice --quiet --oknodo" |
| 53 | 53 | | if [ $force = 1 ]; then |
| 54 | 54 | | /sbin/start-stop-daemon $args --chdir "$PWD" --startas $exec --pidfile /dev/null -- "$@" |
| 55 | 55 | | elif [ $pidfile ]; then |
| 56 | 56 | | /sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec --oknodo --pidfile "$pidfile" -- "$@" |
| 57 | 57 | | else |
| 58 | 58 | | /sbin/start-stop-daemon $args --chdir "$PWD" --exec $exec -- "$@" |
| 59 | 59 | | fi |
| 60 | 60 | | } |
| 62 | 62 | | pidofproc () { |
| 63 | 63 | | local pidfile line i pids= status specified pid |
| 64 | 64 | | pidfile= |
| 65 | 65 | | specified= |
| 67 | 67 | | OPTIND=1 |
| 68 | 68 | | while getopts p: opt ; do |
| 69 | 69 | | case "$opt" in |
| 70 | 70 | | p) pidfile="$OPTARG"; specified=1;; |
| 71 | 71 | | esac |
| 72 | 72 | | done |
| 73 | 73 | | shift $(($OPTIND - 1)) |
| 75 | 75 | | base=${1##*/} |
| 76 | 76 | | if [ ! "$specified" ]; then |
| 77 | 77 | | pidfile="/var/run/$base.pid" |
| 78 | 78 | | fi |
| 80 | 80 | | if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then |
| 81 | 81 | | read pid < "$pidfile" |
| 82 | 82 | | if [ -n "${pid:-}" ]; then |
| 83 | 83 | | if $(kill -0 "${pid:-}" 2> /dev/null); then |
| 84 | 84 | | echo "$pid" |
| 85 | 85 | | return 0 |
| 86 | 86 | | elif ps "${pid:-}" >/dev/null 2>&1; then |
| 87 | 87 | | echo "$pid" |
| 88 | 88 | | return 0 # program is running, but not owned by this user |
| 89 | 89 | | else |
| 90 | 90 | | return 1 # program is dead and /var/run pid file exists |
| 91 | 91 | | fi |
| 92 | 92 | | fi |
| 93 | 93 | | fi |
| 94 | 94 | | if [ -x /bin/pidof -a ! "$specified" ]; then |
| 95 | 95 | | status="0" |
| 96 | 96 | | /bin/pidof -o %PPID -x $1 || status="$?" |
| 97 | 97 | | if [ "$status" = 1 ]; then |
| 98 | 98 | | return 3 # program is not running |
| 99 | 99 | | fi |
| 100 | 100 | | return 0 |
| 101 | 101 | | fi |
| 102 | 102 | | return 4 # Unable to determine status |
| 103 | 103 | | } |
| 105 | 105 | | # start-stop-daemon uses the same algorithm as "pidofproc" above. |
| 106 | 106 | | killproc () { |
| 107 | 107 | | local pidfile sig status base i name_param is_term_sig |
| 108 | 108 | | pidfile= |
| 109 | 109 | | name_param= |
| 110 | 110 | | is_term_sig=no |
| 112 | 112 | | OPTIND=1 |
| 113 | 113 | | while getopts p: opt ; do |
| 114 | 114 | | case "$opt" in |
| 115 | 115 | | p) pidfile="$OPTARG";; |
| 116 | 116 | | esac |
| 117 | 117 | | done |
| 118 | 118 | | shift $(($OPTIND - 1)) |
| 120 | 120 | | base=${1##*/} |
| 121 | 121 | | if [ ! $pidfile ]; then |
| 122 | 122 | | name_param="--name $base --pidfile /var/run/$base.pid" |
| 123 | 123 | | else |
| 124 | 124 | | name_param="--pidfile $pidfile" |
| 125 | 125 | | fi |
| 127 | 127 | | sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/') |
| 128 | 128 | | sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/') |
| 129 | 129 | | if [ -z "$sig" -o "$sig" = 15 -o "$sig" = TERM ]; then |
| 130 | 130 | | is_term_sig=yes |
| 131 | 131 | | fi |
| 132 | 132 | | status=0 |
| 133 | 133 | | if [ ! "$is_term_sig" = yes ]; then |
| 134 | 134 | | if [ -n "$sig" ]; then |
| 135 | 135 | | /sbin/start-stop-daemon --stop --signal "$sig" --quiet $name_param || status="$?" |
| 136 | 136 | | else |
| 137 | 137 | | /sbin/start-stop-daemon --stop --quiet $name_param || status="$?" |
| 138 | 138 | | fi |
| 139 | 139 | | else |
| 140 | 140 | | /sbin/start-stop-daemon --stop --quiet --oknodo $name_param || status="$?" |
| 141 | 141 | | fi |
| 142 | 142 | | if [ "$status" = 1 ]; then |
| 143 | 143 | | if [ -n "$sig" ]; then |
| 144 | 144 | | return 0 |
| 145 | 145 | | fi |
| 146 | 146 | | return 3 # program is not running |
| 147 | 147 | | fi |
| 149 | 149 | | if [ "$status" = 0 -a "$is_term_sig" = yes -a "$pidfile" ]; then |
| 150 | 150 | | pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile" |
| 151 | 151 | | fi |
| 152 | 152 | | return 0 |
| 153 | 153 | | } |
| 155 | 155 | | # Return LSB status |
| 156 | 156 | | status_of_proc () { |
| 157 | 157 | | local pidfile daemon name status |
| 159 | 159 | | pidfile= |
| 160 | 160 | | OPTIND=1 |
| 161 | 161 | | while getopts p: opt ; do |
| 162 | 162 | | case "$opt" in |
| 163 | 163 | | p) pidfile="$OPTARG";; |
| 164 | 164 | | esac |
| 165 | 165 | | done |
| 166 | 166 | | shift $(($OPTIND - 1)) |
| 168 | 168 | | if [ -n "$pidfile" ]; then |
| 169 | 169 | | pidfile="-p $pidfile" |
| 170 | 170 | | fi |
| 171 | 171 | | daemon="$1" |
| 172 | 172 | | name="$2" |
| 174 | 174 | | status="0" |
| 175 | 175 | | pidofproc $pidfile $daemon >/dev/null || status="$?" |
| 176 | 176 | | if [ "$status" = 0 ]; then |
| 177 | 177 | | log_success_msg "$name is running" |
| 178 | 178 | | return 0 |
| 179 | 179 | | elif [ "$status" = 4 ]; then |
| 180 | 180 | | log_failure_msg "could not access PID file for $name" |
| 181 | 181 | | return $status |
| 182 | 182 | | else |
| 183 | 183 | | log_failure_msg "$name is not running" |
| 184 | 184 | | return $status |
| 185 | 185 | | fi |
| 186 | 186 | | } |
| 188 | 188 | | log_use_fancy_output () { |
| 189 | 189 | | TPUT=/usr/bin/tput |
| 190 | 190 | | EXPR=/usr/bin/expr |
| 191 | 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 | 192 | | [ -z $FANCYTTY ] && FANCYTTY=1 || true |
| 193 | 193 | | else |
| 194 | 194 | | FANCYTTY=0 |
| 195 | 195 | | fi |
| 196 | 196 | | case "$FANCYTTY" in |
| 197 | 197 | | 1|Y|yes|true) true;; |
| 198 | 198 | | *) false;; |
| 199 | 199 | | esac |
| 200 | 200 | | } |
| 202 | 202 | | log_success_msg () { |
| 203 | 203 | | if [ -n "${1:-}" ]; then |
| 204 | 204 | | log_begin_msg $@ |
| 205 | 205 | | fi |
| 206 | 206 | | log_end_msg 0 |
| 207 | 207 | | } |
| 209 | 209 | | log_failure_msg () { |
| 210 | 210 | | if [ -n "${1:-}" ]; then |
| 211 | 211 | | log_begin_msg $@ "..." |
| 212 | 212 | | fi |
| 213 | 213 | | log_end_msg 1 || true |
| 214 | 214 | | } |
| 216 | 216 | | log_warning_msg () { |
| 217 | 217 | | if [ -n "${1:-}" ]; then |
| 218 | 218 | | log_begin_msg $@ "..." |
| 219 | 219 | | fi |
| 220 | 220 | | log_end_msg 255 || true |
| 221 | 221 | | } |
| 223 | 223 | | # |
| 224 | 224 | | # NON-LSB HELPER FUNCTIONS |
| 225 | 225 | | # |
| 226 | 226 | | # int get_lsb_header_val (char *scriptpathname, char *key) |
| 227 | 227 | | get_lsb_header_val () { |
| 228 | 228 | | if [ ! -f "$1" ] || [ -z "${2:-}" ]; then |
| 229 | 229 | | return 1 |
| 230 | 230 | | fi |
| 231 | 231 | | LSB_S="### BEGIN INIT INFO" |
| 232 | 232 | | LSB_E="### END INIT INFO" |
| 233 | 233 | | sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1 |
| 234 | 234 | | } |
| 236 | + | # SEND MESSAGES TO PLYMOUTH |