English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/common-nfo/package/fwifupdown/files/main.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3427 - (show annotations) (download) (as text)
Wed Aug 8 11:08:41 2007 UTC (6 years, 6 months ago) by tg
File MIME type: application/x-sh
File size: 7505 byte(s)
merge in trunk rev. 3426
1 #!/bin/sh
2 #
3 # This file is part of the FreeWRT project. FreeWRT is copyrighted
4 # material, please see the LICENCE file in the top-level directory
5 # or at http://www.freewrt.org/licence for details.
6 #
7 # Christian Fischer <spaetzle@freewrt.org>
8 #
9
10 main_ifupdown() {
11 local environ=$(cat $ENVFILE)
12
13 IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\
14 echo " PARENT_IFACE_TYPE=$IFACE_TYPE"; \
15 echo " PARENT_IFACE=$IFACE"; \
16 echo " MDENT=$MDENT") \
17 busybox "$@"
18
19 retval=$?
20 grep -q RT_BB_NOEXEC=1 $ENVFILE && retval=0
21 echo $environ >$ENVFILE
22 return $retval
23 }
24
25 main_exit() {
26 [[ $1 != 0 ]] && echo "RT_ERR=1" >>$ENVFILE
27 exit $1
28 }
29
30 main_env_update() {
31 local t_mdent lastmatch=
32
33 # get submode from calling hook script
34 SUBMODE=$(echo ${0%/*} |sed '{ s/\(.*\/\)*//; s/\.d$//}')
35
36 # create env file
37 if ! [[ -f $ENVFILE ]]; then
38 [[ -d ${ENVFILE%/*} ]] || mkdir -p ${ENVFILE%/*}
39 echo "MDENT=0" >> $ENVFILE
40 echo "LASTLOG=0" >> $ENVFILE
41
42 is_up && IFACE_STATE=up || IFACE_STATE=down
43 echo "IFACE_STATE=$IFACE_STATE" >>$ENVFILE
44
45 if [[ $IFACE != lo ]]; then
46 # search for existing lib files end evaluate iface type by using the appropriate
47 # function from lib file
48 for lib in ${LIBDIR}/iftypes/*; do
49 [[ -f $lib ]] && (. $lib; iface_type 2>&-) || continue
50 if [[ -n $lastmatch ]]; then
51 if [[ ${lib##*/} = ${lastmatch##*/} ]]; then
52 mprint -s "Warning: $lib overlays $lastmatch"
53 else
54 mprint -s "Error: iface type $lib overlays $lastmatch"
55 fi
56 fi
57 lastmatch=$lib
58 done
59 fi
60
61 # if iface type isn't evaluated type "iface" is default
62 [[ -n $lastmatch ]] && IFACE_TYPE=${lastmatch##*/} || IFACE_TYPE=iface
63
64 # update env
65 echo "IFACE_TYPE=$IFACE_TYPE" >>$ENVFILE
66 fi
67
68 . $ENVFILE
69 t_mdent=$MDENT
70
71 # expand $IFUPDOWN_ENV
72 for var in $IFUPDOWN_ENV; do
73 eval $var
74 done
75
76 [[ $t_mdent -gt $MDENT ]] && MDENT=$t_mdent
77 main_pos_update
78 }
79
80 main_pos_update() {
81 [[ $MDENT = 0 ]] && STARTCOL= || \
82 STARTCOL="[$(( ${MDENT:-0} * $TAB))C"
83 sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
84 }
85
86 ### global helper functions ###
87
88 config() {
89 [[ $1 != 0 ]]
90 }
91
92 iface_exists() {
93 grep -q "${1:-$IFACE}:" /proc/net/dev
94 }
95
96 phys_exist() {
97 config $IFACE_LOGICAL && return
98 iface_exists && return
99 merr "No such device"
100 return 1
101 }
102
103 is_up() {
104 local iface=${1:-$IFACE}
105
106 iface_exists $iface || return 1
107 ip a|grep ${iface}:[[:space:]]|grep -q UP && return
108
109 ### ifupdown netlink bug workaround ###
110 # suppress "ip: RTNETLINK answers: File exists" message in the case of
111 # iface is down but an ip address is assigned
112 ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1
113 ### end workaround ###
114
115 return 1
116 }
117
118 ### functions used by main up/down functions ###
119 ### DON'T remove empty ones ###
120
121 main_exec_hookscripts() {
122 for hook in ${LIBDIR}/${1}/${2}; do
123 [[ -f $hook ]] || continue
124 . $hook
125 eval "${hook##*/}_$3" || [[ $? = 127 ]] || exit
126 done
127 }
128
129 main_bb_methods_override() {
130 [[ -r ${LIBDIR}/methods/$METHOD ]] || return
131 . ${LIBDIR}/methods/$METHOD
132 whence ${METHOD}_$1 2>&- 1>&- || return
133
134 # fake busybox ifupdown.c up or down code
135 case $1 in
136 up)
137 eval $IF_PRE_UP
138 eval ${METHOD}_$1 || exit
139 eval $IF_UP
140 # call up function from here because we leave
141 # run-parts after returning
142 main_up
143 ;;
144 down)
145 eval $IF_DOWN
146 eval ${METHOD}_$1 || exit
147 eval $IF_POST_DOWN
148 main_postdown
149 ;;
150 esac
151
152 RT_BB_NOEXEC=1
153 }
154
155 main_exec_pcode() {
156 [[ -d /tmp/ifupdown/pcode/$IFACE ]] || return
157 for dir in /tmp/ifupdown/pcode/${IFACE}/*; do
158 [[ -f ${dir}/$1 ]] || continue
159 . ${dir}/$1
160 done
161 }
162
163 prereq_preup() {
164 main_prereq_preup
165 }
166
167 main_prereq_preup() {
168 # workaround for the failsafe
169 # ip link set down dev eth0
170 # pingpong problem
171 [[ -f /tmp/.booting ]] && return
172
173 is_up || return 0
174 mup
175 mstate 1
176 minfo "Interface already up"
177 exit 1
178 }
179
180 prereq_up() {
181 main_prereq_up
182 }
183
184 main_prereq_up() {
185 is_up
186 mstate $? || exit
187 }
188
189 prereq_down() {
190 :
191 }
192
193 prereq_postdown() {
194 main_prereq_postdown
195 }
196
197 main_prereq_postdown() {
198 is_up
199 mstate $((!$?)) || exit
200 [[ $IFACE_STATE = down ]] && minfo "Interface already down"
201 return 0
202 }
203
204 if_preup() {
205 :
206 }
207
208 if_up() {
209 :
210 }
211
212 if_down() {
213 :
214 }
215
216 if_postdown() {
217 :
218 }
219
220 ### main up/down functions ###
221
222 main_preup() {
223 # print start message
224 mstart
225
226 # break if device is physical and not exist
227 phys_exist || exit
228
229 # check iface state and print state message
230 # break if iface is already up
231 prereq_preup || exit
232
233 # execute "pre busybox up" function
234 # sourced from appropriate iftype script
235 if_preup || fail_preup || exit
236
237 # print "going up" message
238 mup
239
240 # execute plugins
241 main_exec_hookscripts plugins '*' preup
242
243 # execute method preup function if present
244 main_exec_hookscripts methods $METHOD preup
245
246 # override busybox ifupdown.c method if function present
247 main_bb_methods_override up
248
249 # bypass execution of further busybox ifupdown.c code
250 config $RT_BB_NOEXEC || return 0
251 echo RT_BB_NOEXEC=1 >>$ENVFILE
252 builtin exit 1
253 }
254
255 main_up() {
256 # execute method postup function if present
257 main_exec_hookscripts methods $METHOD postup
258
259 # execute plugins
260 main_exec_hookscripts plugins '*' up
261
262 # check iface state and print state message
263 # break if iface isn't up
264 prereq_up || exit
265
266 # execute "post busybox up" function
267 # sourced from appropriate iftype script
268 if_up || fail_up || exit
269
270 # execute postcode if present
271 main_exec_pcode postup
272 }
273
274 main_down() {
275 # print stop message
276 mstop
277
278 # break if device is physical and not exist
279 phys_exist || exit
280
281 # nothing predefined
282 prereq_down || exit
283
284 # execute postcode if present
285 main_exec_pcode predown
286
287 # execute "pre busybox down" function
288 # sourced from appropriate iftype script
289 if_down || fail_down || exit
290
291 # print "going down" message
292 mdown
293
294 # execute plugins
295 main_exec_hookscripts plugins '*' down
296
297 # execute method predown function if present
298 main_exec_hookscripts methods $METHOD predown
299
300 # override busybox ifupdown.c method if userspace function present
301 main_bb_methods_override down
302
303 # bypass execution of further busybox ifupdown.c code
304 config $RT_BB_NOEXEC || return 0
305 echo RT_BB_NOEXEC=1 >>$ENVFILE
306 builtin exit 1
307 }
308
309 main_postdown() {
310 # execute method postdown function if present
311 main_exec_hookscripts methods $METHOD postdown
312
313 # execute plugins
314 main_exec_hookscripts plugins '*' postdown
315
316 # check iface state and print state message
317 # break if iface isn't down
318 prereq_postdown || exit
319
320 # execute "post busybox down" function
321 # sourced from appropriate iftype script
322 if_postdown || fail_postdown || exit
323
324 # pcode cleanup
325 rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
326 }
327
328 ### main entry point ###
329
330 FWIFUPDOWN_PRINTING_OFF=0
331 FWIFUPDOWN_SYSLOG_OFF=0
332 FWIFUPDOWN_FANCY=0
333 FWIFUPDOWN_DEBUG=0
334
335 . /etc/rc.conf
336
337 config $FWIFUPDOWN_DEBUG && set -x
338
339 RT_BB_NOEXEC=0
340 RT_ERR=0
341 readonly ENVFILE="/tmp/ifupdown/env"
342 readonly LIBDIR="/etc/network"
343 IFACE_TYPE="none"
344
345 alias exit="main_exit"
346 alias ifup="main_ifupdown ifup"
347 alias ifdown="main_ifupdown ifdown"
348
349 if [[ ! -f /etc/network/mfunctions.sh ]]; then
350 logger -t ifupdown "/etc/network/mfunctions.sh not found"
351 exit 1
352 fi
353 . /etc/network/mfunctions.sh
354
355 main_env_update
356
357 config $RT_ERR && builtin exit 1
358
359 if [[ $IFACE_TYPE != iface ]]; then
360 if [[ -f ${LIBDIR}/iftypes/$IFACE_TYPE ]]; then
361 . ${LIBDIR}/iftypes/$IFACE_TYPE
362 else
363 mprint -s "Error: libfile $IFACE_TYPE not found"
364 fi
365 fi
366
367 case $SUBMODE in
368 if-pre-up)
369 main_preup
370 ;;
371 if-up)
372 main_up
373 ;;
374 if-down)
375 main_down
376 ;;
377 if-post-down)
378 main_postdown
379 ;;
380 esac
381
382 exit 0
383
384 # vim:ts=4

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20