2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 #include <cutils/sockets.h>
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "utils/list.h"
23 #include "eapol_supp/eapol_supp_sm.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
28 /* Per-interface ctrl_iface */
31 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
33 * This structure is used to store information about registered control
34 * interface monitors into struct wpa_supplicant. This data is private to
35 * ctrl_iface_unix.c and should not be touched directly from other files.
39 struct sockaddr_un addr;
46 struct ctrl_iface_priv {
47 struct wpa_supplicant *wpa_s;
49 struct dl_list ctrl_dst;
53 struct ctrl_iface_global_priv {
54 struct wpa_global *global;
56 struct dl_list ctrl_dst;
60 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
61 const char *ifname, int sock,
62 struct dl_list *ctrl_dst,
63 int level, const char *buf,
65 struct ctrl_iface_priv *priv,
66 struct ctrl_iface_global_priv *gp);
67 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
68 struct ctrl_iface_priv *priv);
69 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
70 struct ctrl_iface_global_priv *priv);
73 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
74 struct sockaddr_un *from,
77 struct wpa_ctrl_dst *dst;
80 dst = os_zalloc(sizeof(*dst));
83 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
84 dst->addrlen = fromlen;
85 dst->debug_level = MSG_INFO;
86 dl_list_add(ctrl_dst, &dst->list);
87 printf_encode(addr_txt, sizeof(addr_txt),
88 (u8 *) from->sun_path,
89 fromlen - offsetof(struct sockaddr_un, sun_path));
90 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s", addr_txt);
95 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
96 struct sockaddr_un *from,
99 struct wpa_ctrl_dst *dst;
101 dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
102 if (fromlen == dst->addrlen &&
103 os_memcmp(from->sun_path, dst->addr.sun_path,
104 fromlen - offsetof(struct sockaddr_un, sun_path))
107 printf_encode(addr_txt, sizeof(addr_txt),
108 (u8 *) from->sun_path,
110 offsetof(struct sockaddr_un, sun_path));
111 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
113 dl_list_del(&dst->list);
122 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
123 struct sockaddr_un *from,
127 struct wpa_ctrl_dst *dst;
129 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
131 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
132 if (fromlen == dst->addrlen &&
133 os_memcmp(from->sun_path, dst->addr.sun_path,
134 fromlen - offsetof(struct sockaddr_un, sun_path))
137 dst->debug_level = atoi(level);
138 printf_encode(addr_txt, sizeof(addr_txt),
139 (u8 *) from->sun_path, fromlen -
140 offsetof(struct sockaddr_un, sun_path));
141 wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
142 dst->debug_level, addr_txt);
151 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
154 struct wpa_supplicant *wpa_s = eloop_ctx;
155 struct ctrl_iface_priv *priv = sock_ctx;
158 struct sockaddr_un from;
159 socklen_t fromlen = sizeof(from);
160 char *reply = NULL, *reply_buf = NULL;
161 size_t reply_len = 0;
162 int new_attached = 0;
164 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
165 (struct sockaddr *) &from, &fromlen);
167 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
173 if (os_strcmp(buf, "ATTACH") == 0) {
174 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
181 } else if (os_strcmp(buf, "DETACH") == 0) {
182 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
187 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
188 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
194 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
199 if (!reply && reply_len == 1) {
202 } else if (!reply && reply_len == 2) {
208 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
211 wpa_dbg(wpa_s, MSG_DEBUG,
212 "ctrl_iface sendto failed: %d - %s",
213 _errno, strerror(_errno));
214 if (_errno == ENOBUFS || _errno == EAGAIN) {
216 * The socket send buffer could be full. This
217 * may happen if client programs are not
218 * receiving their pending messages. Close and
219 * reopen the socket as a workaround to avoid
220 * getting stuck being unable to send any new
223 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
225 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
229 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
231 wpa_supplicant_ctrl_iface_detach(
232 &priv->ctrl_dst, &from, fromlen);
239 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
243 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
247 char *pbuf, *dir = NULL;
250 if (wpa_s->conf->ctrl_interface == NULL)
253 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
256 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
259 gid_str = os_strstr(dir, " GROUP=");
265 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
266 buf = os_malloc(len);
272 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
273 if (res < 0 || (size_t) res >= len) {
280 /* Windows/WinPcap uses interface names that are not suitable
281 * as a file name - convert invalid chars to underscores */
289 #endif /* __CYGWIN__ */
295 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
296 const char *txt, size_t len)
298 struct wpa_supplicant *wpa_s = ctx;
303 if (global != 2 && wpa_s->global->ctrl_iface) {
304 struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
305 if (!dl_list_empty(&priv->ctrl_dst)) {
306 wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL :
310 level, txt, len, NULL,
315 if (wpa_s->ctrl_iface == NULL)
317 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
318 &wpa_s->ctrl_iface->ctrl_dst,
319 level, txt, len, wpa_s->ctrl_iface,
324 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
325 struct ctrl_iface_priv *priv)
327 struct sockaddr_un addr;
331 char *buf, *dir = NULL, *gid_str = NULL;
336 buf = os_strdup(wpa_s->conf->ctrl_interface);
340 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
341 wpa_s->conf->ctrl_interface);
342 priv->sock = android_get_control_socket(addr.sun_path);
346 if (os_strncmp(buf, "DIR=", 4) == 0) {
348 gid_str = os_strstr(dir, " GROUP=");
355 gid_str = wpa_s->conf->ctrl_interface_group;
358 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
359 if (errno == EEXIST) {
360 wpa_printf(MSG_DEBUG, "Using existing control "
361 "interface directory.");
363 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
364 dir, strerror(errno));
371 * wpa_supplicant is started from /init.*.rc on Android and that seems
372 * to be using umask 0077 which would leave the control interface
373 * directory without group access. This breaks things since Wi-Fi
374 * framework assumes that this directory can be accessed by other
375 * applications in the wifi group. Fix this by adding group access even
376 * if umask value would prevent this.
378 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
379 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
381 /* Try to continue anyway */
386 grp = getgrnam(gid_str);
390 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
391 " (from group name '%s')",
394 /* Group name not found - try to parse this as gid */
395 gid = strtol(gid_str, &endp, 10);
396 if (*gid_str == '\0' || *endp != '\0') {
397 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
402 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
407 if (gid_set && chown(dir, -1, gid) < 0) {
408 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
409 dir, (int) gid, strerror(errno));
413 /* Make sure the group can enter and read the directory */
415 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
416 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
421 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
422 sizeof(addr.sun_path)) {
423 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
427 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
428 if (priv->sock < 0) {
429 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
433 os_memset(&addr, 0, sizeof(addr));
434 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
435 addr.sun_len = sizeof(addr);
436 #endif /* __FreeBSD__ */
437 addr.sun_family = AF_UNIX;
438 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
441 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
442 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
443 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
445 if (connect(priv->sock, (struct sockaddr *) &addr,
447 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
448 " allow connections - assuming it was left"
449 "over from forced program termination");
450 if (unlink(fname) < 0) {
451 wpa_printf(MSG_ERROR,
452 "Could not unlink existing ctrl_iface socket '%s': %s",
453 fname, strerror(errno));
456 if (bind(priv->sock, (struct sockaddr *) &addr,
458 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
462 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
463 "ctrl_iface socket '%s'", fname);
465 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
466 "be in use - cannot override it");
467 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
468 "not used anymore", fname);
475 if (gid_set && chown(fname, -1, gid) < 0) {
476 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
477 fname, (int) gid, strerror(errno));
481 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
482 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
483 fname, strerror(errno));
493 * Make socket non-blocking so that we don't hang forever if
494 * target dies unexpectedly.
496 flags = fcntl(priv->sock, F_GETFL);
499 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
500 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
502 /* Not fatal, continue on.*/
506 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
508 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
514 if (priv->sock >= 0) {
527 struct ctrl_iface_priv *
528 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
530 struct ctrl_iface_priv *priv;
532 priv = os_zalloc(sizeof(*priv));
535 dl_list_init(&priv->ctrl_dst);
539 if (wpa_s->conf->ctrl_interface == NULL)
542 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
551 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
552 struct ctrl_iface_priv *priv)
559 eloop_unregister_read_sock(priv->sock);
562 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
569 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
571 struct wpa_ctrl_dst *dst, *prev;
573 if (priv->sock > -1) {
575 char *buf, *dir = NULL;
576 eloop_unregister_read_sock(priv->sock);
577 if (!dl_list_empty(&priv->ctrl_dst)) {
579 * Wait before closing the control socket if
580 * there are any attached monitors in order to allow
581 * them to receive any pending messages.
583 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
584 "monitors to receive messages");
589 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
595 if (priv->wpa_s->conf->ctrl_interface == NULL)
597 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
600 if (os_strncmp(buf, "DIR=", 4) == 0) {
603 gid_str = os_strstr(dir, " GROUP=");
609 if (rmdir(dir) < 0) {
610 if (errno == ENOTEMPTY) {
611 wpa_printf(MSG_DEBUG, "Control interface "
612 "directory not empty - leaving it "
615 wpa_printf(MSG_ERROR,
616 "rmdir[ctrl_interface=%s]: %s",
617 dir, strerror(errno));
624 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
632 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
633 * @ifname: Interface name for global control socket or %NULL
634 * @sock: Local socket fd
635 * @ctrl_dst: List of attached listeners
636 * @level: Priority level of the message
638 * @len: Message length
640 * Send a packet to all monitor programs attached to the control interface.
642 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
643 const char *ifname, int sock,
644 struct dl_list *ctrl_dst,
645 int level, const char *buf,
647 struct ctrl_iface_priv *priv,
648 struct ctrl_iface_global_priv *gp)
650 struct wpa_ctrl_dst *dst, *next;
656 if (sock < 0 || dl_list_empty(ctrl_dst))
659 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
660 if (res < 0 || (size_t) res >= sizeof(levelstr))
664 io[idx].iov_base = "IFNAME=";
667 io[idx].iov_base = (char *) ifname;
668 io[idx].iov_len = os_strlen(ifname);
670 io[idx].iov_base = " ";
674 io[idx].iov_base = levelstr;
675 io[idx].iov_len = os_strlen(levelstr);
677 io[idx].iov_base = (char *) buf;
678 io[idx].iov_len = len;
680 os_memset(&msg, 0, sizeof(msg));
682 msg.msg_iovlen = idx;
684 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
688 if (level < dst->debug_level)
691 printf_encode(addr_txt, sizeof(addr_txt),
692 (u8 *) dst->addr.sun_path, dst->addrlen -
693 offsetof(struct sockaddr_un, sun_path));
694 msg.msg_name = (void *) &dst->addr;
695 msg.msg_namelen = dst->addrlen;
696 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
697 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor sent successfully to %s",
704 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
705 addr_txt, errno, strerror(errno));
708 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
709 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
711 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
715 if (_errno == ENOBUFS || _errno == EAGAIN) {
717 * The socket send buffer could be full. This may happen
718 * if client programs are not receiving their pending
719 * messages. Close and reopen the socket as a workaround
720 * to avoid getting stuck being unable to send any new
724 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
726 sock = wpas_ctrl_iface_global_reinit(
731 wpa_dbg(wpa_s, MSG_DEBUG,
732 "Failed to reinitialize ctrl_iface socket");
740 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
744 struct sockaddr_un from;
745 socklen_t fromlen = sizeof(from);
748 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
749 "attach", priv->wpa_s->ifname);
750 eloop_wait_for_read_sock(priv->sock);
752 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
753 (struct sockaddr *) &from, &fromlen);
755 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
761 if (os_strcmp(buf, "ATTACH") == 0) {
762 /* handle ATTACH signal of first monitor interface */
763 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
765 if (sendto(priv->sock, "OK\n", 3, 0,
766 (struct sockaddr *) &from, fromlen) <
768 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
774 if (sendto(priv->sock, "FAIL\n", 5, 0,
775 (struct sockaddr *) &from, fromlen) <
777 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
782 /* return FAIL for all other signals */
783 if (sendto(priv->sock, "FAIL\n", 5, 0,
784 (struct sockaddr *) &from, fromlen) < 0) {
785 wpa_printf(MSG_DEBUG,
786 "ctrl_iface sendto failed: %s",
794 /* Global ctrl_iface */
796 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
799 struct wpa_global *global = eloop_ctx;
800 struct ctrl_iface_global_priv *priv = sock_ctx;
803 struct sockaddr_un from;
804 socklen_t fromlen = sizeof(from);
805 char *reply = NULL, *reply_buf = NULL;
808 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
809 (struct sockaddr *) &from, &fromlen);
811 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
817 if (os_strcmp(buf, "ATTACH") == 0) {
818 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
823 } else if (os_strcmp(buf, "DETACH") == 0) {
824 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
830 reply_buf = wpa_supplicant_global_ctrl_iface_process(
831 global, buf, &reply_len);
835 if (!reply && reply_len == 1) {
838 } else if (!reply && reply_len == 2) {
844 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
846 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
854 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
855 struct ctrl_iface_global_priv *priv)
857 struct sockaddr_un addr;
858 const char *ctrl = global->params.ctrl_interface;
861 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
864 if (os_strncmp(ctrl, "@android:", 9) == 0) {
865 priv->sock = android_get_control_socket(ctrl + 9);
866 if (priv->sock < 0) {
867 wpa_printf(MSG_ERROR, "Failed to open Android control "
868 "socket '%s'", ctrl + 9);
871 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
876 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
878 * Backwards compatibility - try to open an Android control
879 * socket and if that fails, assume this was a UNIX domain
882 priv->sock = android_get_control_socket(ctrl);
883 if (priv->sock >= 0) {
884 wpa_printf(MSG_DEBUG,
885 "Using Android control socket '%s'",
892 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
893 if (priv->sock < 0) {
894 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
898 os_memset(&addr, 0, sizeof(addr));
899 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
900 addr.sun_len = sizeof(addr);
901 #endif /* __FreeBSD__ */
902 addr.sun_family = AF_UNIX;
904 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
905 addr.sun_path[0] = '\0';
906 os_strlcpy(addr.sun_path + 1, ctrl + 10,
907 sizeof(addr.sun_path) - 1);
908 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
910 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
911 "bind(PF_UNIX;%s) failed: %s",
912 ctrl, strerror(errno));
915 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
920 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
921 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
922 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
923 ctrl, strerror(errno));
924 if (connect(priv->sock, (struct sockaddr *) &addr,
926 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
927 " allow connections - assuming it was left"
928 "over from forced program termination");
929 if (unlink(ctrl) < 0) {
930 wpa_printf(MSG_ERROR,
931 "Could not unlink existing ctrl_iface socket '%s': %s",
932 ctrl, strerror(errno));
935 if (bind(priv->sock, (struct sockaddr *) &addr,
937 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
938 ctrl, strerror(errno));
941 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
942 "ctrl_iface socket '%s'",
945 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
946 "be in use - cannot override it");
947 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
954 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
956 if (global->params.ctrl_interface_group) {
957 char *gid_str = global->params.ctrl_interface_group;
962 grp = getgrnam(gid_str);
965 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
966 " (from group name '%s')",
969 /* Group name not found - try to parse this as gid */
970 gid = strtol(gid_str, &endp, 10);
971 if (*gid_str == '\0' || *endp != '\0') {
972 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
976 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
979 if (chown(ctrl, -1, gid) < 0) {
980 wpa_printf(MSG_ERROR,
981 "chown[global_ctrl_interface=%s,gid=%d]: %s",
982 ctrl, (int) gid, strerror(errno));
986 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
987 wpa_printf(MSG_ERROR,
988 "chmod[global_ctrl_interface=%s]: %s",
989 ctrl, strerror(errno));
993 if (chmod(ctrl, S_IRWXU) < 0) {
994 wpa_printf(MSG_DEBUG,
995 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
996 ctrl, strerror(errno));
997 /* continue anyway since group change was not required
1005 * Make socket non-blocking so that we don't hang forever if
1006 * target dies unexpectedly.
1008 flags = fcntl(priv->sock, F_GETFL);
1010 flags |= O_NONBLOCK;
1011 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1012 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1014 /* Not fatal, continue on.*/
1018 eloop_register_read_sock(priv->sock,
1019 wpa_supplicant_global_ctrl_iface_receive,
1025 if (priv->sock >= 0) {
1033 struct ctrl_iface_global_priv *
1034 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1036 struct ctrl_iface_global_priv *priv;
1038 priv = os_zalloc(sizeof(*priv));
1041 dl_list_init(&priv->ctrl_dst);
1042 priv->global = global;
1045 if (global->params.ctrl_interface == NULL)
1048 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1053 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1059 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1060 struct ctrl_iface_global_priv *priv)
1064 if (priv->sock <= 0)
1067 eloop_unregister_read_sock(priv->sock);
1070 res = wpas_global_ctrl_iface_open_sock(global, priv);
1078 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1080 struct wpa_ctrl_dst *dst, *prev;
1082 if (priv->sock >= 0) {
1083 eloop_unregister_read_sock(priv->sock);
1086 if (priv->global->params.ctrl_interface)
1087 unlink(priv->global->params.ctrl_interface);
1088 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,