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