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

Last change on this file since ff38870 was ad56710, checked in by Christian Fischer <spaetzle@…>, 18 years ago
  • added back method manual to the valid methods
  • moved lladdr to the right place, this is cosmetic
  • removed /etc/init.d/ifupdown, nobody needs this
  • removed zero size file wl-broadcom
  • fixed fwifupdown Makefile changes from tg@

git-svn-id: svn://www.freewrt.org/trunk/freewrt@2958 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 || [ "$METHOD" = "manual" ] && return
144 mup
145}
146
147main_up() {
148 if ! config $RT_UP_IFUP_CHECK_OFF && [ "$METHOD" != "manual" ]
149 then
150 is_up
151 retval=$?
152 config $RT_UP_PRINTING_OFF || mstate $retval
153 [ x"$retval" != x"0" ] && exit 1
154 fi
155
156 if_up 2>&-
157 retval=$?
158
159 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
160 fail_up 2>&- || exit 1
161 }
162
163 if [ -d /tmp/ifupdown/pcode/${IFACE} ]
164 then
165 for dir in /tmp/ifupdown/pcode/${IFACE}/*
166 do
167 [ -f ${dir}/postup ] || continue
168 . ${dir}/postup
169 done
170 fi
171}
172
173main_down() {
174 config $RT_DOWN_PRINTING_OFF || mstop
175
176 if [ -d /tmp/ifupdown/pcode/${IFACE} ]
177 then
178 for dir in /tmp/ifupdown/pcode/${IFACE}/*
179 do
180 [ -f ${dir}/predown ] || continue
181 . ${dir}/predown
182 done
183 fi
184
185 if_down 2>&-
186 retval=$?
187
188 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
189 fail_down 2>&- || exit 1
190 }
191
192 config $RT_DOWN_PRINTING_OFF || [ "$METHOD" = "manual" ] && return
193 mdown
194}
195
196main_postdown() {
197 if ! config $RT_POSTDOWN_IFDOWN_CHECK_OFF && [ "$METHOD" != "manual" ]
198 then
199 if [ $IFACE_STATE = "down" ]
200 then
201 if ! config $RT_POSTDOWN_PRINTING_OFF
202 then
203 mdown
204 mstate 1
205 minfo "Interface already down"
206 fi
207 else
208 is_up && retval=1 || retval=0
209 config $RT_POSTDOWN_PRINTING_OFF || mstate $retval
210 [ "$retval" != "0" ] && exit 1
211 fi
212 fi
213
214 if_postdown 2>&-
215 retval=$?
216
217 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
218 fail_postdown 2>&- || exit 1
219 }
220
221 # pcode cleanup
222 rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
223}
224
225main_exec_dirhooks() {
226 for hook in ${LIBDIR}/${1}/${2}
227 do
228 [ -f $hook ] || continue
229 . $hook
230 eval $3 2>&-
231 retval=$?
232 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
233 return
234 done
235}
236
237main_exec_plugins() {
238 for plugin in ${LIBDIR}/plugins/*; do
239 [ -f $plugin ] || continue
240 . $plugin
241 eval "${plugin##*/}_${1}" 2>&-
242 retval=$?
243 [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
244 done
245}
246
247main_exec_inlinehooks() {
248 case $SUBMODE in
249 if-pre-up)
250 [ -n "$IF_PRE_UP" ] && eval $IF_PRE_UP
251 ;;
252 if-up)
253 [ -n "$IF_UP" ] && eval $IF_UP
254 ;;
255 if-down)
256 [ -n "$IF_DOWN" ] && eval $IF_DOWN
257 ;;
258 if-post-down)
259 [ -n "$IF_POST_DOWN" ] && eval $IF_POST_DOWN
260 ;;
261 esac
262}
263
264### main entry point ###
265
266FWIFUPDOWN_PRINTING_OFF=0
267FWIFUPDOWN_SYSLOG_OFF=0
268FWIFUPDOWN_FANCY=0
269FWIFUPDOWN_DEBUG=0
270
271. /etc/rc.conf
272
273config $FWIFUPDOWN_DEBUG && set -x
274
275RT_PREUP_PRINTING_OFF=0
276RT_UP_PRINTING_OFF=0
277RT_DOWN_PRINTING_OFF=0
278RT_POSTDOWN_PRINTING_OFF=0
279RT_PREUP_IFUP_CHECK_OFF=0
280RT_UP_IFUP_CHECK_OFF=0
281RT_POSTDOWN_IFDOWN_CHECK_OFF=0
282RT_BB_NOEXEC=0
283RT_ERR=0
284readonly ENVFILE="/tmp/ifupdown/env"
285readonly LIBDIR="/etc/network"
286IFACE_TYPE="none"
287
288alias exit="__exit"
289alias ifup="__ifupdown ifup"
290alias ifdown="__ifupdown ifdown"
291[ -z "$KSH_VERSION" ] || alias which='whence -p'
292
293if ! [ -f /etc/network/mfunctions.sh ]
294then
295 logger -t ifupdown "/etc/network/mfunctions.sh not found"
296 exit 1
297fi
298. /etc/network/mfunctions.sh
299
300main_env_update
301
302config $RT_ERR && built_in exit 1
303
304if [ x"$IFACE_TYPE" != x"iface" ]
305then
306 if [ -f "${LIBDIR}/iftypes/${IFACE_TYPE}" ]
307 then
308 . ${LIBDIR}/iftypes/$IFACE_TYPE
309 else
310 mprint -s "Error: libfile $IFACE_TYPE not found"
311 fi
312fi
313
314case $SUBMODE in
315 if-pre-up)
316 main_preup
317 main_exec_dirhooks methods $METHOD method_preup
318 main_exec_plugins preup
319
320 # bypass execution of further busybox ifupdown.c code
321 config $RT_BB_NOEXEC && built_in exit 1
322 ;;
323 if-up)
324 main_exec_plugins up
325 main_exec_dirhooks methods $METHOD method_up
326 main_up
327 ;;
328 if-down)
329 main_down
330 main_exec_dirhooks methods $METHOD method_down
331 main_exec_plugins down
332
333 # bypass execution of further busybox ifupdown.c code
334 config $RT_BB_NOEXEC && built_in exit 1
335 ;;
336 if-post-down)
337 main_exec_plugins postdown
338 main_exec_dirhooks methods $METHOD method_postdown
339 main_postdown
340 ;;
341esac
342
343exit 0
344
345# vim:ts=4
Note: See TracBrowser for help on using the repository browser.