shill: Fix intermittent autotest failure during connect step

Autotest occassionally fails during the connect step because the client
does not get a DHCP response after broadcasting for a lease.  The log
shows that hostapd could not add the station to the driver with
error=-EINVAL.  This was caused by a race in the test framework between
killing of the existing hostapd and launching of a new hostapd.  If the
new hostapd starts before the old hostapd exists, the old hostapd will
overwrite the interface mode and sets it to station (instead of ap).

BUG=chromium-os:25034
TEST=network_WiFiManager

Change-Id: I1c909d4dd5ec49b276b5362a12ae51602e5c6638
Reviewed-on: https://gerrit.chromium.org/gerrit/14995
Reviewed-by: Paul Stewart <pstew@chromium.org>
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Ready: Thieu Le <thieule@chromium.org>
Tested-by: Thieu Le <thieule@chromium.org>
diff --git a/server/site_linux_router.py b/server/site_linux_router.py
index bae1575..54b7ef0 100644
--- a/server/site_linux_router.py
+++ b/server/site_linux_router.py
@@ -65,7 +65,7 @@
         self.dhcp_high = 128
 
         # Kill hostapd if already running.
-        self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True)
+        self.kill_hostapd()
 
         # Place us in the US by default
         self.router.run("%s reg set US" % self.cmd_iw)
@@ -107,6 +107,19 @@
         # For linux, this is a no-op
         pass
 
+    def kill_hostapd(self):
+        """
+        Kills the hostapd process.  Makes sure hostapd exits before
+        continuing since it sets the interface back to station mode in its
+        cleanup path.  If we start another instance of hostapd before the
+        previous instance exits, the interface station mode will overwrite the
+        ap mode.
+        """
+        self.router.run("pkill hostapd >/dev/null 2>&1 && "
+                        "while pgrep hostapd &> /dev/null; do sleep 1; done",
+                        timeout=30,
+                        ignore_status=True)
+
     def hostap_config(self, params):
         """ Configure the AP per test requirements """
 
@@ -258,7 +271,7 @@
 
         # Run hostapd.
         logging.info("Starting hostapd...")
-        self.router.run("%s -dd %s > %s &" %
+        self.router.run("%s -dd %s &> %s &" %
             (self.cmd_hostapd, self.hostapd['file'], self.hostapd['log']))
 
         if not multi_interface:
@@ -426,7 +439,7 @@
                 # interface hostapd uses to send beacon and DEAUTH packets
                 self._remove_interfaces()
 
-            self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True)
+            self.kill_hostapd()
 #           self.router.run("rm -f %s" % self.hostapd['file'])
             self.router.get_file(self.hostapd['log'],
                                  'debug/hostapd_router_%d.log' %