OSDN Git Service

wifi: determine whether to unload the driver by a property
[android-x86/hardware-libhardware_legacy.git] / wifi / wifi.c
index dd216f1..96cf0da 100644 (file)
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <dirent.h>
 #include <sys/socket.h>
+#include <unistd.h>
 #include <poll.h>
 
 #include "hardware_legacy/wifi.h"
 #include <sys/_system_properties.h>
 #endif
 
-/* PRIMARY refers to the connection on the primary interface
- * SECONDARY refers to an optional connection on a p2p interface
- *
- * For concurrency, we only support one active p2p connection and
- * one active STA connection at a time
- */
-#define PRIMARY     0
-#define SECONDARY   1
-#define MAX_CONNS   2
-
-static struct wpa_ctrl *ctrl_conn[MAX_CONNS];
-static struct wpa_ctrl *monitor_conn[MAX_CONNS];
+static struct wpa_ctrl *ctrl_conn;
+static struct wpa_ctrl *monitor_conn;
 
 /* socket pair used to exit from a blocking read */
-static int exit_sockets[MAX_CONNS][2];
+static int exit_sockets[2];
 
 extern int do_dhcp();
 extern int ifc_init();
@@ -59,6 +50,7 @@ extern char *dhcp_lasterror();
 extern void get_dhcp_info();
 extern int init_module(void *, unsigned long, const char *);
 extern int delete_module(const char *, unsigned int);
+void wifi_close_sockets();
 
 static char primary_iface[PROPERTY_VALUE_MAX];
 // TODO: use new ANDROID_SOCKET mechanism, once support for multiple
@@ -87,23 +79,32 @@ static char primary_iface[PROPERTY_VALUE_MAX];
 #endif
 
 #define WIFI_DRIVER_LOADER_DELAY       1000000
+#define SYSFS_PATH_MAX                 256
 
 static const char IFACE_DIR[]           = "/data/system/wpa_supplicant";
 #ifdef WIFI_DRIVER_MODULE_PATH
-static const char DRIVER_MODULE_NAME[]  = WIFI_DRIVER_MODULE_NAME;
-static const char DRIVER_MODULE_TAG[]   = WIFI_DRIVER_MODULE_NAME " ";
-static const char DRIVER_MODULE_PATH[]  = WIFI_DRIVER_MODULE_PATH;
+static const char DRIVER_NAME_PROP[]    = "wlan.modname";
+static const char DRIVER_PATH_PROP[]    = "wlan.modpath";
 static const char DRIVER_MODULE_ARG[]   = WIFI_DRIVER_MODULE_ARG;
 #endif
 static const char FIRMWARE_LOADER[]     = WIFI_FIRMWARE_LOADER;
 static const char DRIVER_PROP_NAME[]    = "wlan.driver.status";
 static const char SUPPLICANT_NAME[]     = "wpa_supplicant";
 static const char SUPP_PROP_NAME[]      = "init.svc.wpa_supplicant";
+static const char P2P_SUPPLICANT_NAME[] = "p2p_supplicant";
+static const char P2P_PROP_NAME[]       = "init.svc.p2p_supplicant";
 static const char SUPP_CONFIG_TEMPLATE[]= "/system/etc/wifi/wpa_supplicant.conf";
 static const char SUPP_CONFIG_FILE[]    = "/data/misc/wifi/wpa_supplicant.conf";
 static const char P2P_CONFIG_FILE[]     = "/data/misc/wifi/p2p_supplicant.conf";
-static const char CONTROL_IFACE_PATH[]  = "/data/misc/wifi";
+static const char CONTROL_IFACE_PATH[]  = "/data/misc/wifi/sockets";
 static const char MODULE_FILE[]         = "/proc/modules";
+static const char SYSFS_CLASS_NET[]     = "/sys/class/net";
+static const char MODULE_DEFAULT_DIR[]  = "/system/lib/modules";
+static const char SYS_MOD_NAME_DIR[]    = "device/driver/module";
+
+static const char IFNAME[]              = "IFNAME=";
+#define IFNAMELEN                      (sizeof(IFNAME) - 1)
+static const char WPA_EVENT_IGNORE[]    = "CTRL-EVENT-IGNORE ";
 
 static const char SUPP_ENTROPY_FILE[]   = WIFI_ENTROPY_FILE;
 static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
@@ -111,15 +112,10 @@ static unsigned char dummy_key[21] = { 0x02, 0x11, 0xbe, 0x33, 0x43, 0x35,
                                        0x1c, 0xd3, 0xee, 0xff, 0xf1, 0xe2,
                                        0xf3, 0xf4, 0xf5 };
 
-static int is_primary_interface(const char *ifname)
-{
-    //Treat NULL as primary interface to allow control
-    //on STA without an interface
-    if (ifname == NULL || !strncmp(ifname, primary_iface, strlen(primary_iface))) {
-        return 1;
-    }
-    return 0;
-}
+/* Is either SUPPLICANT_NAME or P2P_SUPPLICANT_NAME */
+static char supplicant_name[PROPERTY_VALUE_MAX];
+/* Is either SUPP_PROP_NAME or P2P_PROP_NAME */
+static char supplicant_prop_name[PROPERTY_KEY_MAX];
 
 static int insmod(const char *filename, const char *args)
 {
@@ -152,7 +148,7 @@ static int rmmod(const char *modname)
     }
 
     if (ret != 0)
-        LOGD("Unable to unload driver module \"%s\": %s\n",
+        ALOGD("Unable to unload driver module \"%s\": %s\n",
              modname, strerror(errno));
     return ret;
 }
@@ -179,18 +175,93 @@ const char *get_dhcp_error_string() {
     return dhcp_lasterror();
 }
 
+#ifdef WIFI_DRIVER_MODULE_PATH
+static int get_driver_path(const char *mod, const char *path, char *buf) {
+    DIR *dir;
+    struct dirent *de;
+    char modpath[SYSFS_PATH_MAX];
+    int ret = 0;
+
+    if ((dir = opendir(path))) {
+        while ((de = readdir(dir))) {
+            struct stat sb;
+            if (de->d_name[0] == '.')
+                continue;
+            snprintf(modpath, SYSFS_PATH_MAX, "%s/%s", path, de->d_name);
+            if (!strcmp(de->d_name, mod)) {
+                strncpy(buf, modpath, SYSFS_PATH_MAX - 1);
+                buf[SYSFS_PATH_MAX - 1] = '\0';
+                ret = 1;
+                break;
+            }
+            if (!stat(modpath, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR)
+                if ((ret = get_driver_path(mod, modpath, buf)))
+                    break;
+        }
+        closedir(dir);
+    }
+    return ret;
+}
+
+static int get_driver_info(char *buf) {
+    DIR *netdir;
+    struct dirent *de;
+    char path[SYSFS_PATH_MAX];
+    char link[SYSFS_PATH_MAX];
+    int ret = 0;
+
+    if ((netdir = opendir(SYSFS_CLASS_NET))) {
+        while ((de = readdir(netdir))) {
+            int cnt;
+            char *pos;
+            if (de->d_name[0] == '.')
+                continue;
+            snprintf(path, SYSFS_PATH_MAX, "%s/%s/wireless", SYSFS_CLASS_NET, de->d_name);
+            if (access(path, F_OK)) {
+                snprintf(path, SYSFS_PATH_MAX, "%s/%s/phy80211", SYSFS_CLASS_NET, de->d_name);
+                if (access(path, F_OK))
+                    continue;
+            }
+            /* found the wifi interface */
+            property_set("wlan.interface", de->d_name);
+            snprintf(path, SYSFS_PATH_MAX, "%s/%s/%s", SYSFS_CLASS_NET, de->d_name, SYS_MOD_NAME_DIR);
+            if ((cnt = readlink(path, link, SYSFS_PATH_MAX - 1)) < 0) {
+                ALOGW("can not find link of %s", path);
+                continue;
+            }
+            link[cnt] = '\0';
+            if ((pos = strrchr(link, '/'))) {
+                property_set(DRIVER_NAME_PROP, ++pos);
+                strncpy(buf, pos, PROPERTY_VALUE_MAX - 1);
+                buf[PROPERTY_VALUE_MAX - 1] = '\0';
+                ret = 1;
+                break;
+            }
+        }
+        closedir(netdir);
+    }
+
+    return ret;
+}
+#endif
+
 int is_wifi_driver_loaded() {
-    char driver_status[PROPERTY_VALUE_MAX];
 #ifdef WIFI_DRIVER_MODULE_PATH
+    char modname[PROPERTY_VALUE_MAX];
+    char line[PROPERTY_VALUE_MAX];
     FILE *proc;
-    char line[sizeof(DRIVER_MODULE_TAG)+10];
-#endif
+    int cnt = property_get(DRIVER_NAME_PROP, modname, NULL);
 
-    if (!property_get(DRIVER_PROP_NAME, driver_status, NULL)
-            || strcmp(driver_status, "ok") != 0) {
-        return 0;  /* driver not loaded */
+    if (!cnt) {
+        if (get_driver_info(modname))
+            cnt = strlen(modname);
+        else if (property_get("wlan.interface", line, NULL))
+            return 1; // found an interface without modname, assume built-in
+        else
+            goto unloaded;
     }
-#ifdef WIFI_DRIVER_MODULE_PATH
+    modname[cnt++] = ' ';
+
     /*
      * If the property says the driver is loaded, check to
      * make sure that the property setting isn't just left
@@ -198,17 +269,19 @@ int is_wifi_driver_loaded() {
      * crash.
      */
     if ((proc = fopen(MODULE_FILE, "r")) == NULL) {
-        LOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
-        property_set(DRIVER_PROP_NAME, "unloaded");
-        return 0;
+        ALOGW("Could not open %s: %s", MODULE_FILE, strerror(errno));
+        goto unloaded;
     }
+
     while ((fgets(line, sizeof(line), proc)) != NULL) {
-        if (strncmp(line, DRIVER_MODULE_TAG, strlen(DRIVER_MODULE_TAG)) == 0) {
+        if (strncmp(line, modname, cnt) == 0) {
             fclose(proc);
             return 1;
         }
     }
     fclose(proc);
+
+unloaded:
     property_set(DRIVER_PROP_NAME, "unloaded");
     return 0;
 #else
@@ -220,13 +293,23 @@ int wifi_load_driver()
 {
 #ifdef WIFI_DRIVER_MODULE_PATH
     char driver_status[PROPERTY_VALUE_MAX];
+    char modname[PROPERTY_VALUE_MAX];
+    char modpath[SYSFS_PATH_MAX];
     int count = 100; /* wait at most 20 seconds for completion */
 
     if (is_wifi_driver_loaded()) {
         return 0;
     }
 
-    if (insmod(DRIVER_MODULE_PATH, DRIVER_MODULE_ARG) < 0)
+    if (!property_get(DRIVER_PATH_PROP, modpath, NULL)) {
+        property_get(DRIVER_NAME_PROP, modname, NULL);
+        strcat(modname, ".ko");
+        if (!get_driver_path(modname, MODULE_DEFAULT_DIR, modpath))
+            strcpy(modpath, WIFI_DRIVER_MODULE_PATH);
+    }
+
+    ALOGI("got module path %s", modpath);
+    if (insmod(modpath, DRIVER_MODULE_ARG) < 0)
         return -1;
 
     if (strcmp(FIRMWARE_LOADER,"") == 0) {
@@ -239,9 +322,10 @@ int wifi_load_driver()
     sched_yield();
     while (count-- > 0) {
         if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
-            if (strcmp(driver_status, "ok") == 0)
+            if (strcmp(driver_status, "ok") == 0) {
+                get_driver_info(modname);
                 return 0;
-            else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
+            else if (strcmp(DRIVER_PROP_NAME, "failed") == 0) {
                 wifi_unload_driver();
                 return -1;
             }
@@ -261,7 +345,14 @@ int wifi_unload_driver()
 {
     usleep(200000); /* allow to finish interface down */
 #ifdef WIFI_DRIVER_MODULE_PATH
-    if (rmmod(DRIVER_MODULE_NAME) == 0) {
+    char modname[PROPERTY_VALUE_MAX];
+    if (!property_get(DRIVER_NAME_PROP, modname, NULL))
+        return -1;
+    if (property_get("wlan.no-unload-driver", modname, NULL)
+            && strcmp(modname, "1") == 0)
+        return 0;
+
+    if (rmmod(modname) == 0) {
         int count = 20; /* wait at most 10 seconds for completion */
         while (count-- > 0) {
             if (!is_wifi_driver_loaded())
@@ -272,9 +363,8 @@ int wifi_unload_driver()
         if (count) {
             return 0;
         }
-        return -1;
-    } else
-        return -1;
+    }
+    return -1;
 #else
     property_set(DRIVER_PROP_NAME, "unloaded");
     return 0;
@@ -290,19 +380,19 @@ int ensure_entropy_file_exists()
     if ((ret == 0) || (errno == EACCES)) {
         if ((ret != 0) &&
             (chmod(SUPP_ENTROPY_FILE, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
-            LOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
+            ALOGE("Cannot set RW to \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
             return -1;
         }
         return 0;
     }
-    destfd = open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660);
+    destfd = TEMP_FAILURE_RETRY(open(SUPP_ENTROPY_FILE, O_CREAT|O_RDWR, 0660));
     if (destfd < 0) {
-        LOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
+        ALOGE("Cannot create \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
         return -1;
     }
 
-    if (write(destfd, dummy_key, sizeof(dummy_key)) != sizeof(dummy_key)) {
-        LOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
+    if (TEMP_FAILURE_RETRY(write(destfd, dummy_key, sizeof(dummy_key))) != sizeof(dummy_key)) {
+        ALOGE("Error writing \"%s\": %s", SUPP_ENTROPY_FILE, strerror(errno));
         close(destfd);
         return -1;
     }
@@ -310,14 +400,14 @@ int ensure_entropy_file_exists()
 
     /* chmod is needed because open() didn't set permisions properly */
     if (chmod(SUPP_ENTROPY_FILE, 0660) < 0) {
-        LOGE("Error changing permissions of %s to 0660: %s",
+        ALOGE("Error changing permissions of %s to 0660: %s",
              SUPP_ENTROPY_FILE, strerror(errno));
         unlink(SUPP_ENTROPY_FILE);
         return -1;
     }
 
     if (chown(SUPP_ENTROPY_FILE, AID_SYSTEM, AID_WIFI) < 0) {
-        LOGE("Error changing group ownership of %s to %d: %s",
+        ALOGE("Error changing group ownership of %s to %d: %s",
              SUPP_ENTROPY_FILE, AID_WIFI, strerror(errno));
         unlink(SUPP_ENTROPY_FILE);
         return -1;
@@ -333,6 +423,7 @@ int update_ctrl_interface(const char *config_file) {
     char *pbuf;
     char *sptr;
     struct stat sb;
+    int ret;
 
     if (stat(config_file, &sb) != 0)
         return -1;
@@ -340,50 +431,66 @@ int update_ctrl_interface(const char *config_file) {
     pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX);
     if (!pbuf)
         return 0;
-    srcfd = open(config_file, O_RDONLY);
+    srcfd = TEMP_FAILURE_RETRY(open(config_file, O_RDONLY));
     if (srcfd < 0) {
-        LOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
+        ALOGE("Cannot open \"%s\": %s", config_file, strerror(errno));
         free(pbuf);
         return 0;
     }
-    nread = read(srcfd, pbuf, sb.st_size);
+    nread = TEMP_FAILURE_RETRY(read(srcfd, pbuf, sb.st_size));
     close(srcfd);
     if (nread < 0) {
-        LOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
+        ALOGE("Cannot read \"%s\": %s", config_file, strerror(errno));
         free(pbuf);
         return 0;
     }
 
     if (!strcmp(config_file, SUPP_CONFIG_FILE)) {
-        property_get("wifi.interface", ifc, WIFI_TEST_INTERFACE);
+        property_get("wlan.interface", ifc, WIFI_TEST_INTERFACE);
     } else {
         strcpy(ifc, CONTROL_IFACE_PATH);
     }
+    /* Assume file is invalid to begin with */
+    ret = -1;
+    /*
+     * if there is a "ctrl_interface=<value>" entry, re-write it ONLY if it is
+     * NOT a directory.  The non-directory value option is an Android add-on
+     * that allows the control interface to be exchanged through an environment
+     * variable (initialized by the "init" program when it starts a service
+     * with a "socket" option).
+     *
+     * The <value> is deemed to be a directory if the "DIR=" form is used or
+     * the value begins with "/".
+     */
     if ((sptr = strstr(pbuf, "ctrl_interface="))) {
-        char *iptr = sptr + strlen("ctrl_interface=");
-        int ilen = 0;
-        int mlen = strlen(ifc);
-        int nwrite;
-        if (strncmp(ifc, iptr, mlen) != 0) {
-            LOGE("ctrl_interface != %s", ifc);
-            while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
-                ilen++;
-            mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
-            memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
-            memset(iptr, '\n', mlen);
-            memcpy(iptr, ifc, strlen(ifc));
-            destfd = open(config_file, O_RDWR, 0660);
-            if (destfd < 0) {
-                LOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
-                free(pbuf);
-                return -1;
+        ret = 0;
+        if ((!strstr(pbuf, "ctrl_interface=DIR=")) &&
+                (!strstr(pbuf, "ctrl_interface=/"))) {
+            char *iptr = sptr + strlen("ctrl_interface=");
+            int ilen = 0;
+            int mlen = strlen(ifc);
+            int nwrite;
+            if (strncmp(ifc, iptr, mlen) != 0) {
+                ALOGE("ctrl_interface != %s", ifc);
+                while (((ilen + (iptr - pbuf)) < nread) && (iptr[ilen] != '\n'))
+                    ilen++;
+                mlen = ((ilen >= mlen) ? ilen : mlen) + 1;
+                memmove(iptr + mlen, iptr + ilen + 1, nread - (iptr + ilen + 1 - pbuf));
+                memset(iptr, '\n', mlen);
+                memcpy(iptr, ifc, strlen(ifc));
+                destfd = TEMP_FAILURE_RETRY(open(config_file, O_RDWR, 0660));
+                if (destfd < 0) {
+                    ALOGE("Cannot update \"%s\": %s", config_file, strerror(errno));
+                    free(pbuf);
+                    return -1;
+                }
+                TEMP_FAILURE_RETRY(write(destfd, pbuf, nread + mlen - ilen -1));
+                close(destfd);
             }
-            write(destfd, pbuf, nread + mlen - ilen -1);
-            close(destfd);
         }
     }
     free(pbuf);
-    return 0;
+    return ret;
 }
 
 int ensure_config_file_exists(const char *config_file)
@@ -398,40 +505,44 @@ int ensure_config_file_exists(const char *config_file)
     if ((ret == 0) || (errno == EACCES)) {
         if ((ret != 0) &&
             (chmod(config_file, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) != 0)) {
-            LOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
+            ALOGE("Cannot set RW to \"%s\": %s", config_file, strerror(errno));
             return -1;
         }
-        /* return if filesize is at least 10 bytes */
-        if (stat(config_file, &sb) == 0 && sb.st_size > 10) {
-            return update_ctrl_interface(config_file);
+        /* return if we were able to update control interface properly */
+        if (update_ctrl_interface(config_file) >=0) {
+            return 0;
+        } else {
+            /* This handles the scenario where the file had bad data
+             * for some reason. We continue and recreate the file.
+             */
         }
     } else if (errno != ENOENT) {
-        LOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
+        ALOGE("Cannot access \"%s\": %s", config_file, strerror(errno));
         return -1;
     }
 
-    srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
+    srcfd = TEMP_FAILURE_RETRY(open(SUPP_CONFIG_TEMPLATE, O_RDONLY));
     if (srcfd < 0) {
-        LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
+        ALOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
         return -1;
     }
 
-    destfd = open(config_file, O_CREAT|O_RDWR, 0660);
+    destfd = TEMP_FAILURE_RETRY(open(config_file, O_CREAT|O_RDWR, 0660));
     if (destfd < 0) {
         close(srcfd);
-        LOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
+        ALOGE("Cannot create \"%s\": %s", config_file, strerror(errno));
         return -1;
     }
 
-    while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
+    while ((nread = TEMP_FAILURE_RETRY(read(srcfd, buf, sizeof(buf)))) != 0) {
         if (nread < 0) {
-            LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
+            ALOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
             close(srcfd);
             close(destfd);
             unlink(config_file);
             return -1;
         }
-        write(destfd, buf, nread);
+        TEMP_FAILURE_RETRY(write(destfd, buf, nread));
     }
 
     close(destfd);
@@ -439,14 +550,14 @@ int ensure_config_file_exists(const char *config_file)
 
     /* chmod is needed because open() didn't set permisions properly */
     if (chmod(config_file, 0660) < 0) {
-        LOGE("Error changing permissions of %s to 0660: %s",
+        ALOGE("Error changing permissions of %s to 0660: %s",
              config_file, strerror(errno));
         unlink(config_file);
         return -1;
     }
 
     if (chown(config_file, AID_SYSTEM, AID_WIFI) < 0) {
-        LOGE("Error changing group ownership of %s to %d: %s",
+        ALOGE("Error changing group ownership of %s to %d: %s",
              config_file, AID_WIFI, strerror(errno));
         unlink(config_file);
         return -1;
@@ -454,46 +565,7 @@ int ensure_config_file_exists(const char *config_file)
     return update_ctrl_interface(config_file);
 }
 
-/**
- * wifi_wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
- * may be left over from clients that were previously connected to
- * wpa_supplicant. This keeps these files from being orphaned in the
- * event of crashes that prevented them from being removed as part
- * of the normal orderly shutdown.
- */
-void wifi_wpa_ctrl_cleanup(void)
-{
-    DIR *dir;
-    struct dirent entry;
-    struct dirent *result;
-    size_t dirnamelen;
-    size_t maxcopy;
-    char pathname[PATH_MAX];
-    char *namep;
-    char *local_socket_dir = CONFIG_CTRL_IFACE_CLIENT_DIR;
-    char *local_socket_prefix = CONFIG_CTRL_IFACE_CLIENT_PREFIX;
-
-    if ((dir = opendir(local_socket_dir)) == NULL)
-        return;
-
-    dirnamelen = (size_t)snprintf(pathname, sizeof(pathname), "%s/", local_socket_dir);
-    if (dirnamelen >= sizeof(pathname)) {
-        closedir(dir);
-        return;
-    }
-    namep = pathname + dirnamelen;
-    maxcopy = PATH_MAX - dirnamelen;
-    while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
-        if (strncmp(entry.d_name, local_socket_prefix, strlen(local_socket_prefix)) == 0) {
-            if (strlcpy(namep, entry.d_name, maxcopy) < maxcopy) {
-                unlink(pathname);
-            }
-        }
-    }
-    closedir(dir);
-}
-
-int wifi_start_supplicant_common(const char *config_file)
+int wifi_start_supplicant(int p2p_supported)
 {
     char daemon_cmd[PROPERTY_VALUE_MAX];
     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
@@ -503,29 +575,42 @@ int wifi_start_supplicant_common(const char *config_file)
     unsigned serial = 0, i;
 #endif
 
+    if (p2p_supported) {
+        strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
+        strcpy(supplicant_prop_name, P2P_PROP_NAME);
+
+        /* Ensure p2p config file is created */
+        if (ensure_config_file_exists(P2P_CONFIG_FILE) < 0) {
+            ALOGE("Failed to create a p2p config file");
+            return -1;
+        }
+
+    } else {
+        strcpy(supplicant_name, SUPPLICANT_NAME);
+        strcpy(supplicant_prop_name, SUPP_PROP_NAME);
+    }
+
     /* Check whether already running */
-    if (property_get(SUPP_PROP_NAME, supp_status, NULL)
+    if (property_get(supplicant_name, supp_status, NULL)
             && strcmp(supp_status, "running") == 0) {
         return 0;
     }
 
     /* Before starting the daemon, make sure its config file exists */
-    if (ensure_config_file_exists(config_file) < 0) {
-        LOGE("Wi-Fi will not be enabled");
+    if (ensure_config_file_exists(SUPP_CONFIG_FILE) < 0) {
+        ALOGE("Wi-Fi will not be enabled");
         return -1;
     }
 
     if (ensure_entropy_file_exists() < 0) {
-        LOGE("Wi-Fi entropy file was not created");
+        ALOGE("Wi-Fi entropy file was not created");
     }
 
     /* Clear out any stale socket files that might be left over. */
-    wifi_wpa_ctrl_cleanup();
+    wpa_ctrl_cleanup();
 
     /* Reset sockets used for exiting from hung state */
-    for (i=0; i<MAX_CONNS; i++) {
-        exit_sockets[i][0] = exit_sockets[i][1] = -1;
-    }
+    exit_sockets[0] = exit_sockets[1] = -1;
 
 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
     /*
@@ -535,33 +620,32 @@ int wifi_start_supplicant_common(const char *config_file)
      * it starts in the stopped state and never manages to start
      * running at all.
      */
-    pi = __system_property_find(SUPP_PROP_NAME);
+    pi = __system_property_find(supplicant_prop_name);
     if (pi != NULL) {
-        serial = pi->serial;
+        serial = __system_property_serial(pi);
     }
 #endif
-    property_get("wifi.interface", primary_iface, WIFI_TEST_INTERFACE);
-    snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s -c%s", SUPPLICANT_NAME, primary_iface,
-            config_file);
+    property_get("wlan.interface", primary_iface, WIFI_TEST_INTERFACE);
+    snprintf(daemon_cmd, PROPERTY_VALUE_MAX, "%s:-i%s", supplicant_name, primary_iface);
     property_set("ctl.start", daemon_cmd);
     sched_yield();
 
     while (count-- > 0) {
 #ifdef HAVE_LIBC_SYSTEM_PROPERTIES
         if (pi == NULL) {
-            pi = __system_property_find(SUPP_PROP_NAME);
+            pi = __system_property_find(supplicant_prop_name);
         }
         if (pi != NULL) {
             __system_property_read(pi, NULL, supp_status);
             if (strcmp(supp_status, "running") == 0) {
                 return 0;
-            } else if (pi->serial != serial &&
+            } else if (__system_property_serial(pi) != serial &&
                     strcmp(supp_status, "stopped") == 0) {
                 return -1;
             }
         }
 #else
-        if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
+        if (property_get(supplicant_prop_name, supp_status, NULL)) {
             if (strcmp(supp_status, "running") == 0)
                 return 0;
         }
@@ -571,74 +655,73 @@ int wifi_start_supplicant_common(const char *config_file)
     return -1;
 }
 
-int wifi_start_supplicant()
-{
-    return wifi_start_supplicant_common(SUPP_CONFIG_FILE);
-}
-
-int wifi_start_p2p_supplicant()
-{
-    return wifi_start_supplicant_common(P2P_CONFIG_FILE);
-}
-
-int wifi_stop_supplicant()
+int wifi_stop_supplicant(int p2p_supported)
 {
     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
     int count = 50; /* wait at most 5 seconds for completion */
 
+    if (p2p_supported) {
+        strcpy(supplicant_name, P2P_SUPPLICANT_NAME);
+        strcpy(supplicant_prop_name, P2P_PROP_NAME);
+    } else {
+        strcpy(supplicant_name, SUPPLICANT_NAME);
+        strcpy(supplicant_prop_name, SUPP_PROP_NAME);
+    }
+
     /* Check whether supplicant already stopped */
-    if (property_get(SUPP_PROP_NAME, supp_status, NULL)
+    if (property_get(supplicant_prop_name, supp_status, NULL)
         && strcmp(supp_status, "stopped") == 0) {
         return 0;
     }
 
-    property_set("ctl.stop", SUPPLICANT_NAME);
+    property_set("ctl.stop", supplicant_name);
     sched_yield();
 
     while (count-- > 0) {
-        if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
+        if (property_get(supplicant_prop_name, supp_status, NULL)) {
             if (strcmp(supp_status, "stopped") == 0)
                 return 0;
         }
         usleep(100000);
     }
+    ALOGE("Failed to stop supplicant");
     return -1;
 }
 
-int wifi_connect_on_socket_path(int index, const char *path)
+int wifi_connect_on_socket_path(const char *path)
 {
     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
 
     /* Make sure supplicant is running */
-    if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
+    if (!property_get(supplicant_prop_name, supp_status, NULL)
             || strcmp(supp_status, "running") != 0) {
-        LOGE("Supplicant not running, cannot connect");
+        ALOGE("Supplicant not running, cannot connect");
         return -1;
     }
 
-    ctrl_conn[index] = wpa_ctrl_open(path);
-    if (ctrl_conn[index] == NULL) {
-        LOGE("Unable to open connection to supplicant on \"%s\": %s",
+    ctrl_conn = wpa_ctrl_open(path);
+    if (ctrl_conn == NULL) {
+        ALOGE("Unable to open connection to supplicant on \"%s\": %s",
              path, strerror(errno));
         return -1;
     }
-    monitor_conn[index] = wpa_ctrl_open(path);
-    if (monitor_conn[index] == NULL) {
-        wpa_ctrl_close(ctrl_conn[index]);
-        ctrl_conn[index] = NULL;
+    monitor_conn = wpa_ctrl_open(path);
+    if (monitor_conn == NULL) {
+        wpa_ctrl_close(ctrl_conn);
+        ctrl_conn = NULL;
         return -1;
     }
-    if (wpa_ctrl_attach(monitor_conn[index]) != 0) {
-        wpa_ctrl_close(monitor_conn[index]);
-        wpa_ctrl_close(ctrl_conn[index]);
-        ctrl_conn[index] = monitor_conn[index] = NULL;
+    if (wpa_ctrl_attach(monitor_conn) != 0) {
+        wpa_ctrl_close(monitor_conn);
+        wpa_ctrl_close(ctrl_conn);
+        ctrl_conn = monitor_conn = NULL;
         return -1;
     }
 
-    if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets[index]) == -1) {
-        wpa_ctrl_close(monitor_conn[index]);
-        wpa_ctrl_close(ctrl_conn[index]);
-        ctrl_conn[index] = monitor_conn[index] = NULL;
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {
+        wpa_ctrl_close(monitor_conn);
+        wpa_ctrl_close(ctrl_conn);
+        ctrl_conn = monitor_conn = NULL;
         return -1;
     }
 
@@ -646,36 +729,30 @@ int wifi_connect_on_socket_path(int index, const char *path)
 }
 
 /* Establishes the control and monitor socket connections on the interface */
-int wifi_connect_to_supplicant(const char *ifname)
+int wifi_connect_to_supplicant()
 {
-    char path[256];
+    static char path[PATH_MAX];
 
-    if (is_primary_interface(ifname)) {
-        if (access(IFACE_DIR, F_OK) == 0) {
-            snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
-        } else {
-            strlcpy(path, primary_iface, sizeof(path));
-        }
-        return wifi_connect_on_socket_path(PRIMARY, path);
+    if (access(IFACE_DIR, F_OK) == 0) {
+        snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface);
     } else {
-        sprintf(path, "%s/%s", CONTROL_IFACE_PATH, ifname);
-        return wifi_connect_on_socket_path(SECONDARY, path);
+        snprintf(path, sizeof(path), "@android:wpa_%s", primary_iface);
     }
+    return wifi_connect_on_socket_path(path);
 }
 
-int wifi_send_command(int index, const char *cmd, char *reply, size_t *reply_len)
+int wifi_send_command(const char *cmd, char *reply, size_t *reply_len)
 {
     int ret;
-
-    if (ctrl_conn[index] == NULL) {
+    if (ctrl_conn == NULL) {
         ALOGV("Not connected to wpa_supplicant - \"%s\" command dropped.\n", cmd);
         return -1;
     }
-    ret = wpa_ctrl_request(ctrl_conn[index], cmd, strlen(cmd), reply, reply_len, NULL);
+    ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), reply, reply_len, NULL);
     if (ret == -2) {
-        LOGD("'%s' command timed out.\n", cmd);
+        ALOGD("'%s' command timed out.\n", cmd);
         /* unblocks the monitor receive socket for termination */
-        write(exit_sockets[index][0], "T", 1);
+        TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1));
         return -2;
     } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
         return -1;
@@ -686,132 +763,137 @@ int wifi_send_command(int index, const char *cmd, char *reply, size_t *reply_len
     return 0;
 }
 
-int wifi_ctrl_recv(int index, char *reply, size_t *reply_len)
+int wifi_ctrl_recv(char *reply, size_t *reply_len)
 {
     int res;
-    int ctrlfd = wpa_ctrl_get_fd(ctrl_conn[index]);
+    int ctrlfd = wpa_ctrl_get_fd(monitor_conn);
     struct pollfd rfds[2];
 
     memset(rfds, 0, 2 * sizeof(struct pollfd));
     rfds[0].fd = ctrlfd;
     rfds[0].events |= POLLIN;
-    rfds[1].fd = exit_sockets[index][1];
+    rfds[1].fd = exit_sockets[1];
     rfds[1].events |= POLLIN;
-    res = poll(rfds, 2, -1);
+    res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1));
     if (res < 0) {
-        LOGE("Error poll = %d", res);
+        ALOGE("Error poll = %d", res);
         return res;
     }
     if (rfds[0].revents & POLLIN) {
-        return wpa_ctrl_recv(monitor_conn[index], reply, reply_len);
-    } else {
-        LOGD("Received on exit socket, terminate");
-        return -1;
+        return wpa_ctrl_recv(monitor_conn, reply, reply_len);
     }
-    return 0;
+
+    /* it is not rfds[0], then it must be rfts[1] (i.e. the exit socket)
+     * or we timed out. In either case, this call has failed ..
+     */
+    return -2;
 }
 
-int wifi_wait_on_socket(int index, char *buf, size_t buflen)
+int wifi_wait_on_socket(char *buf, size_t buflen)
 {
     size_t nread = buflen - 1;
-    int fd;
-    fd_set rfds;
     int result;
-    struct timeval tval;
-    struct timeval *tptr;
+    char *match, *match2;
+
+    if (monitor_conn == NULL) {
+        return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
+    }
+
+    result = wifi_ctrl_recv(buf, &nread);
 
-    if (monitor_conn[index] == NULL) {
-        LOGD("Connection closed\n");
-        strncpy(buf, WPA_EVENT_TERMINATING " - connection closed", buflen-1);
-        buf[buflen-1] = '\0';
-        return strlen(buf);
+    /* Terminate reception on exit socket */
+    if (result == -2) {
+        return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed");
     }
 
-    result = wifi_ctrl_recv(index, buf, &nread);
     if (result < 0) {
-        LOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
-        strncpy(buf, WPA_EVENT_TERMINATING " - recv error", buflen-1);
-        buf[buflen-1] = '\0';
-        return strlen(buf);
+        ALOGD("wifi_ctrl_recv failed: %s\n", strerror(errno));
+        return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - recv error");
     }
     buf[nread] = '\0';
-    /* LOGD("wait_for_event: result=%d nread=%d string=\"%s\"\n", result, nread, buf); */
     /* Check for EOF on the socket */
     if (result == 0 && nread == 0) {
         /* Fabricate an event to pass up */
-        LOGD("Received EOF on supplicant socket\n");
-        strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
-        buf[buflen-1] = '\0';
-        return strlen(buf);
+        ALOGD("Received EOF on supplicant socket\n");
+        return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - signal 0 received");
     }
     /*
      * Events strings are in the format
      *
+     *     IFNAME=iface <N>CTRL-EVENT-XXX 
+     *        or
      *     <N>CTRL-EVENT-XXX 
      *
      * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG,
      * etc.) and XXX is the event name. The level information is not useful
      * to us, so strip it off.
      */
-    if (buf[0] == '<') {
-        char *match = strchr(buf, '>');
+
+    if (strncmp(buf, IFNAME, IFNAMELEN) == 0) {
+        match = strchr(buf, ' ');
         if (match != NULL) {
-            nread -= (match+1-buf);
-            memmove(buf, match+1, nread+1);
+            if (match[1] == '<') {
+                match2 = strchr(match + 2, '>');
+                if (match2 != NULL) {
+                    nread -= (match2 - match);
+                    memmove(match + 1, match2 + 1, nread - (match - buf) + 1);
+                }
+            }
+        } else {
+            return snprintf(buf, buflen, "%s", WPA_EVENT_IGNORE);
         }
+    } else if (buf[0] == '<') {
+        match = strchr(buf, '>');
+        if (match != NULL) {
+            nread -= (match + 1 - buf);
+            memmove(buf, match + 1, nread + 1);
+            ALOGV("supplicant generated event without interface - %s\n", buf);
+        }
+    } else {
+        /* let the event go as is! */
+        ALOGW("supplicant generated event without interface and without message level - %s\n", buf);
     }
+
     return nread;
 }
 
-int wifi_wait_for_event(const char *ifname, char *buf, size_t buflen)
+int wifi_wait_for_event(char *buf, size_t buflen)
 {
-    if (is_primary_interface(ifname)) {
-        return wifi_wait_on_socket(PRIMARY, buf, buflen);
-    } else {
-        return wifi_wait_on_socket(SECONDARY, buf, buflen);
-    }
+    return wifi_wait_on_socket(buf, buflen);
 }
 
-void wifi_close_sockets(int index)
+void wifi_close_sockets()
 {
-    if (ctrl_conn[index] != NULL) {
-        wpa_ctrl_close(ctrl_conn[index]);
-        ctrl_conn[index] = NULL;
+    if (ctrl_conn != NULL) {
+        wpa_ctrl_close(ctrl_conn);
+        ctrl_conn = NULL;
     }
 
-    if (monitor_conn[index] != NULL) {
-        wpa_ctrl_close(monitor_conn[index]);
-        monitor_conn[index] = NULL;
+    if (monitor_conn != NULL) {
+        wpa_ctrl_close(monitor_conn);
+        monitor_conn = NULL;
     }
 
-    if (exit_sockets[index][0] >= 0) {
-        close(exit_sockets[index][0]);
-        exit_sockets[index][0] = -1;
+    if (exit_sockets[0] >= 0) {
+        close(exit_sockets[0]);
+        exit_sockets[0] = -1;
     }
 
-    if (exit_sockets[index][1] >= 0) {
-        close(exit_sockets[index][1]);
-        exit_sockets[index][1] = -1;
+    if (exit_sockets[1] >= 0) {
+        close(exit_sockets[1]);
+        exit_sockets[1] = -1;
     }
 }
 
-void wifi_close_supplicant_connection(const char *ifname)
+void wifi_close_supplicant_connection()
 {
     char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
     int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */
 
-
-    if (is_primary_interface(ifname)) {
-        wifi_close_sockets(PRIMARY);
-    } else {
-        wifi_close_sockets(SECONDARY);
-        //closing p2p connection does not need a wait on
-        //supplicant stop
-        return;
-    }
+    wifi_close_sockets();
 
     while (count-- > 0) {
-        if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
+        if (property_get(supplicant_prop_name, supp_status, NULL)) {
             if (strcmp(supp_status, "stopped") == 0)
                 return;
         }
@@ -819,13 +901,9 @@ void wifi_close_supplicant_connection(const char *ifname)
     }
 }
 
-int wifi_command(const char *ifname, const char *command, char *reply, size_t *reply_len)
+int wifi_command(const char *command, char *reply, size_t *reply_len)
 {
-    if (is_primary_interface(ifname)) {
-        return wifi_send_command(PRIMARY, command, reply, reply_len);
-    } else {
-        return wifi_send_command(SECONDARY, command, reply, reply_len);
-    }
+    return wifi_send_command(command, reply, reply_len);
 }
 
 const char *wifi_get_fw_path(int fw_type)
@@ -849,14 +927,14 @@ int wifi_change_fw_path(const char *fwpath)
 
     if (!fwpath)
         return ret;
-    fd = open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY);
+    fd = TEMP_FAILURE_RETRY(open(WIFI_DRIVER_FW_PATH_PARAM, O_WRONLY));
     if (fd < 0) {
-        LOGE("Failed to open wlan fw path param (%s)", strerror(errno));
+        ALOGE("Failed to open wlan fw path param (%s)", strerror(errno));
         return -1;
     }
     len = strlen(fwpath) + 1;
-    if (write(fd, fwpath, len) != len) {
-        LOGE("Failed to write wlan fw path param (%s)", strerror(errno));
+    if (TEMP_FAILURE_RETRY(write(fd, fwpath, len)) != len) {
+        ALOGE("Failed to write wlan fw path param (%s)", strerror(errno));
         ret = -1;
     }
     close(fd);