2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2013, 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
;
79 dst
= os_zalloc(sizeof(*dst
));
82 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
83 dst
->addrlen
= fromlen
;
84 dst
->debug_level
= MSG_INFO
;
85 dl_list_add(ctrl_dst
, &dst
->list
);
86 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor attached",
87 (u8
*) from
->sun_path
,
88 fromlen
- offsetof(struct sockaddr_un
, sun_path
));
93 static int wpa_supplicant_ctrl_iface_detach(struct dl_list
*ctrl_dst
,
94 struct sockaddr_un
*from
,
97 struct wpa_ctrl_dst
*dst
;
99 dl_list_for_each(dst
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
100 if (fromlen
== dst
->addrlen
&&
101 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
102 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
104 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor detached",
105 (u8
*) from
->sun_path
,
107 offsetof(struct sockaddr_un
, sun_path
));
108 dl_list_del(&dst
->list
);
117 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv
*priv
,
118 struct sockaddr_un
*from
,
122 struct wpa_ctrl_dst
*dst
;
124 wpa_printf(MSG_DEBUG
, "CTRL_IFACE LEVEL %s", level
);
126 dl_list_for_each(dst
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
, list
) {
127 if (fromlen
== dst
->addrlen
&&
128 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
129 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
131 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE changed monitor "
132 "level", (u8
*) from
->sun_path
,
134 offsetof(struct sockaddr_un
, sun_path
));
135 dst
->debug_level
= atoi(level
);
144 static void wpa_supplicant_ctrl_iface_receive(int sock
, void *eloop_ctx
,
147 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
148 struct ctrl_iface_priv
*priv
= sock_ctx
;
151 struct sockaddr_un from
;
152 socklen_t fromlen
= sizeof(from
);
153 char *reply
= NULL
, *reply_buf
= NULL
;
154 size_t reply_len
= 0;
155 int new_attached
= 0;
157 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
158 (struct sockaddr
*) &from
, &fromlen
);
160 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
166 if (os_strcmp(buf
, "ATTACH") == 0) {
167 if (wpa_supplicant_ctrl_iface_attach(&priv
->ctrl_dst
, &from
,
174 } else if (os_strcmp(buf
, "DETACH") == 0) {
175 if (wpa_supplicant_ctrl_iface_detach(&priv
->ctrl_dst
, &from
,
180 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
181 if (wpa_supplicant_ctrl_iface_level(priv
, &from
, fromlen
,
187 reply_buf
= wpa_supplicant_ctrl_iface_process(wpa_s
, buf
,
192 if (!reply
&& reply_len
== 1) {
195 } else if (!reply
&& reply_len
== 2) {
201 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
204 wpa_dbg(wpa_s
, MSG_DEBUG
,
205 "ctrl_iface sendto failed: %d - %s",
206 _errno
, strerror(_errno
));
207 if (_errno
== ENOBUFS
|| _errno
== EAGAIN
) {
209 * The socket send buffer could be full. This
210 * may happen if client programs are not
211 * receiving their pending messages. Close and
212 * reopen the socket as a workaround to avoid
213 * getting stuck being unable to send any new
216 sock
= wpas_ctrl_iface_reinit(wpa_s
, priv
);
218 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to reinitialize ctrl_iface socket");
222 wpa_dbg(wpa_s
, MSG_DEBUG
, "Failed to send response to ATTACH - detaching");
224 wpa_supplicant_ctrl_iface_detach(
225 &priv
->ctrl_dst
, &from
, fromlen
);
232 eapol_sm_notify_ctrl_attached(wpa_s
->eapol
);
236 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant
*wpa_s
)
240 char *pbuf
, *dir
= NULL
, *gid_str
= NULL
;
243 if (wpa_s
->conf
->ctrl_interface
== NULL
)
246 pbuf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
249 if (os_strncmp(pbuf
, "DIR=", 4) == 0) {
251 gid_str
= os_strstr(dir
, " GROUP=");
259 len
= os_strlen(dir
) + os_strlen(wpa_s
->ifname
) + 2;
260 buf
= os_malloc(len
);
266 res
= os_snprintf(buf
, len
, "%s/%s", dir
, wpa_s
->ifname
);
267 if (res
< 0 || (size_t) res
>= len
) {
274 /* Windows/WinPcap uses interface names that are not suitable
275 * as a file name - convert invalid chars to underscores */
283 #endif /* __CYGWIN__ */
289 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx
, int level
, int global
,
290 const char *txt
, size_t len
)
292 struct wpa_supplicant
*wpa_s
= ctx
;
297 if (global
!= 2 && wpa_s
->global
->ctrl_iface
) {
298 struct ctrl_iface_global_priv
*priv
= wpa_s
->global
->ctrl_iface
;
299 if (!dl_list_empty(&priv
->ctrl_dst
)) {
300 wpa_supplicant_ctrl_iface_send(wpa_s
, global
? NULL
:
304 level
, txt
, len
, NULL
,
309 if (wpa_s
->ctrl_iface
== NULL
)
311 wpa_supplicant_ctrl_iface_send(wpa_s
, NULL
, wpa_s
->ctrl_iface
->sock
,
312 &wpa_s
->ctrl_iface
->ctrl_dst
,
313 level
, txt
, len
, wpa_s
->ctrl_iface
,
318 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant
*wpa_s
,
319 struct ctrl_iface_priv
*priv
)
321 struct sockaddr_un addr
;
325 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
330 buf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
334 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "wpa_%s",
335 wpa_s
->conf
->ctrl_interface
);
336 priv
->sock
= android_get_control_socket(addr
.sun_path
);
340 if (os_strncmp(buf
, "DIR=", 4) == 0) {
342 gid_str
= os_strstr(dir
, " GROUP=");
349 gid_str
= wpa_s
->conf
->ctrl_interface_group
;
352 if (mkdir(dir
, S_IRWXU
| S_IRWXG
) < 0) {
353 if (errno
== EEXIST
) {
354 wpa_printf(MSG_DEBUG
, "Using existing control "
355 "interface directory.");
357 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface=%s]: %s",
358 dir
, strerror(errno
));
365 * wpa_supplicant is started from /init.*.rc on Android and that seems
366 * to be using umask 0077 which would leave the control interface
367 * directory without group access. This breaks things since Wi-Fi
368 * framework assumes that this directory can be accessed by other
369 * applications in the wifi group. Fix this by adding group access even
370 * if umask value would prevent this.
372 if (chmod(dir
, S_IRWXU
| S_IRWXG
) < 0) {
373 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
375 /* Try to continue anyway */
380 grp
= getgrnam(gid_str
);
384 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
385 " (from group name '%s')",
388 /* Group name not found - try to parse this as gid */
389 gid
= strtol(gid_str
, &endp
, 10);
390 if (*gid_str
== '\0' || *endp
!= '\0') {
391 wpa_printf(MSG_ERROR
, "CTRL: Invalid group "
396 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
401 if (gid_set
&& chown(dir
, -1, gid
) < 0) {
402 wpa_printf(MSG_ERROR
, "chown[ctrl_interface=%s,gid=%d]: %s",
403 dir
, (int) gid
, strerror(errno
));
407 /* Make sure the group can enter and read the directory */
409 chmod(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
) < 0) {
410 wpa_printf(MSG_ERROR
, "CTRL: chmod[ctrl_interface]: %s",
415 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
416 sizeof(addr
.sun_path
)) {
417 wpa_printf(MSG_ERROR
, "ctrl_iface path limit exceeded");
421 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
422 if (priv
->sock
< 0) {
423 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
427 os_memset(&addr
, 0, sizeof(addr
));
428 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
429 addr
.sun_len
= sizeof(addr
);
430 #endif /* __FreeBSD__ */
431 addr
.sun_family
= AF_UNIX
;
432 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
435 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
436 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
437 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
439 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
441 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
442 " allow connections - assuming it was left"
443 "over from forced program termination");
444 if (unlink(fname
) < 0) {
445 wpa_printf(MSG_ERROR
,
446 "Could not unlink existing ctrl_iface socket '%s': %s",
447 fname
, strerror(errno
));
450 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
452 wpa_printf(MSG_ERROR
, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
456 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
457 "ctrl_iface socket '%s'", fname
);
459 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
460 "be in use - cannot override it");
461 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
462 "not used anymore", fname
);
469 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
470 wpa_printf(MSG_ERROR
, "chown[ctrl_interface=%s,gid=%d]: %s",
471 fname
, (int) gid
, strerror(errno
));
475 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
476 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface=%s]: %s",
477 fname
, strerror(errno
));
487 * Make socket non-blocking so that we don't hang forever if
488 * target dies unexpectedly.
490 flags
= fcntl(priv
->sock
, F_GETFL
);
493 if (fcntl(priv
->sock
, F_SETFL
, flags
) < 0) {
494 wpa_printf(MSG_INFO
, "fcntl(ctrl, O_NONBLOCK): %s",
496 /* Not fatal, continue on.*/
500 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
502 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
508 if (priv
->sock
>= 0) {
521 struct ctrl_iface_priv
*
522 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant
*wpa_s
)
524 struct ctrl_iface_priv
*priv
;
526 priv
= os_zalloc(sizeof(*priv
));
529 dl_list_init(&priv
->ctrl_dst
);
533 if (wpa_s
->conf
->ctrl_interface
== NULL
)
536 if (wpas_ctrl_iface_open_sock(wpa_s
, priv
) < 0) {
545 static int wpas_ctrl_iface_reinit(struct wpa_supplicant
*wpa_s
,
546 struct ctrl_iface_priv
*priv
)
553 eloop_unregister_read_sock(priv
->sock
);
556 res
= wpas_ctrl_iface_open_sock(wpa_s
, priv
);
563 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
565 struct wpa_ctrl_dst
*dst
, *prev
;
567 if (priv
->sock
> -1) {
569 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
570 eloop_unregister_read_sock(priv
->sock
);
571 if (!dl_list_empty(&priv
->ctrl_dst
)) {
573 * Wait before closing the control socket if
574 * there are any attached monitors in order to allow
575 * them to receive any pending messages.
577 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
578 "monitors to receive messages");
583 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
589 if (priv
->wpa_s
->conf
->ctrl_interface
== NULL
)
591 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
594 if (os_strncmp(buf
, "DIR=", 4) == 0) {
596 gid_str
= os_strstr(dir
, " GROUP=");
604 if (rmdir(dir
) < 0) {
605 if (errno
== ENOTEMPTY
) {
606 wpa_printf(MSG_DEBUG
, "Control interface "
607 "directory not empty - leaving it "
610 wpa_printf(MSG_ERROR
,
611 "rmdir[ctrl_interface=%s]: %s",
612 dir
, strerror(errno
));
619 dl_list_for_each_safe(dst
, prev
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
,
627 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
628 * @ifname: Interface name for global control socket or %NULL
629 * @sock: Local socket fd
630 * @ctrl_dst: List of attached listeners
631 * @level: Priority level of the message
633 * @len: Message length
635 * Send a packet to all monitor programs attached to the control interface.
637 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant
*wpa_s
,
638 const char *ifname
, int sock
,
639 struct dl_list
*ctrl_dst
,
640 int level
, const char *buf
,
642 struct ctrl_iface_priv
*priv
,
643 struct ctrl_iface_global_priv
*gp
)
645 struct wpa_ctrl_dst
*dst
, *next
;
651 if (sock
< 0 || dl_list_empty(ctrl_dst
))
654 res
= os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
655 if (res
< 0 || (size_t) res
>= sizeof(levelstr
))
659 io
[idx
].iov_base
= "IFNAME=";
662 io
[idx
].iov_base
= (char *) ifname
;
663 io
[idx
].iov_len
= os_strlen(ifname
);
665 io
[idx
].iov_base
= " ";
669 io
[idx
].iov_base
= levelstr
;
670 io
[idx
].iov_len
= os_strlen(levelstr
);
672 io
[idx
].iov_base
= (char *) buf
;
673 io
[idx
].iov_len
= len
;
675 os_memset(&msg
, 0, sizeof(msg
));
677 msg
.msg_iovlen
= idx
;
680 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
684 if (level
< dst
->debug_level
)
687 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
688 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
689 offsetof(struct sockaddr_un
, sun_path
));
690 msg
.msg_name
= (void *) &dst
->addr
;
691 msg
.msg_namelen
= dst
->addrlen
;
692 if (sendmsg(sock
, &msg
, MSG_DONTWAIT
) >= 0) {
699 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: %d - %s",
700 idx
, errno
, strerror(errno
));
703 if (dst
->errors
> 10 || _errno
== ENOENT
|| _errno
== EPERM
) {
704 wpa_printf(MSG_INFO
, "CTRL_IFACE: Detach monitor that cannot receive messages");
705 wpa_supplicant_ctrl_iface_detach(ctrl_dst
, &dst
->addr
,
709 if (_errno
== ENOBUFS
|| _errno
== EAGAIN
) {
711 * The socket send buffer could be full. This may happen
712 * if client programs are not receiving their pending
713 * messages. Close and reopen the socket as a workaround
714 * to avoid getting stuck being unable to send any new
718 sock
= wpas_ctrl_iface_reinit(wpa_s
, priv
);
720 sock
= wpas_ctrl_iface_global_reinit(
725 wpa_dbg(wpa_s
, MSG_DEBUG
,
726 "Failed to reinitialize ctrl_iface socket");
734 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
738 struct sockaddr_un from
;
739 socklen_t fromlen
= sizeof(from
);
742 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor to "
743 "attach", priv
->wpa_s
->ifname
);
744 eloop_wait_for_read_sock(priv
->sock
);
746 res
= recvfrom(priv
->sock
, buf
, sizeof(buf
) - 1, 0,
747 (struct sockaddr
*) &from
, &fromlen
);
749 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
755 if (os_strcmp(buf
, "ATTACH") == 0) {
756 /* handle ATTACH signal of first monitor interface */
757 if (!wpa_supplicant_ctrl_iface_attach(&priv
->ctrl_dst
,
759 if (sendto(priv
->sock
, "OK\n", 3, 0,
760 (struct sockaddr
*) &from
, fromlen
) <
762 wpa_printf(MSG_DEBUG
, "ctrl_iface sendto failed: %s",
768 if (sendto(priv
->sock
, "FAIL\n", 5, 0,
769 (struct sockaddr
*) &from
, fromlen
) <
771 wpa_printf(MSG_DEBUG
, "ctrl_iface sendto failed: %s",
776 /* return FAIL for all other signals */
777 if (sendto(priv
->sock
, "FAIL\n", 5, 0,
778 (struct sockaddr
*) &from
, fromlen
) < 0) {
779 wpa_printf(MSG_DEBUG
,
780 "ctrl_iface sendto failed: %s",
788 /* Global ctrl_iface */
790 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
793 struct wpa_global
*global
= eloop_ctx
;
794 struct ctrl_iface_global_priv
*priv
= sock_ctx
;
797 struct sockaddr_un from
;
798 socklen_t fromlen
= sizeof(from
);
799 char *reply
= NULL
, *reply_buf
= NULL
;
802 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
803 (struct sockaddr
*) &from
, &fromlen
);
805 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
811 if (os_strcmp(buf
, "ATTACH") == 0) {
812 if (wpa_supplicant_ctrl_iface_attach(&priv
->ctrl_dst
, &from
,
817 } else if (os_strcmp(buf
, "DETACH") == 0) {
818 if (wpa_supplicant_ctrl_iface_detach(&priv
->ctrl_dst
, &from
,
824 reply_buf
= wpa_supplicant_global_ctrl_iface_process(
825 global
, buf
, &reply_len
);
829 if (!reply
&& reply_len
== 1) {
832 } else if (!reply
&& reply_len
== 2) {
838 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
840 wpa_printf(MSG_DEBUG
, "ctrl_iface sendto failed: %s",
848 static int wpas_global_ctrl_iface_open_sock(struct wpa_global
*global
,
849 struct ctrl_iface_global_priv
*priv
)
851 struct sockaddr_un addr
;
852 const char *ctrl
= global
->params
.ctrl_interface
;
855 wpa_printf(MSG_DEBUG
, "Global control interface '%s'", ctrl
);
858 if (os_strncmp(ctrl
, "@android:", 9) == 0) {
859 priv
->sock
= android_get_control_socket(ctrl
+ 9);
860 if (priv
->sock
< 0) {
861 wpa_printf(MSG_ERROR
, "Failed to open Android control "
862 "socket '%s'", ctrl
+ 9);
865 wpa_printf(MSG_DEBUG
, "Using Android control socket '%s'",
870 if (os_strncmp(ctrl
, "@abstract:", 10) != 0) {
872 * Backwards compatibility - try to open an Android control
873 * socket and if that fails, assume this was a UNIX domain
876 priv
->sock
= android_get_control_socket(ctrl
);
877 if (priv
->sock
>= 0) {
878 wpa_printf(MSG_DEBUG
,
879 "Using Android control socket '%s'",
886 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
887 if (priv
->sock
< 0) {
888 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
892 os_memset(&addr
, 0, sizeof(addr
));
893 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
894 addr
.sun_len
= sizeof(addr
);
895 #endif /* __FreeBSD__ */
896 addr
.sun_family
= AF_UNIX
;
898 if (os_strncmp(ctrl
, "@abstract:", 10) == 0) {
899 addr
.sun_path
[0] = '\0';
900 os_strlcpy(addr
.sun_path
+ 1, ctrl
+ 10,
901 sizeof(addr
.sun_path
) - 1);
902 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
904 wpa_printf(MSG_ERROR
, "supp-global-ctrl-iface-init: "
905 "bind(PF_UNIX;%s) failed: %s",
906 ctrl
, strerror(errno
));
909 wpa_printf(MSG_DEBUG
, "Using Abstract control socket '%s'",
914 os_strlcpy(addr
.sun_path
, ctrl
, sizeof(addr
.sun_path
));
915 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
916 wpa_printf(MSG_INFO
, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
917 ctrl
, strerror(errno
));
918 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
920 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
921 " allow connections - assuming it was left"
922 "over from forced program termination");
923 if (unlink(ctrl
) < 0) {
924 wpa_printf(MSG_ERROR
,
925 "Could not unlink existing ctrl_iface socket '%s': %s",
926 ctrl
, strerror(errno
));
929 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
931 wpa_printf(MSG_ERROR
, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
932 ctrl
, strerror(errno
));
935 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
936 "ctrl_iface socket '%s'",
939 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
940 "be in use - cannot override it");
941 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
948 wpa_printf(MSG_DEBUG
, "Using UNIX control socket '%s'", ctrl
);
950 if (global
->params
.ctrl_interface_group
) {
951 char *gid_str
= global
->params
.ctrl_interface_group
;
956 grp
= getgrnam(gid_str
);
959 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
960 " (from group name '%s')",
963 /* Group name not found - try to parse this as gid */
964 gid
= strtol(gid_str
, &endp
, 10);
965 if (*gid_str
== '\0' || *endp
!= '\0') {
966 wpa_printf(MSG_ERROR
, "CTRL: Invalid group "
970 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
973 if (chown(ctrl
, -1, gid
) < 0) {
974 wpa_printf(MSG_ERROR
,
975 "chown[global_ctrl_interface=%s,gid=%d]: %s",
976 ctrl
, (int) gid
, strerror(errno
));
980 if (chmod(ctrl
, S_IRWXU
| S_IRWXG
) < 0) {
981 wpa_printf(MSG_ERROR
,
982 "chmod[global_ctrl_interface=%s]: %s",
983 ctrl
, strerror(errno
));
987 chmod(ctrl
, S_IRWXU
);
993 * Make socket non-blocking so that we don't hang forever if
994 * target dies unexpectedly.
996 flags
= fcntl(priv
->sock
, F_GETFL
);
999 if (fcntl(priv
->sock
, F_SETFL
, flags
) < 0) {
1000 wpa_printf(MSG_INFO
, "fcntl(ctrl, O_NONBLOCK): %s",
1002 /* Not fatal, continue on.*/
1006 eloop_register_read_sock(priv
->sock
,
1007 wpa_supplicant_global_ctrl_iface_receive
,
1013 if (priv
->sock
>= 0) {
1021 struct ctrl_iface_global_priv
*
1022 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
1024 struct ctrl_iface_global_priv
*priv
;
1026 priv
= os_zalloc(sizeof(*priv
));
1029 dl_list_init(&priv
->ctrl_dst
);
1030 priv
->global
= global
;
1033 if (global
->params
.ctrl_interface
== NULL
)
1036 if (wpas_global_ctrl_iface_open_sock(global
, priv
) < 0) {
1041 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
1047 static int wpas_ctrl_iface_global_reinit(struct wpa_global
*global
,
1048 struct ctrl_iface_global_priv
*priv
)
1052 if (priv
->sock
<= 0)
1055 eloop_unregister_read_sock(priv
->sock
);
1058 res
= wpas_global_ctrl_iface_open_sock(global
, priv
);
1066 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
1068 struct wpa_ctrl_dst
*dst
, *prev
;
1070 if (priv
->sock
>= 0) {
1071 eloop_unregister_read_sock(priv
->sock
);
1074 if (priv
->global
->params
.ctrl_interface
)
1075 unlink(priv
->global
->params
.ctrl_interface
);
1076 dl_list_for_each_safe(dst
, prev
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
,