~ubuntu-branches/ubuntu/quantal/lsb/quantal-proposed

« back to all changes in this revision

Viewing changes to init-functions

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lawrence
  • Date: 2005-03-27 21:42:24 UTC
  • mto: (1.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050327214224-ptr2vsz557dpai0d
Tags: 2.0-7
Fix Replaces line to use the correct version in lsb-base.
(Closes: #301694, RC; Closes: #301747)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# /lib/lsb/init-functions for Debian -*- shell-script -*-
2
2
#
3
 
#Copyright (c) 2002 Chris Lawrence
 
3
#Copyright (c) 2002-03 Chris Lawrence
4
4
#All rights reserved.
5
5
#
6
6
#Redistribution and use in source and binary forms, with or without
28
28
#SUCH DAMAGE.
29
29
 
30
30
start_daemon () {
31
 
    local force nice
32
 
    set -- `POSIXLY_CORRECT=1 getopt "fn:" $*`
 
31
    local force nice pidfile exec
 
32
    set -- `POSIXLY_CORRECT=1 getopt "fn:p:" $*`
33
33
    force=0
34
34
    nice=0
 
35
    pidfile=/dev/null
35
36
 
36
37
    for i in $*; do
37
38
        case $i in
38
39
            -f)  force=1; shift;;
39
40
            -n)  nice=$2; shift 2;;
 
41
            -p)  pidfile=$2; shift 2;;
40
42
            --)  shift; break;;
41
43
        esac
42
44
    done
45
47
 
46
48
    if [ $force = 1 ]; then
47
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" -- $*
48
52
    else
49
53
        /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo -- $*
50
54
    fi
51
55
}
52
56
 
53
 
# The definition of "return" in the LSB documentation is ambiguous.
54
57
pidofproc () {
55
 
    if [ -e /var/run/$1.pid ]; then
56
 
        cat /var/run/$1.pid
 
58
    local pidfile line i pids= status
 
59
    set -- `POSIXLY_CORRECT=1 getopt "p:" $*`
 
60
    pidfile=
 
61
 
 
62
    for i in $*; do
 
63
        case $i in
 
64
            -p)  pidfile=$2; shift 2;;
 
65
            --)  shift; break;;
 
66
        esac
 
67
    done
 
68
 
 
69
    if [ -z "$pidfile" ]; then
 
70
        pidfile=/var/run/$(basename "$1").pid
 
71
    fi
 
72
 
 
73
    if [ -f "$pidfile" ]; then
 
74
        read -d "" line < "$pidfile"
 
75
        for i in $line; do
 
76
            if [ -z "$(echo $p | sed 's/[0-9]//g')" -a -d "/proc/$i" ]; then 
 
77
                pids="$i $pids"
 
78
            fi
 
79
        done
 
80
        if [ -n "$pids" ]; then
 
81
            echo "$pids"
 
82
            return 0
 
83
        else
 
84
            return 2 # program is dead and /var/run pid file exists
 
85
        fi
 
86
    elif [ -x /bin/pidof ]; then
 
87
        /bin/pidof -o %PPID $1
 
88
        status="$?"
 
89
        [ "$status" = 1 ] && return 3 # program is not running
 
90
        return 0
57
91
    else
58
 
        echo
 
92
        return 4 # program or service is unknown
59
93
    fi
60
 
    return 0
61
94
}
62
95
 
63
96
# start-stop-daemon uses the same algorithm as "pidofproc" above.
64
97
killproc () {
 
98
    local pidfile sig status
 
99
    set -- `POSIXLY_CORRECT=1 getopt "p:" $*`
 
100
    pidfile=
 
101
 
 
102
    for i in $*; do
 
103
        case $i in
 
104
            -p)  pidfile=$2; shift 2;;
 
105
            --)  shift; break;;
 
106
        esac
 
107
    done
 
108
 
 
109
    if [ ! $pidfile ]; then
 
110
        pidfile=/var/run/$(basename "$1").pid
 
111
    fi
 
112
 
65
113
    if [ $2 ]; then
66
 
        /sbin/start-stop-daemon --stop --pidfile /var/run/$1.pid --retry 5 --signal $2 --quiet --oknodo
 
114
        sig=$(echo $2 | sed -e 's/^-\(.*\)/\1/')
 
115
        sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
 
116
        /sbin/start-stop-daemon --stop --pidfile "$pidfile" --signal $sig --quiet
 
117
        status="$?"
 
118
        [ "$status" = 1 ] && return 3 # program is not running
 
119
        return 0
67
120
    else
68
 
        /sbin/start-stop-daemon --stop --pidfile /var/run/$1.pid --retry 5 --retry 5 --quiet --oknodo
 
121
        /sbin/start-stop-daemon --stop --pidfile "$pidfile" --retry 5 --quiet --oknodo
69
122
    fi
70
123
}
71
124
 
72
 
# Something fancier might be nice.  But it meets the spec.
73
125
log_success_msg () {
74
 
    echo "Success: $1"
 
126
    echo " * $@"
75
127
}
 
128
 
76
129
log_failure_msg () {
77
 
    echo "Failure: $1"
 
130
    TPUT=/usr/bin/tput
 
131
    if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
 
132
        RED=`$TPUT setaf 1`
 
133
        NORMAL=`$TPUT op`
 
134
        echo " $RED*$NORMAL $@"
 
135
    else
 
136
        echo " * $@"
 
137
    fi
78
138
}
 
139
 
79
140
log_warning_msg () {
80
 
    echo "Warning: $1"
81
 
}
 
141
    TPUT=/usr/bin/tput
 
142
    if [ -x $TPUT ] && $TPUT hpa 60 >/dev/null 2>&1; then
 
143
        YELLOW=`$TPUT setaf 3`
 
144
        NORMAL=`$TPUT op`
 
145
        echo " $YELLOW*$NORMAL $@"
 
146
    else
 
147
        echo " * $@"
 
148
    fi
 
149
}
 
150
 
 
151
#
 
152
# NON-LSB HELPER FUNCTIONS
 
153
#
 
154
# int get_lsb_header_val (char *scriptpathname, char *key)
 
155
get_lsb_header_val () {
 
156
        if [ ! -f "$1" ] || [ -z "$2" ]; then
 
157
                return 1
 
158
        fi
 
159
        LSB_S="### BEGIN INIT INFO"
 
160
        LSB_E="### END INIT INFO"
 
161
        sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
 
162
}
 
163
 
 
164
# int log_begin_message (char *message)
 
165
log_begin_msg () {
 
166
        if [ -z "$1" ]; then
 
167
                return 1
 
168
        fi
 
169
        echo " * $@"
 
170
}
 
171
 
 
172
# int log_end_message (int exitstatus)
 
173
log_end_msg () {
 
174
 
 
175
    # If no arguments were passed, return
 
176
    [ -z "$1" ] && return 1
 
177
 
 
178
    # Only do the fancy stuff if we have an appropriate terminal
 
179
    # and if /usr is already mounted
 
180
    TPUT=/usr/bin/tput
 
181
    EXPR=/usr/bin/expr
 
182
    if [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1; then
 
183
        COLS=`$TPUT cols`
 
184
        if [ -n "$COLS" ]; then
 
185
            COL=`$EXPR $COLS - 7`
 
186
        else
 
187
            COL=73
 
188
        fi
 
189
        UP=`$TPUT cuu1`
 
190
        END=`$TPUT hpa $COL`
 
191
        START=`$TPUT hpa 0`
 
192
        RED=`$TPUT setaf 1`
 
193
        NORMAL=`$TPUT op`
 
194
        if [ $1 -eq 0 ]; then
 
195
            echo "$UP$END[ ok ]"
 
196
        else
 
197
            echo -e "$UP$START $RED*$NORMAL$END[${RED}fail${NORMAL}]"
 
198
        fi
 
199
    else
 
200
        if [ $1 -eq 0 ]; then
 
201
            echo "   ...done."
 
202
        else
 
203
            echo "   ...fail!"
 
204
        fi
 
205
    fi
 
206
    return $1
 
207
}
 
208