eloop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Event loop based on select() loop
  3. * Copyright (c) 2002-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. #include "includes.h"
  9. #include "common.h"
  10. #include "trace.h"
  11. #include "list.h"
  12. #include "eloop.h"
  13. #ifdef CONFIG_ELOOP_POLL
  14. #include <assert.h>
  15. #include <poll.h>
  16. #endif /* CONFIG_ELOOP_POLL */
  17. struct eloop_sock {
  18. int sock;
  19. void *eloop_data;
  20. void *user_data;
  21. eloop_sock_handler handler;
  22. WPA_TRACE_REF(eloop);
  23. WPA_TRACE_REF(user);
  24. WPA_TRACE_INFO
  25. };
  26. struct eloop_timeout {
  27. struct dl_list list;
  28. struct os_time time;
  29. void *eloop_data;
  30. void *user_data;
  31. eloop_timeout_handler handler;
  32. WPA_TRACE_REF(eloop);
  33. WPA_TRACE_REF(user);
  34. WPA_TRACE_INFO
  35. };
  36. struct eloop_signal {
  37. int sig;
  38. void *user_data;
  39. eloop_signal_handler handler;
  40. int signaled;
  41. };
  42. struct eloop_sock_table {
  43. int count;
  44. struct eloop_sock *table;
  45. int changed;
  46. };
  47. struct eloop_data {
  48. int max_sock;
  49. int count; /* sum of all table counts */
  50. #ifdef CONFIG_ELOOP_POLL
  51. int max_pollfd_map; /* number of pollfds_map currently allocated */
  52. int max_poll_fds; /* number of pollfds currently allocated */
  53. struct pollfd *pollfds;
  54. struct pollfd **pollfds_map;
  55. #endif /* CONFIG_ELOOP_POLL */
  56. struct eloop_sock_table readers;
  57. struct eloop_sock_table writers;
  58. struct eloop_sock_table exceptions;
  59. struct dl_list timeout;
  60. int signal_count;
  61. struct eloop_signal *signals;
  62. int signaled;
  63. int pending_terminate;
  64. int terminate;
  65. int reader_table_changed;
  66. };
  67. static struct eloop_data eloop;
  68. #ifdef WPA_TRACE
  69. static void eloop_sigsegv_handler(int sig)
  70. {
  71. wpa_trace_show("eloop SIGSEGV");
  72. abort();
  73. }
  74. static void eloop_trace_sock_add_ref(struct eloop_sock_table *table)
  75. {
  76. int i;
  77. if (table == NULL || table->table == NULL)
  78. return;
  79. for (i = 0; i < table->count; i++) {
  80. wpa_trace_add_ref(&table->table[i], eloop,
  81. table->table[i].eloop_data);
  82. wpa_trace_add_ref(&table->table[i], user,
  83. table->table[i].user_data);
  84. }
  85. }
  86. static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
  87. {
  88. int i;
  89. if (table == NULL || table->table == NULL)
  90. return;
  91. for (i = 0; i < table->count; i++) {
  92. wpa_trace_remove_ref(&table->table[i], eloop,
  93. table->table[i].eloop_data);
  94. wpa_trace_remove_ref(&table->table[i], user,
  95. table->table[i].user_data);
  96. }
  97. }
  98. #else /* WPA_TRACE */
  99. #define eloop_trace_sock_add_ref(table) do { } while (0)
  100. #define eloop_trace_sock_remove_ref(table) do { } while (0)
  101. #endif /* WPA_TRACE */
  102. int eloop_init(void)
  103. {
  104. os_memset(&eloop, 0, sizeof(eloop));
  105. dl_list_init(&eloop.timeout);
  106. #ifdef WPA_TRACE
  107. signal(SIGSEGV, eloop_sigsegv_handler);
  108. #endif /* WPA_TRACE */
  109. return 0;
  110. }
  111. static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
  112. int sock, eloop_sock_handler handler,
  113. void *eloop_data, void *user_data)
  114. {
  115. struct eloop_sock *tmp;
  116. int new_max_sock;
  117. if (sock > eloop.max_sock)
  118. new_max_sock = sock;
  119. else
  120. new_max_sock = eloop.max_sock;
  121. if (table == NULL)
  122. return -1;
  123. #ifdef CONFIG_ELOOP_POLL
  124. if (new_max_sock >= eloop.max_pollfd_map) {
  125. struct pollfd **nmap;
  126. nmap = os_realloc(eloop.pollfds_map, sizeof(struct pollfd *) *
  127. (new_max_sock + 50));
  128. if (nmap == NULL)
  129. return -1;
  130. eloop.max_pollfd_map = new_max_sock + 50;
  131. eloop.pollfds_map = nmap;
  132. }
  133. if (eloop.count + 1 > eloop.max_poll_fds) {
  134. struct pollfd *n;
  135. int nmax = eloop.count + 1 + 50;
  136. n = os_realloc(eloop.pollfds, sizeof(struct pollfd) * nmax);
  137. if (n == NULL)
  138. return -1;
  139. eloop.max_poll_fds = nmax;
  140. eloop.pollfds = n;
  141. }
  142. #endif /* CONFIG_ELOOP_POLL */
  143. eloop_trace_sock_remove_ref(table);
  144. tmp = (struct eloop_sock *)
  145. os_realloc(table->table,
  146. (table->count + 1) * sizeof(struct eloop_sock));
  147. if (tmp == NULL)
  148. return -1;
  149. tmp[table->count].sock = sock;
  150. tmp[table->count].eloop_data = eloop_data;
  151. tmp[table->count].user_data = user_data;
  152. tmp[table->count].handler = handler;
  153. wpa_trace_record(&tmp[table->count]);
  154. table->count++;
  155. table->table = tmp;
  156. eloop.max_sock = new_max_sock;
  157. eloop.count++;
  158. table->changed = 1;
  159. eloop_trace_sock_add_ref(table);
  160. return 0;
  161. }
  162. static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
  163. int sock)
  164. {
  165. int i;
  166. if (table == NULL || table->table == NULL || table->count == 0)
  167. return;
  168. for (i = 0; i < table->count; i++) {
  169. if (table->table[i].sock == sock)
  170. break;
  171. }
  172. if (i == table->count)
  173. return;
  174. eloop_trace_sock_remove_ref(table);
  175. if (i != table->count - 1) {
  176. os_memmove(&table->table[i], &table->table[i + 1],
  177. (table->count - i - 1) *
  178. sizeof(struct eloop_sock));
  179. }
  180. table->count--;
  181. eloop.count--;
  182. table->changed = 1;
  183. eloop_trace_sock_add_ref(table);
  184. }
  185. #ifdef CONFIG_ELOOP_POLL
  186. static struct pollfd * find_pollfd(struct pollfd **pollfds_map, int fd, int mx)
  187. {
  188. if (fd < mx && fd >= 0)
  189. return pollfds_map[fd];
  190. return NULL;
  191. }
  192. static int eloop_sock_table_set_fds(struct eloop_sock_table *readers,
  193. struct eloop_sock_table *writers,
  194. struct eloop_sock_table *exceptions,
  195. struct pollfd *pollfds,
  196. struct pollfd **pollfds_map,
  197. int max_pollfd_map)
  198. {
  199. int i;
  200. int nxt = 0;
  201. int fd;
  202. struct pollfd *pfd;
  203. /* Clear pollfd lookup map. It will be re-populated below. */
  204. os_memset(pollfds_map, 0, sizeof(struct pollfd *) * max_pollfd_map);
  205. if (readers && readers->table) {
  206. for (i = 0; i < readers->count; i++) {
  207. fd = readers->table[i].sock;
  208. assert(fd >= 0 && fd < max_pollfd_map);
  209. pollfds[nxt].fd = fd;
  210. pollfds[nxt].events = POLLIN;
  211. pollfds[nxt].revents = 0;
  212. pollfds_map[fd] = &(pollfds[nxt]);
  213. nxt++;
  214. }
  215. }
  216. if (writers && writers->table) {
  217. for (i = 0; i < writers->count; i++) {
  218. /*
  219. * See if we already added this descriptor, update it
  220. * if so.
  221. */
  222. fd = writers->table[i].sock;
  223. assert(fd >= 0 && fd < max_pollfd_map);
  224. pfd = pollfds_map[fd];
  225. if (!pfd) {
  226. pfd = &(pollfds[nxt]);
  227. pfd->events = 0;
  228. pfd->fd = fd;
  229. pollfds[i].revents = 0;
  230. pollfds_map[fd] = pfd;
  231. nxt++;
  232. }
  233. pfd->events |= POLLIN;
  234. }
  235. }
  236. /*
  237. * Exceptions are always checked when using poll, but I suppose it's
  238. * possible that someone registered a socket *only* for exception
  239. * handling. Set the POLLIN bit in this case.
  240. */
  241. if (exceptions && exceptions->table) {
  242. for (i = 0; i < exceptions->count; i++) {
  243. /*
  244. * See if we already added this descriptor, just use it
  245. * if so.
  246. */
  247. fd = exceptions->table[i].sock;
  248. assert(fd >= 0 && fd < max_pollfd_map);
  249. pfd = pollfds_map[fd];
  250. if (!pfd) {
  251. pfd = &(pollfds[nxt]);
  252. pfd->events = POLLIN;
  253. pfd->fd = fd;
  254. pollfds[i].revents = 0;
  255. pollfds_map[fd] = pfd;
  256. nxt++;
  257. }
  258. }
  259. }
  260. return nxt;
  261. }
  262. static int eloop_sock_table_dispatch_table(struct eloop_sock_table *table,
  263. struct pollfd **pollfds_map,
  264. int max_pollfd_map,
  265. short int revents)
  266. {
  267. int i;
  268. struct pollfd *pfd;
  269. if (!table || !table->table)
  270. return 0;
  271. table->changed = 0;
  272. for (i = 0; i < table->count; i++) {
  273. pfd = find_pollfd(pollfds_map, table->table[i].sock,
  274. max_pollfd_map);
  275. if (!pfd)
  276. continue;
  277. if (!(pfd->revents & revents))
  278. continue;
  279. table->table[i].handler(table->table[i].sock,
  280. table->table[i].eloop_data,
  281. table->table[i].user_data);
  282. if (table->changed)
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. static void eloop_sock_table_dispatch(struct eloop_sock_table *readers,
  288. struct eloop_sock_table *writers,
  289. struct eloop_sock_table *exceptions,
  290. struct pollfd **pollfds_map,
  291. int max_pollfd_map)
  292. {
  293. if (eloop_sock_table_dispatch_table(readers, pollfds_map,
  294. max_pollfd_map, POLLIN))
  295. return; /* pollfds may be invalid at this point */
  296. if (eloop_sock_table_dispatch_table(writers, pollfds_map,
  297. max_pollfd_map, POLLOUT))
  298. return; /* pollfds may be invalid at this point */
  299. eloop_sock_table_dispatch_table(exceptions, pollfds_map,
  300. max_pollfd_map, POLLERR | POLLHUP);
  301. }
  302. #else /* CONFIG_ELOOP_POLL */
  303. static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
  304. fd_set *fds)
  305. {
  306. int i;
  307. FD_ZERO(fds);
  308. if (table->table == NULL)
  309. return;
  310. for (i = 0; i < table->count; i++)
  311. FD_SET(table->table[i].sock, fds);
  312. }
  313. static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
  314. fd_set *fds)
  315. {
  316. int i;
  317. if (table == NULL || table->table == NULL)
  318. return;
  319. table->changed = 0;
  320. for (i = 0; i < table->count; i++) {
  321. if (FD_ISSET(table->table[i].sock, fds)) {
  322. table->table[i].handler(table->table[i].sock,
  323. table->table[i].eloop_data,
  324. table->table[i].user_data);
  325. if (table->changed)
  326. break;
  327. }
  328. }
  329. }
  330. #endif /* CONFIG_ELOOP_POLL */
  331. static void eloop_sock_table_destroy(struct eloop_sock_table *table)
  332. {
  333. if (table) {
  334. int i;
  335. for (i = 0; i < table->count && table->table; i++) {
  336. wpa_printf(MSG_INFO, "ELOOP: remaining socket: "
  337. "sock=%d eloop_data=%p user_data=%p "
  338. "handler=%p",
  339. table->table[i].sock,
  340. table->table[i].eloop_data,
  341. table->table[i].user_data,
  342. table->table[i].handler);
  343. wpa_trace_dump_funcname("eloop unregistered socket "
  344. "handler",
  345. table->table[i].handler);
  346. wpa_trace_dump("eloop sock", &table->table[i]);
  347. }
  348. os_free(table->table);
  349. }
  350. }
  351. int eloop_register_read_sock(int sock, eloop_sock_handler handler,
  352. void *eloop_data, void *user_data)
  353. {
  354. return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
  355. eloop_data, user_data);
  356. }
  357. void eloop_unregister_read_sock(int sock)
  358. {
  359. eloop_unregister_sock(sock, EVENT_TYPE_READ);
  360. }
  361. static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
  362. {
  363. switch (type) {
  364. case EVENT_TYPE_READ:
  365. return &eloop.readers;
  366. case EVENT_TYPE_WRITE:
  367. return &eloop.writers;
  368. case EVENT_TYPE_EXCEPTION:
  369. return &eloop.exceptions;
  370. }
  371. return NULL;
  372. }
  373. int eloop_register_sock(int sock, eloop_event_type type,
  374. eloop_sock_handler handler,
  375. void *eloop_data, void *user_data)
  376. {
  377. struct eloop_sock_table *table;
  378. table = eloop_get_sock_table(type);
  379. return eloop_sock_table_add_sock(table, sock, handler,
  380. eloop_data, user_data);
  381. }
  382. void eloop_unregister_sock(int sock, eloop_event_type type)
  383. {
  384. struct eloop_sock_table *table;
  385. table = eloop_get_sock_table(type);
  386. eloop_sock_table_remove_sock(table, sock);
  387. }
  388. int eloop_register_timeout(unsigned int secs, unsigned int usecs,
  389. eloop_timeout_handler handler,
  390. void *eloop_data, void *user_data)
  391. {
  392. struct eloop_timeout *timeout, *tmp;
  393. os_time_t now_sec;
  394. timeout = os_zalloc(sizeof(*timeout));
  395. if (timeout == NULL)
  396. return -1;
  397. if (os_get_time(&timeout->time) < 0) {
  398. os_free(timeout);
  399. return -1;
  400. }
  401. now_sec = timeout->time.sec;
  402. timeout->time.sec += secs;
  403. if (timeout->time.sec < now_sec) {
  404. /*
  405. * Integer overflow - assume long enough timeout to be assumed
  406. * to be infinite, i.e., the timeout would never happen.
  407. */
  408. wpa_printf(MSG_DEBUG, "ELOOP: Too long timeout (secs=%u) to "
  409. "ever happen - ignore it", secs);
  410. os_free(timeout);
  411. return 0;
  412. }
  413. timeout->time.usec += usecs;
  414. while (timeout->time.usec >= 1000000) {
  415. timeout->time.sec++;
  416. timeout->time.usec -= 1000000;
  417. }
  418. timeout->eloop_data = eloop_data;
  419. timeout->user_data = user_data;
  420. timeout->handler = handler;
  421. wpa_trace_add_ref(timeout, eloop, eloop_data);
  422. wpa_trace_add_ref(timeout, user, user_data);
  423. wpa_trace_record(timeout);
  424. /* Maintain timeouts in order of increasing time */
  425. dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
  426. if (os_time_before(&timeout->time, &tmp->time)) {
  427. dl_list_add(tmp->list.prev, &timeout->list);
  428. return 0;
  429. }
  430. }
  431. dl_list_add_tail(&eloop.timeout, &timeout->list);
  432. return 0;
  433. }
  434. static void eloop_remove_timeout(struct eloop_timeout *timeout)
  435. {
  436. dl_list_del(&timeout->list);
  437. wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
  438. wpa_trace_remove_ref(timeout, user, timeout->user_data);
  439. os_free(timeout);
  440. }
  441. int eloop_cancel_timeout(eloop_timeout_handler handler,
  442. void *eloop_data, void *user_data)
  443. {
  444. struct eloop_timeout *timeout, *prev;
  445. int removed = 0;
  446. dl_list_for_each_safe(timeout, prev, &eloop.timeout,
  447. struct eloop_timeout, list) {
  448. if (timeout->handler == handler &&
  449. (timeout->eloop_data == eloop_data ||
  450. eloop_data == ELOOP_ALL_CTX) &&
  451. (timeout->user_data == user_data ||
  452. user_data == ELOOP_ALL_CTX)) {
  453. eloop_remove_timeout(timeout);
  454. removed++;
  455. }
  456. }
  457. return removed;
  458. }
  459. int eloop_is_timeout_registered(eloop_timeout_handler handler,
  460. void *eloop_data, void *user_data)
  461. {
  462. struct eloop_timeout *tmp;
  463. dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
  464. if (tmp->handler == handler &&
  465. tmp->eloop_data == eloop_data &&
  466. tmp->user_data == user_data)
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. #ifndef CONFIG_NATIVE_WINDOWS
  472. static void eloop_handle_alarm(int sig)
  473. {
  474. wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
  475. "two seconds. Looks like there\n"
  476. "is a bug that ends up in a busy loop that "
  477. "prevents clean shutdown.\n"
  478. "Killing program forcefully.\n");
  479. exit(1);
  480. }
  481. #endif /* CONFIG_NATIVE_WINDOWS */
  482. static void eloop_handle_signal(int sig)
  483. {
  484. int i;
  485. #ifndef CONFIG_NATIVE_WINDOWS
  486. if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
  487. /* Use SIGALRM to break out from potential busy loops that
  488. * would not allow the program to be killed. */
  489. eloop.pending_terminate = 1;
  490. signal(SIGALRM, eloop_handle_alarm);
  491. alarm(2);
  492. }
  493. #endif /* CONFIG_NATIVE_WINDOWS */
  494. eloop.signaled++;
  495. for (i = 0; i < eloop.signal_count; i++) {
  496. if (eloop.signals[i].sig == sig) {
  497. eloop.signals[i].signaled++;
  498. break;
  499. }
  500. }
  501. }
  502. static void eloop_process_pending_signals(void)
  503. {
  504. int i;
  505. if (eloop.signaled == 0)
  506. return;
  507. eloop.signaled = 0;
  508. if (eloop.pending_terminate) {
  509. #ifndef CONFIG_NATIVE_WINDOWS
  510. alarm(0);
  511. #endif /* CONFIG_NATIVE_WINDOWS */
  512. eloop.pending_terminate = 0;
  513. }
  514. for (i = 0; i < eloop.signal_count; i++) {
  515. if (eloop.signals[i].signaled) {
  516. eloop.signals[i].signaled = 0;
  517. eloop.signals[i].handler(eloop.signals[i].sig,
  518. eloop.signals[i].user_data);
  519. }
  520. }
  521. }
  522. int eloop_register_signal(int sig, eloop_signal_handler handler,
  523. void *user_data)
  524. {
  525. struct eloop_signal *tmp;
  526. tmp = (struct eloop_signal *)
  527. os_realloc(eloop.signals,
  528. (eloop.signal_count + 1) *
  529. sizeof(struct eloop_signal));
  530. if (tmp == NULL)
  531. return -1;
  532. tmp[eloop.signal_count].sig = sig;
  533. tmp[eloop.signal_count].user_data = user_data;
  534. tmp[eloop.signal_count].handler = handler;
  535. tmp[eloop.signal_count].signaled = 0;
  536. eloop.signal_count++;
  537. eloop.signals = tmp;
  538. signal(sig, eloop_handle_signal);
  539. return 0;
  540. }
  541. int eloop_register_signal_terminate(eloop_signal_handler handler,
  542. void *user_data)
  543. {
  544. int ret = eloop_register_signal(SIGINT, handler, user_data);
  545. if (ret == 0)
  546. ret = eloop_register_signal(SIGTERM, handler, user_data);
  547. return ret;
  548. }
  549. int eloop_register_signal_reconfig(eloop_signal_handler handler,
  550. void *user_data)
  551. {
  552. #ifdef CONFIG_NATIVE_WINDOWS
  553. return 0;
  554. #else /* CONFIG_NATIVE_WINDOWS */
  555. return eloop_register_signal(SIGHUP, handler, user_data);
  556. #endif /* CONFIG_NATIVE_WINDOWS */
  557. }
  558. void eloop_run(void)
  559. {
  560. #ifdef CONFIG_ELOOP_POLL
  561. int num_poll_fds;
  562. int timeout_ms = 0;
  563. #else /* CONFIG_ELOOP_POLL */
  564. fd_set *rfds, *wfds, *efds;
  565. struct timeval _tv;
  566. #endif /* CONFIG_ELOOP_POLL */
  567. int res;
  568. struct os_time tv, now;
  569. #ifndef CONFIG_ELOOP_POLL
  570. rfds = os_malloc(sizeof(*rfds));
  571. wfds = os_malloc(sizeof(*wfds));
  572. efds = os_malloc(sizeof(*efds));
  573. if (rfds == NULL || wfds == NULL || efds == NULL)
  574. goto out;
  575. #endif /* CONFIG_ELOOP_POLL */
  576. while (!eloop.terminate &&
  577. (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
  578. eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
  579. struct eloop_timeout *timeout;
  580. timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
  581. list);
  582. if (timeout) {
  583. os_get_time(&now);
  584. if (os_time_before(&now, &timeout->time))
  585. os_time_sub(&timeout->time, &now, &tv);
  586. else
  587. tv.sec = tv.usec = 0;
  588. #ifdef CONFIG_ELOOP_POLL
  589. timeout_ms = tv.sec * 1000 + tv.usec / 1000;
  590. #else /* CONFIG_ELOOP_POLL */
  591. _tv.tv_sec = tv.sec;
  592. _tv.tv_usec = tv.usec;
  593. #endif /* CONFIG_ELOOP_POLL */
  594. }
  595. #ifdef CONFIG_ELOOP_POLL
  596. num_poll_fds = eloop_sock_table_set_fds(
  597. &eloop.readers, &eloop.writers, &eloop.exceptions,
  598. eloop.pollfds, eloop.pollfds_map,
  599. eloop.max_pollfd_map);
  600. res = poll(eloop.pollfds, num_poll_fds,
  601. timeout ? timeout_ms : -1);
  602. if (res < 0 && errno != EINTR && errno != 0) {
  603. perror("poll");
  604. goto out;
  605. }
  606. #else /* CONFIG_ELOOP_POLL */
  607. eloop_sock_table_set_fds(&eloop.readers, rfds);
  608. eloop_sock_table_set_fds(&eloop.writers, wfds);
  609. eloop_sock_table_set_fds(&eloop.exceptions, efds);
  610. res = select(eloop.max_sock + 1, rfds, wfds, efds,
  611. timeout ? &_tv : NULL);
  612. if (res < 0 && errno != EINTR && errno != 0) {
  613. perror("select");
  614. goto out;
  615. }
  616. #endif /* CONFIG_ELOOP_POLL */
  617. eloop_process_pending_signals();
  618. /* check if some registered timeouts have occurred */
  619. timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
  620. list);
  621. if (timeout) {
  622. os_get_time(&now);
  623. if (!os_time_before(&now, &timeout->time)) {
  624. void *eloop_data = timeout->eloop_data;
  625. void *user_data = timeout->user_data;
  626. eloop_timeout_handler handler =
  627. timeout->handler;
  628. eloop_remove_timeout(timeout);
  629. handler(eloop_data, user_data);
  630. }
  631. }
  632. if (res <= 0)
  633. continue;
  634. #ifdef CONFIG_ELOOP_POLL
  635. eloop_sock_table_dispatch(&eloop.readers, &eloop.writers,
  636. &eloop.exceptions, eloop.pollfds_map,
  637. eloop.max_pollfd_map);
  638. #else /* CONFIG_ELOOP_POLL */
  639. eloop_sock_table_dispatch(&eloop.readers, rfds);
  640. eloop_sock_table_dispatch(&eloop.writers, wfds);
  641. eloop_sock_table_dispatch(&eloop.exceptions, efds);
  642. #endif /* CONFIG_ELOOP_POLL */
  643. }
  644. out:
  645. #ifndef CONFIG_ELOOP_POLL
  646. os_free(rfds);
  647. os_free(wfds);
  648. os_free(efds);
  649. #endif /* CONFIG_ELOOP_POLL */
  650. return;
  651. }
  652. void eloop_terminate(void)
  653. {
  654. eloop.terminate = 1;
  655. }
  656. void eloop_destroy(void)
  657. {
  658. struct eloop_timeout *timeout, *prev;
  659. struct os_time now;
  660. os_get_time(&now);
  661. dl_list_for_each_safe(timeout, prev, &eloop.timeout,
  662. struct eloop_timeout, list) {
  663. int sec, usec;
  664. sec = timeout->time.sec - now.sec;
  665. usec = timeout->time.usec - now.usec;
  666. if (timeout->time.usec < now.usec) {
  667. sec--;
  668. usec += 1000000;
  669. }
  670. wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
  671. "eloop_data=%p user_data=%p handler=%p",
  672. sec, usec, timeout->eloop_data, timeout->user_data,
  673. timeout->handler);
  674. wpa_trace_dump_funcname("eloop unregistered timeout handler",
  675. timeout->handler);
  676. wpa_trace_dump("eloop timeout", timeout);
  677. eloop_remove_timeout(timeout);
  678. }
  679. eloop_sock_table_destroy(&eloop.readers);
  680. eloop_sock_table_destroy(&eloop.writers);
  681. eloop_sock_table_destroy(&eloop.exceptions);
  682. os_free(eloop.signals);
  683. #ifdef CONFIG_ELOOP_POLL
  684. os_free(eloop.pollfds);
  685. os_free(eloop.pollfds_map);
  686. #endif /* CONFIG_ELOOP_POLL */
  687. }
  688. int eloop_terminated(void)
  689. {
  690. return eloop.terminate;
  691. }
  692. void eloop_wait_for_read_sock(int sock)
  693. {
  694. #ifdef CONFIG_ELOOP_POLL
  695. struct pollfd pfd;
  696. if (sock < 0)
  697. return;
  698. os_memset(&pfd, 0, sizeof(pfd));
  699. pfd.fd = sock;
  700. pfd.events = POLLIN;
  701. poll(&pfd, 1, -1);
  702. #else /* CONFIG_ELOOP_POLL */
  703. fd_set rfds;
  704. if (sock < 0)
  705. return;
  706. FD_ZERO(&rfds);
  707. FD_SET(sock, &rfds);
  708. select(sock + 1, &rfds, NULL, NULL, NULL);
  709. #endif /* CONFIG_ELOOP_POLL */
  710. }