Android: Add wpa_ctrl_cleanup()
authorDmitry Shmidt <dimitrysh@google.com>
Tue, 18 Oct 2011 14:27:53 +0000 (17:27 +0300)
committerJouni Malinen <j@w1.fi>
Tue, 18 Oct 2011 14:27:53 +0000 (17:27 +0300)
This function can be used to clean up local UNIX domain socket files
that may be left over from clients that were previously connected to
wpa_supplicant. At least for now, this is only available for Android
builds.

src/common/wpa_ctrl.c
src/common/wpa_ctrl.h

index 88d3a02..3b25f77 100644 (file)
@@ -21,6 +21,7 @@
 #endif /* CONFIG_CTRL_IFACE_UNIX */
 
 #ifdef ANDROID
+#include <dirent.h>
 #include <cutils/sockets.h>
 #include "private/android_filesystem_config.h"
 #endif /* ANDROID */
@@ -175,6 +176,56 @@ void wpa_ctrl_close(struct wpa_ctrl *ctrl)
        os_free(ctrl);
 }
 
+
+#ifdef ANDROID
+/**
+ * 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 wpa_ctrl_cleanup(void)
+{
+       DIR *dir;
+       struct dirent entry;
+       struct dirent *result;
+       size_t dirnamelen;
+       int prefixlen = os_strlen(CONFIG_CTRL_IFACE_CLIENT_PREFIX);
+       size_t maxcopy;
+       char pathname[PATH_MAX];
+       char *namep;
+
+       if ((dir = opendir(CONFIG_CTRL_IFACE_CLIENT_DIR)) == NULL)
+               return;
+
+       dirnamelen = (size_t) os_snprintf(pathname, sizeof(pathname), "%s/",
+                                         CONFIG_CTRL_IFACE_CLIENT_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 (os_strncmp(entry.d_name, CONFIG_CTRL_IFACE_CLIENT_PREFIX,
+                              prefixlen) == 0) {
+                       if (os_strlcpy(namep, entry.d_name, maxcopy) < maxcopy)
+                               unlink(pathname);
+               }
+       }
+       closedir(dir);
+}
+#endif /* ANDROID */
+
+#else /* CONFIG_CTRL_IFACE_UNIX */
+
+#ifdef ANDROID
+void wpa_ctrl_cleanup(void)
+{
+}
+#endif /* ANDROID */
+
 #endif /* CONFIG_CTRL_IFACE_UNIX */
 
 
index 1d1e3d0..fd096e7 100644 (file)
@@ -264,6 +264,17 @@ int wpa_ctrl_pending(struct wpa_ctrl *ctrl);
  */
 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl);
 
+#ifdef ANDROID
+/**
+ * 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 wpa_ctrl_cleanup(void);
+#endif /* ANDROID */
+
 #ifdef CONFIG_CTRL_IFACE_UDP
 #define WPA_CTRL_IFACE_PORT 9877
 #define WPA_GLOBAL_CTRL_IFACE_PORT 9878