[PATCH 1/2] Add Android socket support for control part

Jouni Malinen j
Sun Feb 27 08:49:28 PST 2011


On Thu, Feb 24, 2011 at 04:00:40PM -0800, Dmitry Shmidt wrote:
> +#ifdef ANDROID
> +static const char *local_socket_dir = "/data/misc/wifi/sockets";
> +static const char *local_socket_prefix = "wpa_ctrl_";
> +#endif /* ANDROID */

I made this a bit generic and to get this particular behavior, you would
just add the following line to Android.mk:
L_CFLAGS += -DCONFIG_CTRL_IFACE_CLIENT_DIR=\"/data/misc/wifi/sockets\"

> +	/*
> +	 * If the ctrl_path isn't an absolute pathname, assume that
> +	 * it's the name of a socket in the Android reserved namespace.
> +	 * Otherwise, it's a normal UNIX domain socket appearing in the
> +	 * filesystem.
> +	 */

Is the Android reserved namespace used always for the wpa_supplicant
control interface (both for the per-interface one and for the global
one)?

> +/**
> + * 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)

This looks like a bit dangerous operation.. What program would be using
this? Please note that there may be multiple programs using the control
interface and not any single one of those should be in control of this
type of cleanup. Wouldn't it be simpler to just run "rm
/data/misc/wifi/sockets/wpa_ctrl_*" just before starting wpa_supplicant?

Another option could be to use abstract namespace for the client socket.
It should get cleaned up automatically whenever the client program
termiantes. Originally, I did not want to use this because of avoiding
Linux specific extensions, but that should be fine with ANDROID (or
__linux__ for that matter) #ifdef..


I merged other parts of the control interface socket changes, but this
one is still pending while we figure out what is the best way of
handling the cleanup:


diff --git a/src/common/wpa_ctrl.c b/src/common/wpa_ctrl.c
index 546699b..9fd34c8 100644
--- a/src/common/wpa_ctrl.c
+++ b/src/common/wpa_ctrl.c
@@ -21,6 +21,8 @@
 #endif /* CONFIG_CTRL_IFACE_UNIX */
 
 #ifdef ANDROID
+#include <dirent.h>
+#include <linux/limits.h>
 #include <cutils/sockets.h>
 #include "private/android_filesystem_config.h"
 #endif /* ANDROID */
@@ -178,6 +180,50 @@ void wpa_ctrl_close(struct wpa_ctrl *ctrl)
 #endif /* CONFIG_CTRL_IFACE_UNIX */
 
 
+#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)
+{
+#ifdef CONFIG_CTRL_IFACE_UNIX
+	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 /* CONFIG_CTRL_IFACE_UNIX */
+}
+#endif /* ANDROID */
+
+
 #ifdef CONFIG_CTRL_IFACE_UDP
 
 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
diff --git a/src/common/wpa_ctrl.h b/src/common/wpa_ctrl.h
index 528cc16..4f70993 100644
--- a/src/common/wpa_ctrl.h
+++ b/src/common/wpa_ctrl.h
@@ -261,6 +261,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
 
-- 
Jouni Malinen                                            PGP id EFC895FA



More information about the Hostap mailing list