source: freewrt/package/fwifupdown/files/main.sh@ 655dec8

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

some fixes

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

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