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

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

changed some more config variables

git-svn-id: svn://www.freewrt.org/trunk/freewrt@2437 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 if [ "$IFACE" != "lo" ]
78 then
79 # search for existing lib files end evaluate iface type by using the appropriate
80 # function from lib file
81 for lib in ${LIBDIR}/* ${LIBDIR_OVERLAY}/*
82 do
83 if [ -f $lib ]
84 then
85 if (. $lib; iface_type 2>&-)
86 then
87 if [ "$lastmatch" != "" ]
88 then
89 if [ "$(basename $lib)" = "$(basename $lastmatch)" ]
90 then
91 logger "Warning: $lib overlays $lastmatch"
92 else
93 logger "Error: iface type $lib overlays $lastmatch"
94 fi
95 fi
96 lastmatch=$lib
97 fi
98 fi
99 done
100 fi
101
102 if [ "$lastmatch" != "" ]
103 then
104 IFACE_TYPE=$(basename $lastmatch)
105 else
106 # if iface type isn't evaluated type "iface" is default
107 IFACE_TYPE="iface"
108 fi
109
110 # update env
111 echo "IFACE_TYPE=${IFACE_TYPE}" >> $ENVFILE
112 fi
113
114 . $ENVFILE
115 # expand $IFUPDOWN_ENV
116 for var in $IFUPDOWN_ENV; do
117 eval $var
118 done
119 pos_update
120}
121
122pos_update() {
123 if [ "${MDENT:-0}" = "0" ]
124 then
125 STARTCOL=""
126 # MPREFIX=$MP_GOOD
127 else
128 STARTCOL="\x1B[$(expr ${MDENT:-0} \* ${TAB})C"
129 # MPREFIX="\x1B[3C"
130 fi
131 sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
132}
133
134
135
136
137### main entry point ###
138
139CFG_PRINTING_OFF=0
140CFG_SYSLOG_OFF=0
141CFG_BUSYBOX_COMPAT=0
142CFG_PREUP_PRINTING_OFF=0
143CFG_UP_PRINTING_OFF=0
144CFG_DOWN_PRINTING_OFF=0
145CFG_POSTDOWN_PRINTING_OFF=0
146CFG_IFUP_CHECK_OFF=0
147CFG_IFDOWN_CHECK_OFF=0
148
149if [ -f /etc/conf.d/ifupdown ]; then
150 . /etc/conf.d/ifupdown
151 [ "$CFG_BUSYBOX_COMPAT" = "1" ] && exit 1
152else
153 exit 1
154fi
155
156ENVFILE="/tmp/ifupdown/env"
157LIBDIR="/opt/ifupdown/lib"
158LIBDIR_OVERLAY="/etc/network/lib"
159IFACE_TYPE="none"
160
161alias exit="__exit"
162alias ifup="__ifupdown ifup"
163alias ifdown="__ifupdown ifdown"
164
165. /opt/ifupdown/bin/mfunctions.sh
166
167env_update
168
169if [ "$IFACE_TYPE" != "iface" ]
170then
171 if [ -f "${LIBDIR_OVERLAY}/$IFACE_TYPE" ]
172 then
173 . ${LIBDIR_OVERLAY}/$IFACE_TYPE
174 elif [ -f "${LIBDIR}/$IFACE_TYPE" ]
175 then
176 . ${LIBDIR}/$IFACE_TYPE
177 else
178 logger "Error file not found and abort"
179 fi
180fi
181
182[ "${H_ERR:=""}" = "1" ] && builtin exit 1
183
184case $SUBMODE in
185 if-pre-up)
186 config $CFG_PREUP_PRINTING_OFF || mstart
187
188 if ! config $CFG_IFUP_CHECK_OFF && is_up
189 then
190 if ! config $CFG_PREUP_PRINTING_OFF
191 then
192 mup
193 mstate 1
194 minfo "Interface already up"
195 fi
196 exit 1
197 fi
198
199 if_preup 2>&-
200 retval=$?
201 if [ "$retval" != "0" ]
202 then
203 [ $retval != "127" ] && exit 1
204 fi
205
206 if ! config $CFG_PREUP_PRINTING_OFF
207 then
208 if [ "$METHOD" = "manual" ]
209 then
210 minfo "Can not bringing up ${IFACE}, method manual is set"
211 else
212 mup
213 fi
214 fi
215 ;;
216 if-up)
217 if ! config $CFG_UP_PRINTING_OFF
218 then
219 is_up
220 mstate $?
221 fi
222
223 if_up 2>&-
224 retval=$?
225 if [ "$retval" != "0" ]
226 then
227 [ $retval != "127" ] && exit 1
228 fi
229 if [ -f /tmp/ifupdown/pcode/${IFACE}/postup ]
230 then
231 . /tmp/ifupdown/pcode/${IFACE}/postup
232 #rm /tmp/ifupdown/pcode/${IFACE}/postup
233 fi
234 ;;
235 if-down)
236 config $CFG_DOWN_PRINTING_OFF || mstop
237
238 if ! config $CFG_IFDOWN_CHECK_OFF && ! is_up
239 then
240 if ! config $CFG_DOWN_PRINTING_OFF
241 then
242 mdown
243 mstate 1
244 minfo "Can not bringing down, interface $IFACE is down."
245 fi
246 exit 1
247 fi
248
249 if [ -f /tmp/ifupdown/pcode/${IFACE}/predown ]
250 then
251 . /tmp/ifupdown/pcode/${IFACE}/predown
252 #rm /tmp/ifupdown/pcode/${IFACE}/predown
253 fi
254
255 if_down 2>&-
256 retval=$?
257 if [ "$retval" != "0" ]
258 then
259 [ $retval != "127" ] && exit 1
260 fi
261
262 if ! config $CFG_DOWN_PRINTING_OFF
263 then
264 if [ "$METHOD" = "manual" ]
265 then
266 minfo "Can not bringing down ${IFACE}, method manual is set"
267 else
268 mdown
269 fi
270 fi
271 ;;
272 if-post-down)
273 if ! config $CFG_POSTDOWN_PRINTING_OFF
274 then
275 ! is_up
276 mstate $?
277 fi
278
279 if_postdown 2>&-
280 retval=$?
281 if [ "$retval" != "0" ]
282 then
283 [ $retval != "127" ] && exit 1
284 fi
285
286 ;;
287esac
288
289return 0
290
291# vim:ts=4
Note: See TracBrowser for help on using the repository browser.