make network scripts more robust, prevent multiple instances of pppd and udhcpc for...
[openwrt/svn-archive/archive.git] / package / base-files / default / lib / network / config.sh
index 18f794a31b25a1c32d9366983d6d6cfaee30969a..25c7278e99239c7d9f32e998d2fc7d3aeb47b055 100755 (executable)
@@ -70,6 +70,11 @@ setup_interface() {
        proto="${3:-$(config_get "$config" proto)}"
        config_get iftype "$config" type
        
+       ifconfig "$iface" 2>/dev/null >/dev/null && {
+               # make sure the interface is removed from any existing bridge
+               unbridge "$iface"
+       }
+
        # Setup VLAN interfaces
        add_vlan "$iface"
 
@@ -115,8 +120,9 @@ setup_interface() {
                        env -i ACTION="ifup" INTERFACE="config" DEVICE="$iface" PROTO=static /sbin/hotplug "iface" &
                ;;
                dhcp)
+                       # prevent udhcpc from starting more than once
                        pid="$(cat "$pidfile" 2>/dev/null)"
-                       [ -n "$pid" -a -d "/proc/$pid" ] && kill -9 "$pid"
+                       [ -d "/proc/$pid" ] && grep udhcpc "/proc/${pid}/cmdline" >/dev/null 2>/dev/null && return 0
 
                        config_get ipaddr "$config" ipaddr
                        config_get netmask "$config" netmask
@@ -141,3 +147,15 @@ setup_interface() {
        esac
 }
 
+unbridge() {
+       local dev="$1"
+       local brdev
+
+       brctl show | grep "$dev" >/dev/null && {
+               # interface is still part of a bridge, correct that
+
+               for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
+                       brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
+               done
+       }
+}