source: freewrt/package/quagga/files/quagga.init@ 621d5d2

freewrt_2_0
Last change on this file since 621d5d2 was b1bc4e2a, checked in by Waldemar Brodkorb <wbx@…>, 19 years ago

fix all remaining init scripts, normalize

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@1029 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100644
File size: 6.2 KB
Line 
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
12ME=$(basename $0)
13
14usage() {
15 echo "Usage: ${ME} {start|stop|restart} [daemon ...]"
16 exit 2
17}
18
19if [ -z "$1" ]
20then
21 usage
22else
23 COMMAND=$1
24fi
25shift
26ARG_DAEMONS=$*
27BINDIR=/usr/sbin
28CONFDIR=/etc/quagga
29STATEDIR=/var/run/quagga
30DAEMONS="zebra ripd ripngd ospfd ospf6d bgpd"
31DAEMON_FLAGS=-d
32WATCHQUAGGA_FLAGS="-d -z -T 60 -R"
33WATCHQUAGGA_CMD="$0 watchrestart"
34if [ ${COMMAND} != "watchrestart" ]
35then
36 DAEMONS="${DAEMONS} watchquagga"
37fi
38DAEMONS_STARTSEQ=${DAEMONS}
39
40reverse()
41{
42 local revlist r
43 revlist=
44 for r
45 do
46 revlist="$r $revlist"
47 done
48 echo $revlist
49}
50
51DAEMONS_STOPSEQ=$(reverse ${DAEMONS_STARTSEQ})
52
53#pidof() {
54# ps ax | awk 'match($5, "(^|/)'"$1"'$") > 0 { printf " %s", $1 }'
55#}
56
57quit() {
58 echo "${ME}: $1"
59 exit 0
60}
61
62die() {
63 echo "${ME}: $1"
64 exit 1
65}
66
67is_in() {
68 local i
69 for i in $2
70 do
71 [ "$1" = "$i" ] && return 0
72 done
73 return 1
74}
75
76select_subset() {
77 local unknown i j
78 unknown=
79 RESULT=
80 for i in $1
81 do
82 is_in $i "$2" || unknown="$unknown $i"
83 done
84 if [ -n "$unknown" ]
85 then
86 RESULT=$unknown
87 return 1
88 else
89 for j in $2
90 do
91 is_in $j "$1" && RESULT="$RESULT $j"
92 done
93 return 0
94 fi
95}
96
97# check command
98. /etc/rc.conf
99
100case ${COMMAND} in
101autostart|start|stop|restart)
102 ;;
103watchrestart)
104 if [ -n "$ARG_DAEMONS" ]
105 then
106 echo "${ME}: watchrestart mode is only for use by watchquagga"
107 exit 2
108 fi
109 ;;
110*)
111 usage
112 ;;
113esac
114
115# select daemons to start
116
117case ${COMMAND} in
118autostart)
119 test x"${quagga:-NO}" = x"NO" && exit 0
120 exec $0 start
121 ;;
122start|restart|watchrestart)
123 START_DAEMONS=
124 for d in ${DAEMONS_STARTSEQ}
125 do
126 [ -x "${BINDIR}/${d}" -a -f "${CONFDIR}/${d}.conf" ] \
127 && START_DAEMONS="${START_DAEMONS}${d} "
128 done
129 WATCHQUAGGA_DAEMONS=${START_DAEMONS}
130 if is_in watchquagga "${DAEMONS_STARTSEQ}"
131 then
132 START_DAEMONS="${START_DAEMONS} watchquagga"
133 fi
134 if [ -n "${ARG_DAEMONS}" ]
135 then
136 if select_subset "${ARG_DAEMONS}" "${DAEMONS}"
137 then
138 if select_subset "${ARG_DAEMONS}" "${START_DAEMONS}"
139 then
140 START_DAEMONS=${RESULT}
141 else
142 die "these daemons are not startable:${RESULT}."
143 fi
144 else
145 die "unknown daemons:${RESULT}; choose from: ${DAEMONS}."
146 fi
147 fi
148 ;;
149esac
150
151# select daemons to stop
152
153case ${COMMAND} in
154stop|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 ;;
179esac
180
181# stop daemons
182
183for d in $STOP_DAEMONS
184do
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 quagga:quagga ${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
271done
272
273# start daemons
274
275if [ -n "$START_DAEMONS" ]
276then
277 [ -d ${CONFDIR} ] \
278 || quit "${ME}: no config directory ${CONFDIR} - exiting."
279 chown -R quagga:quagga ${CONFDIR}
280 [ -d ${STATEDIR} ] || mkdir -p ${STATEDIR} \
281 || die "${ME}: could not create state directory ${STATEDIR} - exiting."
282 chown -R quagga:quagga ${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 quagga:quagga ${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
335fi
Note: See TracBrowser for help on using the repository browser.