ctrl_iface_unix.c 16 KB

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