source: freewrt/package/fwifupdown/files/main.sh@ f16a763

Last change on this file since f16a763 was aa2bc52f, checked in by Christian Fischer <spaetzle@…>, 18 years ago
  • updated wl-broadcom script to work with latest wl this is untested but the old one doesn't work and i have no time to test next days
  • removed output noise from ifup, used mprint instead of echo
  • updated manual stuff, from now manual doesn't configure iface but sets it up or down
  • removed bridge shutdown if bridge init fails not to 100 percent complete initialized bridge is better than having no network, the normal user doesn't have serial access

git-svn-id: svn://www.freewrt.org/trunk/freewrt@3057 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100644
File size: 6.7 KB
Line 
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
10built_in() {
11 local what=$1
12 local alias=$(alias $what)
13 unalias $what
14 alias $(echo $alias|sed 's/'\''//g')
15 shift
16 $what $@
17}
18
19config() {
20 [ x"$1" != x"0" ]
21}
22
23__ifupdown() {
24 local environ=$(cat $ENVFILE)
25
26 IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\
27 echo " PARENT_IFACE_TYPE=$IFACE_TYPE"; \
28 echo " PARENT_IFACE=$IFACE"; \
29 echo " MDENT=$MDENT") \
30 busybox "$@"
31
32 retval=$?
33 echo $environ > $ENVFILE
34 return $retval
35}
36
37__exit() {
38 [ x"$1" != x"0" ] && echo "RT_ERR=1" >> $ENVFILE
39 exit $1
40}
41
42iface_exists() {
43 grep -q "${1:-$IFACE}:" /proc/net/dev
44}
45
46is_up() {
47 local iface=${1:-$IFACE}
48
49 if iface_exists $iface
50 then
51 ip a|grep ${iface}:[[:space:]]|grep -q UP && return
52
53 ### ifupdown netlink bug workaround ###
54 # suppress "ip: RTNETLINK answers: File exists" message in the case of
55 # iface is down but an ip address is assigned
56 ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1
57 ### end workaround ###
58 fi
59 return 1
60}
61
62main_env_update() {
63 local t_mdent lastmatch=
64
65 # get submode from calling hook script
66 SUBMODE=$(echo ${0%/*} |sed '{ s/\(.*\/\)*//; s/\.d$//}')
67
68 # create env file
69 if ! [ -f "$ENVFILE" ]
70 then
71 [ -d ${ENVFILE%/*} ] || mkdir -p ${ENVFILE%/*}
72 echo "MDENT=0" >> $ENVFILE
73 echo "LASTLOG=0" >> $ENVFILE
74
75 is_up && IFACE_STATE="up" || IFACE_STATE="down"
76 echo "IFACE_STATE=$IFACE_STATE" >> $ENVFILE
77
78 if [ x"$IFACE" != x"lo" ]
79 then
80 # search for existing lib files end evaluate iface type by using the appropriate
81 # function from lib file
82 for lib in ${LIBDIR}/iftypes/*
83 do
84 [ -f $lib ] && (. $lib; iface_type 2>&-) || continue
85 if [ -n "$lastmatch" ]
86 then
87 if [ ${lib##*/} = ${lastmatch##*/} ]
88 then
89 mprint -s "Warning: $lib overlays $lastmatch"
90 else
91 mprint -s "Error: iface type $lib overlays $lastmatch"
92 fi
93 fi
94 lastmatch=$lib
95 done
96 fi
97
98 # if iface type isn't evaluated type "iface" is default
99 [ -n "$lastmatch" ] && IFACE_TYPE=${lastmatch##*/} || IFACE_TYPE="iface"
100
101 # update env
102 echo "IFACE_TYPE=${IFACE_TYPE}" >> $ENVFILE
103 fi
104
105 . $ENVFILE
106 t_mdent=$MDENT
107
108 # expand $IFUPDOWN_ENV
109 for var in $IFUPDOWN_ENV; do
110 eval $var
111 done
112
113 [ $t_mdent -gt $MDENT ] && MDENT=$t_mdent
114 main_pos_update
115}
116
117main_pos_update() {
118 [ x"$MDENT" = x"0" ] && STARTCOL= || STARTCOL="[$(expr ${MDENT:-0} \* ${TAB})C"
119 sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
120}
121
122main_preup() {
123 config $RT_PREUP_PRINTING_OFF || mstart
124
125 if ! config $RT_PREUP_IFUP_CHECK_OFF && is_up
126 then
127 if ! config $RT_PREUP_PRINTING_OFF
128 then
129 mup
130 mstate 1
131 minfo "Interface already up"
132 fi
133 exit 1
134 fi
135
136 if_preup 2>&-
137 retval=$?
138
139 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
140 fail_preup 2>&- || exit 1
141 }
142
143 config $RT_PREUP_PRINTING_OFF && return
144 mup
145}
146
147main_up() {
148 [ x"$METHOD" = x"manual" ] && ip link set up dev $IFACE
149
150 if ! config $RT_UP_IFUP_CHECK_OFF
151 then
152 is_up
153 retval=$?
154 config $RT_UP_PRINTING_OFF || mstate $retval
155 [ x"$retval" != x"0" ] && exit 1
156 fi
157
158 if_up 2>&-
159 retval=$?
160
161 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
162 fail_up 2>&- || exit 1
163 }
164
165 if [ -d /tmp/ifupdown/pcode/${IFACE} ]
166 then
167 for dir in /tmp/ifupdown/pcode/${IFACE}/*
168 do
169 [ -f ${dir}/postup ] || continue
170 . ${dir}/postup
171 done
172 fi
173}
174
175main_down() {
176 config $RT_DOWN_PRINTING_OFF || mstop
177
178 if [ -d /tmp/ifupdown/pcode/${IFACE} ]
179 then
180 for dir in /tmp/ifupdown/pcode/${IFACE}/*
181 do
182 [ -f ${dir}/predown ] || continue
183 . ${dir}/predown
184 done
185 fi
186
187 if_down 2>&-
188 retval=$?
189
190 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
191 fail_down 2>&- || exit 1
192 }
193
194 config $RT_DOWN_PRINTING_OFF && return
195 mdown
196}
197
198main_postdown() {
199 [ x"$METHOD" = x"manual" ] && {
200 ip addr flush dev %ifac
201 ip link set down dev $IFACE
202 }
203
204 if ! config $RT_POSTDOWN_IFDOWN_CHECK_OFF
205 then
206 if [ $IFACE_STATE = "down" ]
207 then
208 if ! config $RT_POSTDOWN_PRINTING_OFF
209 then
210 mdown
211 mstate 1
212 minfo "Interface already down"
213 fi
214 else
215 is_up && retval=1 || retval=0
216 config $RT_POSTDOWN_PRINTING_OFF || mstate $retval
217 [ "$retval" != "0" ] && exit 1
218 fi
219 fi
220
221 if_postdown 2>&-
222 retval=$?
223
224 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
225 fail_postdown 2>&- || exit 1
226 }
227
228 # pcode cleanup
229 rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
230}
231
232main_exec_dirhooks() {
233 for hook in ${LIBDIR}/${1}/${2}
234 do
235 [ -f $hook ] || continue
236 . $hook
237 eval $3 2>&-
238 retval=$?
239 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
240 return
241 done
242}
243
244main_exec_plugins() {
245 for plugin in ${LIBDIR}/plugins/*; do
246 [ -f $plugin ] || continue
247 . $plugin
248 eval "${plugin##*/}_${1}" 2>&-
249 retval=$?
250 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
251 done
252}
253
254main_exec_inlinehooks() {
255 case $SUBMODE in
256 if-pre-up)
257 [ -n "$IF_PRE_UP" ] && eval $IF_PRE_UP
258 ;;
259 if-up)
260 [ -n "$IF_UP" ] && eval $IF_UP
261 ;;
262 if-down)
263 [ -n "$IF_DOWN" ] && eval $IF_DOWN
264 ;;
265 if-post-down)
266 [ -n "$IF_POST_DOWN" ] && eval $IF_POST_DOWN
267 ;;
268 esac
269}
270
271### main entry point ###
272
273FWIFUPDOWN_PRINTING_OFF=0
274FWIFUPDOWN_SYSLOG_OFF=0
275FWIFUPDOWN_FANCY=0
276FWIFUPDOWN_DEBUG=0
277
278. /etc/rc.conf
279
280config $FWIFUPDOWN_DEBUG && set -x
281
282RT_PREUP_PRINTING_OFF=0
283RT_UP_PRINTING_OFF=0
284RT_DOWN_PRINTING_OFF=0
285RT_POSTDOWN_PRINTING_OFF=0
286RT_PREUP_IFUP_CHECK_OFF=0
287RT_UP_IFUP_CHECK_OFF=0
288RT_POSTDOWN_IFDOWN_CHECK_OFF=0
289RT_BB_NOEXEC=0
290RT_ERR=0
291readonly ENVFILE="/tmp/ifupdown/env"
292readonly LIBDIR="/etc/network"
293IFACE_TYPE="none"
294
295alias exit="__exit"
296alias ifup="__ifupdown ifup"
297alias ifdown="__ifupdown ifdown"
298[ -z "$KSH_VERSION" ] || alias which='whence -p'
299
300if ! [ -f /etc/network/mfunctions.sh ]
301then
302 logger -t ifupdown "/etc/network/mfunctions.sh not found"
303 exit 1
304fi
305. /etc/network/mfunctions.sh
306
307main_env_update
308
309config $RT_ERR && built_in exit 1
310
311if [ x"$IFACE_TYPE" != x"iface" ]
312then
313 if [ -f "${LIBDIR}/iftypes/${IFACE_TYPE}" ]
314 then
315 . ${LIBDIR}/iftypes/$IFACE_TYPE
316 else
317 mprint -s "Error: libfile $IFACE_TYPE not found"
318 fi
319fi
320
321case $SUBMODE in
322 if-pre-up)
323 main_preup
324 main_exec_dirhooks methods $METHOD method_preup
325 main_exec_plugins preup
326
327 # bypass execution of further busybox ifupdown.c code
328 config $RT_BB_NOEXEC && built_in exit 1
329 ;;
330 if-up)
331 main_exec_plugins up
332 main_exec_dirhooks methods $METHOD method_up
333 main_up
334 ;;
335 if-down)
336 main_down
337 main_exec_dirhooks methods $METHOD method_down
338 main_exec_plugins down
339
340 # bypass execution of further busybox ifupdown.c code
341 config $RT_BB_NOEXEC && built_in exit 1
342 ;;
343 if-post-down)
344 main_exec_plugins postdown
345 main_exec_dirhooks methods $METHOD method_postdown
346 main_postdown
347 ;;
348esac
349
350exit 0
351
352# vim:ts=4
Note: See TracBrowser for help on using the repository browser.