Advertisement
Guest User

net.wlan0

a guest
Feb 26th, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 17.10 KB | None | 0 0
  1. #!/sbin/runscript
  2. # Copyright (c) 2007-2009 Roy Marples <roy@marples.name>
  3. # Released under the 2-clause BSD license.
  4.  
  5. MODULESDIR="${RC_LIBEXECDIR}/net"
  6. MODULESLIST="${RC_SVCDIR}/nettree"
  7. _config_vars="config routes"
  8.  
  9. [ -z "${IN_BACKGROUND}" ] && IN_BACKGROUND="NO"
  10.  
  11. description="Configures network interfaces."
  12.  
  13. # Handy var so we don't have to embed new lines everywhere for array splitting
  14. __IFS="
  15. "
  16. depend()
  17. {
  18.     local IFACE=${RC_SVCNAME#*.}
  19.     local IFVAR=$(shell_var "${IFACE}")
  20.  
  21.     need localmount
  22.     after bootmisc
  23.     provide net
  24.     keyword -jail -prefix -vserver
  25.  
  26.     case "${IFACE}" in
  27.         lo|lo0);;
  28.         *) after net.lo net.lo0 dbus;;
  29.     esac
  30.  
  31.     if [ "$(command -v "depend_${IFVAR}")" = "depend_${IFVAR}" ]; then
  32.         depend_${IFVAR}
  33.     fi
  34.  
  35.     local dep= prov=
  36.     for dep in need use before after provide keyword; do
  37.         eval prov=\$rc_${dep}_${IFVAR}
  38.         if [ -n "${prov}" ]; then
  39.             ${dep} ${prov}
  40.         fi
  41.     done
  42. }
  43.  
  44. # Support bash arrays - sigh
  45. _array_helper()
  46. {
  47.     local _a=
  48.  
  49.     eval _a=\$$1
  50.     _a=$(echo "${_a}" | sed -e 's:^[[:space:]]*::' -e 's:[[:space:]]*$::' -e '/^$/d' -e 's:[[:space:]]\{1,\}: :g')
  51.  
  52.     [ -n "${_a}" ] && printf "%s\n" "${_a}"
  53. }
  54.  
  55. _get_array()
  56. {
  57.     local _a=
  58.     if [ -n "${BASH}" ]; then
  59.         case "$(declare -p "$1" 2>/dev/null)" in
  60.             "declare -a "*)
  61.                 ewarn "You are using a bash array for $1."
  62.                 ewarn "This feature will be removed in the future."
  63.                 ewarn "Please see net.example for the correct format for $1."
  64.                 eval "set -- \"\${$1[@]}\""
  65.                 for _a; do
  66.                     printf "%s\n" "${_a}"
  67.                 done
  68.                 return 0
  69.                 ;;
  70.         esac
  71.     fi
  72.  
  73.     _array_helper $1
  74. }
  75.  
  76. # Flatten bash arrays to simple strings
  77. _flatten_array()
  78. {
  79.     if [ -n "${BASH}" ]; then
  80.         case "$(declare -p "$1" 2>/dev/null)" in
  81.             "declare -a "*)
  82.                 ewarn "You are using a bash array for $1."
  83.                 ewarn "This feature will be removed in the future."
  84.                 ewarn "Please see net.example for the correct format for $1."
  85.                 eval "set -- \"\${$1[@]}\""
  86.                 for x; do
  87.                     printf "'%s' " "$(printf "$x" | sed "s:':'\\\'':g")"
  88.                 done
  89.                 return 0
  90.                 ;;
  91.         esac
  92.     fi
  93.  
  94.     _array_helper $1
  95. }
  96.  
  97. _wait_for_carrier()
  98. {
  99.     local timeout= efunc=einfon
  100.  
  101.     _has_carrier  && return 0
  102.  
  103.     eval timeout=\$carrier_timeout_${IFVAR}
  104.     timeout=${timeout:-${carrier_timeout:-5}}
  105.  
  106.     # Incase users don't want this nice feature ...
  107.     [ ${timeout} -le 0 ] && return 0
  108.  
  109.     yesno ${RC_PARALLEL} && efunc=einfo
  110.     ${efunc} "Waiting for carrier (${timeout} seconds) "
  111.     while [ ${timeout} -gt 0 ]; do
  112.         sleep 1
  113.         if _has_carrier; then
  114.             [ "${efunc}" = "einfon" ] && echo
  115.             eend 0
  116.             return 0
  117.         fi
  118.         : $(( timeout -= 1 ))
  119.         [ "${efunc}" = "einfon" ] && printf "."
  120.     done
  121.  
  122.     [ "${efunc}" = "einfon" ] && echo
  123.     eend 1
  124.     return 1
  125. }
  126.  
  127. _netmask2cidr()
  128. {
  129.     # Some shells cannot handle hex arithmetic, so we massage it slightly
  130.     # Buggy shells include FreeBSD sh, dash and busybox.
  131.     # bash and NetBSD sh don't need this.
  132.     case $1 in
  133.         0x*)
  134.         local hex=${1#0x*} quad=
  135.         while [ -n "${hex}" ]; do
  136.             local lastbut2=${hex#??*}
  137.             quad=${quad}${quad:+.}0x${hex%${lastbut2}*}
  138.             hex=${lastbut2}
  139.         done
  140.         set -- ${quad}
  141.         ;;
  142.     esac
  143.  
  144.     local i= len=
  145.     local IFS=.
  146.     for i in $1; do
  147.         while [ ${i} -ne 0 ]; do
  148.             : $(( len += i % 2 ))
  149.             : $(( i >>= 1 ))
  150.         done
  151.     done
  152.  
  153.     echo "${len}"
  154. }
  155.  
  156. _configure_variables()
  157. {
  158.     local var= v= t=
  159.  
  160.     for var in ${_config_vars}; do
  161.         local v=
  162.         for t; do
  163.             eval v=\$${var}_${t}
  164.             if [ -n "${v}" ]; then
  165.                 eval ${var}_${IFVAR}=\$${var}_${t}
  166.                 continue 2
  167.             fi
  168.         done
  169.     done
  170. }
  171.  
  172. _which()
  173. {
  174.     local i OIFS
  175.     # Empty
  176.     [ -z "$1" ] && return
  177.     # check paths
  178.     OIFS="$IFS"
  179.     IFS=:
  180.     for i in $PATH ; do
  181.         [ -x $i/$1 ] && echo $i/$1 && break
  182.     done
  183.     IFS=$OIFS
  184. }
  185.  
  186. # Like _which, but also consider shell builtins, and multiple alternatives
  187. _program_available()
  188. {
  189.     [ -z "$1" ] && return 0
  190.     local x=
  191.     for x; do
  192.         case "${x}" in
  193.             /*) [ -x "${x}" ] && break;;
  194.             *) type "${x}" >/dev/null 2>&1 && break;;
  195.         esac
  196.         unset x
  197.     done
  198.     [ -n "${x}" ] && echo $x && return 0
  199.     return 1
  200. }
  201.  
  202. _show_address()
  203. {
  204.     einfo "received address $(_get_inet_address "${IFACE}")"
  205. }
  206.  
  207. # Basically sorts our modules into order and saves the list
  208. _gen_module_list()
  209. {
  210.     local x= f= force=$1
  211.     if ! ${force} && [ -s "${MODULESLIST}" -a "${MODULESLIST}" -nt "${MODULESDIR}" ]; then
  212.         local update=false
  213.         for x in "${MODULESDIR}"/*.sh; do
  214.             [ -e "${x}" ] || continue
  215.             if [ "${x}" -nt "${MODULESLIST}" ]; then
  216.                 update=true
  217.                 break
  218.             fi
  219.         done
  220.         ${update} || return 0
  221.     fi
  222.  
  223.     einfo "Caching network module dependencies"
  224.     # Run in a subshell to protect the main script
  225.     (
  226.     after() {
  227.         eval ${MODULE}_after="\"\${${MODULE}_after}\${${MODULE}_after:+ }$*\""
  228.     }
  229.  
  230.     before() {
  231.         local mod=${MODULE}
  232.         local MODULE=
  233.         for MODULE; do
  234.             after "${mod}"
  235.         done
  236.     }
  237.  
  238.     program() {
  239.         if [ "$1" = "start" -o "$1" = "stop" ]; then
  240.             local s="$1"
  241.             shift
  242.             eval ${MODULE}_program_${s}="\"\${${MODULE}_program_${s}}\${${MODULE}_program_${s}:+ }$*\""
  243.         else
  244.             eval ${MODULE}_program="\"\${${MODULE}_program}\${${MODULE}_program:+ }$*\""
  245.         fi
  246.     }
  247.  
  248.     provide() {
  249.         eval ${MODULE}_provide="\"\${${MODULE}_provide}\${${MODULE}_provide:+ }$*\""
  250.         local x
  251.         for x in $*; do
  252.             eval ${x}_providedby="\"\${${MODULE}_providedby}\${${MODULE}_providedby:+ }${MODULE}\""
  253.         done
  254.     }
  255.  
  256.     for MODULE in "${MODULESDIR}"/*.sh; do
  257.         sh -n "${MODULE}" || continue
  258.         . "${MODULE}" || continue
  259.         MODULE=${MODULE#${MODULESDIR}/}
  260.         MODULE=${MODULE%.sh}
  261.         eval ${MODULE}_depend
  262.         MODULES="${MODULES} ${MODULE}"
  263.     done
  264.  
  265.     VISITED=
  266.     SORTED=
  267.     visit() {
  268.         case " ${VISITED} " in
  269.             *" $1 "*) return;;
  270.         esac
  271.         VISITED="${VISITED} $1"
  272.  
  273.         eval AFTER=\$${1}_after
  274.         for MODULE in ${AFTER}; do
  275.             eval PROVIDEDBY=\$${MODULE}_providedby
  276.             if [ -n "${PROVIDEDBY}" ]; then
  277.                 for MODULE in ${PROVIDEDBY}; do
  278.                     visit "${MODULE}"
  279.                 done
  280.             else
  281.                 visit "${MODULE}"
  282.             fi
  283.         done
  284.  
  285.         eval PROVIDE=\$${1}_provide
  286.         for MODULE in ${PROVIDE}; do
  287.             visit "${MODULE}"
  288.         done
  289.  
  290.         eval PROVIDEDBY=\$${1}_providedby
  291.         [ -z "${PROVIDEDBY}" ] && SORTED="${SORTED} $1"
  292.     }
  293.  
  294.     for MODULE in ${MODULES}; do
  295.         visit "${MODULE}"
  296.     done
  297.  
  298.     printf "" > "${MODULESLIST}"
  299.     i=0
  300.     for MODULE in ${SORTED}; do
  301.         eval PROGRAM=\$${MODULE}_program
  302.         eval PROGRAM_START=\$${MODULE}_program_start
  303.         eval PROGRAM_STOP=\$${MODULE}_program_stop
  304.         eval PROVIDE=\$${MODULE}_provide
  305.         echo "module_${i}='${MODULE}'" >> "${MODULESLIST}"
  306.         echo "module_${i}_program='${PROGRAM}'" >> "${MODULESLIST}"
  307.         echo "module_${i}_program_start='${PROGRAM_START}'" >> "${MODULESLIST}"
  308.         echo "module_${i}_program_stop='${PROGRAM_STOP}'" >> "${MODULESLIST}"
  309.         echo "module_${i}_provide='${PROVIDE}'" >> "${MODULESLIST}"
  310.         : $(( i += 1 ))
  311.     done
  312.     echo "module_${i}=" >> "${MODULESLIST}"
  313.     )
  314.  
  315.     return 0
  316. }
  317.  
  318. _load_modules()
  319. {
  320.     local starting=$1 mymods=
  321.  
  322.     # Ensure our list is up to date
  323.     _gen_module_list false
  324.     if ! . "${MODULESLIST}"; then
  325.         _gen_module_list true
  326.         . "${MODULESLIST}"
  327.     fi
  328.  
  329.     MODULES=
  330.     if [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ]; then
  331.         eval mymods=\$modules_${IFVAR}
  332.         [ -z "${mymods}" ] && mymods=${modules}
  333.     fi
  334.  
  335.     local i=-1 x= mod= f= provides=
  336.     while true; do
  337.         : $(( i += 1 ))
  338.         eval mod=\$module_${i}
  339.         [ -z "${mod}" ] && break
  340.         [ -e "${MODULESDIR}/${mod}.sh" ] || continue
  341.  
  342.         eval set -- \$module_${i}_program
  343.         if [ -n "$1" ]; then
  344.             if ! _program_available "$@" >/dev/null; then
  345.                 vewarn "Skipping module $mod due to missing program: $@"
  346.                 continue
  347.             fi
  348.         fi
  349.         if ${starting}; then
  350.             eval set -- \$module_${i}_program_start
  351.         else
  352.             eval set -- \$module_${i}_program_stop
  353.         fi
  354.         if [ -n "$1" ]; then
  355.             if ! _program_available "$@" >/dev/null; then
  356.                 vewarn "Skipping module $mod due to missing program: $@"
  357.                 continue
  358.             fi
  359.         fi
  360.  
  361.         eval provides=\$module_${i}_provide
  362.         if ${starting}; then
  363.             case " ${mymods} " in
  364.                 *" !${mod} "*) continue;;
  365.                 *" !${provides} "*) [ -n "${provides}" ] && continue;;
  366.             esac
  367.         fi
  368.         MODULES="${MODULES}${MODULES:+ }${mod}"
  369.  
  370.         # Now load and wrap our functions
  371.         if ! . "${MODULESDIR}/${mod}.sh"; then
  372.             eend 1 "${RC_SVCNAME}: error loading module \`${mod}'"
  373.             exit 1
  374.         fi
  375.  
  376.         [ -z "${provides}" ] && continue
  377.  
  378.         # Wrap our provides
  379.         local f=
  380.         for f in pre_start start post_start; do
  381.             eval "${provides}_${f}() { [ "$(command -v "${mod}_${f}")" = "${mod}_${f}" ] || return 0; ${mod}_${f} \"\$@\"; }"
  382.         done
  383.  
  384.         eval module_${mod}_provides="${provides}"
  385.         eval module_${provides}_providedby="${mod}"
  386.     done
  387.  
  388.     # Wrap our preferred modules
  389.     for mod in ${mymods}; do
  390.         case " ${MODULES} " in
  391.             *" ${mod} "*)
  392.             eval x=\$module_${mod}_provides
  393.             [ -z "${x}" ] && continue
  394.             for f in pre_start start post_start; do
  395.                 eval "${x}_${f}() { [ "$(command -v "${mod}_${f}")" = "${mod}_${f}" ] || return 0; ${mod}_${f} \"\$@\"; }"
  396.             done
  397.             eval module_${x}_providedby="${mod}"
  398.             ;;
  399.         esac
  400.     done
  401.  
  402.     # Finally remove any duplicated provides from our list if we're starting
  403.     # Otherwise reverse the list
  404.     local LIST="${MODULES}" p=
  405.     MODULES=
  406.     if ${starting}; then
  407.         for mod in ${LIST}; do
  408.             eval x=\$module_${mod}_provides
  409.             if [ -n "${x}" ]; then
  410.                 eval p=\$module_${x}_providedby
  411.                 [ "${mod}" != "${p}" ] && continue
  412.             fi
  413.             MODULES="${MODULES}${MODULES:+ }${mod}"
  414.         done
  415.     else
  416.         for mod in ${LIST}; do
  417.             MODULES="${mod}${MODULES:+ }${MODULES}"
  418.         done
  419.     fi
  420.  
  421.     veinfo "Loaded modules: ${MODULES}"
  422. }
  423.  
  424. _load_config()
  425. {
  426.     local config="$(_get_array "config_${IFVAR}")"
  427.     local fallback="$(_get_array fallback_${IFVAR})"
  428.  
  429.     config_index=0
  430.     local IFS="$__IFS"
  431.     set -- ${config}
  432.  
  433.     # We should support a space separated array for cidr configs
  434.     # But only as long as they do not contain other parameters for the address
  435.     if [ $# = 1 ]; then
  436.         unset IFS
  437.         set -- ${config}
  438.         # Of course, we may have a single address added old style.
  439.         # If the NEXT argument is a v4 or v6 address, it's the next config.
  440.         # Otherwise, it's arguments to the first config...
  441.         if [ "${2#*.*}" = "${2}" -a "${2#*:*}" = "${2}" ]; then
  442.             # Not an IPv4/IPv6
  443.             local IFS="$__IFS"
  444.             set -- ${config}
  445.         fi
  446.     fi
  447.  
  448.     # Ensure that loopback has the correct address
  449.     if [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ]; then
  450.         if [ "$1" != "null" ]; then
  451.             config_0="127.0.0.1/8"
  452.             config_index=1
  453.         fi
  454.     else
  455.         if [ -z "$1" ]; then
  456.             ewarn "No configuration specified; defaulting to DHCP"
  457.             config_0="dhcp"
  458.             config_index=1
  459.         fi
  460.     fi
  461.  
  462.  
  463.     # We store our config in an array like vars
  464.     # so modules can influence it
  465.     for cmd; do
  466.         eval config_${config_index}="'${cmd}'"
  467.         : $(( config_index += 1 ))
  468.     done
  469.     # Terminate the list
  470.     eval config_${config_index}=
  471.  
  472.     config_index=0
  473.     for cmd in ${fallback}; do
  474.         eval fallback_${config_index}="'${cmd}'"
  475.         : $(( config_index += 1 ))
  476.     done
  477.     # Terminate the list
  478.     eval fallback_${config_index}=
  479.  
  480.     # Don't set to zero, so any net modules don't have to do anything extra
  481.     config_index=-1
  482. }
  483.  
  484. # Support functions
  485. _run_if()
  486. {
  487.     local cmd=$1 iface=$2 ifr=${IFACE} ifv=${IFVAR}
  488.     # Ensure that we don't stamp on real values
  489.     local IFACE= IFVAR=
  490.     shift
  491.     if [ -n "${iface}" ]; then
  492.         IFACE="${iface}"
  493.         [ "${iface}" != "${ifr}" ] && IFVAR=$(shell_var "${IFACE}")
  494.     else
  495.         IFACE=${ifr}
  496.         IFVAR=${ifv}
  497.     fi
  498.     ${cmd}
  499. }
  500. interface_exists()
  501. {
  502.     _run_if _exists "$@"
  503. }
  504. interface_up()
  505. {
  506.     _run_if _up "$@"
  507. }
  508. interface_down()
  509. {
  510.     _run_if _down "$@"
  511. }
  512.  
  513. start()
  514. {
  515.     local IFACE=${RC_SVCNAME#*.} oneworked=false fallback=false module=
  516.     local IFVAR=$(shell_var "${IFACE}") cmd= our_metric=
  517.     local metric=0 _up_before_preup
  518.     eval _up_before_preup="\$up_before_preup_${IFVAR}"
  519.     [ -z "${_up_before_preup}" ] && _up_before_preup=$up_before_preup
  520.  
  521.     einfo "Bringing up interface ${IFACE}"
  522.     eindent
  523.  
  524.     if [ -z "${MODULES}" ]; then
  525.         local MODULES=
  526.         _load_modules true
  527.     fi
  528.  
  529.     # We up the iface twice if we have a preup to ensure it's up if
  530.     # available in preup and afterwards incase the user inadvertently
  531.     # brings it down
  532.     if [ "$(command -v preup)" = "preup" ]; then
  533.         yesno "${_up_before_preup:-yes}" && _up 2>/dev/null
  534.         ebegin "Running preup"
  535.         eindent
  536.         preup || return 1
  537.         eoutdent
  538.     fi
  539.  
  540.     _up 2>/dev/null
  541.  
  542.     for module in ${MODULES}; do
  543.         if [ "$(command -v "${module}_pre_start")" = "${module}_pre_start" ]; then
  544.             ${module}_pre_start || exit $?
  545.         fi
  546.     done
  547.  
  548.     if ! _exists; then
  549.         eerror "ERROR: interface ${IFACE} does not exist"
  550.         eerror "Ensure that you have loaded the correct kernel module for your hardware"
  551.         return 1
  552.     fi
  553.  
  554.     if ! _wait_for_carrier; then
  555.         if service_started devd; then
  556.             ewarn "no carrier, but devd will start us when we have one"
  557.             mark_service_inactive "${RC_SVCNAME}"
  558.         else
  559.             eerror "no carrier"
  560.         fi
  561.         return 1
  562.     fi
  563.  
  564.     local config= config_index=
  565.     _load_config
  566.     config_index=0
  567.  
  568.     eval our_metric=\$metric_${IFVAR}
  569.     if [ -n "${our_metric}" ]; then
  570.         metric=${our_metric}
  571.     elif [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ]; then
  572.         : $(( metric += $(_ifindex) ))
  573.     fi
  574.  
  575.     while true; do
  576.         eval config=\$config_${config_index}
  577.         [ -z "${config}" ] && break
  578.  
  579.         set -- ${config}
  580.         if [ "$1" != "null" -a "$1" != "noop" ]; then
  581.             ebegin "$1"
  582.         fi
  583.         eindent
  584.         case "$1" in
  585.             noop)
  586.                 if [ -n "$(_get_inet_address)" ]; then
  587.                     oneworked=true
  588.                     break
  589.                 fi
  590.                 ;;
  591.             null) :;;
  592.             [0-9]*|*:*) _add_address ${config};;
  593.             *)
  594.                 if [ "$(command -v "${config}_start")" = "${config}_start" ]; then
  595.                     "${config}"_start
  596.                 else
  597.                     eerror "nothing provides \`${config}'"
  598.                 fi
  599.                 ;;
  600.         esac
  601.         if eend $?; then
  602.             oneworked=true
  603.         else
  604.             eval config=\$fallback_${config_index}
  605.             if [ -n "${config}" ]; then
  606.                 fallback=true
  607.                 eoutdent
  608.                 ewarn "Trying fallback configuration ${config}"
  609.                 eindent
  610.                 eval config_${config_index}=\$config
  611.                 unset fallback_${config_index}
  612.                 : $(( config_index -= 1 ))
  613.             fi
  614.         fi
  615.         eoutdent
  616.         : $(( config_index += 1 ))
  617.     done
  618.  
  619.     if ! ${oneworked}; then
  620.         if [ "$(command -v failup)" = "failup" ]; then
  621.             ebegin "Running failup"
  622.             eindent
  623.             failup
  624.             eoutdent
  625.         fi
  626.         return 1
  627.     fi
  628.  
  629.     local hidefirstroute=false first=true routes=
  630.     if ${fallback}; then
  631.         routes="$(_get_array "fallback_routes_${IFVAR}")"
  632.     fi
  633.     if [ -z "${routes}" ]; then
  634.         routes="$(_get_array "routes_${IFVAR}")"
  635.     fi
  636.     if [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ]; then
  637.         if [ "${config_0}" != "null" ]; then
  638.             routes="127.0.0.0/8 via 127.0.0.1
  639. ${routes}"
  640.             hidefirstroute=true
  641.         fi
  642.     fi
  643.  
  644.     local OIFS="${IFS}" SIFS="${IFS-y}"
  645.     local IFS="$__IFS"
  646.     for cmd in ${routes}; do
  647.         unset IFS
  648.         if ${first}; then
  649.             first=false
  650.             einfo "Adding routes"
  651.         fi
  652.         eindent
  653.         ebegin ${cmd}
  654.         # Work out if we're a host or a net if not told
  655.         case ${cmd} in
  656.             -net" "*|-host" "*);;
  657.             *" "netmask" "*)                   cmd="-net ${cmd}";;
  658.             *.*.*.*/32*)                       cmd="-host ${cmd}";;
  659.             *.*.*.*/*|0.0.0.0|0.0.0.0" "*)     cmd="-net ${cmd}";;
  660.             default|default" "*)               cmd="-net ${cmd}";;
  661.             *)                                 cmd="-host ${cmd}";;
  662.         esac
  663.         if ${hidefirstroute}; then
  664.             _add_route ${cmd} >/dev/null 2>&1
  665.             hidefirstroute=false
  666.         else
  667.             _add_route ${cmd} >/dev/null
  668.         fi
  669.         eend $?
  670.         eoutdent
  671.     done
  672.     if [ "${SIFS}" = "y" ]; then
  673.         unset IFS
  674.     else
  675.         IFS="${OIFS}"
  676.     fi
  677.  
  678.     for module in ${MODULES}; do
  679.         if [ "$(command -v "${module}_post_start")" = "${module}_post_start" ]; then
  680.             ${module}_post_start || exit $?
  681.         fi
  682.     done
  683.  
  684.     if [ "$(command -v postup)" = "postup" ]; then
  685.         ebegin "Running postup"
  686.         eindent
  687.         postup
  688.         eoutdent
  689.     fi
  690.  
  691.     return 0
  692. }
  693.  
  694. stop()
  695. {
  696.     local IFACE=${RC_SVCNAME#*.} module=
  697.     local IFVAR=$(shell_var "${IFACE}") opts=
  698.  
  699.     einfo "Bringing down interface ${IFACE}"
  700.     eindent
  701.  
  702.     if [ -z "${MODULES}" ]; then
  703.         local MODULES=
  704.         _load_modules false
  705.     fi
  706.  
  707.     if [ "$(command -v predown)" = "predown" ]; then
  708.         ebegin "Running predown"
  709.         eindent
  710.         predown || return 1
  711.         eoutdent
  712.     else
  713.         if is_net_fs /; then
  714.             eerror "root filesystem is network mounted -- can't stop ${IFACE}"
  715.             return 1
  716.         fi
  717.     fi
  718.  
  719.     for module in ${MODULES}; do
  720.         if [ "$(command -v "${module}_pre_stop")" = "${module}_pre_stop" ]; then
  721.             ${module}_pre_stop || exit $?
  722.         fi
  723.     done
  724.  
  725.     for module in ${MODULES}; do
  726.         if [ "$(command -v "${module}_stop")" = "${module}_stop" ]; then
  727.             ${module}_stop
  728.         fi
  729.     done
  730.  
  731.     # Only delete addresses for interfaces that exist
  732.     if _exists; then
  733.         # PPP can manage it's own addresses when IN_BACKGROUND
  734.         # Important in case "demand" set on the ppp link
  735.         if ! (yesno ${IN_BACKGROUND} && is_ppp) ; then
  736.             _delete_addresses "${IFACE}"
  737.         fi
  738.     fi
  739.  
  740.     for module in ${MODULES}; do
  741.         if [ "$(command -v "${module}_post_stop")" = "${module}_post_stop" ]; then
  742.             ${module}_post_stop
  743.         fi
  744.     done
  745.  
  746.     # If not in background, and not loopback then bring the interface down
  747.     # unless overridden.
  748.     if ! yesno ${IN_BACKGROUND} && \
  749.     [ "${IFACE}" != "lo" -a "${IFACE}" != "lo0" ]; then
  750.         eval module=\$ifdown_${IFVAR}
  751.         module=${module:-${ifdown:-YES}}
  752.         yesno ${module} && _down 2>/dev/null
  753.     fi
  754.  
  755.     type resolvconf >/dev/null 2>&1 && resolvconf -d "${IFACE}" 2>/dev/null
  756.  
  757.     if [ "$(command -v "postdown")" = "postdown" ]; then
  758.         ebegin "Running postdown"
  759.         eindent
  760.         postdown
  761.         eoutdent
  762.     fi
  763.  
  764.     return 0
  765. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement