Changeset 8414c1b in freewrt for package/fwifupdown/files/main.sh


Ignore:
Timestamp:
Apr 29, 2007, 8:56:17 PM (19 years ago)
Author:
Christian Fischer <spaetzle@…>
Children:
898d1f8
Parents:
8b73212
Message:

tag: fwifupdown_0.2-1

  • lot of fixes and code cleanup
  • nas startup und shutdown fixes
  • dhcp support added
  • documentation update will follow as soon as possible

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • package/fwifupdown/files/main.sh

    r8b73212 r8414c1b  
    2626
    2727        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") \
     28                echo " PARENT_IFACE_TYPE=$IFACE_TYPE"; \
     29                echo " PARENT_IFACE=$IFACE"; \
     30                echo " MDENT=$MDENT") \
    3231                busybox $@
    3332
    34         local retval=$?
     33        retval=$?
    3534        echo $environ > $ENVFILE
    3635        return $retval
     
    6261}
    6362
    64 env_update() {
    65         local lastmatch=""
     63main_env_update() {
     64        local t_mdent lastmatch=""
    6665
    6766        # get submode from calling hook script
     
    7473                echo "MDENT=0" >> $ENVFILE
    7574                echo "LASTLOG=0" >> $ENVFILE
     75
     76                if is_up
     77                then
     78                        IFACE_STATE="up"
     79                else
     80                        IFACE_STATE="down"
     81                fi
     82                echo "IFACE_STATE=$IFACE_STATE" >> $ENVFILE
    7683
    7784                if [ "$IFACE" != "lo" ]
     
    8996                                                        if [ "$(basename $lib)" = "$(basename $lastmatch)" ]
    9097                                                        then
    91                                                                 logger "Warning: $lib overlays $lastmatch"
     98                                                                mprint -s "Warning: $lib overlays $lastmatch"
    9299                                                        else
    93                                                                 logger "Error: iface type $lib overlays $lastmatch"
     100                                                                mprint -s "Error: iface type $lib overlays $lastmatch"
    94101                                                        fi
    95102                                                fi
     
    113120
    114121        . $ENVFILE
     122        t_mdent=$MDENT
     123
    115124        # expand $IFUPDOWN_ENV
    116125        for var in $IFUPDOWN_ENV; do
    117126                eval $var
    118127        done
    119         pos_update
    120 }
    121 
    122 pos_update() {
     128
     129        [ $t_mdent -gt $MDENT ] && MDENT=$t_mdent
     130        main_pos_update
     131}
     132
     133main_pos_update() {
    123134        if [ "${MDENT:-0}" = "0" ]
    124135        then
    125136                STARTCOL=""
    126         #       MPREFIX=$MP_GOOD
    127137        else
    128138                STARTCOL="\x1B[$(expr ${MDENT:-0} \* ${TAB})C"
    129         #       MPREFIX="\x1B[3C"
    130139        fi
    131140        sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
    132141}
    133142
    134 
    135 
     143main_preup() {
     144        config $RT_PREUP_PRINTING_OFF || mstart
     145
     146        if ! config $RT_PREUP_IFUP_CHECK_OFF && is_up
     147        then
     148                if ! config $RT_PREUP_PRINTING_OFF
     149                then
     150                        mup
     151                        mstate 1
     152                        minfo "Interface already up"
     153                fi
     154                exit 1
     155        fi
     156
     157        if_preup 2>&-
     158        retval=$?
     159
     160        if [ $retval != 0 ]
     161        then
     162                if [ $retval != 127 ]
     163                then
     164                        fail_preup 2>&-
     165                        retval=$?
     166
     167                        [ $retval != 0 ] && [ $retval != 127 ] && exit 1
     168                fi
     169        fi
     170
     171        config $RT_PREUP_PRINTING_OFF || mup
     172}
     173
     174main_up() {
     175        if ! config $RT_UP_IFUP_CHECK_OFF
     176        then
     177                is_up
     178                retval=$?
     179
     180                if [ $retval != 0 ]
     181                then
     182
     183                        if ! config $RT_UP_PRINTING_OFF
     184                        then
     185                                mstate $retval
     186                                [ "$METHOD" = "manual" ] && minfo "Method manual is set"
     187                        fi
     188                        exit 1
     189                else
     190                        config $RT_UP_PRINTING_OFF || mstate $retval
     191                fi
     192        fi
     193
     194        if_up 2>&-
     195        retval=$?
     196
     197        if [ $retval != 0 ]
     198        then
     199                if [ $retval != 127 ]
     200                then
     201                        fail_up 2>&-
     202                        retval=$?
     203
     204                        [ $retval != 0 ] && [ $retval != "127" ] && exit 1
     205                fi
     206        fi
     207
     208        if [ -d /tmp/ifupdown/pcode/${IFACE} ]
     209        then
     210                for dir in /tmp/ifupdown/pcode/${IFACE}/*
     211                do
     212                        if [ -f ${dir}/postup ]
     213                        then
     214                                . ${dir}/postup
     215                        fi
     216                done
     217        fi
     218}
     219
     220main_down() {
     221        config $RT_DOWN_PRINTING_OFF || mstop
     222
     223        if [ -d /tmp/ifupdown/pcode/${IFACE} ]
     224        then
     225                for dir in /tmp/ifupdown/pcode/${IFACE}/*
     226                do
     227                        if [ -f ${dir}/predown ]
     228                        then
     229                                . ${dir}/predown
     230                        fi
     231                done
     232        fi
     233
     234        if_down 2>&-
     235        retval=$?
     236
     237        if [ $retval != 0 ]
     238        then
     239                if [ $retval != 127 ]
     240                then
     241                        fail_down 2>&-
     242                        retval=$?
     243
     244                        [ $retval != 0 ] && [ $retval != "127" ] && exit 1
     245                fi
     246        fi
     247
     248        config $RT_DOWN_PRINTING_OFF || mdown
     249}
     250
     251main_postdown() {
     252        if ! config $RT_POSTDOWN_IFDOWN_CHECK_OFF
     253        then
     254                if [ $IFACE_STATE = "down" ]
     255                then
     256                        if ! config $RT_POSTDOWN_PRINTING_OFF
     257                        then
     258                                mdown
     259                                mstate 1
     260                                minfo "Interface already down"
     261                        fi
     262                else
     263                        if is_up
     264                        then
     265                                if ! config $RT_POSTDOWN_PRINTING_OFF
     266                                then
     267                                        mstate 1
     268                                        [ "$METHOD" = "manual" ] && minfo "Method manual is set"
     269                                fi
     270                                return 1
     271                        else
     272                                config $RT_POSTDOWN_PRINTING_OFF || mstate 0
     273                        fi
     274                fi
     275        fi
     276
     277        if_postdown 2>&-
     278        retval=$?
     279
     280        if [ $retval != 0 ]
     281        then
     282                if [ $retval != 127 ]
     283                then
     284                        fail_postdown 2>&-
     285                        retval=$?
     286
     287                        [ $retval != 0 ] && [ $retval != "127" ] && exit 1
     288                fi
     289        fi
     290
     291        # pcode cleanup
     292        rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
     293}
     294
     295main_ifup() {
     296        case $METHOD in
     297        dhcp)
     298                main_dhcp_up || exit 1
     299
     300                # don't handle any failures, it's really not my problem if that goes wrong
     301                main_exec_inlinehooks
     302
     303                main_up || exit 1
     304                RT_BB_NOEXEC=1
     305        ;;
     306        esac
     307}
     308
     309main_ifdown() {
     310        case $METHOD in
     311        dhcp)
     312                main_dhcp_down
     313
     314                # don't handle any failures, it's really not my problem if that goes wrong
     315                main_exec_inlinehooks
     316
     317                main_postdown || exit 1
     318                RT_BB_NOEXEC=1
     319        ;;
     320        esac
     321}
     322
     323main_exec_inlinehooks() {
     324        case $SUBMODE in
     325                if-pre-up)
     326                        [ "${IF_PRE_UP:-""}" != "" ] && eval $IF_PRE_UP
     327                ;;
     328                if-up)
     329                        [ "${IF_UP:-""}" != "" ] && eval $IF_UP
     330                ;;
     331                if-down)
     332                        [ "${IF_DOWN:-""}" != "" ] && eval $IF_DOWN
     333                ;;
     334                if-post-down)
     335                        [ "${IF_POST_DOWN:-""}" != "" ] && eval $IF_POST_DOWN
     336                ;;
     337        esac
     338}
     339
     340main_dhcp_up() {
     341        local err
     342
     343        if ! err=$(ip link set $IFACE up 2>&1 1>&-)
     344        then
     345                mprint -s "main_dhcp_up: $err"
     346                return 1
     347        fi
     348
     349        if [ "${IF_DHCPCLIENT:-""}" != "" -a  "${IF_DHCPCLIENT_OPTS:-""}" != "udhcpc" ]
     350        then
     351                if [ -x "$(which $IF_DHCPCLIENT)" ]
     352                then
     353                        err=$(eval $IF_DHCPCLIENT ${IF_DHCPCLIENT_OPTS:-""} 2>&1 1>&-) || mprint -s "main_dhcp_up: $err" &
     354                        return 0
     355                fi
     356                mprint -s "$IF_DHCPCCLIENT not found, using builtin udhcpcd"
     357        fi
     358
     359        if ! err=$(udhcpc -b -t 0 -p /var/run/udhcpc.${IFACE}.pid -i $IFACE ${IF_HOSTNAME:+"-H $IF_HOSTNAME"} ${IF_CLIENTID:+"-c $IF_CLIENTID"} \
     360                                ${IF_SCRIPT:+"-s $IF_SCRIPT"} 2>&1 1>&-)
     361        then
     362                mprint -s "main_dhcp_up: $err"
     363                return 1
     364        fi
     365
     366        return 0
     367}
     368
     369main_dhcp_down() {
     370        local err
     371
     372        if [ -f /var/run/udhcpc.${IFACE}.pid ]
     373        then
     374                err=$(kill -TERM "$(cat /var/run/udhcpc.${IFACE}.pid)" 2>&1 1>&-) || mprint -s "main_dhcp_down: $err"
     375        fi
     376
     377        if is_up
     378        then
     379                err=$(ip addr flush dev $IFACE 2>&1 1>&-) || mprint -s "main_dhcp_down: $err"
     380                err=$(ip link set $IFACE down 2>&1 1>&-) || mprint -s "main_dhcp_down: $err"
     381        fi
     382}
    136383
    137384### main entry point ###
     
    140387CFG_SYSLOG_OFF=0
    141388CFG_BUSYBOX_COMPAT=0
    142 CFG_PREUP_PRINTING_OFF=0
    143 CFG_UP_PRINTING_OFF=0
    144 CFG_DOWN_PRINTING_OFF=0
    145 CFG_POSTDOWN_PRINTING_OFF=0
    146 CFG_IFUP_CHECK_OFF=0
    147 CFG_IFDOWN_CHECK_OFF=0
    148 
    149 if [ -f /etc/conf.d/ifupdown ]; then
    150         . /etc/conf.d/ifupdown
    151         [ "$CFG_BUSYBOX_COMPAT" = "1" ] && exit 1
    152 else
     389
     390if ! [ -f /etc/conf.d/ifupdown ]
     391then
     392        logger -t ifupdown "/etc/conf.d/ifupdown not found"
    153393        exit 1
    154394fi
    155 
    156 ENVFILE="/tmp/ifupdown/env"
    157 LIBDIR="/opt/ifupdown/lib"
    158 LIBDIR_OVERLAY="/etc/network/lib"
     395. /etc/conf.d/ifupdown
     396
     397RT_PREUP_PRINTING_OFF=0
     398RT_UP_PRINTING_OFF=0
     399RT_DOWN_PRINTING_OFF=0
     400RT_POSTDOWN_PRINTING_OFF=0
     401RT_PREUP_IFUP_CHECK_OFF=0
     402RT_UP_IFUP_CHECK_OFF=0
     403RT_POSTDOWN_IFDOWN_CHECK_OFF=0
     404RT_BB_NOEXEC=0
     405readonly ENVFILE="/tmp/ifupdown/env"
     406readonly LIBDIR="/opt/ifupdown/lib"
     407readonly LIBDIR_OVERLAY="/etc/network/lib"
    159408IFACE_TYPE="none"
     409H_ERR=0
    160410
    161411alias exit="__exit"
     
    163413alias ifdown="__ifupdown ifdown"
    164414
     415if ! [ -f /opt/ifupdown/bin/mfunctions.sh ]
     416then
     417        logger -t ifupdown "/opt/ifupdown/bin/mfunctions.sh not found"
     418        exit 1
     419fi
    165420. /opt/ifupdown/bin/mfunctions.sh
    166421
    167 env_update
     422main_env_update
     423
     424config $H_ERR && builtin exit 1
    168425
    169426if [ "$IFACE_TYPE" != "iface" ]
     
    176433                . ${LIBDIR}/$IFACE_TYPE
    177434        else
    178                 logger "Error file not found and abort"
     435                mprint -s "Error: libfile not found"
    179436        fi
    180437fi
    181 
    182 [ "${H_ERR:=""}" = "1" ] && builtin exit 1
    183438
    184439case $SUBMODE in
    185440        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
     441                main_preup
     442                main_ifup
     443                # bypass execution of further busybox ifupdown.c code
     444                config $RT_BB_NOEXEC && builtin exit 1
    215445        ;;
    216446        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
     447                main_up
    234448        ;;
    235449        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
     450                main_down
     451                main_ifdown
     452                # bypass execution of further busybox ifupdown.c code
     453                config $RT_BB_NOEXEC && builtin exit 1
    271454        ;;
    272455        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 
     456                main_postdown
    286457        ;;
    287458esac
    288459
    289 return 0
     460exit 0
    290461
    291462# vim:ts=4
Note: See TracChangeset for help on using the changeset viewer.