Make bind failure messages unique
[mech_eap.git] / wpa_supplicant / ctrl_iface_unix.c
1 /*
2  * WPA Supplicant / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10 #include <sys/un.h>
11 #include <sys/stat.h>
12 #include <grp.h>
13 #include <stddef.h>
14 #ifdef ANDROID
15 #include <cutils/sockets.h>
16 #endif /* ANDROID */
17
18 #include "utils/common.h"
19 #include "utils/eloop.h"
20 #include "utils/list.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "config.h"
23 #include "wpa_supplicant_i.h"
24 #include "ctrl_iface.h"
25
26 /* Per-interface ctrl_iface */
27
28 /**
29  * struct wpa_ctrl_dst - Internal data structure of control interface monitors
30  *
31  * This structure is used to store information about registered control
32  * interface monitors into struct wpa_supplicant. This data is private to
33  * ctrl_iface_unix.c and should not be touched directly from other files.
34  */
35 struct wpa_ctrl_dst {
36         struct dl_list list;
37         struct sockaddr_un addr;
38         socklen_t addrlen;
39         int debug_level;
40         int errors;
41 };
42
43
44 struct ctrl_iface_priv {
45         struct wpa_supplicant *wpa_s;
46         int sock;
47         struct dl_list ctrl_dst;
48 };
49
50
51 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
52                                            int level, const char *buf,
53                                            size_t len);
54
55
56 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
57                                             struct sockaddr_un *from,
58                                             socklen_t fromlen)
59 {
60         struct wpa_ctrl_dst *dst;
61
62         dst = os_zalloc(sizeof(*dst));
63         if (dst == NULL)
64                 return -1;
65         os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
66         dst->addrlen = fromlen;
67         dst->debug_level = MSG_INFO;
68         dl_list_add(&priv->ctrl_dst, &dst->list);
69         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70                     (u8 *) from->sun_path,
71                     fromlen - offsetof(struct sockaddr_un, sun_path));
72         return 0;
73 }
74
75
76 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
77                                             struct sockaddr_un *from,
78                                             socklen_t fromlen)
79 {
80         struct wpa_ctrl_dst *dst;
81
82         dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
83                 if (fromlen == dst->addrlen &&
84                     os_memcmp(from->sun_path, dst->addr.sun_path,
85                               fromlen - offsetof(struct sockaddr_un, sun_path))
86                     == 0) {
87                         dl_list_del(&dst->list);
88                         os_free(dst);
89                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
90                                     (u8 *) from->sun_path,
91                                     fromlen -
92                                     offsetof(struct sockaddr_un, sun_path));
93                         return 0;
94                 }
95         }
96         return -1;
97 }
98
99
100 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
101                                            struct sockaddr_un *from,
102                                            socklen_t fromlen,
103                                            char *level)
104 {
105         struct wpa_ctrl_dst *dst;
106
107         wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
108
109         dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
110                 if (fromlen == dst->addrlen &&
111                     os_memcmp(from->sun_path, dst->addr.sun_path,
112                               fromlen - offsetof(struct sockaddr_un, sun_path))
113                     == 0) {
114                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
115                                     "level", (u8 *) from->sun_path,
116                                     fromlen -
117                                     offsetof(struct sockaddr_un, sun_path));
118                         dst->debug_level = atoi(level);
119                         return 0;
120                 }
121         }
122
123         return -1;
124 }
125
126
127 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
128                                               void *sock_ctx)
129 {
130         struct wpa_supplicant *wpa_s = eloop_ctx;
131         struct ctrl_iface_priv *priv = sock_ctx;
132         char buf[4096];
133         int res;
134         struct sockaddr_un from;
135         socklen_t fromlen = sizeof(from);
136         char *reply = NULL;
137         size_t reply_len = 0;
138         int new_attached = 0;
139
140         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
141                        (struct sockaddr *) &from, &fromlen);
142         if (res < 0) {
143                 perror("recvfrom(ctrl_iface)");
144                 return;
145         }
146         buf[res] = '\0';
147
148         if (os_strcmp(buf, "ATTACH") == 0) {
149                 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
150                         reply_len = 1;
151                 else {
152                         new_attached = 1;
153                         reply_len = 2;
154                 }
155         } else if (os_strcmp(buf, "DETACH") == 0) {
156                 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
157                         reply_len = 1;
158                 else
159                         reply_len = 2;
160         } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
161                 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
162                                                     buf + 6))
163                         reply_len = 1;
164                 else
165                         reply_len = 2;
166         } else {
167                 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
168                                                           &reply_len);
169         }
170
171         if (reply) {
172                 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
173                        fromlen);
174                 os_free(reply);
175         } else if (reply_len == 1) {
176                 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
177                        fromlen);
178         } else if (reply_len == 2) {
179                 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
180                        fromlen);
181         }
182
183         if (new_attached)
184                 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
185 }
186
187
188 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
189 {
190         char *buf;
191         size_t len;
192         char *pbuf, *dir = NULL, *gid_str = NULL;
193         int res;
194
195         if (wpa_s->conf->ctrl_interface == NULL)
196                 return NULL;
197
198         pbuf = os_strdup(wpa_s->conf->ctrl_interface);
199         if (pbuf == NULL)
200                 return NULL;
201         if (os_strncmp(pbuf, "DIR=", 4) == 0) {
202                 dir = pbuf + 4;
203                 gid_str = os_strstr(dir, " GROUP=");
204                 if (gid_str) {
205                         *gid_str = '\0';
206                         gid_str += 7;
207                 }
208         } else
209                 dir = pbuf;
210
211         len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
212         buf = os_malloc(len);
213         if (buf == NULL) {
214                 os_free(pbuf);
215                 return NULL;
216         }
217
218         res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
219         if (res < 0 || (size_t) res >= len) {
220                 os_free(pbuf);
221                 os_free(buf);
222                 return NULL;
223         }
224 #ifdef __CYGWIN__
225         {
226                 /* Windows/WinPcap uses interface names that are not suitable
227                  * as a file name - convert invalid chars to underscores */
228                 char *pos = buf;
229                 while (*pos) {
230                         if (*pos == '\\')
231                                 *pos = '_';
232                         pos++;
233                 }
234         }
235 #endif /* __CYGWIN__ */
236         os_free(pbuf);
237         return buf;
238 }
239
240
241 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
242                                              const char *txt, size_t len)
243 {
244         struct wpa_supplicant *wpa_s = ctx;
245         if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
246                 return;
247         wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
248 }
249
250
251 struct ctrl_iface_priv *
252 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
253 {
254         struct ctrl_iface_priv *priv;
255         struct sockaddr_un addr;
256         char *fname = NULL;
257         gid_t gid = 0;
258         int gid_set = 0;
259         char *buf, *dir = NULL, *gid_str = NULL;
260         struct group *grp;
261         char *endp;
262
263         priv = os_zalloc(sizeof(*priv));
264         if (priv == NULL)
265                 return NULL;
266         dl_list_init(&priv->ctrl_dst);
267         priv->wpa_s = wpa_s;
268         priv->sock = -1;
269
270         if (wpa_s->conf->ctrl_interface == NULL)
271                 return priv;
272
273         buf = os_strdup(wpa_s->conf->ctrl_interface);
274         if (buf == NULL)
275                 goto fail;
276 #ifdef ANDROID
277         os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
278                     wpa_s->conf->ctrl_interface);
279         priv->sock = android_get_control_socket(addr.sun_path);
280         if (priv->sock >= 0)
281                 goto havesock;
282 #endif /* ANDROID */
283         if (os_strncmp(buf, "DIR=", 4) == 0) {
284                 dir = buf + 4;
285                 gid_str = os_strstr(dir, " GROUP=");
286                 if (gid_str) {
287                         *gid_str = '\0';
288                         gid_str += 7;
289                 }
290         } else {
291                 dir = buf;
292                 gid_str = wpa_s->conf->ctrl_interface_group;
293         }
294
295         if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
296                 if (errno == EEXIST) {
297                         wpa_printf(MSG_DEBUG, "Using existing control "
298                                    "interface directory.");
299                 } else {
300                         perror("mkdir[ctrl_interface]");
301                         goto fail;
302                 }
303         }
304
305         if (gid_str) {
306                 grp = getgrnam(gid_str);
307                 if (grp) {
308                         gid = grp->gr_gid;
309                         gid_set = 1;
310                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
311                                    " (from group name '%s')",
312                                    (int) gid, gid_str);
313                 } else {
314                         /* Group name not found - try to parse this as gid */
315                         gid = strtol(gid_str, &endp, 10);
316                         if (*gid_str == '\0' || *endp != '\0') {
317                                 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
318                                            "'%s'", gid_str);
319                                 goto fail;
320                         }
321                         gid_set = 1;
322                         wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
323                                    (int) gid);
324                 }
325         }
326
327         if (gid_set && chown(dir, -1, gid) < 0) {
328                 perror("chown[ctrl_interface]");
329                 goto fail;
330         }
331
332         /* Make sure the group can enter and read the directory */
333         if (gid_set &&
334             chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
335                 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
336                            strerror(errno));
337                 goto fail;
338         }
339
340         if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
341             sizeof(addr.sun_path)) {
342                 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
343                 goto fail;
344         }
345
346         priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
347         if (priv->sock < 0) {
348                 perror("socket(PF_UNIX)");
349                 goto fail;
350         }
351
352         os_memset(&addr, 0, sizeof(addr));
353 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
354         addr.sun_len = sizeof(addr);
355 #endif /* __FreeBSD__ */
356         addr.sun_family = AF_UNIX;
357         fname = wpa_supplicant_ctrl_iface_path(wpa_s);
358         if (fname == NULL)
359                 goto fail;
360         os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
361         if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
362                 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
363                            strerror(errno));
364                 if (connect(priv->sock, (struct sockaddr *) &addr,
365                             sizeof(addr)) < 0) {
366                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
367                                    " allow connections - assuming it was left"
368                                    "over from forced program termination");
369                         if (unlink(fname) < 0) {
370                                 perror("unlink[ctrl_iface]");
371                                 wpa_printf(MSG_ERROR, "Could not unlink "
372                                            "existing ctrl_iface socket '%s'",
373                                            fname);
374                                 goto fail;
375                         }
376                         if (bind(priv->sock, (struct sockaddr *) &addr,
377                                  sizeof(addr)) < 0) {
378                                 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
379                                 goto fail;
380                         }
381                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
382                                    "ctrl_iface socket '%s'", fname);
383                 } else {
384                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
385                                    "be in use - cannot override it");
386                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
387                                    "not used anymore", fname);
388                         os_free(fname);
389                         fname = NULL;
390                         goto fail;
391                 }
392         }
393
394         if (gid_set && chown(fname, -1, gid) < 0) {
395                 perror("chown[ctrl_interface/ifname]");
396                 goto fail;
397         }
398
399         if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
400                 perror("chmod[ctrl_interface/ifname]");
401                 goto fail;
402         }
403         os_free(fname);
404
405 #ifdef ANDROID
406 havesock:
407 #endif /* ANDROID */
408         eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
409                                  wpa_s, priv);
410         wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
411
412         os_free(buf);
413         return priv;
414
415 fail:
416         if (priv->sock >= 0)
417                 close(priv->sock);
418         os_free(priv);
419         if (fname) {
420                 unlink(fname);
421                 os_free(fname);
422         }
423         os_free(buf);
424         return NULL;
425 }
426
427
428 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
429 {
430         struct wpa_ctrl_dst *dst, *prev;
431
432         if (priv->sock > -1) {
433                 char *fname;
434                 char *buf, *dir = NULL, *gid_str = NULL;
435                 eloop_unregister_read_sock(priv->sock);
436                 if (!dl_list_empty(&priv->ctrl_dst)) {
437                         /*
438                          * Wait a second before closing the control socket if
439                          * there are any attached monitors in order to allow
440                          * them to receive any pending messages.
441                          */
442                         wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
443                                    "monitors to receive messages");
444                         os_sleep(1, 0);
445                 }
446                 close(priv->sock);
447                 priv->sock = -1;
448                 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
449                 if (fname) {
450                         unlink(fname);
451                         os_free(fname);
452                 }
453
454                 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
455                 if (buf == NULL)
456                         goto free_dst;
457                 if (os_strncmp(buf, "DIR=", 4) == 0) {
458                         dir = buf + 4;
459                         gid_str = os_strstr(dir, " GROUP=");
460                         if (gid_str) {
461                                 *gid_str = '\0';
462                                 gid_str += 7;
463                         }
464                 } else
465                         dir = buf;
466
467                 if (rmdir(dir) < 0) {
468                         if (errno == ENOTEMPTY) {
469                                 wpa_printf(MSG_DEBUG, "Control interface "
470                                            "directory not empty - leaving it "
471                                            "behind");
472                         } else {
473                                 perror("rmdir[ctrl_interface]");
474                         }
475                 }
476                 os_free(buf);
477         }
478
479 free_dst:
480         dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
481                               list)
482                 os_free(dst);
483         os_free(priv);
484 }
485
486
487 /**
488  * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
489  * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
490  * @level: Priority level of the message
491  * @buf: Message data
492  * @len: Message length
493  *
494  * Send a packet to all monitor programs attached to the control interface.
495  */
496 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
497                                            int level, const char *buf,
498                                            size_t len)
499 {
500         struct wpa_ctrl_dst *dst, *next;
501         char levelstr[10];
502         int idx, res;
503         struct msghdr msg;
504         struct iovec io[2];
505
506         if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
507                 return;
508
509         res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
510         if (res < 0 || (size_t) res >= sizeof(levelstr))
511                 return;
512         io[0].iov_base = levelstr;
513         io[0].iov_len = os_strlen(levelstr);
514         io[1].iov_base = (char *) buf;
515         io[1].iov_len = len;
516         os_memset(&msg, 0, sizeof(msg));
517         msg.msg_iov = io;
518         msg.msg_iovlen = 2;
519
520         idx = 0;
521         dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
522                               list) {
523                 if (level >= dst->debug_level) {
524                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
525                                     (u8 *) dst->addr.sun_path, dst->addrlen -
526                                     offsetof(struct sockaddr_un, sun_path));
527                         msg.msg_name = (void *) &dst->addr;
528                         msg.msg_namelen = dst->addrlen;
529                         if (sendmsg(priv->sock, &msg, 0) < 0) {
530                                 int _errno = errno;
531                                 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
532                                            "%d - %s",
533                                            idx, errno, strerror(errno));
534                                 dst->errors++;
535                                 if (dst->errors > 1000 ||
536                                     (_errno != ENOBUFS && dst->errors > 10) ||
537                                     _errno == ENOENT) {
538                                         wpa_supplicant_ctrl_iface_detach(
539                                                 priv, &dst->addr,
540                                                 dst->addrlen);
541                                 }
542                         } else
543                                 dst->errors = 0;
544                 }
545                 idx++;
546         }
547 }
548
549
550 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
551 {
552         char buf[256];
553         int res;
554         struct sockaddr_un from;
555         socklen_t fromlen = sizeof(from);
556
557         for (;;) {
558                 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
559                            "attach", priv->wpa_s->ifname);
560                 eloop_wait_for_read_sock(priv->sock);
561
562                 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
563                                (struct sockaddr *) &from, &fromlen);
564                 if (res < 0) {
565                         perror("recvfrom(ctrl_iface)");
566                         continue;
567                 }
568                 buf[res] = '\0';
569
570                 if (os_strcmp(buf, "ATTACH") == 0) {
571                         /* handle ATTACH signal of first monitor interface */
572                         if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
573                                                               fromlen)) {
574                                 sendto(priv->sock, "OK\n", 3, 0,
575                                        (struct sockaddr *) &from, fromlen);
576                                 /* OK to continue */
577                                 return;
578                         } else {
579                                 sendto(priv->sock, "FAIL\n", 5, 0,
580                                        (struct sockaddr *) &from, fromlen);
581                         }
582                 } else {
583                         /* return FAIL for all other signals */
584                         sendto(priv->sock, "FAIL\n", 5, 0,
585                                (struct sockaddr *) &from, fromlen);
586                 }
587         }
588 }
589
590
591 /* Global ctrl_iface */
592
593 struct ctrl_iface_global_priv {
594         struct wpa_global *global;
595         int sock;
596 };
597
598
599 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
600                                                      void *sock_ctx)
601 {
602         struct wpa_global *global = eloop_ctx;
603         char buf[256];
604         int res;
605         struct sockaddr_un from;
606         socklen_t fromlen = sizeof(from);
607         char *reply;
608         size_t reply_len;
609
610         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
611                        (struct sockaddr *) &from, &fromlen);
612         if (res < 0) {
613                 perror("recvfrom(ctrl_iface)");
614                 return;
615         }
616         buf[res] = '\0';
617
618         reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
619                                                          &reply_len);
620
621         if (reply) {
622                 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
623                        fromlen);
624                 os_free(reply);
625         } else if (reply_len) {
626                 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
627                        fromlen);
628         }
629 }
630
631
632 struct ctrl_iface_global_priv *
633 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
634 {
635         struct ctrl_iface_global_priv *priv;
636         struct sockaddr_un addr;
637
638         priv = os_zalloc(sizeof(*priv));
639         if (priv == NULL)
640                 return NULL;
641         priv->global = global;
642         priv->sock = -1;
643
644         if (global->params.ctrl_interface == NULL)
645                 return priv;
646
647 #ifdef ANDROID
648         priv->sock = android_get_control_socket(global->params.ctrl_interface);
649         if (priv->sock >= 0)
650                 goto havesock;
651 #endif /* ANDROID */
652
653         wpa_printf(MSG_DEBUG, "Global control interface '%s'",
654                    global->params.ctrl_interface);
655
656         priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
657         if (priv->sock < 0) {
658                 perror("socket(PF_UNIX)");
659                 goto fail;
660         }
661
662         os_memset(&addr, 0, sizeof(addr));
663 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
664         addr.sun_len = sizeof(addr);
665 #endif /* __FreeBSD__ */
666         addr.sun_family = AF_UNIX;
667         os_strlcpy(addr.sun_path, global->params.ctrl_interface,
668                    sizeof(addr.sun_path));
669         if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
670                 perror("supp-global-ctrl-iface-init (will try fixup): "
671                        "bind(PF_UNIX)");
672                 if (connect(priv->sock, (struct sockaddr *) &addr,
673                             sizeof(addr)) < 0) {
674                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
675                                    " allow connections - assuming it was left"
676                                    "over from forced program termination");
677                         if (unlink(global->params.ctrl_interface) < 0) {
678                                 perror("unlink[ctrl_iface]");
679                                 wpa_printf(MSG_ERROR, "Could not unlink "
680                                            "existing ctrl_iface socket '%s'",
681                                            global->params.ctrl_interface);
682                                 goto fail;
683                         }
684                         if (bind(priv->sock, (struct sockaddr *) &addr,
685                                  sizeof(addr)) < 0) {
686                                 perror("supp-glb-iface-init: bind(PF_UNIX)");
687                                 goto fail;
688                         }
689                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
690                                    "ctrl_iface socket '%s'",
691                                    global->params.ctrl_interface);
692                 } else {
693                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
694                                    "be in use - cannot override it");
695                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
696                                    "not used anymore",
697                                    global->params.ctrl_interface);
698                         goto fail;
699                 }
700         }
701
702 #ifdef ANDROID
703 havesock:
704 #endif /* ANDROID */
705         eloop_register_read_sock(priv->sock,
706                                  wpa_supplicant_global_ctrl_iface_receive,
707                                  global, NULL);
708
709         return priv;
710
711 fail:
712         if (priv->sock >= 0)
713                 close(priv->sock);
714         os_free(priv);
715         return NULL;
716 }
717
718
719 void
720 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
721 {
722         if (priv->sock >= 0) {
723                 eloop_unregister_read_sock(priv->sock);
724                 close(priv->sock);
725         }
726         if (priv->global->params.ctrl_interface)
727                 unlink(priv->global->params.ctrl_interface);
728         os_free(priv);
729 }