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;
50 int android_control_socket;
54 struct ctrl_iface_global_priv {
55 struct wpa_global *global;
57 struct dl_list ctrl_dst;
58 int android_control_socket;
62 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
63 const char *ifname, int sock,
64 struct dl_list *ctrl_dst,
65 int level, const char *buf,
67 struct ctrl_iface_priv *priv,
68 struct ctrl_iface_global_priv *gp);
69 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
70 struct ctrl_iface_priv *priv);
71 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
72 struct ctrl_iface_global_priv *priv);
75 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
76 struct sockaddr_un *from,
79 struct wpa_ctrl_dst *dst;
82 dst = os_zalloc(sizeof(*dst));
85 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
86 dst->addrlen = fromlen;
87 dst->debug_level = MSG_INFO;
88 dl_list_add(ctrl_dst, &dst->list);
89 printf_encode(addr_txt, sizeof(addr_txt),
90 (u8 *) from->sun_path,
91 fromlen - offsetof(struct sockaddr_un, sun_path));
92 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s", addr_txt);
97 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
98 struct sockaddr_un *from,
101 struct wpa_ctrl_dst *dst;
103 dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
104 if (fromlen == dst->addrlen &&
105 os_memcmp(from->sun_path, dst->addr.sun_path,
106 fromlen - offsetof(struct sockaddr_un, sun_path))
109 printf_encode(addr_txt, sizeof(addr_txt),
110 (u8 *) from->sun_path,
112 offsetof(struct sockaddr_un, sun_path));
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
115 dl_list_del(&dst->list);
124 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
125 struct sockaddr_un *from,
129 struct wpa_ctrl_dst *dst;
131 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
133 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
134 if (fromlen == dst->addrlen &&
135 os_memcmp(from->sun_path, dst->addr.sun_path,
136 fromlen - offsetof(struct sockaddr_un, sun_path))
139 dst->debug_level = atoi(level);
140 printf_encode(addr_txt, sizeof(addr_txt),
141 (u8 *) from->sun_path, fromlen -
142 offsetof(struct sockaddr_un, sun_path));
143 wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
144 dst->debug_level, addr_txt);
153 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
156 struct wpa_supplicant *wpa_s = eloop_ctx;
157 struct ctrl_iface_priv *priv = sock_ctx;
160 struct sockaddr_un from;
161 socklen_t fromlen = sizeof(from);
162 char *reply = NULL, *reply_buf = NULL;
163 size_t reply_len = 0;
164 int new_attached = 0;
166 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
167 (struct sockaddr *) &from, &fromlen);
169 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
175 if (os_strcmp(buf, "ATTACH") == 0) {
176 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
183 } else if (os_strcmp(buf, "DETACH") == 0) {
184 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
189 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
190 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
196 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
201 if (!reply && reply_len == 1) {
204 } else if (!reply && reply_len == 2) {
210 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
213 wpa_dbg(wpa_s, MSG_DEBUG,
214 "ctrl_iface sendto failed: %d - %s",
215 _errno, strerror(_errno));
216 if (_errno == ENOBUFS || _errno == EAGAIN) {
218 * The socket send buffer could be full. This
219 * may happen if client programs are not
220 * receiving their pending messages. Close and
221 * reopen the socket as a workaround to avoid
222 * getting stuck being unable to send any new
225 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
227 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
231 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
233 wpa_supplicant_ctrl_iface_detach(
234 &priv->ctrl_dst, &from, fromlen);
241 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
245 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
249 char *pbuf, *dir = NULL;
252 if (wpa_s->conf->ctrl_interface == NULL)
255 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
258 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
261 gid_str = os_strstr(dir, " GROUP=");
267 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
268 buf = os_malloc(len);
274 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
275 if (res < 0 || (size_t) res >= len) {
282 /* Windows/WinPcap uses interface names that are not suitable
283 * as a file name - convert invalid chars to underscores */
291 #endif /* __CYGWIN__ */
297 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, int global,
298 const char *txt, size_t len)
300 struct wpa_supplicant *wpa_s = ctx;
305 if (global != 2 && wpa_s->global->ctrl_iface) {
306 struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
307 if (!dl_list_empty(&priv->ctrl_dst)) {
308 wpa_supplicant_ctrl_iface_send(wpa_s, global ? NULL :
312 level, txt, len, NULL,
317 if (wpa_s->ctrl_iface == NULL)
319 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
320 &wpa_s->ctrl_iface->ctrl_dst,
321 level, txt, len, wpa_s->ctrl_iface,
326 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
327 struct ctrl_iface_priv *priv)
329 struct sockaddr_un addr;
333 char *buf, *dir = NULL, *gid_str = NULL;
338 buf = os_strdup(wpa_s->conf->ctrl_interface);
342 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
343 wpa_s->conf->ctrl_interface);
344 priv->sock = android_get_control_socket(addr.sun_path);
345 if (priv->sock >= 0) {
346 priv->android_control_socket = 1;
350 if (os_strncmp(buf, "DIR=", 4) == 0) {
352 gid_str = os_strstr(dir, " GROUP=");
359 gid_str = wpa_s->conf->ctrl_interface_group;
362 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
363 if (errno == EEXIST) {
364 wpa_printf(MSG_DEBUG, "Using existing control "
365 "interface directory.");
367 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
368 dir, strerror(errno));
375 * wpa_supplicant is started from /init.*.rc on Android and that seems
376 * to be using umask 0077 which would leave the control interface
377 * directory without group access. This breaks things since Wi-Fi
378 * framework assumes that this directory can be accessed by other
379 * applications in the wifi group. Fix this by adding group access even
380 * if umask value would prevent this.
382 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
383 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
385 /* Try to continue anyway */
390 grp = getgrnam(gid_str);
394 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
395 " (from group name '%s')",
398 /* Group name not found - try to parse this as gid */
399 gid = strtol(gid_str, &endp, 10);
400 if (*gid_str == '\0' || *endp != '\0') {
401 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
406 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
411 if (gid_set && chown(dir, -1, gid) < 0) {
412 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
413 dir, (int) gid, strerror(errno));
417 /* Make sure the group can enter and read the directory */
419 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
420 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
425 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
426 sizeof(addr.sun_path)) {
427 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
431 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
432 if (priv->sock < 0) {
433 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
437 os_memset(&addr, 0, sizeof(addr));
438 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
439 addr.sun_len = sizeof(addr);
440 #endif /* __FreeBSD__ */
441 addr.sun_family = AF_UNIX;
442 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
445 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
446 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
447 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
449 if (connect(priv->sock, (struct sockaddr *) &addr,
451 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
452 " allow connections - assuming it was left"
453 "over from forced program termination");
454 if (unlink(fname) < 0) {
455 wpa_printf(MSG_ERROR,
456 "Could not unlink existing ctrl_iface socket '%s': %s",
457 fname, strerror(errno));
460 if (bind(priv->sock, (struct sockaddr *) &addr,
462 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
466 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
467 "ctrl_iface socket '%s'", fname);
469 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
470 "be in use - cannot override it");
471 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
472 "not used anymore", fname);
479 if (gid_set && chown(fname, -1, gid) < 0) {
480 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
481 fname, (int) gid, strerror(errno));
485 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
486 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
487 fname, strerror(errno));
497 * Make socket non-blocking so that we don't hang forever if
498 * target dies unexpectedly.
500 flags = fcntl(priv->sock, F_GETFL);
503 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
504 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
506 /* Not fatal, continue on.*/
510 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
512 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
518 if (priv->sock >= 0) {
531 struct ctrl_iface_priv *
532 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
534 struct ctrl_iface_priv *priv;
536 priv = os_zalloc(sizeof(*priv));
539 dl_list_init(&priv->ctrl_dst);
543 if (wpa_s->conf->ctrl_interface == NULL)
546 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
555 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
556 struct ctrl_iface_priv *priv)
564 * On Android, the control socket being used may be the socket
565 * that is created when wpa_supplicant is started as a /init.*.rc
566 * service. Such a socket is maintained as a key-value pair in
567 * Android's environment. Closing this control socket would leave us
568 * in a bad state with an invalid socket descriptor.
570 if (priv->android_control_socket)
573 eloop_unregister_read_sock(priv->sock);
576 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
583 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
585 struct wpa_ctrl_dst *dst, *prev;
587 if (priv->sock > -1) {
589 char *buf, *dir = NULL;
590 eloop_unregister_read_sock(priv->sock);
591 if (!dl_list_empty(&priv->ctrl_dst)) {
593 * Wait before closing the control socket if
594 * there are any attached monitors in order to allow
595 * them to receive any pending messages.
597 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
598 "monitors to receive messages");
603 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
609 if (priv->wpa_s->conf->ctrl_interface == NULL)
611 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
614 if (os_strncmp(buf, "DIR=", 4) == 0) {
617 gid_str = os_strstr(dir, " GROUP=");
623 if (rmdir(dir) < 0) {
624 if (errno == ENOTEMPTY) {
625 wpa_printf(MSG_DEBUG, "Control interface "
626 "directory not empty - leaving it "
629 wpa_printf(MSG_ERROR,
630 "rmdir[ctrl_interface=%s]: %s",
631 dir, strerror(errno));
638 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
646 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
647 * @ifname: Interface name for global control socket or %NULL
648 * @sock: Local socket fd
649 * @ctrl_dst: List of attached listeners
650 * @level: Priority level of the message
652 * @len: Message length
654 * Send a packet to all monitor programs attached to the control interface.
656 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
657 const char *ifname, int sock,
658 struct dl_list *ctrl_dst,
659 int level, const char *buf,
661 struct ctrl_iface_priv *priv,
662 struct ctrl_iface_global_priv *gp)
664 struct wpa_ctrl_dst *dst, *next;
670 if (sock < 0 || dl_list_empty(ctrl_dst))
673 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
674 if (res < 0 || (size_t) res >= sizeof(levelstr))
678 io[idx].iov_base = "IFNAME=";
681 io[idx].iov_base = (char *) ifname;
682 io[idx].iov_len = os_strlen(ifname);
684 io[idx].iov_base = " ";
688 io[idx].iov_base = levelstr;
689 io[idx].iov_len = os_strlen(levelstr);
691 io[idx].iov_base = (char *) buf;
692 io[idx].iov_len = len;
694 os_memset(&msg, 0, sizeof(msg));
696 msg.msg_iovlen = idx;
698 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
702 if (level < dst->debug_level)
705 printf_encode(addr_txt, sizeof(addr_txt),
706 (u8 *) dst->addr.sun_path, dst->addrlen -
707 offsetof(struct sockaddr_un, sun_path));
708 msg.msg_name = (void *) &dst->addr;
709 msg.msg_namelen = dst->addrlen;
710 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
711 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor sent successfully to %s",
718 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
719 addr_txt, errno, strerror(errno));
722 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
723 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
725 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
729 if (_errno == ENOBUFS || _errno == EAGAIN) {
731 * The socket send buffer could be full. This may happen
732 * if client programs are not receiving their pending
733 * messages. Close and reopen the socket as a workaround
734 * to avoid getting stuck being unable to send any new
738 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
740 sock = wpas_ctrl_iface_global_reinit(
745 wpa_dbg(wpa_s, MSG_DEBUG,
746 "Failed to reinitialize ctrl_iface socket");
754 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
758 struct sockaddr_un from;
759 socklen_t fromlen = sizeof(from);
762 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
763 "attach", priv->wpa_s->ifname);
764 eloop_wait_for_read_sock(priv->sock);
766 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
767 (struct sockaddr *) &from, &fromlen);
769 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
775 if (os_strcmp(buf, "ATTACH") == 0) {
776 /* handle ATTACH signal of first monitor interface */
777 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
779 if (sendto(priv->sock, "OK\n", 3, 0,
780 (struct sockaddr *) &from, fromlen) <
782 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
788 if (sendto(priv->sock, "FAIL\n", 5, 0,
789 (struct sockaddr *) &from, fromlen) <
791 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
796 /* return FAIL for all other signals */
797 if (sendto(priv->sock, "FAIL\n", 5, 0,
798 (struct sockaddr *) &from, fromlen) < 0) {
799 wpa_printf(MSG_DEBUG,
800 "ctrl_iface sendto failed: %s",
808 /* Global ctrl_iface */
810 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
813 struct wpa_global *global = eloop_ctx;
814 struct ctrl_iface_global_priv *priv = sock_ctx;
817 struct sockaddr_un from;
818 socklen_t fromlen = sizeof(from);
819 char *reply = NULL, *reply_buf = NULL;
822 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
823 (struct sockaddr *) &from, &fromlen);
825 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
831 if (os_strcmp(buf, "ATTACH") == 0) {
832 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
837 } else if (os_strcmp(buf, "DETACH") == 0) {
838 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
844 reply_buf = wpa_supplicant_global_ctrl_iface_process(
845 global, buf, &reply_len);
849 if (!reply && reply_len == 1) {
852 } else if (!reply && reply_len == 2) {
858 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
860 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
868 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
869 struct ctrl_iface_global_priv *priv)
871 struct sockaddr_un addr;
872 const char *ctrl = global->params.ctrl_interface;
875 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
878 if (os_strncmp(ctrl, "@android:", 9) == 0) {
879 priv->sock = android_get_control_socket(ctrl + 9);
880 if (priv->sock < 0) {
881 wpa_printf(MSG_ERROR, "Failed to open Android control "
882 "socket '%s'", ctrl + 9);
885 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
887 priv->android_control_socket = 1;
891 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
893 * Backwards compatibility - try to open an Android control
894 * socket and if that fails, assume this was a UNIX domain
897 priv->sock = android_get_control_socket(ctrl);
898 if (priv->sock >= 0) {
899 wpa_printf(MSG_DEBUG,
900 "Using Android control socket '%s'",
902 priv->android_control_socket = 1;
908 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
909 if (priv->sock < 0) {
910 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
914 os_memset(&addr, 0, sizeof(addr));
915 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
916 addr.sun_len = sizeof(addr);
917 #endif /* __FreeBSD__ */
918 addr.sun_family = AF_UNIX;
920 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
921 addr.sun_path[0] = '\0';
922 os_strlcpy(addr.sun_path + 1, ctrl + 10,
923 sizeof(addr.sun_path) - 1);
924 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
926 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
927 "bind(PF_UNIX;%s) failed: %s",
928 ctrl, strerror(errno));
931 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
936 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
937 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
938 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
939 ctrl, strerror(errno));
940 if (connect(priv->sock, (struct sockaddr *) &addr,
942 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
943 " allow connections - assuming it was left"
944 "over from forced program termination");
945 if (unlink(ctrl) < 0) {
946 wpa_printf(MSG_ERROR,
947 "Could not unlink existing ctrl_iface socket '%s': %s",
948 ctrl, strerror(errno));
951 if (bind(priv->sock, (struct sockaddr *) &addr,
953 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
954 ctrl, strerror(errno));
957 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
958 "ctrl_iface socket '%s'",
961 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
962 "be in use - cannot override it");
963 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
970 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
972 if (global->params.ctrl_interface_group) {
973 char *gid_str = global->params.ctrl_interface_group;
978 grp = getgrnam(gid_str);
981 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
982 " (from group name '%s')",
985 /* Group name not found - try to parse this as gid */
986 gid = strtol(gid_str, &endp, 10);
987 if (*gid_str == '\0' || *endp != '\0') {
988 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
992 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
995 if (chown(ctrl, -1, gid) < 0) {
996 wpa_printf(MSG_ERROR,
997 "chown[global_ctrl_interface=%s,gid=%d]: %s",
998 ctrl, (int) gid, strerror(errno));
1002 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
1003 wpa_printf(MSG_ERROR,
1004 "chmod[global_ctrl_interface=%s]: %s",
1005 ctrl, strerror(errno));
1009 if (chmod(ctrl, S_IRWXU) < 0) {
1010 wpa_printf(MSG_DEBUG,
1011 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1012 ctrl, strerror(errno));
1013 /* continue anyway since group change was not required
1021 * Make socket non-blocking so that we don't hang forever if
1022 * target dies unexpectedly.
1024 flags = fcntl(priv->sock, F_GETFL);
1026 flags |= O_NONBLOCK;
1027 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1028 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1030 /* Not fatal, continue on.*/
1034 eloop_register_read_sock(priv->sock,
1035 wpa_supplicant_global_ctrl_iface_receive,
1041 if (priv->sock >= 0) {
1049 struct ctrl_iface_global_priv *
1050 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1052 struct ctrl_iface_global_priv *priv;
1054 priv = os_zalloc(sizeof(*priv));
1057 dl_list_init(&priv->ctrl_dst);
1058 priv->global = global;
1061 if (global->params.ctrl_interface == NULL)
1064 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1069 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1075 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1076 struct ctrl_iface_global_priv *priv)
1080 if (priv->sock <= 0)
1084 * On Android, the control socket being used may be the socket
1085 * that is created when wpa_supplicant is started as a /init.*.rc
1086 * service. Such a socket is maintained as a key-value pair in
1087 * Android's environment. Closing this control socket would leave us
1088 * in a bad state with an invalid socket descriptor.
1090 if (priv->android_control_socket)
1093 eloop_unregister_read_sock(priv->sock);
1096 res = wpas_global_ctrl_iface_open_sock(global, priv);
1104 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1106 struct wpa_ctrl_dst *dst, *prev;
1108 if (priv->sock >= 0) {
1109 eloop_unregister_read_sock(priv->sock);
1112 if (priv->global->params.ctrl_interface)
1113 unlink(priv->global->params.ctrl_interface);
1114 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,