English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Diff of /branches/common-nfo/package/fwifupdown/files/main.sh

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3426 by tg, Wed Jul 18 12:45:43 2007 UTC revision 3427 by tg, Wed Aug 8 11:08:41 2007 UTC
# Line 7  Line 7 
7  # Christian Fischer <spaetzle@freewrt.org>  # Christian Fischer <spaetzle@freewrt.org>
8  #  #
9    
10  built_in() {  main_ifupdown() {
         local what=$1  
         local alias=$(alias $what)  
         unalias $what  
         alias $(echo $alias|sed 's/'\''//g')  
         shift  
         $what $@  
 }  
   
 config() {  
         [ x"$1" != x"0" ]  
 }  
   
 __ifupdown() {  
11          local environ=$(cat $ENVFILE)          local environ=$(cat $ENVFILE)
12    
13          IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\          IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\
# Line 30  __ifupdown() { Line 17  __ifupdown() {
17                  busybox "$@"                  busybox "$@"
18    
19          retval=$?          retval=$?
20          echo $environ > $ENVFILE          grep -q RT_BB_NOEXEC=1 $ENVFILE && retval=0
21            echo $environ >$ENVFILE
22          return $retval          return $retval
23  }  }
24    
25  __exit() {  main_exit() {
26          [ x"$1" != x"0" ] && echo "RT_ERR=1" >> $ENVFILE          [[ $1 != 0 ]] && echo "RT_ERR=1" >>$ENVFILE
27          exit $1          exit $1
28  }  }
29    
 iface_exists() {  
         grep -q "${1:-$IFACE}:" /proc/net/dev  
 }  
   
 is_up() {  
         local iface=${1:-$IFACE}  
   
         if iface_exists $iface  
         then  
                 ip a|grep ${iface}:[[:space:]]|grep -q UP && return  
   
                 ### ifupdown netlink bug workaround ###  
                 # suppress "ip: RTNETLINK answers: File exists" message in the case of  
                 # iface is down but an ip address is assigned  
                 ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1  
                 ### end workaround ###  
         fi  
         return 1  
 }  
   
30  main_env_update() {  main_env_update() {
31          local t_mdent lastmatch=          local t_mdent lastmatch=
32    
# Line 66  main_env_update() { Line 34  main_env_update() {
34          SUBMODE=$(echo ${0%/*} |sed '{ s/\(.*\/\)*//; s/\.d$//}')          SUBMODE=$(echo ${0%/*} |sed '{ s/\(.*\/\)*//; s/\.d$//}')
35    
36          # create env file          # create env file
37          if ! [ -f "$ENVFILE" ]          if ! [[ -f $ENVFILE ]]; then
38          then                  [[ -d ${ENVFILE%/*} ]] || mkdir -p ${ENVFILE%/*}
                 [ -d ${ENVFILE%/*} ] || mkdir -p ${ENVFILE%/*}  
39                  echo "MDENT=0" >> $ENVFILE                  echo "MDENT=0" >> $ENVFILE
40                  echo "LASTLOG=0" >> $ENVFILE                  echo "LASTLOG=0" >> $ENVFILE
41    
42                  is_up && IFACE_STATE="up" || IFACE_STATE="down"                  is_up && IFACE_STATE=up || IFACE_STATE=down
43                  echo "IFACE_STATE=$IFACE_STATE" >> $ENVFILE                  echo "IFACE_STATE=$IFACE_STATE" >>$ENVFILE
44    
45                  if [ x"$IFACE" != x"lo" ]                  if [[ $IFACE != lo ]]; then
                 then  
46                          # search for existing lib files end evaluate iface type by using the appropriate                          # search for existing lib files end evaluate iface type by using the appropriate
47                          # function from lib file                          # function from lib file
48                          for lib in ${LIBDIR}/iftypes/*                          for lib in ${LIBDIR}/iftypes/*; do
49                          do                                  [[ -f $lib ]] && (. $lib; iface_type 2>&-) || continue
50                                  [ -f $lib ] && (. $lib; iface_type 2>&-) || continue                                  if [[ -n $lastmatch ]]; then
51                                  if [ -n "$lastmatch" ]                                          if [[ ${lib##*/} = ${lastmatch##*/} ]]; then
                                 then  
                                         if [ ${lib##*/} = ${lastmatch##*/} ]  
                                         then  
52                                                  mprint -s "Warning: $lib overlays $lastmatch"                                                  mprint -s "Warning: $lib overlays $lastmatch"
53                                          else                                          else
54                                                  mprint -s "Error: iface type $lib overlays $lastmatch"                                                  mprint -s "Error: iface type $lib overlays $lastmatch"
# Line 96  main_env_update() { Line 59  main_env_update() {
59                  fi                  fi
60    
61                  # if iface type isn't evaluated type "iface" is default                  # if iface type isn't evaluated type "iface" is default
62                  [ -n "$lastmatch" ] && IFACE_TYPE=${lastmatch##*/} || IFACE_TYPE="iface"                  [[ -n $lastmatch ]] && IFACE_TYPE=${lastmatch##*/} || IFACE_TYPE=iface
63    
64                  # update env                  # update env
65                  echo "IFACE_TYPE=${IFACE_TYPE}" >> $ENVFILE                  echo "IFACE_TYPE=$IFACE_TYPE" >>$ENVFILE
66          fi          fi
67    
68          . $ENVFILE          . $ENVFILE
# Line 110  main_env_update() { Line 73  main_env_update() {
73                  eval $var                  eval $var
74          done          done
75    
76          [ $t_mdent -gt $MDENT ] && MDENT=$t_mdent          [[ $t_mdent -gt $MDENT ]] && MDENT=$t_mdent
77          main_pos_update          main_pos_update
78  }  }
79    
80  main_pos_update() {  main_pos_update() {
81          [ x"$MDENT" = x"0" ] && STARTCOL= || STARTCOL="[$(expr ${MDENT:-0} \* ${TAB})C"          [[ $MDENT = 0 ]] && STARTCOL= || \
82                    STARTCOL="[$(( ${MDENT:-0} * $TAB))C"
83          sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE          sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
84  }  }
85    
86  main_preup() {  ### global helper functions ###
         config $RT_PREUP_PRINTING_OFF || mstart  
87    
88          if ! config $RT_PREUP_IFUP_CHECK_OFF && is_up  config() {
89          then          [[ $1 != 0 ]]
90                  if ! config $RT_PREUP_PRINTING_OFF  }
                 then  
                         mup  
                         mstate 1  
                         minfo "Interface already up"  
                 fi  
                 exit 1  
         fi  
91    
92          if_preup 2>&-  iface_exists() {
93          retval=$?          grep -q "${1:-$IFACE}:" /proc/net/dev
94    }
95    
96    phys_exist() {
97            config $IFACE_LOGICAL && return
98            iface_exists && return
99            merr "No such device"
100            return 1
101    }
102    
103    is_up() {
104            local iface=${1:-$IFACE}
105    
106            iface_exists $iface || return 1
107            ip a|grep ${iface}:[[:space:]]|grep -q UP && return
108    
109            ### ifupdown netlink bug workaround ###
110            # suppress "ip: RTNETLINK answers: File exists" message in the case of
111            # iface is down but an ip address is assigned
112            ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1
113            ### end workaround ###
114    
115          [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {          return 1
116                  fail_preup 2>&- || exit 1  }
         }  
117    
118          config $RT_PREUP_PRINTING_OFF && return  ### functions used by main up/down functions ###
119    ### DON'T remove empty ones ###
120    
121    main_exec_hookscripts() {
122            for hook in ${LIBDIR}/${1}/${2}; do
123                    [[ -f $hook ]] || continue
124                    . $hook
125                    eval "${hook##*/}_$3" || [[ $? = 127 ]] || exit
126            done
127    }
128    
129    main_bb_methods_override() {
130            [[ -r ${LIBDIR}/methods/$METHOD ]] || return
131            . ${LIBDIR}/methods/$METHOD
132            whence ${METHOD}_$1 2>&- 1>&- || return
133    
134            # fake busybox ifupdown.c up or down code
135            case $1 in
136            up)
137                    eval $IF_PRE_UP
138                    eval ${METHOD}_$1 || exit
139                    eval $IF_UP
140                    # call up function from here because we leave
141                    # run-parts after returning
142                    main_up
143                    ;;
144            down)
145                    eval $IF_DOWN
146                    eval ${METHOD}_$1 || exit
147                    eval $IF_POST_DOWN
148                    main_postdown
149                    ;;
150            esac
151    
152            RT_BB_NOEXEC=1
153    }
154    
155    main_exec_pcode() {
156            [[ -d /tmp/ifupdown/pcode/$IFACE ]] || return
157            for dir in /tmp/ifupdown/pcode/${IFACE}/*; do
158                    [[ -f ${dir}/$1 ]] || continue
159                    . ${dir}/$1
160            done
161    }
162    
163    prereq_preup() {
164            main_prereq_preup
165    }
166    
167    main_prereq_preup() {
168            # workaround for the failsafe
169            # ip link set down dev eth0
170            # pingpong problem
171            [[ -f /tmp/.booting ]] && return
172    
173            is_up || return 0
174          mup          mup
175            mstate 1
176            minfo "Interface already up"
177            exit 1
178  }  }
179    
180  main_up() {  prereq_up() {
181          [ x"$METHOD" = x"manual" ] && ip link set up dev $IFACE          main_prereq_up
182    }
183    
184          if ! config $RT_UP_IFUP_CHECK_OFF  main_prereq_up() {
185          then          is_up
186                  is_up          mstate $? || exit
187                  retval=$?  }
                 config $RT_UP_PRINTING_OFF || mstate $retval  
                 [ x"$retval" != x"0" ] && exit 1  
         fi  
188    
189          if_up 2>&-  prereq_down() {
190          retval=$?          :
191    }
192    
193          [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {  prereq_postdown() {
194                  fail_up 2>&- || exit 1          main_prereq_postdown
         }  
   
         if [ -d /tmp/ifupdown/pcode/${IFACE} ]  
         then  
                 for dir in /tmp/ifupdown/pcode/${IFACE}/*  
                 do  
                         [ -f ${dir}/postup ] || continue  
                         . ${dir}/postup  
                 done  
         fi  
195  }  }
196    
197  main_down() {  main_prereq_postdown() {
198          config $RT_DOWN_PRINTING_OFF || mstop          is_up
199            mstate $((!$?)) || exit
200            [[ $IFACE_STATE = down ]] && minfo "Interface already down"
201            return 0
202    }
203    
204          if [ -d /tmp/ifupdown/pcode/${IFACE} ]  if_preup() {
205          then          :
206                  for dir in /tmp/ifupdown/pcode/${IFACE}/*  }
                 do  
                         [ -f ${dir}/predown ] || continue  
                         . ${dir}/predown  
                 done  
         fi  
207    
208          if_down 2>&-  if_up() {
209          retval=$?          :
210    }
211    
212          [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {  if_down() {
213                  fail_down 2>&- || exit 1          :
214          }  }
215    
216          config $RT_DOWN_PRINTING_OFF  && return  if_postdown() {
217          mdown          :
218  }  }
219    
220  main_postdown() {  ### main up/down functions ###
         [ x"$METHOD" = x"manual" ] && {  
                 ip addr flush dev $IFACE  
                 ip link set down dev $IFACE  
         }  
   
         if ! config $RT_POSTDOWN_IFDOWN_CHECK_OFF  
         then  
                 if [ $IFACE_STATE = "down" ]  
                 then  
                         if ! config $RT_POSTDOWN_PRINTING_OFF  
                         then  
                                 mdown  
                                 mstate 1  
                                 minfo "Interface already down"  
                         fi  
                 else  
                         is_up && retval=1 || retval=0  
                         config $RT_POSTDOWN_PRINTING_OFF || mstate $retval  
                         [ "$retval" != "0" ] && exit 1  
                 fi  
         fi  
221    
222          if_postdown 2>&-  main_preup() {
223          retval=$?          # print start message
224            mstart
225    
226          [ x"$retval" != x"0" -a  x"$retval" != x"127" ] && {          # break if device is physical and not exist
227                  fail_postdown 2>&- || exit 1          phys_exist || exit
         }  
228    
229          # pcode cleanup          # check iface state and print state message
230          rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-          # break if iface is already up
231            prereq_preup || exit
232            
233            # execute "pre busybox up" function
234            # sourced from appropriate iftype script
235            if_preup || fail_preup || exit
236    
237            # print "going up" message
238            mup
239    
240            # execute plugins
241            main_exec_hookscripts plugins '*' preup
242    
243            # execute method preup function if present
244            main_exec_hookscripts methods $METHOD preup
245    
246            # override busybox ifupdown.c method if function present
247            main_bb_methods_override up
248    
249            # bypass execution of further busybox ifupdown.c code
250            config $RT_BB_NOEXEC || return 0
251            echo RT_BB_NOEXEC=1 >>$ENVFILE
252            builtin exit 1
253  }  }
254    
255  main_exec_dirhooks() {  main_up() {
256          for hook in ${LIBDIR}/${1}/${2}          # execute method postup function if present
257          do          main_exec_hookscripts methods $METHOD postup
258                  [ -f $hook ] || continue  
259                  . $hook          # execute plugins
260                  eval $3 2>&-          main_exec_hookscripts plugins '*' up
261                  retval=$?  
262                  [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1          # check iface state and print state message
263                  return          # break if iface isn't up
264          done          prereq_up || exit
265    
266            # execute "post busybox up" function
267            # sourced from appropriate iftype script
268            if_up || fail_up || exit
269    
270            # execute postcode if present
271            main_exec_pcode postup
272  }  }
273    
274  main_exec_plugins() {  main_down() {
275          for plugin in ${LIBDIR}/plugins/*; do          # print stop message
276                  [ -f $plugin ] || continue          mstop
277                  . $plugin  
278                  eval "${plugin##*/}_${1}" 2>&-          # break if device is physical and not exist
279                  retval=$?          phys_exist || exit
280                  [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1  
281          done          # nothing predefined
282            prereq_down || exit
283    
284            # execute postcode if present
285            main_exec_pcode predown
286    
287            # execute "pre busybox down" function
288            # sourced from appropriate iftype script
289            if_down || fail_down || exit
290    
291            # print "going down" message
292            mdown
293    
294            # execute plugins
295            main_exec_hookscripts plugins '*' down
296    
297            # execute method predown function if present
298            main_exec_hookscripts methods $METHOD predown
299    
300            # override busybox ifupdown.c method if userspace function present
301            main_bb_methods_override down
302    
303            # bypass execution of further busybox ifupdown.c code
304            config $RT_BB_NOEXEC || return 0
305            echo RT_BB_NOEXEC=1 >>$ENVFILE
306            builtin exit 1
307  }  }
308    
309  main_exec_inlinehooks() {  main_postdown() {
310          case $SUBMODE in          # execute method postdown function if present
311                  if-pre-up)          main_exec_hookscripts methods $METHOD postdown
312                          [ -n "$IF_PRE_UP" ] && eval $IF_PRE_UP  
313                  ;;          # execute plugins
314                  if-up)          main_exec_hookscripts plugins '*' postdown
315                          [ -n "$IF_UP" ] && eval $IF_UP  
316                  ;;          # check iface state and print state message
317                  if-down)          # break if iface isn't down
318                          [ -n "$IF_DOWN" ] && eval $IF_DOWN          prereq_postdown || exit
319                  ;;  
320                  if-post-down)          # execute "post busybox down" function
321                          [ -n "$IF_POST_DOWN" ] && eval $IF_POST_DOWN          # sourced from appropriate iftype script
322                  ;;          if_postdown || fail_postdown || exit
323          esac  
324            # pcode cleanup
325            rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
326  }  }
327    
328  ### main entry point ###  ### main entry point ###
# Line 279  FWIFUPDOWN_DEBUG=0 Line 336  FWIFUPDOWN_DEBUG=0
336    
337  config $FWIFUPDOWN_DEBUG && set -x  config $FWIFUPDOWN_DEBUG && set -x
338    
 RT_PREUP_PRINTING_OFF=0  
 RT_UP_PRINTING_OFF=0  
 RT_DOWN_PRINTING_OFF=0  
 RT_POSTDOWN_PRINTING_OFF=0  
 RT_PREUP_IFUP_CHECK_OFF=0  
 RT_UP_IFUP_CHECK_OFF=0  
 RT_POSTDOWN_IFDOWN_CHECK_OFF=0  
339  RT_BB_NOEXEC=0  RT_BB_NOEXEC=0
340  RT_ERR=0  RT_ERR=0
341  readonly ENVFILE="/tmp/ifupdown/env"  readonly ENVFILE="/tmp/ifupdown/env"
342  readonly LIBDIR="/etc/network"  readonly LIBDIR="/etc/network"
343  IFACE_TYPE="none"  IFACE_TYPE="none"
344    
345  alias exit="__exit"  alias exit="main_exit"
346  alias ifup="__ifupdown ifup"  alias ifup="main_ifupdown ifup"
347  alias ifdown="__ifupdown ifdown"  alias ifdown="main_ifupdown ifdown"
 [ -z "$KSH_VERSION" ] || alias which='whence -p'  
348    
349  if ! [ -f /etc/network/mfunctions.sh ]  if [[ ! -f /etc/network/mfunctions.sh ]]; then
 then  
350          logger -t ifupdown "/etc/network/mfunctions.sh not found"          logger -t ifupdown "/etc/network/mfunctions.sh not found"
351          exit 1          exit 1
352  fi  fi
# Line 306  fi Line 354  fi
354    
355  main_env_update  main_env_update
356    
357  config $RT_ERR && built_in exit 1  config $RT_ERR && builtin exit 1
358    
359  if [ x"$IFACE_TYPE" != x"iface" ]  if [[ $IFACE_TYPE != iface ]]; then
360  then          if [[ -f ${LIBDIR}/iftypes/$IFACE_TYPE ]]; then
         if [ -f "${LIBDIR}/iftypes/${IFACE_TYPE}" ]  
         then  
361                  . ${LIBDIR}/iftypes/$IFACE_TYPE                  . ${LIBDIR}/iftypes/$IFACE_TYPE
362          else          else
363                  mprint -s "Error: libfile $IFACE_TYPE not found"                  mprint -s "Error: libfile $IFACE_TYPE not found"
# Line 319  then Line 365  then
365  fi  fi
366    
367  case $SUBMODE in  case $SUBMODE in
368          if-pre-up)  if-pre-up)
369                  main_preup          main_preup
                 main_exec_dirhooks methods $METHOD method_preup  
                 main_exec_plugins preup  
   
                 # bypass execution of further busybox ifupdown.c code  
                 config $RT_BB_NOEXEC && built_in exit 1  
370          ;;          ;;
371          if-up)  if-up)
372                  main_exec_plugins up          main_up
                 main_exec_dirhooks methods $METHOD method_up  
                 main_up  
373          ;;          ;;
374          if-down)  if-down)
375                  main_down          main_down
                 main_exec_dirhooks methods $METHOD method_down  
                 main_exec_plugins down  
   
                 # bypass execution of further busybox ifupdown.c code  
                 config $RT_BB_NOEXEC && built_in exit 1  
376          ;;          ;;
377          if-post-down)  if-post-down)
378                  main_exec_plugins postdown          main_postdown
                 main_exec_dirhooks methods $METHOD method_postdown  
                 main_postdown  
379          ;;          ;;
380  esac  esac
381    

Legend:
Removed from v.3426  
changed lines
  Added in v.3427

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20