AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / contrib / hostapd-0.5.8 / eloop.c
blob232e7533cab286cdc31a812c5ecd6b71608a6726
1 /*
2 * Event loop based on select() loop
3 * Copyright (c) 2002-2005, 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 "eloop.h"
21 struct eloop_sock {
22 int sock;
23 void *eloop_data;
24 void *user_data;
25 eloop_sock_handler handler;
28 struct eloop_timeout {
29 struct os_time time;
30 void *eloop_data;
31 void *user_data;
32 eloop_timeout_handler handler;
33 struct eloop_timeout *next;
36 struct eloop_signal {
37 int sig;
38 void *user_data;
39 eloop_signal_handler handler;
40 int signaled;
43 struct eloop_sock_table {
44 int count;
45 struct eloop_sock *table;
46 int changed;
49 struct eloop_data {
50 void *user_data;
52 int max_sock;
54 struct eloop_sock_table readers;
55 struct eloop_sock_table writers;
56 struct eloop_sock_table exceptions;
58 struct eloop_timeout *timeout;
60 int signal_count;
61 struct eloop_signal *signals;
62 int signaled;
63 int pending_terminate;
65 int terminate;
66 int reader_table_changed;
69 static struct eloop_data eloop;
72 int eloop_init(void *user_data)
74 os_memset(&eloop, 0, sizeof(eloop));
75 eloop.user_data = user_data;
76 return 0;
80 static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
81 int sock, eloop_sock_handler handler,
82 void *eloop_data, void *user_data)
84 struct eloop_sock *tmp;
86 if (table == NULL)
87 return -1;
89 tmp = (struct eloop_sock *)
90 os_realloc(table->table,
91 (table->count + 1) * sizeof(struct eloop_sock));
92 if (tmp == NULL)
93 return -1;
95 tmp[table->count].sock = sock;
96 tmp[table->count].eloop_data = eloop_data;
97 tmp[table->count].user_data = user_data;
98 tmp[table->count].handler = handler;
99 table->count++;
100 table->table = tmp;
101 if (sock > eloop.max_sock)
102 eloop.max_sock = sock;
103 table->changed = 1;
105 return 0;
109 static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
110 int sock)
112 int i;
114 if (table == NULL || table->table == NULL || table->count == 0)
115 return;
117 for (i = 0; i < table->count; i++) {
118 if (table->table[i].sock == sock)
119 break;
121 if (i == table->count)
122 return;
123 if (i != table->count - 1) {
124 os_memmove(&table->table[i], &table->table[i + 1],
125 (table->count - i - 1) *
126 sizeof(struct eloop_sock));
128 table->count--;
129 table->changed = 1;
133 static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
134 fd_set *fds)
136 int i;
138 FD_ZERO(fds);
140 if (table->table == NULL)
141 return;
143 for (i = 0; i < table->count; i++)
144 FD_SET(table->table[i].sock, fds);
148 static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
149 fd_set *fds)
151 int i;
153 if (table == NULL || table->table == NULL)
154 return;
156 table->changed = 0;
157 for (i = 0; i < table->count; i++) {
158 if (FD_ISSET(table->table[i].sock, fds)) {
159 table->table[i].handler(table->table[i].sock,
160 table->table[i].eloop_data,
161 table->table[i].user_data);
162 if (table->changed)
163 break;
169 static void eloop_sock_table_destroy(struct eloop_sock_table *table)
171 if (table)
172 os_free(table->table);
176 int eloop_register_read_sock(int sock, eloop_sock_handler handler,
177 void *eloop_data, void *user_data)
179 return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
180 eloop_data, user_data);
184 void eloop_unregister_read_sock(int sock)
186 eloop_unregister_sock(sock, EVENT_TYPE_READ);
190 static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
192 switch (type) {
193 case EVENT_TYPE_READ:
194 return &eloop.readers;
195 case EVENT_TYPE_WRITE:
196 return &eloop.writers;
197 case EVENT_TYPE_EXCEPTION:
198 return &eloop.exceptions;
201 return NULL;
205 int eloop_register_sock(int sock, eloop_event_type type,
206 eloop_sock_handler handler,
207 void *eloop_data, void *user_data)
209 struct eloop_sock_table *table;
211 table = eloop_get_sock_table(type);
212 return eloop_sock_table_add_sock(table, sock, handler,
213 eloop_data, user_data);
217 void eloop_unregister_sock(int sock, eloop_event_type type)
219 struct eloop_sock_table *table;
221 table = eloop_get_sock_table(type);
222 eloop_sock_table_remove_sock(table, sock);
226 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
227 eloop_timeout_handler handler,
228 void *eloop_data, void *user_data)
230 struct eloop_timeout *timeout, *tmp, *prev;
232 timeout = os_malloc(sizeof(*timeout));
233 if (timeout == NULL)
234 return -1;
235 os_get_time(&timeout->time);
236 timeout->time.sec += secs;
237 timeout->time.usec += usecs;
238 while (timeout->time.usec >= 1000000) {
239 timeout->time.sec++;
240 timeout->time.usec -= 1000000;
242 timeout->eloop_data = eloop_data;
243 timeout->user_data = user_data;
244 timeout->handler = handler;
245 timeout->next = NULL;
247 if (eloop.timeout == NULL) {
248 eloop.timeout = timeout;
249 return 0;
252 prev = NULL;
253 tmp = eloop.timeout;
254 while (tmp != NULL) {
255 if (os_time_before(&timeout->time, &tmp->time))
256 break;
257 prev = tmp;
258 tmp = tmp->next;
261 if (prev == NULL) {
262 timeout->next = eloop.timeout;
263 eloop.timeout = timeout;
264 } else {
265 timeout->next = prev->next;
266 prev->next = timeout;
269 return 0;
273 int eloop_cancel_timeout(eloop_timeout_handler handler,
274 void *eloop_data, void *user_data)
276 struct eloop_timeout *timeout, *prev, *next;
277 int removed = 0;
279 prev = NULL;
280 timeout = eloop.timeout;
281 while (timeout != NULL) {
282 next = timeout->next;
284 if (timeout->handler == handler &&
285 (timeout->eloop_data == eloop_data ||
286 eloop_data == ELOOP_ALL_CTX) &&
287 (timeout->user_data == user_data ||
288 user_data == ELOOP_ALL_CTX)) {
289 if (prev == NULL)
290 eloop.timeout = next;
291 else
292 prev->next = next;
293 os_free(timeout);
294 removed++;
295 } else
296 prev = timeout;
298 timeout = next;
301 return removed;
305 #ifndef CONFIG_NATIVE_WINDOWS
306 static void eloop_handle_alarm(int sig)
308 fprintf(stderr, "eloop: could not process SIGINT or SIGTERM in two "
309 "seconds. Looks like there\n"
310 "is a bug that ends up in a busy loop that "
311 "prevents clean shutdown.\n"
312 "Killing program forcefully.\n");
313 exit(1);
315 #endif /* CONFIG_NATIVE_WINDOWS */
318 static void eloop_handle_signal(int sig)
320 int i;
322 #ifndef CONFIG_NATIVE_WINDOWS
323 if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
324 /* Use SIGALRM to break out from potential busy loops that
325 * would not allow the program to be killed. */
326 eloop.pending_terminate = 1;
327 signal(SIGALRM, eloop_handle_alarm);
328 alarm(2);
330 #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;
342 static void eloop_process_pending_signals(void)
344 int i;
346 if (eloop.signaled == 0)
347 return;
348 eloop.signaled = 0;
350 if (eloop.pending_terminate) {
351 #ifndef CONFIG_NATIVE_WINDOWS
352 alarm(0);
353 #endif /* CONFIG_NATIVE_WINDOWS */
354 eloop.pending_terminate = 0;
357 for (i = 0; i < eloop.signal_count; i++) {
358 if (eloop.signals[i].signaled) {
359 eloop.signals[i].signaled = 0;
360 eloop.signals[i].handler(eloop.signals[i].sig,
361 eloop.user_data,
362 eloop.signals[i].user_data);
368 int eloop_register_signal(int sig, eloop_signal_handler handler,
369 void *user_data)
371 struct eloop_signal *tmp;
373 tmp = (struct eloop_signal *)
374 os_realloc(eloop.signals,
375 (eloop.signal_count + 1) *
376 sizeof(struct eloop_signal));
377 if (tmp == NULL)
378 return -1;
380 tmp[eloop.signal_count].sig = sig;
381 tmp[eloop.signal_count].user_data = user_data;
382 tmp[eloop.signal_count].handler = handler;
383 tmp[eloop.signal_count].signaled = 0;
384 eloop.signal_count++;
385 eloop.signals = tmp;
386 signal(sig, eloop_handle_signal);
388 return 0;
392 int eloop_register_signal_terminate(eloop_signal_handler handler,
393 void *user_data)
395 int ret = eloop_register_signal(SIGINT, handler, user_data);
396 if (ret == 0)
397 ret = eloop_register_signal(SIGTERM, handler, user_data);
398 return ret;
402 int eloop_register_signal_reconfig(eloop_signal_handler handler,
403 void *user_data)
405 #ifdef CONFIG_NATIVE_WINDOWS
406 return 0;
407 #else /* CONFIG_NATIVE_WINDOWS */
408 return eloop_register_signal(SIGHUP, handler, user_data);
409 #endif /* CONFIG_NATIVE_WINDOWS */
413 void eloop_run(void)
415 fd_set *rfds, *wfds, *efds;
416 int res;
417 struct timeval _tv;
418 struct os_time tv, now;
420 rfds = os_malloc(sizeof(*rfds));
421 wfds = os_malloc(sizeof(*wfds));
422 efds = os_malloc(sizeof(*efds));
423 if (rfds == NULL || wfds == NULL || efds == NULL) {
424 printf("eloop_run - malloc failed\n");
425 goto out;
428 while (!eloop.terminate &&
429 (eloop.timeout || eloop.readers.count > 0 ||
430 eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
431 if (eloop.timeout) {
432 os_get_time(&now);
433 if (os_time_before(&now, &eloop.timeout->time))
434 os_time_sub(&eloop.timeout->time, &now, &tv);
435 else
436 tv.sec = tv.usec = 0;
437 #if 0
438 printf("next timeout in %lu.%06lu sec\n",
439 tv.sec, tv.usec);
440 #endif
441 _tv.tv_sec = tv.sec;
442 _tv.tv_usec = tv.usec;
445 eloop_sock_table_set_fds(&eloop.readers, rfds);
446 eloop_sock_table_set_fds(&eloop.writers, wfds);
447 eloop_sock_table_set_fds(&eloop.exceptions, efds);
448 res = select(eloop.max_sock + 1, rfds, wfds, efds,
449 eloop.timeout ? &_tv : NULL);
450 if (res < 0 && errno != EINTR && errno != 0) {
451 perror("select");
452 goto out;
454 eloop_process_pending_signals();
456 /* check if some registered timeouts have occurred */
457 if (eloop.timeout) {
458 struct eloop_timeout *tmp;
460 os_get_time(&now);
461 if (!os_time_before(&now, &eloop.timeout->time)) {
462 tmp = eloop.timeout;
463 eloop.timeout = eloop.timeout->next;
464 tmp->handler(tmp->eloop_data,
465 tmp->user_data);
466 os_free(tmp);
471 if (res <= 0)
472 continue;
474 eloop_sock_table_dispatch(&eloop.readers, rfds);
475 eloop_sock_table_dispatch(&eloop.writers, wfds);
476 eloop_sock_table_dispatch(&eloop.exceptions, efds);
479 out:
480 os_free(rfds);
481 os_free(wfds);
482 os_free(efds);
486 void eloop_terminate(void)
488 eloop.terminate = 1;
492 void eloop_destroy(void)
494 struct eloop_timeout *timeout, *prev;
496 timeout = eloop.timeout;
497 while (timeout != NULL) {
498 prev = timeout;
499 timeout = timeout->next;
500 os_free(prev);
502 eloop_sock_table_destroy(&eloop.readers);
503 eloop_sock_table_destroy(&eloop.writers);
504 eloop_sock_table_destroy(&eloop.exceptions);
505 os_free(eloop.signals);
509 int eloop_terminated(void)
511 return eloop.terminate;
515 void eloop_wait_for_read_sock(int sock)
517 fd_set rfds;
519 if (sock < 0)
520 return;
522 FD_ZERO(&rfds);
523 FD_SET(sock, &rfds);
524 select(sock + 1, &rfds, NULL, NULL, NULL);
528 void * eloop_get_user_data(void)
530 return eloop.user_data;