First commit with the existing packages
[feed/routing.git] / packages / qmp-quagga / files / quagga
1 #!/bin/sh
2 #
3 # quagga Starts/stops quagga daemons and watchquagga.
4 # Create a daemon.conf file to have that routing daemon
5 # started/stopped automagically when using this script
6 # without any daemon names as args.
7 # If watchquagga is available, it will also be
8 # started/stopped if the script is called without
9 # any daemon names.
10 #
11
12 ME=$(basename $0)
13
14 usage() {
15 echo "Usage: ${ME} {start|stop|restart} [daemon ...]"
16 exit 2
17 }
18
19 if [ -z "$1" ]
20 then
21 usage
22 else
23 COMMAND=$1
24 fi
25 shift
26 ARG_DAEMONS=$*
27 BINDIR=/usr/sbin
28 CONFDIR=/etc/quagga
29 STATEDIR=/var/run/quagga
30 RUNUSER=network
31 RUNGROUP=$RUNUSER
32 DAEMONS="zebra ripd ripngd ospfd ospf6d bgpd"
33 DAEMON_FLAGS=-d
34 WATCHQUAGGA_FLAGS="-d -z -T 60 -R"
35 WATCHQUAGGA_CMD="$0 watchrestart"
36 if [ ${COMMAND} != "watchrestart" ]
37 then
38 DAEMONS="${DAEMONS} watchquagga"
39 fi
40 DAEMONS_STARTSEQ=${DAEMONS}
41
42 reverse()
43 {
44 local revlist r
45 revlist=
46 for r
47 do
48 revlist="$r $revlist"
49 done
50 echo $revlist
51 }
52
53 DAEMONS_STOPSEQ=$(reverse ${DAEMONS_STARTSEQ})
54
55 #pidof() {
56 # ps ax | awk 'match($5, "(^|/)'"$1"'$") > 0 { printf " %s", $1 }'
57 #}
58
59 quit() {
60 echo "${ME}: $1"
61 exit 0
62 }
63
64 die() {
65 echo "${ME}: $1"
66 exit 1
67 }
68
69 is_in() {
70 local i
71 for i in $2
72 do
73 [ "$1" = "$i" ] && return 0
74 done
75 return 1
76 }
77
78 select_subset() {
79 local unknown i j
80 unknown=
81 RESULT=
82 for i in $1
83 do
84 is_in $i "$2" || unknown="$unknown $i"
85 done
86 if [ -n "$unknown" ]
87 then
88 RESULT=$unknown
89 return 1
90 else
91 for j in $2
92 do
93 is_in $j "$1" && RESULT="$RESULT $j"
94 done
95 return 0
96 fi
97 }
98
99 # check command
100
101 case ${COMMAND}
102 in
103 start|stop|restart)
104 ;;
105 watchrestart)
106 if [ -n "$ARG_DAEMONS" ]
107 then
108 echo "${ME}: watchrestart mode is only for use by watchquagga"
109 exit 2
110 fi
111 ;;
112 *)
113 usage
114 ;;
115 esac
116
117 # select daemons to start
118
119 case ${COMMAND}
120 in
121 start|restart|watchrestart)
122 START_DAEMONS=
123 for d in ${DAEMONS_STARTSEQ}
124 do
125 [ -x "${BINDIR}/${d}" -a -f "${CONFDIR}/${d}.conf" ] \
126 && START_DAEMONS="${START_DAEMONS}${d} "
127 done
128 WATCHQUAGGA_DAEMONS=${START_DAEMONS}
129 if is_in watchquagga "${DAEMONS_STARTSEQ}"
130 then
131 START_DAEMONS="${START_DAEMONS} watchquagga"
132 fi
133 if [ -n "${ARG_DAEMONS}" ]
134 then
135 if select_subset "${ARG_DAEMONS}" "${DAEMONS}"
136 then
137 if select_subset "${ARG_DAEMONS}" "${START_DAEMONS}"
138 then
139 START_DAEMONS=${RESULT}
140 else
141 die "these daemons are not startable:${RESULT}."
142 fi
143 else
144 die "unknown daemons:${RESULT}; choose from: ${DAEMONS}."
145 fi
146 fi
147 ;;
148 esac
149
150 # select daemons to stop
151
152 case ${COMMAND}
153 in
154 stop|restart|watchrestart)
155 STOP_DAEMONS=${DAEMONS_STOPSEQ}
156 if [ -n "${ARG_DAEMONS}" ]
157 then
158 if select_subset "${ARG_DAEMONS}" "${STOP_DAEMONS}"
159 then
160 STOP_DAEMONS=${RESULT}
161 else
162 die "unknown daemons:${RESULT}; choose from: ${DAEMONS}."
163 fi
164 fi
165 stop_daemons=
166 for d in ${STOP_DAEMONS}
167 do
168 pidfile=${STATEDIR}/${d}.pid
169 if [ -f "${pidfile}" -o -n "$(pidof ${d})" ]
170 then
171 stop_daemons="${stop_daemons}${d} "
172 elif [ -n "${ARG_DAEMONS}" ]
173 then
174 echo "${ME}: found no ${d} process running."
175 fi
176 done
177 STOP_DAEMONS=${stop_daemons}
178 ;;
179 esac
180
181 # stop daemons
182
183 for d in $STOP_DAEMONS
184 do
185 echo -n "${ME}: Stopping ${d} ... "
186 pidfile=${STATEDIR}/${d}.pid
187 if [ -f "${pidfile}" ]
188 then
189 file_pid=$(cat ${pidfile})
190 if [ -z "${file_pid}" ]
191 then
192 echo -n "no pid file entry found ... "
193 fi
194 else
195 file_pid=
196 echo -n "no pid file found ... "
197 fi
198 proc_pid=$(pidof ${d})
199 if [ -z "${proc_pid}" ]
200 then
201 echo -n "found no ${d} process running ... "
202 else
203 count=0
204 notinpidfile=
205 for p in ${proc_pid}
206 do
207 count=$((${count}+1))
208 if kill ${p}
209 then
210 echo -n "killed ${p} ... "
211 else
212 echo -n "failed to kill ${p} ... "
213 fi
214 [ "${p}" = "${file_pid}" ] \
215 || notinpidfile="${notinpidfile} ${p}"
216 done
217 [ ${count} -le 1 ] \
218 || echo -n "WARNING: ${count} ${d} processes were found running ... "
219 for n in ${notinpidfile}
220 do
221 echo -n "WARNING: process ${n} was not in pid file ... "
222 done
223 fi
224 count=0
225 survivors=$(pidof ${d})
226 while [ -n "${survivors}" ]
227 do
228 sleep 1
229 count=$((${count}+1))
230 survivors=$(pidof ${d})
231 [ -z "${survivors}" -o ${count} -gt 5 ] && break
232 for p in ${survivors}
233 do
234 sleep 1
235 echo -n "${p} "
236 kill ${p}
237 done
238 done
239 survivors=$(pidof ${d})
240 [ -n "${survivors}" ] && \
241 if kill -KILL ${survivors}
242 then
243 echo -n "KILLed ${survivors} ... "
244 else
245 echo -n "failed to KILL ${survivors} ... "
246 fi
247 sleep 1
248 survivors=$(pidof ${d})
249 if [ -z "${survivors}" ]
250 then
251 echo -n "done."
252 if [ -f "${pidfile}" ]
253 then
254 rm -f ${pidfile} \
255 || echo -n " Failed to remove pidfile."
256 fi
257 else
258 echo -n "failed to stop ${survivors} - giving up."
259 if [ "${survivors}" != "${file_pid}" ]
260 then
261 if echo "${survivors}" > ${pidfile}
262 then
263 chown ${RUNUSER}:${RUNGROUP} ${pidfile}
264 echo -n " Wrote ${survivors} to pidfile."
265 else
266 echo -n " Failed to write ${survivors} to pidfile."
267 fi
268 fi
269 fi
270 echo
271 done
272
273 # start daemons
274
275 if [ -n "$START_DAEMONS" ]
276 then
277 [ -d ${CONFDIR} ] \
278 || quit "${ME}: no config directory ${CONFDIR} - exiting."
279 chown -R ${RUNUSER}:${RUNGROUP} ${CONFDIR}
280 [ -d ${STATEDIR} ] || mkdir -p ${STATEDIR} \
281 || die "${ME}: could not create state directory ${STATEDIR} - exiting."
282 chown -R ${RUNUSER}:${RUNGROUP} ${STATEDIR}
283
284 for d in $START_DAEMONS
285 do
286 echo -n "${ME}: Starting ${d} ... "
287 proc_pid=$(pidof ${d})
288 pidfile=${STATEDIR}/${d}.pid
289 file_pid=
290 if [ -f "${pidfile}" ]
291 then
292 file_pid=$(cat ${pidfile})
293 if [ -n "${file_pid}" ]
294 then
295 echo -n "found old pid file entry ${file_pid} ... "
296 fi
297 fi
298 if [ -n "${proc_pid}" ]
299 then
300 echo -n "found ${d} running (${proc_pid}) - skipping ${d}."
301 if [ "${proc_pid}" != "${file_pid}" ]
302 then
303 if echo "${proc_pid}" > ${pidfile}
304 then
305 chown ${RUNUSER}:${RUNGROUP} ${pidfile}
306 echo -n " Wrote ${proc_pid} to pidfile."
307 else
308 echo -n " Failed to write ${proc_pid} to pidfile."
309 fi
310 fi
311 elif rm -f "${pidfile}"
312 then
313 if [ "${d}" = "watchquagga" ]
314 then
315 "${BINDIR}/${d}" \
316 ${WATCHQUAGGA_FLAGS} \
317 "${WATCHQUAGGA_CMD}" \
318 ${WATCHQUAGGA_DAEMONS}
319 status=$?
320 else
321 "${BINDIR}/${d}" ${DAEMON_FLAGS}
322 status=$?
323 fi
324 if [ $status -eq 0 ]
325 then
326 echo -n "done."
327 else
328 echo -n "failed."
329 fi
330 else
331 echo -n " failed to remove pidfile."
332 fi
333 echo
334 done
335 fi