I got some feedback from rpe@, which relied heavily on grep and sed and
removed $ifscandir.
The reason I build this patch the way it is is because, according to
line 27 of the original script we can't rely on /usr yet, so no grep and
sed.
Furthermore, if the $ifscandir isn't used every interface is scanned as
many times as there are hostname[.$nwid].$if files. I think waiting just
one time on a scan operation takes long enough, let alone multiple
times.

Below a new patch based on rpe@'s feedback with extra comments, minor
cleanups and extra ifconfig $if sanitizing.

On Tue, 2014-02-04 at 22:32 +0100, Martijn van Duren wrote:
> On 02/04/14 22:27, Ted Unangst wrote:
> > On Tue, Feb 04, 2014 at 22:14, Martijn van Duren wrote:
> >
> >> +  # Test if we already configured the interface
> >> +  test -f /tmp/$if.scan && return
> >> +  touch /tmp/$if.scan
> >
> > this absolutely needs to use mktemp. It may be easier to use
> > mktemp -d and then put your files inside.
> >
> 
> updated diff
> 
Index: netstart
===================================================================
RCS file: /cvs/src/etc/netstart,v
retrieving revision 1.139
diff -u -r1.139 netstart
--- netstart    22 Aug 2013 07:53:11 -0000      1.139
+++ netstart    5 Feb 2014 07:14:13 -0000
@@ -12,6 +12,9 @@
        done<$1
 }
 
+# Use the filesystem to make sure every interface is parsed once
+ifscandir=`mktemp -d`
+
 # Start the $1 interface
 ifstart() {
        if=$1
@@ -19,11 +22,52 @@
        # configuring backup or temp files, and to catch the "*" case.
        [[ $if != +([[:alpha:]])+([[:digit:]]) ]] && return
 
-       file=/etc/hostname.$if
+       local file=/etc/hostname.$if nwid
+
+       # Check for ifconfig'able interface.
+       (ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
+
+       # Test if we already configured the interface
+       test -f $ifscandir/$if.scan && return
+       touch $ifscandir/$if.scan
+
+       test -h $file rm -f $file
+
        if ! [ -f $file ]; then
-               echo "netstart: $file: No such file or directory"
-               return
+               if ! ifconfig $if -chan -nwid -bssid -nwkey -wpakey \
+                    2> /dev/null; then
+                       echo "netstart: $file: No such file or directory"
+                       return
+               fi
+               ifconfig $if scan > $ifscandir/$if.scan
+               IFS_OLD=$IFS
+               IFS='
+'
+               # Test if any of the nwids has a config file
+               # Not using grep(1) and sed(1), we can't rely on having /usr yet
+               while read line; do
+                       line="$line nwid "
+                       line="${line#*nwid }"
+                       test -z "$line" && continue;
+
+                       if [[ "$line" = \"* ]]; then
+                               IFS='"'
+                               set -A scan -- $line
+                               nwid="${scan[1]}"
+                       else
+                               IFS=' '
+                               set -A scan -- $line
+                               nwid="${scan[0]}"
+                       fi
+
+                       if [ -f /etc/hostname.$nwid.$if ]; then
+                               ln -s /etc/hostname.$nwid.$if $file
+                               break
+                       fi
+               done < $ifscandir/$if.scan
+               IFS=$IFS_OLD
        fi
+
        # Not using stat(1), we can't rely on having /usr yet
        set -A stat -- `ls -nL $file`
        if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
@@ -31,8 +75,6 @@
                chmod -LR o-rwx $file
                chown -LR root.wheel $file
        fi
-       # Check for ifconfig'able interface.
-       (ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
 
        # Now parse the hostname.* file
        while :; do
@@ -129,8 +171,8 @@
 ifmstart() {
        for sif in ${1:-ALL}; do
                for hn in /etc/hostname.*; do
-                       # Strip off /etc/hostname. prefix
-                       if=${hn#/etc/hostname.}
+                       # Strip off /etc/hostname. and potential nwid 
information
+                       if=${hn##*.}
                        test "$if" = "*" && continue
 
                        # Skip unwanted ifs
@@ -146,6 +188,7 @@
                                && ifstart $if
                done
        done
+       rm -rf $ifscandir
 }
 
 # Re-read /etc/rc.conf
@@ -161,6 +204,7 @@
                ifstart $1
                shift
        done
+       rm -rf $ifscandir
        return
 fi

Reply via email to