Use common driver code for Linux hwaddr get/set
[hostap-gosc2009.git] / src / utils / eloop.c
blob81843c2540f9934b5dc50fa15ca02695cb79af01
1 /*
2 * Event loop based on select() loop
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "trace.h"
19 #include "list.h"
20 #include "eloop.h"
23 struct eloop_sock {
24 int sock;
25 void *eloop_data;
26 void *user_data;
27 eloop_sock_handler handler;
28 WPA_TRACE_REF(eloop);
29 WPA_TRACE_REF(user);
30 WPA_TRACE_INFO
33 struct eloop_timeout {
34 struct dl_list list;
35 struct os_time time;
36 void *eloop_data;
37 void *user_data;
38 eloop_timeout_handler handler;
39 WPA_TRACE_REF(eloop);
40 WPA_TRACE_REF(user);
41 WPA_TRACE_INFO
44 struct eloop_signal {
45 int sig;
46 void *user_data;
47 eloop_signal_handler handler;
48 int signaled;
51 struct eloop_sock_table {
52 int count;
53 struct eloop_sock *table;
54 int changed;
57 struct eloop_data {
58 int max_sock;
60 struct eloop_sock_table readers;
61 struct eloop_sock_table writers;
62 struct eloop_sock_table exceptions;
64 struct dl_list timeout;
66 int signal_count;
67 struct eloop_signal *signals;
68 int signaled;
69 int pending_terminate;
71 int terminate;
72 int reader_table_changed;
75 static struct eloop_data eloop;
78 #ifdef WPA_TRACE
80 static void eloop_sigsegv_handler(int sig)
82 wpa_trace_show("eloop SIGSEGV");
83 abort();
86 static void eloop_trace_sock_add_ref(struct eloop_sock_table *table)
88 int i;
89 if (table == NULL || table->table == NULL)
90 return;
91 for (i = 0; i < table->count; i++) {
92 wpa_trace_add_ref(&table->table[i], eloop,
93 table->table[i].eloop_data);
94 wpa_trace_add_ref(&table->table[i], user,
95 table->table[i].user_data);
100 static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
102 int i;
103 if (table == NULL || table->table == NULL)
104 return;
105 for (i = 0; i < table->count; i++) {
106 wpa_trace_remove_ref(&table->table[i], eloop,
107 table->table[i].eloop_data);
108 wpa_trace_remove_ref(&table->table[i], user,
109 table->table[i].user_data);
113 #else /* WPA_TRACE */
115 #define eloop_trace_sock_add_ref(table) do { } while (0)
116 #define eloop_trace_sock_remove_ref(table) do { } while (0)
118 #endif /* WPA_TRACE */
121 int eloop_init(void)
123 os_memset(&eloop, 0, sizeof(eloop));
124 dl_list_init(&eloop.timeout);
125 #ifdef WPA_TRACE
126 signal(SIGSEGV, eloop_sigsegv_handler);
127 #endif /* WPA_TRACE */
128 return 0;
132 static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
133 int sock, eloop_sock_handler handler,
134 void *eloop_data, void *user_data)
136 struct eloop_sock *tmp;
138 if (table == NULL)
139 return -1;
141 eloop_trace_sock_remove_ref(table);
142 tmp = (struct eloop_sock *)
143 os_realloc(table->table,
144 (table->count + 1) * sizeof(struct eloop_sock));
145 if (tmp == NULL)
146 return -1;
148 tmp[table->count].sock = sock;
149 tmp[table->count].eloop_data = eloop_data;
150 tmp[table->count].user_data = user_data;
151 tmp[table->count].handler = handler;
152 wpa_trace_record(&tmp[table->count]);
153 table->count++;
154 table->table = tmp;
155 if (sock > eloop.max_sock)
156 eloop.max_sock = sock;
157 table->changed = 1;
158 eloop_trace_sock_add_ref(table);
160 return 0;
164 static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
165 int sock)
167 int i;
169 if (table == NULL || table->table == NULL || table->count == 0)
170 return;
172 for (i = 0; i < table->count; i++) {
173 if (table->table[i].sock == sock)
174 break;
176 if (i == table->count)
177 return;
178 eloop_trace_sock_remove_ref(table);
179 if (i != table->count - 1) {
180 os_memmove(&table->table[i], &table->table[i + 1],
181 (table->count - i - 1) *
182 sizeof(struct eloop_sock));
184 table->count--;
185 table->changed = 1;
186 eloop_trace_sock_add_ref(table);
190 static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
191 fd_set *fds)
193 int i;
195 FD_ZERO(fds);
197 if (table->table == NULL)
198 return;
200 for (i = 0; i < table->count; i++)
201 FD_SET(table->table[i].sock, fds);
205 static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
206 fd_set *fds)
208 int i;
210 if (table == NULL || table->table == NULL)
211 return;
213 table->changed = 0;
214 for (i = 0; i < table->count; i++) {
215 if (FD_ISSET(table->table[i].sock, fds)) {
216 table->table[i].handler(table->table[i].sock,
217 table->table[i].eloop_data,
218 table->table[i].user_data);
219 if (table->changed)
220 break;
226 static void eloop_sock_table_destroy(struct eloop_sock_table *table)
228 if (table) {
229 int i;
230 for (i = 0; i < table->count && table->table; i++) {
231 wpa_printf(MSG_INFO, "ELOOP: remaining socket: "
232 "sock=%d eloop_data=%p user_data=%p "
233 "handler=%p",
234 table->table[i].sock,
235 table->table[i].eloop_data,
236 table->table[i].user_data,
237 table->table[i].handler);
238 wpa_trace_dump_funcname("eloop unregistered socket "
239 "handler",
240 table->table[i].handler);
241 wpa_trace_dump("eloop sock", &table->table[i]);
243 os_free(table->table);
248 int eloop_register_read_sock(int sock, eloop_sock_handler handler,
249 void *eloop_data, void *user_data)
251 return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
252 eloop_data, user_data);
256 void eloop_unregister_read_sock(int sock)
258 eloop_unregister_sock(sock, EVENT_TYPE_READ);
262 static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
264 switch (type) {
265 case EVENT_TYPE_READ:
266 return &eloop.readers;
267 case EVENT_TYPE_WRITE:
268 return &eloop.writers;
269 case EVENT_TYPE_EXCEPTION:
270 return &eloop.exceptions;
273 return NULL;
277 int eloop_register_sock(int sock, eloop_event_type type,
278 eloop_sock_handler handler,
279 void *eloop_data, void *user_data)
281 struct eloop_sock_table *table;
283 table = eloop_get_sock_table(type);
284 return eloop_sock_table_add_sock(table, sock, handler,
285 eloop_data, user_data);
289 void eloop_unregister_sock(int sock, eloop_event_type type)
291 struct eloop_sock_table *table;
293 table = eloop_get_sock_table(type);
294 eloop_sock_table_remove_sock(table, sock);
298 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
299 eloop_timeout_handler handler,
300 void *eloop_data, void *user_data)
302 struct eloop_timeout *timeout, *tmp;
304 timeout = os_malloc(sizeof(*timeout));
305 if (timeout == NULL)
306 return -1;
307 if (os_get_time(&timeout->time) < 0) {
308 os_free(timeout);
309 return -1;
311 timeout->time.sec += secs;
312 timeout->time.usec += usecs;
313 while (timeout->time.usec >= 1000000) {
314 timeout->time.sec++;
315 timeout->time.usec -= 1000000;
317 timeout->eloop_data = eloop_data;
318 timeout->user_data = user_data;
319 timeout->handler = handler;
320 wpa_trace_add_ref(timeout, eloop, eloop_data);
321 wpa_trace_add_ref(timeout, user, user_data);
322 wpa_trace_record(timeout);
324 /* Maintain timeouts in order of increasing time */
325 dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
326 if (os_time_before(&timeout->time, &tmp->time)) {
327 dl_list_add(tmp->list.prev, &timeout->list);
328 return 0;
331 dl_list_add_tail(&eloop.timeout, &timeout->list);
333 return 0;
337 static void eloop_remove_timeout(struct eloop_timeout *timeout)
339 dl_list_del(&timeout->list);
340 wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
341 wpa_trace_remove_ref(timeout, user, timeout->user_data);
342 os_free(timeout);
346 int eloop_cancel_timeout(eloop_timeout_handler handler,
347 void *eloop_data, void *user_data)
349 struct eloop_timeout *timeout, *prev;
350 int removed = 0;
352 dl_list_for_each_safe(timeout, prev, &eloop.timeout,
353 struct eloop_timeout, list) {
354 if (timeout->handler == handler &&
355 (timeout->eloop_data == eloop_data ||
356 eloop_data == ELOOP_ALL_CTX) &&
357 (timeout->user_data == user_data ||
358 user_data == ELOOP_ALL_CTX)) {
359 eloop_remove_timeout(timeout);
360 removed++;
364 return removed;
368 int eloop_is_timeout_registered(eloop_timeout_handler handler,
369 void *eloop_data, void *user_data)
371 struct eloop_timeout *tmp;
373 dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
374 if (tmp->handler == handler &&
375 tmp->eloop_data == eloop_data &&
376 tmp->user_data == user_data)
377 return 1;
380 return 0;
384 #ifndef CONFIG_NATIVE_WINDOWS
385 static void eloop_handle_alarm(int sig)
387 wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
388 "two seconds. Looks like there\n"
389 "is a bug that ends up in a busy loop that "
390 "prevents clean shutdown.\n"
391 "Killing program forcefully.\n");
392 exit(1);
394 #endif /* CONFIG_NATIVE_WINDOWS */
397 static void eloop_handle_signal(int sig)
399 int i;
401 #ifndef CONFIG_NATIVE_WINDOWS
402 if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
403 /* Use SIGALRM to break out from potential busy loops that
404 * would not allow the program to be killed. */
405 eloop.pending_terminate = 1;
406 signal(SIGALRM, eloop_handle_alarm);
407 alarm(2);
409 #endif /* CONFIG_NATIVE_WINDOWS */
411 eloop.signaled++;
412 for (i = 0; i < eloop.signal_count; i++) {
413 if (eloop.signals[i].sig == sig) {
414 eloop.signals[i].signaled++;
415 break;
421 static void eloop_process_pending_signals(void)
423 int i;
425 if (eloop.signaled == 0)
426 return;
427 eloop.signaled = 0;
429 if (eloop.pending_terminate) {
430 #ifndef CONFIG_NATIVE_WINDOWS
431 alarm(0);
432 #endif /* CONFIG_NATIVE_WINDOWS */
433 eloop.pending_terminate = 0;
436 for (i = 0; i < eloop.signal_count; i++) {
437 if (eloop.signals[i].signaled) {
438 eloop.signals[i].signaled = 0;
439 eloop.signals[i].handler(eloop.signals[i].sig,
440 eloop.signals[i].user_data);
446 int eloop_register_signal(int sig, eloop_signal_handler handler,
447 void *user_data)
449 struct eloop_signal *tmp;
451 tmp = (struct eloop_signal *)
452 os_realloc(eloop.signals,
453 (eloop.signal_count + 1) *
454 sizeof(struct eloop_signal));
455 if (tmp == NULL)
456 return -1;
458 tmp[eloop.signal_count].sig = sig;
459 tmp[eloop.signal_count].user_data = user_data;
460 tmp[eloop.signal_count].handler = handler;
461 tmp[eloop.signal_count].signaled = 0;
462 eloop.signal_count++;
463 eloop.signals = tmp;
464 signal(sig, eloop_handle_signal);
466 return 0;
470 int eloop_register_signal_terminate(eloop_signal_handler handler,
471 void *user_data)
473 int ret = eloop_register_signal(SIGINT, handler, user_data);
474 if (ret == 0)
475 ret = eloop_register_signal(SIGTERM, handler, user_data);
476 return ret;
480 int eloop_register_signal_reconfig(eloop_signal_handler handler,
481 void *user_data)
483 #ifdef CONFIG_NATIVE_WINDOWS
484 return 0;
485 #else /* CONFIG_NATIVE_WINDOWS */
486 return eloop_register_signal(SIGHUP, handler, user_data);
487 #endif /* CONFIG_NATIVE_WINDOWS */
491 void eloop_run(void)
493 fd_set *rfds, *wfds, *efds;
494 int res;
495 struct timeval _tv;
496 struct os_time tv, now;
498 rfds = os_malloc(sizeof(*rfds));
499 wfds = os_malloc(sizeof(*wfds));
500 efds = os_malloc(sizeof(*efds));
501 if (rfds == NULL || wfds == NULL || efds == NULL)
502 goto out;
504 while (!eloop.terminate &&
505 (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
506 eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
507 struct eloop_timeout *timeout;
508 timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
509 list);
510 if (timeout) {
511 os_get_time(&now);
512 if (os_time_before(&now, &timeout->time))
513 os_time_sub(&timeout->time, &now, &tv);
514 else
515 tv.sec = tv.usec = 0;
516 _tv.tv_sec = tv.sec;
517 _tv.tv_usec = tv.usec;
520 eloop_sock_table_set_fds(&eloop.readers, rfds);
521 eloop_sock_table_set_fds(&eloop.writers, wfds);
522 eloop_sock_table_set_fds(&eloop.exceptions, efds);
523 res = select(eloop.max_sock + 1, rfds, wfds, efds,
524 timeout ? &_tv : NULL);
525 if (res < 0 && errno != EINTR && errno != 0) {
526 perror("select");
527 goto out;
529 eloop_process_pending_signals();
531 /* check if some registered timeouts have occurred */
532 if (timeout) {
533 os_get_time(&now);
534 if (!os_time_before(&now, &timeout->time)) {
535 void *eloop_data = timeout->eloop_data;
536 void *user_data = timeout->user_data;
537 eloop_timeout_handler handler =
538 timeout->handler;
539 eloop_remove_timeout(timeout);
540 handler(eloop_data, user_data);
545 if (res <= 0)
546 continue;
548 eloop_sock_table_dispatch(&eloop.readers, rfds);
549 eloop_sock_table_dispatch(&eloop.writers, wfds);
550 eloop_sock_table_dispatch(&eloop.exceptions, efds);
553 out:
554 os_free(rfds);
555 os_free(wfds);
556 os_free(efds);
560 void eloop_terminate(void)
562 eloop.terminate = 1;
566 void eloop_destroy(void)
568 struct eloop_timeout *timeout, *prev;
569 struct os_time now;
571 os_get_time(&now);
572 dl_list_for_each_safe(timeout, prev, &eloop.timeout,
573 struct eloop_timeout, list) {
574 int sec, usec;
575 sec = timeout->time.sec - now.sec;
576 usec = timeout->time.usec - now.usec;
577 if (timeout->time.usec < now.usec) {
578 sec--;
579 usec += 1000000;
581 wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
582 "eloop_data=%p user_data=%p handler=%p",
583 sec, usec, timeout->eloop_data, timeout->user_data,
584 timeout->handler);
585 wpa_trace_dump_funcname("eloop unregistered timeout handler",
586 timeout->handler);
587 wpa_trace_dump("eloop timeout", timeout);
588 eloop_remove_timeout(timeout);
590 eloop_sock_table_destroy(&eloop.readers);
591 eloop_sock_table_destroy(&eloop.writers);
592 eloop_sock_table_destroy(&eloop.exceptions);
593 os_free(eloop.signals);
597 int eloop_terminated(void)
599 return eloop.terminate;
603 void eloop_wait_for_read_sock(int sock)
605 fd_set rfds;
607 if (sock < 0)
608 return;
610 FD_ZERO(&rfds);
611 FD_SET(sock, &rfds);
612 select(sock + 1, &rfds, NULL, NULL, NULL);