busybox: udhcpc.script: fix resolv.conf handling with multiple interfaces
authorPeter Korsgaard <jacmet@sunsite.dk>
Wed, 26 Jun 2013 13:01:46 +0000 (15:01 +0200)
committerPeter Korsgaard <jacmet@sunsite.dk>
Wed, 26 Jun 2013 13:20:50 +0000 (15:20 +0200)
When udhcpc is used on multiple network devices at the same time (or a mix
of dhcp and fixed configuration), /etc/resolv.conf should contain the
union of information from all the interfaces.

Currently that's not the case. The udhcpc script simply overwrites
resolv.conf with the information from the specific interface on each dhcp
bound/renew event.

Fix it by tagging lines with the interface they came from when added,
and drop the affected lines on deconfig/renew. As /etc/resolv.conf is
often a symlink to /tmp (and rootfs might be read only), special care
has to be taken when it is updated.

Notice that I'm not really aware of any official documentation requiring
that '#' comments in /etc/resolv.conf must be supported, but atleast
glibc and uClibc do.

Signed-off-by: Peter Korsgaard <jacmet@sunsite.dk>
package/busybox/udhcpc.script

index f7beb56a08048390c80ebfd3b6da16b40014a056..43742fbd530269c5a45edb5fdc22f191fce571ac 100755 (executable)
@@ -13,6 +13,13 @@ case "$1" in
                /sbin/ifconfig $interface up
                /sbin/ifconfig $interface 0.0.0.0
 
+               # drop info from this interface
+               # resolv.conf may be a symlink to /tmp/, so take care
+               TMPFILE=$(mktemp)
+               grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
+               cat $TMPFILE > $RESOLV_CONF
+               rm -f $TMPFILE
+
                if [ -x /usr/sbin/avahi-autoipd ]; then
                        /usr/sbin/avahi-autoipd -k $interface
                fi
@@ -41,11 +48,17 @@ case "$1" in
                        done
                fi
 
-               echo -n > $RESOLV_CONF
-               [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
+               # drop info from this interface
+               # resolv.conf may be a symlink to /tmp/, so take care
+               TMPFILE=$(mktemp)
+               grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
+               cat $TMPFILE > $RESOLV_CONF
+               rm -f $TMPFILE
+
+               [ -n "$domain" ] && echo "search $domain # $interface" >> $RESOLV_CONF
                for i in $dns ; do
                        echo adding dns $i
-                       echo nameserver $i >> $RESOLV_CONF
+                       echo "nameserver $i # $interface" >> $RESOLV_CONF
                done
                ;;
 esac