Advertisement
Guest User

Untitled

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