binutils: update to 2.43.1
[openadk.git] / package / quagga / files / quagga.init
blobb5147b7bcac9bcf50284d47221c89a559ff4e991
1 #!/bin/sh
2 #PKG quagga
3 #INIT 50
5 ME=$(basename $0)
7 usage() {
8 echo "Usage: ${ME} {start|stop|restart} [daemon ...]"
9 exit 2
12 if [ -z "$1" ]
13 then
14 usage
15 else
16 COMMAND=$1
18 shift
19 ARG_DAEMONS=$*
20 BINDIR=/usr/sbin
21 CONFDIR=/etc/quagga
22 STATEDIR=/var/run/quagga
23 DAEMONS="zebra ripd ripngd ospfd ospf6d bgpd"
24 DAEMON_FLAGS=-d
25 WATCHQUAGGA_FLAGS="-d -z -T 60 -R"
26 WATCHQUAGGA_CMD="sh $0 watchrestart"
27 if [ ${COMMAND} != "watchrestart" ]
28 then
29 DAEMONS="${DAEMONS} watchquagga"
31 DAEMONS_STARTSEQ=${DAEMONS}
33 reverse()
35 local revlist r
36 revlist=
37 for r
39 revlist="$r $revlist"
40 done
41 echo $revlist
44 DAEMONS_STOPSEQ=$(reverse ${DAEMONS_STARTSEQ})
46 #pidof() {
47 # ps ax | awk 'match($5, "(^|/)'"$1"'$") > 0 { printf " %s", $1 }'
50 quit() {
51 echo "${ME}: $1"
52 exit 0
55 die() {
56 echo "${ME}: $1"
57 exit 1
60 is_in() {
61 local i
62 for i in $2
64 [ "$1" = "$i" ] && return 0
65 done
66 return 1
69 select_subset() {
70 local unknown i j
71 unknown=
72 RESULT=
73 for i in $1
75 is_in $i "$2" || unknown="$unknown $i"
76 done
77 if [ -n "$unknown" ]
78 then
79 RESULT=$unknown
80 return 1
81 else
82 for j in $2
84 is_in $j "$1" && RESULT="$RESULT $j"
85 done
86 return 0
90 # check command
91 . /etc/rc.conf
93 case ${COMMAND} in
94 autostop) ;;
95 autostart|start|stop|restart)
97 watchrestart)
98 if [ -n "$ARG_DAEMONS" ]
99 then
100 echo "${ME}: watchrestart mode is only for use by watchquagga"
101 exit 2
105 usage
107 esac
109 # select daemons to start
111 case ${COMMAND} in
112 autostart)
113 test x"${quagga:-NO}" = x"NO" && exit 0
114 test x"$quagga" = x"DAEMON" && test -x /bin/mksh && exec mksh -T- $0 start
115 exec sh $0 start
117 start|restart|watchrestart)
118 START_DAEMONS=
119 for d in ${DAEMONS_STARTSEQ}
121 [ -x "${BINDIR}/${d}" -a -f "${CONFDIR}/${d}.conf" ] \
122 && START_DAEMONS="${START_DAEMONS}${d} "
123 done
124 WATCHQUAGGA_DAEMONS=${START_DAEMONS}
125 if is_in watchquagga "${DAEMONS_STARTSEQ}"
126 then
127 START_DAEMONS="${START_DAEMONS} watchquagga"
129 if [ -n "${ARG_DAEMONS}" ]
130 then
131 if select_subset "${ARG_DAEMONS}" "${DAEMONS}"
132 then
133 if select_subset "${ARG_DAEMONS}" "${START_DAEMONS}"
134 then
135 START_DAEMONS=${RESULT}
136 else
137 die "these daemons are not startable:${RESULT}."
139 else
140 die "unknown daemons:${RESULT}; choose from: ${DAEMONS}."
144 esac
146 # select daemons to stop
148 case ${COMMAND} in
149 stop|restart|watchrestart)
150 STOP_DAEMONS=${DAEMONS_STOPSEQ}
151 if [ -n "${ARG_DAEMONS}" ]
152 then
153 if select_subset "${ARG_DAEMONS}" "${STOP_DAEMONS}"
154 then
155 STOP_DAEMONS=${RESULT}
156 else
157 die "unknown daemons:${RESULT}; choose from: ${DAEMONS}."
160 stop_daemons=
161 for d in ${STOP_DAEMONS}
163 pidfile=${STATEDIR}/${d}.pid
164 if [ -f "${pidfile}" -o -n "$(pidof ${d})" ]
165 then
166 stop_daemons="${stop_daemons}${d} "
167 elif [ -n "${ARG_DAEMONS}" ]
168 then
169 echo "${ME}: found no ${d} process running."
171 done
172 STOP_DAEMONS=${stop_daemons}
174 esac
176 # stop daemons
178 for d in $STOP_DAEMONS
180 echo -n "${ME}: Stopping ${d} ... "
181 pidfile=${STATEDIR}/${d}.pid
182 if [ -f "${pidfile}" ]
183 then
184 file_pid=$(cat ${pidfile})
185 if [ -z "${file_pid}" ]
186 then
187 echo -n "no pid file entry found ... "
189 else
190 file_pid=
191 echo -n "no pid file found ... "
193 proc_pid=$(pidof ${d})
194 if [ -z "${proc_pid}" ]
195 then
196 echo -n "found no ${d} process running ... "
197 else
198 count=0
199 notinpidfile=
200 for p in ${proc_pid}
202 count=$((${count}+1))
203 if kill ${p}
204 then
205 echo -n "killed ${p} ... "
206 else
207 echo -n "failed to kill ${p} ... "
209 [ "${p}" = "${file_pid}" ] \
210 || notinpidfile="${notinpidfile} ${p}"
211 done
212 [ ${count} -le 1 ] \
213 || echo -n "WARNING: ${count} ${d} processes were found running ... "
214 for n in ${notinpidfile}
216 echo -n "WARNING: process ${n} was not in pid file ... "
217 done
219 count=0
220 survivors=$(pidof ${d})
221 while [ -n "${survivors}" ]
223 sleep 1
224 count=$((${count}+1))
225 survivors=$(pidof ${d})
226 [ -z "${survivors}" -o ${count} -gt 5 ] && break
227 for p in ${survivors}
229 sleep 1
230 echo -n "${p} "
231 kill ${p}
232 done
233 done
234 survivors=$(pidof ${d})
235 [ -n "${survivors}" ] && \
236 if kill -KILL ${survivors}
237 then
238 echo -n "KILLed ${survivors} ... "
239 else
240 echo -n "failed to KILL ${survivors} ... "
242 sleep 1
243 survivors=$(pidof ${d})
244 if [ -z "${survivors}" ]
245 then
246 echo -n "done."
247 if [ -f "${pidfile}" ]
248 then
249 rm -f ${pidfile} \
250 || echo -n " Failed to remove pidfile."
252 else
253 echo -n "failed to stop ${survivors} - giving up."
254 if [ "${survivors}" != "${file_pid}" ]
255 then
256 if echo "${survivors}" > ${pidfile}
257 then
258 chown quagga:quagga ${pidfile}
259 echo -n " Wrote ${survivors} to pidfile."
260 else
261 echo -n " Failed to write ${survivors} to pidfile."
265 echo
266 done
268 # start daemons
270 if [ -n "$START_DAEMONS" ]
271 then
272 [ -d ${CONFDIR} ] \
273 || quit "${ME}: no config directory ${CONFDIR} - exiting."
274 chown -R quagga:quagga ${CONFDIR}
275 [ -d ${STATEDIR} ] || mkdir -p ${STATEDIR} \
276 || die "${ME}: could not create state directory ${STATEDIR} - exiting."
277 chown -R quagga:quagga ${STATEDIR}
279 for d in $START_DAEMONS
281 echo -n "${ME}: Starting ${d} ... "
282 proc_pid=$(pidof ${d})
283 pidfile=${STATEDIR}/${d}.pid
284 file_pid=
285 if [ -f "${pidfile}" ]
286 then
287 file_pid=$(cat ${pidfile})
288 if [ -n "${file_pid}" ]
289 then
290 echo -n "found old pid file entry ${file_pid} ... "
293 if [ -n "${proc_pid}" ]
294 then
295 echo -n "found ${d} running (${proc_pid}) - skipping ${d}."
296 if [ "${proc_pid}" != "${file_pid}" ]
297 then
298 if echo "${proc_pid}" > ${pidfile}
299 then
300 chown quagga:quagga ${pidfile}
301 echo -n " Wrote ${proc_pid} to pidfile."
302 else
303 echo -n " Failed to write ${proc_pid} to pidfile."
306 elif rm -f "${pidfile}"
307 then
308 if [ "${d}" = "watchquagga" ]
309 then
310 $("${BINDIR}/${d}" \
311 ${WATCHQUAGGA_FLAGS} \
312 "${WATCHQUAGGA_CMD}" \
313 ${WATCHQUAGGA_DAEMONS})
314 status=$?
315 else
316 $("${BINDIR}/${d}" ${DAEMON_FLAGS})
317 status=$?
319 if [ $status -eq 0 ]
320 then
321 echo -n "done."
322 else
323 echo -n "failed."
325 else
326 echo -n " failed to remove pidfile."
328 echo
329 done