source: freewrt/package/fwifupdown/files/main.sh@ 3517c1f

Last change on this file since 3517c1f was 3517c1f, checked in by Christian Fischer <spaetzle@…>, 19 years ago

some config variables changes, removed some unnecessary code

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

  • Property mode set to 100755
File size: 5.2 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
10set -u
11
12builtin() {
13 local builtin=$1
14 local alias=$(alias $builtin)
15 unalias $builtin
16 alias $(echo $alias|sed 's/'\''//g')
17 shift
18 $builtin $@
19}
20
21config() {
22 [ "$1" != "0" ]
23}
24__ifupdown() {
25 local environ=$(cat $ENVFILE)
26
27 IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\
28 echo "PARENT_IFACE_TYPE=$IFACE_TYPE"; \
29 echo "PARENT_IFACE=$IFACE"; \
30 echo " MDENT=$MDENT"; \
31 echo -e " MOFFSET=$MOFFSET") \
32 busybox $@
33
34 local retval=$?
35 echo $environ > $ENVFILE
36 return $retval
37}
38
39__exit() {
40 [ "$1" != "0" ] && echo "H_ERR=1" >> $ENVFILE
41 exit $1
42}
43
44iface_exists() {
45 grep -q "${1:-$IFACE}:" /proc/net/dev
46}
47
48is_up() {
49 local iface=${1:-$IFACE}
50
51 if iface_exists $iface
52 then
53 ip a|grep ${iface}:[[:space:]]|grep -q UP && return
54
55 ### ifupdown netlink bug workaround ###
56 # suppress "ip: RTNETLINK answers: File exists" message in the case of
57 # iface is down but an ip address is assigned
58 ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1
59 ### end workaround ###
60 fi
61 return 1
62}
63
64env_update() {
65 local lastmatch=""
66
67 # get submode from calling hook script
68 SUBMODE=$(dirname $0|sed '{ s/\(.*\/\)*//; s/\.d$//}')
69
70 # create env file
71 if ! [ -f "$ENVFILE" ]
72 then
73 [ -d $(dirname $ENVFILE) ] || mkdir -p $(dirname $ENVFILE)
74 echo "MDENT=0" >> $ENVFILE
75 echo "LASTLOG=0" >> $ENVFILE
76
77 # search for existing lib files end evaluate iface type by using the appropriate
78 # function from lib file
79 for lib in ${LIBDIR}/* ${LIBDIR_OVERLAY}/*
80 do
81 if [ -f $lib ]
82 then
83 if (. $lib; iface_type 2>&-)
84 then
85 if [ "$lastmatch" != "" ]
86 then
87 if [ "$(basename $lib)" = "$(basename $lastmatch)" ]
88 then
89 logger "Warning: $lib overlays $lastmatch"
90 else
91 logger "Error: iface type $lib overlays $lastmatch"
92 fi
93 fi
94 lastmatch=$lib
95 fi
96 fi
97 done
98
99 if [ "$lastmatch" != "" ]
100 then
101 IFACE_TYPE=$(basename $lastmatch)
102 else
103 # if iface type isn't evaluated type "iface" is default
104 IFACE_TYPE="iface"
105 fi
106
107 # update env
108 echo "IFACE_TYPE=${IFACE_TYPE}" >> $ENVFILE
109 fi
110
111 . $ENVFILE
112 # expand $IFUPDOWN_ENV
113 for var in $IFUPDOWN_ENV; do
114 eval $var
115 done
116 pos_update
117}
118
119pos_update() {
120 if [ "${MDENT:-0}" = "0" ]
121 then
122 STARTCOL=""
123 # MPREFIX=$MP_GOOD
124 else
125 STARTCOL="\x1B[$(expr ${MDENT:-0} \* ${TAB})C"
126 # MPREFIX="\x1B[3C"
127 fi
128 sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
129}
130
131
132
133
134### main entry point ###
135
136CFG_PRINTING_OFF=0
137CFG_SYSLOG_OFF=0
138CFG_BUSYBOX_COMPAT=0
139CFG_PREUP_PRINTING_OFF=0
140CFG_UP_PRINTING_OFF=0
141CFG_DOWN_PRINTING_OFF=0
142CFG_POSTDOWN_PRINTING_OFF=0
143CFG_PREUP_IFUP_CHECK_OFF=0
144CFG_DOWN_IFUP_CHECK_OFF=0
145
146if [ -f /etc/conf.d/ifupdown ]; then
147 . /etc/conf.d/ifupdown
148 [ "$CFG_BUSYBOX_COMPAT" = "1" ] && exit 1
149else
150 exit 1
151fi
152
153ENVFILE="/tmp/ifupdown/env"
154LIBDIR="/opt/ifupdown/lib"
155LIBDIR_OVERLAY="/etc/network/lib"
156IFACE_TYPE="none"
157
158alias exit="__exit"
159alias ifup="__ifupdown ifup"
160alias ifdown="__ifupdown ifdown"
161
162. /opt/ifupdown/bin/mfunctions.sh
163
164env_update
165
166if [ "IFACE_TYPE" != "iface" ]
167then
168 if [ -f "${LIBDIR_OVERLAY}/$IFACE_TYPE" ]
169 then
170 . ${LIBDIR_OVERLAY}/$IFACE_TYPE
171 elif [ -f "${LIBDIR}/$IFACE_TYPE" ]
172 then
173 . ${LIBDIR}/$IFACE_TYPE
174 else
175 logger "Error file not found and abort"
176 fi
177fi
178
179[ "${H_ERR:=""}" = "1" ] && builtin exit 1
180
181case $SUBMODE in
182 if-pre-up)
183 config $CFG_PREUP_PRINTING_OFF || mstart
184
185 if ! config $CFG_PREUP_IFUP_CHECK_OFF && is_up
186 then
187 if ! config $CFG_PREUP_PRINTING_OFF
188 then
189 mup
190 mstate 1
191 minfo "Interface already up, run ifdown ${IFACE} first."
192 fi
193 exit 1
194 fi
195
196 if_preup 2>&-
197 retval=$?
198 if [ "$retval" != "0" ]
199 then
200 [ $retval != "127" ] && exit 1
201 fi
202
203 if ! config $CFG_PREUP_PRINTING_OFF
204 then
205 if [ "$METHOD" = "manual" ]
206 then
207 minfo "Can not bringing up ${IFACE}, method manual is set"
208 else
209 mup
210 fi
211 fi
212 ;;
213 if-up)
214 if ! config $CFG_UP_PRINTING_OFF
215 then
216 is_up
217 mstate $?
218 fi
219
220 if_up 2>&-
221 retval=$?
222 if [ "$retval" != "0" ]
223 then
224 [ $retval != "127" ] && exit 1
225 fi
226 if [ -f /tmp/ifupdown/pcode/${IFACE}/postup ]
227 then
228 . /tmp/ifupdown/pcode/${IFACE}/postup
229 #rm /tmp/ifupdown/pcode/${IFACE}/postup
230 fi
231 ;;
232 if-down)
233 config $CFG_DOWN_PRINTING_OFF || mstop
234
235 if ! config $CFG_DOWN_IFUP_CHECK_OFF && ! is_up
236 then
237 if ! config $CFG_DOWN_PRINTING_OFF
238 then
239 mdown
240 mstate 1
241 minfo "Can not bringing down, interface $IFACE is down."
242 fi
243 exit 1
244 fi
245
246 if [ -f /tmp/ifupdown/pcode/${IFACE}/predown ]
247 then
248 . /tmp/ifupdown/pcode/${IFACE}/predown
249 #rm /tmp/ifupdown/pcode/${IFACE}/predown
250 fi
251
252 if_down 2>&-
253 retval=$?
254 if [ "$retval" != "0" ]
255 then
256 [ $retval != "127" ] && exit 1
257 fi
258
259 if ! config $CFG_DOWN_PRINTING_OFF
260 then
261 if [ "$METHOD" = "manual" ]
262 then
263 minfo "Can not bringing down ${IFACE}, method manual is set"
264 else
265 mdown
266 fi
267 fi
268 ;;
269 if-post-down)
270 if ! config $CFG_POSTDOWN_PRINTING_OFF
271 then
272 ! is_up
273 mstate $?
274 fi
275
276 if_postdown 2>&-
277 retval=$?
278 if [ "$retval" != "0" ]
279 then
280 [ $retval != "127" ] && exit 1
281 fi
282
283 ;;
284esac
285
286return 0
287
288# vim:ts=4
Note: See TracBrowser for help on using the repository browser.