android_get_control_socket() 是什麼用途啊?

看見ctrl_iface_unix.c的函式wpa_supplicant_global_ctrl_iface_init()有一段寫到
而ctrl為 wpa_wlan0

		/*
		 * Backwards compatibility - try to open an Android control
		 * socket and if that fails, assume this was a UNIX domain
		 * socket instead.
		 */
		priv->sock = android_get_control_socket(ctrl);
		if (priv->sock >= 0) {
			wpa_printf(MSG_INFO,
				   "Using Android control socket '%s'",
				   ctrl);
			goto havesock;
		}

这个android_get_control_socket函数有什么作用呢?
android_get_control_socket函数定义在/system/core/include/cutils/sockets.h:

#define ANDROID_SOCKET_ENV_PREFIX   "ANDROID_SOCKET_"
#define ANDROID_SOCKET_DIR      "/dev/socket"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * android_get_control_socket - simple helper function to get the file
 * descriptor of our init-managed Unix domain socket. `name' is the name of the
 * socket, as given in init.rc. Returns -1 on error.
翻成中文就是
android_get_control_socket - 簡單的輔助功能,
讓我們的初始化管理Unix domain socket的文件描述符(file descriptor)。 
參數`name'是socket的名字,可在init.rc類型檔案被找到. 
如果Return -1表示錯誤。
 *
 * This is inline and not in libcutils proper because we want to use this in
 * third-party daemons with minimal modification.
 */
static inline int android_get_control_socket(const char *name)
{
    char key[64] = ANDROID_SOCKET_ENV_PREFIX;
    const char *val;
    int fd;

    /* build our environment variable, counting cycles like a wolf ... */
#if HAVE_STRLCPY
    strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
        name,
        sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
#else   /* for the host, which may lack the almightly strncpy ... */
    strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
        name,
        sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
    key[sizeof(key)-1] = '\0';
#endif

    val = getenv(key); // socket名字和对应的socket文件的文件描述符以键值对的形式存储在环境变量中, 
    if (!val)
        return -1;

    errno = 0;
    fd = strtol(val, NULL, 10);
    if (errno)
        return -1;

    return fd;
}

也就是說…

//这一步很关键,就是获取init.rc中配置的名为 “SOCKET_NAME定義的字串" 的socket
fdListen = android_get_control_socket(SOCKET_NAME);

發表迴響