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, *gid_str = 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) {
258 gid_str = os_strstr(dir, " GROUP=");
266 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
267 buf = os_malloc(len);
273 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
274 if (res < 0 || (size_t) res >= len) {
281 /* Windows/WinPcap uses interface names that are not suitable
282 * as a file name - convert invalid chars to underscores */
290 #endif /* __CYGWIN__ */
296 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
297 const char *txt, size_t len)
299 struct wpa_supplicant *wpa_s = ctx;
304 if (global != 2 && wpa_s->global->ctrl_iface) {
305 struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
306 if (!dl_list_empty(&priv->ctrl_dst)) {
307 wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL :
311 level, txt, len, NULL,
316 if (wpa_s->ctrl_iface == NULL)
318 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
319 &wpa_s->ctrl_iface->ctrl_dst,
320 level, txt, len, wpa_s->ctrl_iface,
325 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
326 struct ctrl_iface_priv *priv)
328 struct sockaddr_un addr;
332 char *buf, *dir = NULL, *gid_str = NULL;
337 buf = os_strdup(wpa_s->conf->ctrl_interface);
341 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
342 wpa_s->conf->ctrl_interface);
343 priv->sock = android_get_control_socket(addr.sun_path);
347 if (os_strncmp(buf, "DIR=", 4) == 0) {
349 gid_str = os_strstr(dir, " GROUP=");
356 gid_str = wpa_s->conf->ctrl_interface_group;
359 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
360 if (errno == EEXIST) {
361 wpa_printf(MSG_DEBUG, "Using existing control "
362 "interface directory.");
364 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
365 dir, strerror(errno));
372 * wpa_supplicant is started from /init.*.rc on Android and that seems
373 * to be using umask 0077 which would leave the control interface
374 * directory without group access. This breaks things since Wi-Fi
375 * framework assumes that this directory can be accessed by other
376 * applications in the wifi group. Fix this by adding group access even
377 * if umask value would prevent this.
379 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
380 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
382 /* Try to continue anyway */
387 grp = getgrnam(gid_str);
391 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
392 " (from group name '%s')",
395 /* Group name not found - try to parse this as gid */
396 gid = strtol(gid_str, &endp, 10);
397 if (*gid_str == '\0' || *endp != '\0') {
398 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
403 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
408 if (gid_set && chown(dir, -1, gid) < 0) {
409 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
410 dir, (int) gid, strerror(errno));
414 /* Make sure the group can enter and read the directory */
416 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
417 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
422 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
423 sizeof(addr.sun_path)) {
424 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
428 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
429 if (priv->sock < 0) {
430 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
434 os_memset(&addr, 0, sizeof(addr));
435 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
436 addr.sun_len = sizeof(addr);
437 #endif /* __FreeBSD__ */
438 addr.sun_family = AF_UNIX;
439 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
442 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
443 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
444 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
446 if (connect(priv->sock, (struct sockaddr *) &addr,
448 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
449 " allow connections - assuming it was left"
450 "over from forced program termination");
451 if (unlink(fname) < 0) {
452 wpa_printf(MSG_ERROR,
453 "Could not unlink existing ctrl_iface socket '%s': %s",
454 fname, strerror(errno));
457 if (bind(priv->sock, (struct sockaddr *) &addr,
459 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
463 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
464 "ctrl_iface socket '%s'", fname);
466 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
467 "be in use - cannot override it");
468 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
469 "not used anymore", fname);
476 if (gid_set && chown(fname, -1, gid) < 0) {
477 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
478 fname, (int) gid, strerror(errno));
482 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
483 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
484 fname, strerror(errno));
494 * Make socket non-blocking so that we don't hang forever if
495 * target dies unexpectedly.
497 flags = fcntl(priv->sock, F_GETFL);
500 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
501 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
503 /* Not fatal, continue on.*/
507 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
509 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
515 if (priv->sock >= 0) {
528 struct ctrl_iface_priv *
529 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
531 struct ctrl_iface_priv *priv;
533 priv = os_zalloc(sizeof(*priv));
536 dl_list_init(&priv->ctrl_dst);
540 if (wpa_s->conf->ctrl_interface == NULL)
543 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
552 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
553 struct ctrl_iface_priv *priv)
560 eloop_unregister_read_sock(priv->sock);
563 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
570 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
572 struct wpa_ctrl_dst *dst, *prev;
574 if (priv->sock > -1) {
576 char *buf, *dir = NULL, *gid_str = NULL;
577 eloop_unregister_read_sock(priv->sock);
578 if (!dl_list_empty(&priv->ctrl_dst)) {
580 * Wait before closing the control socket if
581 * there are any attached monitors in order to allow
582 * them to receive any pending messages.
584 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
585 "monitors to receive messages");
590 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
596 if (priv->wpa_s->conf->ctrl_interface == NULL)
598 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
601 if (os_strncmp(buf, "DIR=", 4) == 0) {
603 gid_str = os_strstr(dir, " GROUP=");
611 if (rmdir(dir) < 0) {
612 if (errno == ENOTEMPTY) {
613 wpa_printf(MSG_DEBUG, "Control interface "
614 "directory not empty - leaving it "
617 wpa_printf(MSG_ERROR,
618 "rmdir[ctrl_interface=%s]: %s",
619 dir, strerror(errno));
626 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
634 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
635 * @ifname: Interface name for global control socket or %NULL
636 * @sock: Local socket fd
637 * @ctrl_dst: List of attached listeners
638 * @level: Priority level of the message
640 * @len: Message length
642 * Send a packet to all monitor programs attached to the control interface.
644 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
645 const char *ifname, int sock,
646 struct dl_list *ctrl_dst,
647 int level, const char *buf,
649 struct ctrl_iface_priv *priv,
650 struct ctrl_iface_global_priv *gp)
652 struct wpa_ctrl_dst *dst, *next;
658 if (sock < 0 || dl_list_empty(ctrl_dst))
661 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
662 if (res < 0 || (size_t) res >= sizeof(levelstr))
666 io[idx].iov_base = "IFNAME=";
669 io[idx].iov_base = (char *) ifname;
670 io[idx].iov_len = os_strlen(ifname);
672 io[idx].iov_base = " ";
676 io[idx].iov_base = levelstr;
677 io[idx].iov_len = os_strlen(levelstr);
679 io[idx].iov_base = (char *) buf;
680 io[idx].iov_len = len;
682 os_memset(&msg, 0, sizeof(msg));
684 msg.msg_iovlen = idx;
686 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
690 if (level < dst->debug_level)
693 printf_encode(addr_txt, sizeof(addr_txt),
694 (u8 *) dst->addr.sun_path, dst->addrlen -
695 offsetof(struct sockaddr_un, sun_path));
696 msg.msg_name = (void *) &dst->addr;
697 msg.msg_namelen = dst->addrlen;
698 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
699 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor sent successfully to %s",
706 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
707 addr_txt, errno, strerror(errno));
710 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
711 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
713 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
717 if (_errno == ENOBUFS || _errno == EAGAIN) {
719 * The socket send buffer could be full. This may happen
720 * if client programs are not receiving their pending
721 * messages. Close and reopen the socket as a workaround
722 * to avoid getting stuck being unable to send any new
726 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
728 sock = wpas_ctrl_iface_global_reinit(
733 wpa_dbg(wpa_s, MSG_DEBUG,
734 "Failed to reinitialize ctrl_iface socket");
742 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
746 struct sockaddr_un from;
747 socklen_t fromlen = sizeof(from);
750 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
751 "attach", priv->wpa_s->ifname);
752 eloop_wait_for_read_sock(priv->sock);
754 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
755 (struct sockaddr *) &from, &fromlen);
757 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
763 if (os_strcmp(buf, "ATTACH") == 0) {
764 /* handle ATTACH signal of first monitor interface */
765 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
767 if (sendto(priv->sock, "OK\n", 3, 0,
768 (struct sockaddr *) &from, fromlen) <
770 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
776 if (sendto(priv->sock, "FAIL\n", 5, 0,
777 (struct sockaddr *) &from, fromlen) <
779 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
784 /* return FAIL for all other signals */
785 if (sendto(priv->sock, "FAIL\n", 5, 0,
786 (struct sockaddr *) &from, fromlen) < 0) {
787 wpa_printf(MSG_DEBUG,
788 "ctrl_iface sendto failed: %s",
796 /* Global ctrl_iface */
798 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
801 struct wpa_global *global = eloop_ctx;
802 struct ctrl_iface_global_priv *priv = sock_ctx;
805 struct sockaddr_un from;
806 socklen_t fromlen = sizeof(from);
807 char *reply = NULL, *reply_buf = NULL;
810 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
811 (struct sockaddr *) &from, &fromlen);
813 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
819 if (os_strcmp(buf, "ATTACH") == 0) {
820 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
825 } else if (os_strcmp(buf, "DETACH") == 0) {
826 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
832 reply_buf = wpa_supplicant_global_ctrl_iface_process(
833 global, buf, &reply_len);
837 if (!reply && reply_len == 1) {
840 } else if (!reply && reply_len == 2) {
846 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
848 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
856 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
857 struct ctrl_iface_global_priv *priv)
859 struct sockaddr_un addr;
860 const char *ctrl = global->params.ctrl_interface;
863 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
866 if (os_strncmp(ctrl, "@android:", 9) == 0) {
867 priv->sock = android_get_control_socket(ctrl + 9);
868 if (priv->sock < 0) {
869 wpa_printf(MSG_ERROR, "Failed to open Android control "
870 "socket '%s'", ctrl + 9);
873 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
878 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
880 * Backwards compatibility - try to open an Android control
881 * socket and if that fails, assume this was a UNIX domain
884 priv->sock = android_get_control_socket(ctrl);
885 if (priv->sock >= 0) {
886 wpa_printf(MSG_DEBUG,
887 "Using Android control socket '%s'",
894 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
895 if (priv->sock < 0) {
896 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
900 os_memset(&addr, 0, sizeof(addr));
901 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
902 addr.sun_len = sizeof(addr);
903 #endif /* __FreeBSD__ */
904 addr.sun_family = AF_UNIX;
906 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
907 addr.sun_path[0] = '\0';
908 os_strlcpy(addr.sun_path + 1, ctrl + 10,
909 sizeof(addr.sun_path) - 1);
910 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
912 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
913 "bind(PF_UNIX;%s) failed: %s",
914 ctrl, strerror(errno));
917 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
922 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
923 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
924 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
925 ctrl, strerror(errno));
926 if (connect(priv->sock, (struct sockaddr *) &addr,
928 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
929 " allow connections - assuming it was left"
930 "over from forced program termination");
931 if (unlink(ctrl) < 0) {
932 wpa_printf(MSG_ERROR,
933 "Could not unlink existing ctrl_iface socket '%s': %s",
934 ctrl, strerror(errno));
937 if (bind(priv->sock, (struct sockaddr *) &addr,
939 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
940 ctrl, strerror(errno));
943 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
944 "ctrl_iface socket '%s'",
947 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
948 "be in use - cannot override it");
949 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
956 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
958 if (global->params.ctrl_interface_group) {
959 char *gid_str = global->params.ctrl_interface_group;
964 grp = getgrnam(gid_str);
967 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
968 " (from group name '%s')",
971 /* Group name not found - try to parse this as gid */
972 gid = strtol(gid_str, &endp, 10);
973 if (*gid_str == '\0' || *endp != '\0') {
974 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
978 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
981 if (chown(ctrl, -1, gid) < 0) {
982 wpa_printf(MSG_ERROR,
983 "chown[global_ctrl_interface=%s,gid=%d]: %s",
984 ctrl, (int) gid, strerror(errno));
988 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
989 wpa_printf(MSG_ERROR,
990 "chmod[global_ctrl_interface=%s]: %s",
991 ctrl, strerror(errno));
995 chmod(ctrl, S_IRWXU);
1001 * Make socket non-blocking so that we don't hang forever if
1002 * target dies unexpectedly.
1004 flags = fcntl(priv->sock, F_GETFL);
1006 flags |= O_NONBLOCK;
1007 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1008 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1010 /* Not fatal, continue on.*/
1014 eloop_register_read_sock(priv->sock,
1015 wpa_supplicant_global_ctrl_iface_receive,
1021 if (priv->sock >= 0) {
1029 struct ctrl_iface_global_priv *
1030 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1032 struct ctrl_iface_global_priv *priv;
1034 priv = os_zalloc(sizeof(*priv));
1037 dl_list_init(&priv->ctrl_dst);
1038 priv->global = global;
1041 if (global->params.ctrl_interface == NULL)
1044 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1049 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1055 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1056 struct ctrl_iface_global_priv *priv)
1060 if (priv->sock <= 0)
1063 eloop_unregister_read_sock(priv->sock);
1066 res = wpas_global_ctrl_iface_open_sock(global, priv);
1074 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1076 struct wpa_ctrl_dst *dst, *prev;
1078 if (priv->sock >= 0) {
1079 eloop_unregister_read_sock(priv->sock);
1082 if (priv->global->params.ctrl_interface)
1083 unlink(priv->global->params.ctrl_interface);
1084 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,