eloop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Event loop based on select() loop
  3. * Copyright (c) 2002-2009, 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 "common.h"
  16. #include "trace.h"
  17. #include "list.h"
  18. #include "eloop.h"
  19. struct eloop_sock {
  20. int sock;
  21. void *eloop_data;
  22. void *user_data;
  23. eloop_sock_handler handler;
  24. WPA_TRACE_REF(eloop);
  25. WPA_TRACE_REF(user);
  26. WPA_TRACE_INFO
  27. };
  28. struct eloop_timeout {
  29. struct dl_list list;
  30. struct os_time time;
  31. void *eloop_data;
  32. void *user_data;
  33. eloop_timeout_handler handler;
  34. WPA_TRACE_REF(eloop);
  35. WPA_TRACE_REF(user);
  36. WPA_TRACE_INFO
  37. };
  38. struct eloop_signal {
  39. int sig;
  40. void *user_data;
  41. eloop_signal_handler handler;
  42. int signaled;
  43. };
  44. struct eloop_sock_table {
  45. int count;
  46. struct eloop_sock *table;
  47. int changed;
  48. };
  49. struct eloop_data {
  50. int max_sock;
  51. struct eloop_sock_table readers;
  52. struct eloop_sock_table writers;
  53. struct eloop_sock_table exceptions;
  54. struct dl_list timeout;
  55. int signal_count;
  56. struct eloop_signal *signals;
  57. int signaled;
  58. int pending_terminate;
  59. int terminate;
  60. int reader_table_changed;
  61. };
  62. static struct eloop_data eloop;
  63. #ifdef WPA_TRACE
  64. static void eloop_sigsegv_handler(int sig)
  65. {
  66. wpa_trace_show("eloop SIGSEGV");
  67. abort();
  68. }
  69. static void eloop_trace_sock_add_ref(struct eloop_sock_table *table)
  70. {
  71. int i;
  72. if (table == NULL || table->table == NULL)
  73. return;
  74. for (i = 0; i < table->count; i++) {
  75. wpa_trace_add_ref(&table->table[i], eloop,
  76. table->table[i].eloop_data);
  77. wpa_trace_add_ref(&table->table[i], user,
  78. table->table[i].user_data);
  79. }
  80. }
  81. static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
  82. {
  83. int i;
  84. if (table == NULL || table->table == NULL)
  85. return;
  86. for (i = 0; i < table->count; i++) {
  87. wpa_trace_remove_ref(&table->table[i], eloop,
  88. table->table[i].eloop_data);
  89. wpa_trace_remove_ref(&table->table[i], user,
  90. table->table[i].user_data);
  91. }
  92. }
  93. #else /* WPA_TRACE */
  94. #define eloop_trace_sock_add_ref(table) do { } while (0)
  95. #define eloop_trace_sock_remove_ref(table) do { } while (0)
  96. #endif /* WPA_TRACE */
  97. int eloop_init(void)
  98. {
  99. os_memset(&eloop, 0, sizeof(eloop));
  100. dl_list_init(&eloop.timeout);
  101. #ifdef WPA_TRACE
  102. signal(SIGSEGV, eloop_sigsegv_handler);
  103. #endif /* WPA_TRACE */
  104. return 0;
  105. }
  106. static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
  107. int sock, eloop_sock_handler handler,
  108. void *eloop_data, void *user_data)
  109. {
  110. struct eloop_sock *tmp;
  111. if (table == NULL)
  112. return -1;
  113. eloop_trace_sock_remove_ref(table);
  114. tmp = (struct eloop_sock *)
  115. os_realloc(table->table,
  116. (table->count + 1) * sizeof(struct eloop_sock));
  117. if (tmp == NULL)
  118. return -1;
  119. tmp[table->count].sock = sock;
  120. tmp[table->count].eloop_data = eloop_data;
  121. tmp[table->count].user_data = user_data;
  122. tmp[table->count].handler = handler;
  123. wpa_trace_record(&tmp[table->count]);
  124. table->count++;
  125. table->table = tmp;
  126. if (sock > eloop.max_sock)
  127. eloop.max_sock = sock;
  128. table->changed = 1;
  129. eloop_trace_sock_add_ref(table);
  130. return 0;
  131. }
  132. static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
  133. int sock)
  134. {
  135. int i;
  136. if (table == NULL || table->table == NULL || table->count == 0)
  137. return;
  138. for (i = 0; i < table->count; i++) {
  139. if (table->table[i].sock == sock)
  140. break;
  141. }
  142. if (i == table->count)
  143. return;
  144. eloop_trace_sock_remove_ref(table);
  145. if (i != table->count - 1) {
  146. os_memmove(&table->table[i], &table->table[i + 1],
  147. (table->count - i - 1) *
  148. sizeof(struct eloop_sock));
  149. }
  150. table->count--;
  151. table->changed = 1;
  152. eloop_trace_sock_add_ref(table);
  153. }
  154. static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
  155. fd_set *fds)
  156. {
  157. int i;
  158. FD_ZERO(fds);
  159. if (table->table == NULL)
  160. return;
  161. for (i = 0; i < table->count; i++)
  162. FD_SET(table->table[i].sock, fds);
  163. }
  164. static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
  165. fd_set *fds)
  166. {
  167. int i;
  168. if (table == NULL || table->table == NULL)
  169. return;
  170. table->changed = 0;
  171. for (i = 0; i < table->count; i++) {
  172. if (FD_ISSET(table->table[i].sock, fds)) {
  173. table->table[i].handler(table->table[i].sock,
  174. table->table[i].eloop_data,
  175. table->table[i].user_data);
  176. if (table->changed)
  177. break;
  178. }
  179. }
  180. }
  181. static void eloop_sock_table_destroy(struct eloop_sock_table *table)
  182. {
  183. if (table) {
  184. int i;
  185. for (i = 0; i < table->count && table->table; i++) {
  186. wpa_printf(MSG_INFO, "ELOOP: remaining socket: "
  187. "sock=%d eloop_data=%p user_data=%p "
  188. "handler=%p",
  189. table->table[i].sock,
  190. table->table[i].eloop_data,
  191. table->table[i].user_data,
  192. table->table[i].handler);
  193. wpa_trace_dump_funcname("eloop unregistered socket "
  194. "handler",
  195. table->table[i].handler);
  196. wpa_trace_dump("eloop sock", &table->table[i]);
  197. }
  198. os_free(table->table);
  199. }
  200. }
  201. int eloop_register_read_sock(int sock, eloop_sock_handler handler,
  202. void *eloop_data, void *user_data)
  203. {
  204. return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
  205. eloop_data, user_data);
  206. }
  207. void eloop_unregister_read_sock(int sock)
  208. {
  209. eloop_unregister_sock(sock, EVENT_TYPE_READ);
  210. }
  211. static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
  212. {
  213. switch (type) {
  214. case EVENT_TYPE_READ:
  215. return &eloop.readers;
  216. case EVENT_TYPE_WRITE:
  217. return &eloop.writers;
  218. case EVENT_TYPE_EXCEPTION:
  219. return &eloop.exceptions;
  220. }
  221. return NULL;
  222. }
  223. int eloop_register_sock(int sock, eloop_event_type type,
  224. eloop_sock_handler handler,
  225. void *eloop_data, void *user_data)
  226. {
  227. struct eloop_sock_table *table;
  228. table = eloop_get_sock_table(type);
  229. return eloop_sock_table_add_sock(table, sock, handler,
  230. eloop_data, user_data);
  231. }
  232. void eloop_unregister_sock(int sock, eloop_event_type type)
  233. {
  234. struct eloop_sock_table *table;
  235. table = eloop_get_sock_table(type);
  236. eloop_sock_table_remove_sock(table, sock);
  237. }
  238. int eloop_register_timeout(unsigned int secs, unsigned int usecs,
  239. eloop_timeout_handler handler,
  240. void *eloop_data, void *user_data)
  241. {
  242. struct eloop_timeout *timeout, *tmp;
  243. timeout = os_malloc(sizeof(*timeout));
  244. if (timeout == NULL)
  245. return -1;
  246. if (os_get_time(&timeout->time) < 0) {
  247. os_free(timeout);
  248. return -1;
  249. }
  250. timeout->time.sec += secs;
  251. timeout->time.usec += usecs;
  252. while (timeout->time.usec >= 1000000) {
  253. timeout->time.sec++;
  254. timeout->time.usec -= 1000000;
  255. }
  256. timeout->eloop_data = eloop_data;
  257. timeout->user_data = user_data;
  258. timeout->handler = handler;
  259. wpa_trace_add_ref(timeout, eloop, eloop_data);
  260. wpa_trace_add_ref(timeout, user, user_data);
  261. wpa_trace_record(timeout);
  262. /* Maintain timeouts in order of increasing time */
  263. dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
  264. if (os_time_before(&timeout->time, &tmp->time)) {
  265. dl_list_add(tmp->list.prev, &timeout->list);
  266. return 0;
  267. }
  268. }
  269. dl_list_add_tail(&eloop.timeout, &timeout->list);
  270. return 0;
  271. }
  272. static void eloop_remove_timeout(struct eloop_timeout *timeout)
  273. {
  274. dl_list_del(&timeout->list);
  275. wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
  276. wpa_trace_remove_ref(timeout, user, timeout->user_data);
  277. os_free(timeout);
  278. }
  279. int eloop_cancel_timeout(eloop_timeout_handler handler,
  280. void *eloop_data, void *user_data)
  281. {
  282. struct eloop_timeout *timeout, *prev;
  283. int removed = 0;
  284. dl_list_for_each_safe(timeout, prev, &eloop.timeout,
  285. struct eloop_timeout, list) {
  286. if (timeout->handler == handler &&
  287. (timeout->eloop_data == eloop_data ||
  288. eloop_data == ELOOP_ALL_CTX) &&
  289. (timeout->user_data == user_data ||
  290. user_data == ELOOP_ALL_CTX)) {
  291. eloop_remove_timeout(timeout);
  292. removed++;
  293. }
  294. }
  295. return removed;
  296. }
  297. int eloop_is_timeout_registered(eloop_timeout_handler handler,
  298. void *eloop_data, void *user_data)
  299. {
  300. struct eloop_timeout *tmp;
  301. dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
  302. if (tmp->handler == handler &&
  303. tmp->eloop_data == eloop_data &&
  304. tmp->user_data == user_data)
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. #ifndef CONFIG_NATIVE_WINDOWS
  310. static void eloop_handle_alarm(int sig)
  311. {
  312. wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
  313. "two seconds. Looks like there\n"
  314. "is a bug that ends up in a busy loop that "
  315. "prevents clean shutdown.\n"
  316. "Killing program forcefully.\n");
  317. exit(1);
  318. }
  319. #endif /* CONFIG_NATIVE_WINDOWS */
  320. static void eloop_handle_signal(int sig)
  321. {
  322. int i;
  323. #ifndef CONFIG_NATIVE_WINDOWS
  324. if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
  325. /* Use SIGALRM to break out from potential busy loops that
  326. * would not allow the program to be killed. */
  327. eloop.pending_terminate = 1;
  328. signal(SIGALRM, eloop_handle_alarm);
  329. alarm(2);
  330. }
  331. #endif /* CONFIG_NATIVE_WINDOWS */
  332. eloop.signaled++;
  333. for (i = 0; i < eloop.signal_count; i++) {
  334. if (eloop.signals[i].sig == sig) {
  335. eloop.signals[i].signaled++;
  336. break;
  337. }
  338. }
  339. }
  340. static void eloop_process_pending_signals(void)
  341. {
  342. int i;
  343. if (eloop.signaled == 0)
  344. return;
  345. eloop.signaled = 0;
  346. if (eloop.pending_terminate) {
  347. #ifndef CONFIG_NATIVE_WINDOWS
  348. alarm(0);
  349. #endif /* CONFIG_NATIVE_WINDOWS */
  350. eloop.pending_terminate = 0;
  351. }
  352. for (i = 0; i < eloop.signal_count; i++) {
  353. if (eloop.signals[i].signaled) {
  354. eloop.signals[i].signaled = 0;
  355. eloop.signals[i].handler(eloop.signals[i].sig,
  356. eloop.signals[i].user_data);
  357. }
  358. }
  359. }
  360. int eloop_register_signal(int sig, eloop_signal_handler handler,
  361. void *user_data)
  362. {
  363. struct eloop_signal *tmp;
  364. tmp = (struct eloop_signal *)
  365. os_realloc(eloop.signals,
  366. (eloop.signal_count + 1) *
  367. sizeof(struct eloop_signal));
  368. if (tmp == NULL)
  369. return -1;
  370. tmp[eloop.signal_count].sig = sig;
  371. tmp[eloop.signal_count].user_data = user_data;
  372. tmp[eloop.signal_count].handler = handler;
  373. tmp[eloop.signal_count].signaled = 0;
  374. eloop.signal_count++;
  375. eloop.signals = tmp;
  376. signal(sig, eloop_handle_signal);
  377. return 0;
  378. }
  379. int eloop_register_signal_terminate(eloop_signal_handler handler,
  380. void *user_data)
  381. {
  382. int ret = eloop_register_signal(SIGINT, handler, user_data);
  383. if (ret == 0)
  384. ret = eloop_register_signal(SIGTERM, handler, user_data);
  385. return ret;
  386. }
  387. int eloop_register_signal_reconfig(eloop_signal_handler handler,
  388. void *user_data)
  389. {
  390. #ifdef CONFIG_NATIVE_WINDOWS
  391. return 0;
  392. #else /* CONFIG_NATIVE_WINDOWS */
  393. return eloop_register_signal(SIGHUP, handler, user_data);
  394. #endif /* CONFIG_NATIVE_WINDOWS */
  395. }
  396. void eloop_run(void)
  397. {
  398. fd_set *rfds, *wfds, *efds;
  399. int res;
  400. struct timeval _tv;
  401. struct os_time tv, now;
  402. rfds = os_malloc(sizeof(*rfds));
  403. wfds = os_malloc(sizeof(*wfds));
  404. efds = os_malloc(sizeof(*efds));
  405. if (rfds == NULL || wfds == NULL || efds == NULL)
  406. goto out;
  407. while (!eloop.terminate &&
  408. (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
  409. eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
  410. struct eloop_timeout *timeout;
  411. timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
  412. list);
  413. if (timeout) {
  414. os_get_time(&now);
  415. if (os_time_before(&now, &timeout->time))
  416. os_time_sub(&timeout->time, &now, &tv);
  417. else
  418. tv.sec = tv.usec = 0;
  419. _tv.tv_sec = tv.sec;
  420. _tv.tv_usec = tv.usec;
  421. }
  422. eloop_sock_table_set_fds(&eloop.readers, rfds);
  423. eloop_sock_table_set_fds(&eloop.writers, wfds);
  424. eloop_sock_table_set_fds(&eloop.exceptions, efds);
  425. res = select(eloop.max_sock + 1, rfds, wfds, efds,
  426. timeout ? &_tv : NULL);
  427. if (res < 0 && errno != EINTR && errno != 0) {
  428. perror("select");
  429. goto out;
  430. }
  431. eloop_process_pending_signals();
  432. /* check if some registered timeouts have occurred */
  433. if (timeout) {
  434. os_get_time(&now);
  435. if (!os_time_before(&now, &timeout->time)) {
  436. void *eloop_data = timeout->eloop_data;
  437. void *user_data = timeout->user_data;
  438. eloop_remove_timeout(timeout);
  439. timeout->handler(eloop_data,
  440. user_data);
  441. }
  442. }
  443. if (res <= 0)
  444. continue;
  445. eloop_sock_table_dispatch(&eloop.readers, rfds);
  446. eloop_sock_table_dispatch(&eloop.writers, wfds);
  447. eloop_sock_table_dispatch(&eloop.exceptions, efds);
  448. }
  449. out:
  450. os_free(rfds);
  451. os_free(wfds);
  452. os_free(efds);
  453. }
  454. void eloop_terminate(void)
  455. {
  456. eloop.terminate = 1;
  457. }
  458. void eloop_destroy(void)
  459. {
  460. struct eloop_timeout *timeout, *prev;
  461. struct os_time now;
  462. os_get_time(&now);
  463. dl_list_for_each_safe(timeout, prev, &eloop.timeout,
  464. struct eloop_timeout, list) {
  465. int sec, usec;
  466. sec = timeout->time.sec - now.sec;
  467. usec = timeout->time.usec - now.usec;
  468. if (timeout->time.usec < now.usec) {
  469. sec--;
  470. usec += 1000000;
  471. }
  472. wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
  473. "eloop_data=%p user_data=%p handler=%p",
  474. sec, usec, timeout->eloop_data, timeout->user_data,
  475. timeout->handler);
  476. wpa_trace_dump_funcname("eloop unregistered timeout handler",
  477. timeout->handler);
  478. wpa_trace_dump("eloop timeout", timeout);
  479. eloop_remove_timeout(timeout);
  480. }
  481. eloop_sock_table_destroy(&eloop.readers);
  482. eloop_sock_table_destroy(&eloop.writers);
  483. eloop_sock_table_destroy(&eloop.exceptions);
  484. os_free(eloop.signals);
  485. }
  486. int eloop_terminated(void)
  487. {
  488. return eloop.terminate;
  489. }
  490. void eloop_wait_for_read_sock(int sock)
  491. {
  492. fd_set rfds;
  493. if (sock < 0)
  494. return;
  495. FD_ZERO(&rfds);
  496. FD_SET(sock, &rfds);
  497. select(sock + 1, &rfds, NULL, NULL, NULL);
  498. }