~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to vivid/etc/init.d/quagga

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-19 12:58:41 UTC
  • Revision ID: dimitri.j.ledkov@intel.com-20141119125841-98dr37roy8dvcv3b
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
### BEGIN INIT INFO
 
4
# Provides: quagga
 
5
# Required-Start: $local_fs $network $remote_fs $syslog
 
6
# Required-Stop: $local_fs $network $remote_fs $syslog
 
7
# Default-Start:  2 3 4 5
 
8
# Default-Stop: 0 1 6
 
9
# Short-Description: start and stop the Quagga routing suite
 
10
# Description: Quagga is a routing suite for IP routing protocols like 
 
11
#              BGP, OSPF, RIP and others. This script contols the main 
 
12
#              daemon "quagga" as well as the individual protocol daemons.
 
13
### END INIT INFO
 
14
#
 
15
 
 
16
PATH=/bin:/usr/bin:/sbin:/usr/sbin
 
17
D_PATH=/usr/lib/quagga
 
18
C_PATH=/etc/quagga
 
19
 
 
20
# Local Daemon selection may be done by using /etc/quagga/daemons.
 
21
# See /usr/share/doc/quagga/README.Debian.gz for further information.
 
22
# Keep zebra first and do not list watchquagga!
 
23
DAEMONS="zebra bgpd ripd ripngd ospfd ospf6d isisd babeld"
 
24
 
 
25
. /lib/lsb/init-functions
 
26
 
 
27
# Print the name of the pidfile.
 
28
pidfile()
 
29
{
 
30
        echo "/var/run/quagga/$1.pid"
 
31
}
 
32
 
 
33
# Check if daemon is started by using the pidfile.
 
34
started()
 
35
{
 
36
        [ -e `pidfile $1` ] && kill -0 `cat \`pidfile $1\`` 2> /dev/null && return 0
 
37
        return 1
 
38
}
 
39
 
 
40
# Loads the config via vtysh -b if configured to do so.
 
41
vtysh_b ()
 
42
{
 
43
        # Rember, that all variables have been incremented by 1 in convert_daemon_prios()
 
44
        if [ "$vtysh_enable" = 2 -a -f $C_PATH/Quagga.conf ]; then
 
45
                /usr/bin/vtysh -b
 
46
        fi
 
47
}
 
48
 
 
49
# Check if the daemon is activated and if its executable and config files 
 
50
# are in place.
 
51
# params:       daemon name
 
52
# returns:      0=ok, 1=error
 
53
check_daemon()
 
54
{
 
55
        # If the integrated config file is used the others are not checked.
 
56
        if [ -r "$C_PATH/Quagga.conf" ]; then
 
57
          return 0
 
58
        fi 
 
59
 
 
60
        # vtysh_enable has no config file nor binary so skip check.
 
61
        # (Not sure why vtysh_enable is in this list but does not hurt)
 
62
        if [ $1 != "watchquagga" -a $1 != "vtysh_enable" ]; then
 
63
          # check for daemon binary
 
64
          if [ ! -x "$D_PATH/$1" ]; then return 1; fi
 
65
                
 
66
          # check for config file                 
 
67
          if [ ! -r "$C_PATH/$1.conf" ]; then
 
68
            echo -n " (not started without config file)"
 
69
            return 1
 
70
          fi
 
71
        fi
 
72
        return 0
 
73
}
 
74
 
 
75
# Starts the server if it's not alrady running according to the pid file.
 
76
# The Quagga daemons creates the pidfile when starting.
 
77
start()
 
78
{
 
79
        echo -n " $1"
 
80
        if ! check_daemon $1; then return; fi
 
81
 
 
82
        if [ "$1" = "watchquagga" ]; then
 
83
            start-stop-daemon \
 
84
                --oknodo \
 
85
                --start \
 
86
                --pidfile=`pidfile $1` \
 
87
                --exec "$D_PATH/$1" \
 
88
                -- \
 
89
                "${watchquagga_options[@]}"
 
90
        else
 
91
            start-stop-daemon \
 
92
                --start \
 
93
                --pidfile=`pidfile $1` \
 
94
                --exec "$D_PATH/$1" \
 
95
                -- \
 
96
                `eval echo "$""$1""_options"`
 
97
        fi
 
98
}
 
99
 
 
100
# Stop the daemon given in the parameter, printing its name to the terminal.
 
101
stop()
 
102
{
 
103
    if ! started "$1" ; then
 
104
        echo -n " ($1)"
 
105
        return 0
 
106
    else
 
107
        PIDFILE=`pidfile $1`
 
108
        PID=`cat $PIDFILE 2>/dev/null`
 
109
        start-stop-daemon --stop --quiet --oknodo --exec "$D_PATH/$1"
 
110
        #
 
111
        #       Now we have to wait until $DAEMON has _really_ stopped.
 
112
        #
 
113
        if test -n "$PID" && kill -0 $PID 2>/dev/null; then
 
114
            echo -n " (waiting) ."
 
115
            cnt=0
 
116
            while kill -0 $PID 2>/dev/null; do
 
117
                cnt=`expr $cnt + 1`
 
118
                if [ $cnt -gt 60 ]; then
 
119
                    # Waited 120 secs now, fail.
 
120
                    echo -n "Failed.. "
 
121
                    break
 
122
                fi
 
123
                sleep 2
 
124
                echo -n "."
 
125
                done
 
126
            fi
 
127
        echo -n " $1"
 
128
        rm -f `pidfile $1`
 
129
    fi
 
130
}
 
131
 
 
132
# Converts values from /etc/quagga/daemons to all-numeric values.
 
133
convert_daemon_prios()
 
134
{
 
135
        for name in $DAEMONS zebra vtysh_enable watchquagga_enable; do
 
136
          # First, assign the value set by the user to $value 
 
137
          eval value=\$$name
 
138
 
 
139
          # Daemon not activated or entry missing?
 
140
          if [ "$value" = "no" -o "$value" = "" ]; then value=0; fi
 
141
 
 
142
          # These strings parsed for backwards compatibility.
 
143
          if [ "$value" = "yes"  -o  "$value" = "true" ]; then value=1; fi
 
144
 
 
145
          # Zebra is threatened special. It must be between 0=off and the first
 
146
      # user assigned value "1" so we increase all other enabled daemons' values.
 
147
          if [ "$name" != "zebra" -a "$value" -gt 0 ]; then value=`expr "$value" + 1`; fi
 
148
 
 
149
          # If e.g. name is zebra then we set "zebra=yes".
 
150
          eval $name=$value
 
151
        done
 
152
}
 
153
 
 
154
# Starts watchquagga for all wanted daemons.
 
155
start_watchquagga()
 
156
{
 
157
    local daemon_name
 
158
    local daemon_prio
 
159
    local found_one
 
160
 
 
161
    # Start the monitor daemon only if desired.
 
162
    if [ 0 -eq "$watchquagga_enable" ]; then
 
163
        return
 
164
    fi
 
165
 
 
166
    # Check variable type
 
167
    if ! declare -p watchquagga_options | grep -q '^declare \-a'; then
 
168
      echo
 
169
      echo "ERROR: The variable watchquagga_options from /etc/quagga/debian.cnf must be a BASH array!"
 
170
      echo "ERROR: Please convert config file and restart!"
 
171
      exit 1
 
172
    fi
 
173
 
 
174
    # Which daemons have been started?
 
175
    found_one=0
 
176
    for daemon_name in $DAEMONS; do
 
177
        eval daemon_prio=\$$daemon_name
 
178
        if [ "$daemon_prio" -gt 0 ]; then
 
179
            watchquagga_options+=($daemon_name)
 
180
            found_one=1
 
181
        fi
 
182
    done
 
183
 
 
184
    # Start if at least one daemon is activated.
 
185
    if [ $found_one -eq 1 ]; then
 
186
      echo -n "Starting Quagga monitor daemon:"
 
187
      start watchquagga
 
188
      echo "."
 
189
    fi
 
190
}
 
191
 
 
192
# Stopps watchquagga.
 
193
stop_watchquagga()
 
194
{
 
195
    echo -n "Stopping Quagga monitor daemon:"
 
196
    stop watchquagga
 
197
    echo "."
 
198
}
 
199
 
 
200
# Stops all daemons that have a lower level of priority than the given.
 
201
# (technically if daemon_prio >= wanted_prio)
 
202
stop_prio() 
 
203
{
 
204
        local wanted_prio
 
205
        local daemon_prio
 
206
        local daemon_list
 
207
 
 
208
        wanted_prio=$1
 
209
        daemon_list=${2:-$DAEMONS}
 
210
 
 
211
        echo -n "Stopping Quagga daemons (prio:$wanted_prio):"
 
212
 
 
213
        for prio_i in `seq 10 -1 $wanted_prio`; do
 
214
            for daemon_name in $daemon_list; do
 
215
                eval daemon_prio=\$$daemon_name
 
216
                if [ $daemon_prio -eq $prio_i ]; then
 
217
                    stop "$daemon_name"
 
218
                fi
 
219
            done
 
220
        done
 
221
        echo "."
 
222
}
 
223
 
 
224
# Starts all daemons that have a higher level of priority than the given.
 
225
# (technically if daemon_prio <= wanted_prio)
 
226
start_prio()
 
227
{
 
228
        local wanted_prio
 
229
        local daemon_prio
 
230
        local daemon_list
 
231
        
 
232
        wanted_prio=$1
 
233
        daemon_list=${2:-$DAEMONS}
 
234
 
 
235
        echo -n "Starting Quagga daemons (prio:$wanted_prio):"
 
236
 
 
237
        for prio_i in `seq 1 $wanted_prio`; do
 
238
            for daemon_name in $daemon_list; do
 
239
                eval daemon_prio=\$$daemon_name
 
240
                if [ $daemon_prio -eq $prio_i ]; then
 
241
                    start "$daemon_name"
 
242
                fi
 
243
            done
 
244
        done
 
245
        echo "."
 
246
}
 
247
 
 
248
#########################################################
 
249
#               Main program                            #
 
250
#########################################################
 
251
 
 
252
# Config broken but script must exit silently.
 
253
[ ! -r "$C_PATH/daemons" ] && exit 0
 
254
 
 
255
# Load configuration
 
256
. "$C_PATH/daemons"
 
257
. "$C_PATH/debian.conf"
 
258
 
 
259
# Set priority of un-startable daemons to 'no' and substitute 'yes' to '0'
 
260
convert_daemon_prios
 
261
 
 
262
if [ ! -d /var/run/quagga ]; then
 
263
    mkdir -p /var/run/quagga
 
264
    chown quagga:quagga /var/run/quagga
 
265
    chmod 755 /var/run/quagga
 
266
fi
 
267
 
 
268
case "$1" in
 
269
    start)
 
270
        # Try to load this necessary (at least for 2.6) module.
 
271
        if [ -d /lib/modules/`uname -r` ] ; then
 
272
          echo "Loading capability module if not yet done."
 
273
          set +e; LC_ALL=C modprobe -a capability 2>&1 | egrep -v "(not found|Can't locate)"; set -e
 
274
        fi
 
275
 
 
276
        # Start all daemons
 
277
        cd $C_PATH/
 
278
        if [ "$2" != "watchquagga" ]; then
 
279
          start_prio 10 $2
 
280
        fi
 
281
        vtysh_b
 
282
        start_watchquagga
 
283
        ;;
 
284
        
 
285
    1|2|3|4|5|6|7|8|9|10)
 
286
        # Stop/start daemons for the appropriate priority level
 
287
        stop_prio $1
 
288
        start_prio $1
 
289
        vtysh_b
 
290
        ;;
 
291
 
 
292
    stop|0)
 
293
        # Stop all daemons at level '0' or 'stop'
 
294
        stop_watchquagga
 
295
        if [ "$2" != "watchquagga" ]; then
 
296
          stop_prio 0 $2
 
297
        fi
 
298
 
 
299
        if [ -z "$2" -o "$2" = "zebra" ]; then
 
300
          echo "Removing all routes made by zebra."
 
301
          ip route flush proto zebra
 
302
        fi
 
303
        ;;
 
304
 
 
305
    restart|force-reload)
 
306
        $0 stop $2
 
307
        sleep 1
 
308
        $0 start $2
 
309
        ;;
 
310
 
 
311
    status)
 
312
        rc=3
 
313
        for i in /var/run/quagga/*.pid; do
 
314
          if [ -f "$i" ]; then
 
315
            d=`basename $i .pid`
 
316
            running="$running $d"
 
317
            rc=0
 
318
          fi
 
319
        done
 
320
        echo $running
 
321
        exit $rc
 
322
        ;;
 
323
 
 
324
    *)
 
325
        echo "Usage: /etc/init.d/quagga {start|stop|restart|force-reload|<priority>} [daemon]"
 
326
        echo "       E.g. '/etc/init.d/quagga 5' would start all daemons with a prio 1-5."
 
327
        echo "       Read /usr/share/doc/quagga/README.Debian for details."
 
328
        exit 1
 
329
        ;;
 
330
esac
 
331
 
 
332
exit 0