trace: Filter out uninteresting functions from backtrace
[libeap.git] / src / utils / eloop.c
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
15 #include "includes.h"
16
17 #include "common.h"
18 #include "trace.h"
19 #include "list.h"
20 #include "eloop.h"
21
22
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
31 };
32
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
42 };
43
44 struct eloop_signal {
45         int sig;
46         void *user_data;
47         eloop_signal_handler handler;
48         int signaled;
49 };
50
51 struct eloop_sock_table {
52         int count;
53         struct eloop_sock *table;
54         int changed;
55 };
56
57 struct eloop_data {
58         int max_sock;
59
60         struct eloop_sock_table readers;
61         struct eloop_sock_table writers;
62         struct eloop_sock_table exceptions;
63
64         struct dl_list timeout;
65
66         int signal_count;
67         struct eloop_signal *signals;
68         int signaled;
69         int pending_terminate;
70
71         int terminate;
72         int reader_table_changed;
73 };
74
75 static struct eloop_data eloop;
76
77
78 #ifdef WPA_TRACE
79
80 static void eloop_sigsegv_handler(int sig)
81 {
82         wpa_trace_show("eloop SIGSEGV");
83         abort();
84 }
85
86 static void eloop_trace_sock_add_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_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);
96         }
97 }
98
99
100 static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
101 {
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);
110         }
111 }
112
113 #else /* WPA_TRACE */
114
115 #define eloop_trace_sock_add_ref(table) do { } while (0)
116 #define eloop_trace_sock_remove_ref(table) do { } while (0)
117
118 #endif /* WPA_TRACE */
119
120
121 int eloop_init(void)
122 {
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;
129 }
130
131
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)
135 {
136         struct eloop_sock *tmp;
137
138         if (table == NULL)
139                 return -1;
140
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;
147
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);
159
160         return 0;
161 }
162
163
164 static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
165                                          int sock)
166 {
167         int i;
168
169         if (table == NULL || table->table == NULL || table->count == 0)
170                 return;
171
172         for (i = 0; i < table->count; i++) {
173                 if (table->table[i].sock == sock)
174                         break;
175         }
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));
183         }
184         table->count--;
185         table->changed = 1;
186         eloop_trace_sock_add_ref(table);
187 }
188
189
190 static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
191                                      fd_set *fds)
192 {
193         int i;
194
195         FD_ZERO(fds);
196
197         if (table->table == NULL)
198                 return;
199
200         for (i = 0; i < table->count; i++)
201                 FD_SET(table->table[i].sock, fds);
202 }
203
204
205 static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
206                                       fd_set *fds)
207 {
208         int i;
209
210         if (table == NULL || table->table == NULL)
211                 return;
212
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;
221                 }
222         }
223 }
224
225
226 static void eloop_sock_table_destroy(struct eloop_sock_table *table)
227 {
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\n",
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("eloop sock", &table->table[i]);
239                 }
240                 os_free(table->table);
241         }
242 }
243
244
245 int eloop_register_read_sock(int sock, eloop_sock_handler handler,
246                              void *eloop_data, void *user_data)
247 {
248         return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
249                                    eloop_data, user_data);
250 }
251
252
253 void eloop_unregister_read_sock(int sock)
254 {
255         eloop_unregister_sock(sock, EVENT_TYPE_READ);
256 }
257
258
259 static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
260 {
261         switch (type) {
262         case EVENT_TYPE_READ:
263                 return &eloop.readers;
264         case EVENT_TYPE_WRITE:
265                 return &eloop.writers;
266         case EVENT_TYPE_EXCEPTION:
267                 return &eloop.exceptions;
268         }
269
270         return NULL;
271 }
272
273
274 int eloop_register_sock(int sock, eloop_event_type type,
275                         eloop_sock_handler handler,
276                         void *eloop_data, void *user_data)
277 {
278         struct eloop_sock_table *table;
279
280         table = eloop_get_sock_table(type);
281         return eloop_sock_table_add_sock(table, sock, handler,
282                                          eloop_data, user_data);
283 }
284
285
286 void eloop_unregister_sock(int sock, eloop_event_type type)
287 {
288         struct eloop_sock_table *table;
289
290         table = eloop_get_sock_table(type);
291         eloop_sock_table_remove_sock(table, sock);
292 }
293
294
295 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
296                            eloop_timeout_handler handler,
297                            void *eloop_data, void *user_data)
298 {
299         struct eloop_timeout *timeout, *tmp;
300
301         timeout = os_malloc(sizeof(*timeout));
302         if (timeout == NULL)
303                 return -1;
304         if (os_get_time(&timeout->time) < 0) {
305                 os_free(timeout);
306                 return -1;
307         }
308         timeout->time.sec += secs;
309         timeout->time.usec += usecs;
310         while (timeout->time.usec >= 1000000) {
311                 timeout->time.sec++;
312                 timeout->time.usec -= 1000000;
313         }
314         timeout->eloop_data = eloop_data;
315         timeout->user_data = user_data;
316         timeout->handler = handler;
317         wpa_trace_add_ref(timeout, eloop, eloop_data);
318         wpa_trace_add_ref(timeout, user, user_data);
319         wpa_trace_record(timeout);
320
321         /* Maintain timeouts in order of increasing time */
322         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
323                 if (os_time_before(&timeout->time, &tmp->time)) {
324                         dl_list_add(tmp->list.prev, &timeout->list);
325                         return 0;
326                 }
327         }
328         dl_list_add_tail(&eloop.timeout, &timeout->list);
329
330         return 0;
331 }
332
333
334 static void eloop_remove_timeout(struct eloop_timeout *timeout)
335 {
336         wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
337         wpa_trace_remove_ref(timeout, user, timeout->user_data);
338         os_free(timeout);
339 }
340
341
342 int eloop_cancel_timeout(eloop_timeout_handler handler,
343                          void *eloop_data, void *user_data)
344 {
345         struct eloop_timeout *timeout, *prev;
346         int removed = 0;
347
348         dl_list_for_each_safe(timeout, prev, &eloop.timeout,
349                               struct eloop_timeout, list) {
350                 if (timeout->handler == handler &&
351                     (timeout->eloop_data == eloop_data ||
352                      eloop_data == ELOOP_ALL_CTX) &&
353                     (timeout->user_data == user_data ||
354                      user_data == ELOOP_ALL_CTX)) {
355                         dl_list_del(&timeout->list);
356                         eloop_remove_timeout(timeout);
357                         removed++;
358                 }
359         }
360
361         return removed;
362 }
363
364
365 int eloop_is_timeout_registered(eloop_timeout_handler handler,
366                                 void *eloop_data, void *user_data)
367 {
368         struct eloop_timeout *tmp;
369
370         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
371                 if (tmp->handler == handler &&
372                     tmp->eloop_data == eloop_data &&
373                     tmp->user_data == user_data)
374                         return 1;
375         }
376
377         return 0;
378 }
379
380
381 #ifndef CONFIG_NATIVE_WINDOWS
382 static void eloop_handle_alarm(int sig)
383 {
384         wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
385                    "two seconds. Looks like there\n"
386                    "is a bug that ends up in a busy loop that "
387                    "prevents clean shutdown.\n"
388                    "Killing program forcefully.\n");
389         exit(1);
390 }
391 #endif /* CONFIG_NATIVE_WINDOWS */
392
393
394 static void eloop_handle_signal(int sig)
395 {
396         int i;
397
398 #ifndef CONFIG_NATIVE_WINDOWS
399         if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
400                 /* Use SIGALRM to break out from potential busy loops that
401                  * would not allow the program to be killed. */
402                 eloop.pending_terminate = 1;
403                 signal(SIGALRM, eloop_handle_alarm);
404                 alarm(2);
405         }
406 #endif /* CONFIG_NATIVE_WINDOWS */
407
408         eloop.signaled++;
409         for (i = 0; i < eloop.signal_count; i++) {
410                 if (eloop.signals[i].sig == sig) {
411                         eloop.signals[i].signaled++;
412                         break;
413                 }
414         }
415 }
416
417
418 static void eloop_process_pending_signals(void)
419 {
420         int i;
421
422         if (eloop.signaled == 0)
423                 return;
424         eloop.signaled = 0;
425
426         if (eloop.pending_terminate) {
427 #ifndef CONFIG_NATIVE_WINDOWS
428                 alarm(0);
429 #endif /* CONFIG_NATIVE_WINDOWS */
430                 eloop.pending_terminate = 0;
431         }
432
433         for (i = 0; i < eloop.signal_count; i++) {
434                 if (eloop.signals[i].signaled) {
435                         eloop.signals[i].signaled = 0;
436                         eloop.signals[i].handler(eloop.signals[i].sig,
437                                                  eloop.signals[i].user_data);
438                 }
439         }
440 }
441
442
443 int eloop_register_signal(int sig, eloop_signal_handler handler,
444                           void *user_data)
445 {
446         struct eloop_signal *tmp;
447
448         tmp = (struct eloop_signal *)
449                 os_realloc(eloop.signals,
450                            (eloop.signal_count + 1) *
451                            sizeof(struct eloop_signal));
452         if (tmp == NULL)
453                 return -1;
454
455         tmp[eloop.signal_count].sig = sig;
456         tmp[eloop.signal_count].user_data = user_data;
457         tmp[eloop.signal_count].handler = handler;
458         tmp[eloop.signal_count].signaled = 0;
459         eloop.signal_count++;
460         eloop.signals = tmp;
461         signal(sig, eloop_handle_signal);
462
463         return 0;
464 }
465
466
467 int eloop_register_signal_terminate(eloop_signal_handler handler,
468                                     void *user_data)
469 {
470         int ret = eloop_register_signal(SIGINT, handler, user_data);
471         if (ret == 0)
472                 ret = eloop_register_signal(SIGTERM, handler, user_data);
473         return ret;
474 }
475
476
477 int eloop_register_signal_reconfig(eloop_signal_handler handler,
478                                    void *user_data)
479 {
480 #ifdef CONFIG_NATIVE_WINDOWS
481         return 0;
482 #else /* CONFIG_NATIVE_WINDOWS */
483         return eloop_register_signal(SIGHUP, handler, user_data);
484 #endif /* CONFIG_NATIVE_WINDOWS */
485 }
486
487
488 void eloop_run(void)
489 {
490         fd_set *rfds, *wfds, *efds;
491         int res;
492         struct timeval _tv;
493         struct os_time tv, now;
494
495         rfds = os_malloc(sizeof(*rfds));
496         wfds = os_malloc(sizeof(*wfds));
497         efds = os_malloc(sizeof(*efds));
498         if (rfds == NULL || wfds == NULL || efds == NULL)
499                 goto out;
500
501         while (!eloop.terminate &&
502                (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
503                 eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
504                 struct eloop_timeout *timeout;
505                 timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
506                                         list);
507                 if (timeout) {
508                         os_get_time(&now);
509                         if (os_time_before(&now, &timeout->time))
510                                 os_time_sub(&timeout->time, &now, &tv);
511                         else
512                                 tv.sec = tv.usec = 0;
513                         _tv.tv_sec = tv.sec;
514                         _tv.tv_usec = tv.usec;
515                 }
516
517                 eloop_sock_table_set_fds(&eloop.readers, rfds);
518                 eloop_sock_table_set_fds(&eloop.writers, wfds);
519                 eloop_sock_table_set_fds(&eloop.exceptions, efds);
520                 res = select(eloop.max_sock + 1, rfds, wfds, efds,
521                              timeout ? &_tv : NULL);
522                 if (res < 0 && errno != EINTR && errno != 0) {
523                         perror("select");
524                         goto out;
525                 }
526                 eloop_process_pending_signals();
527
528                 /* check if some registered timeouts have occurred */
529                 if (timeout) {
530                         os_get_time(&now);
531                         if (!os_time_before(&now, &timeout->time)) {
532                                 dl_list_del(&timeout->list);
533                                 timeout->handler(timeout->eloop_data,
534                                                  timeout->user_data);
535                                 eloop_remove_timeout(timeout);
536                         }
537
538                 }
539
540                 if (res <= 0)
541                         continue;
542
543                 eloop_sock_table_dispatch(&eloop.readers, rfds);
544                 eloop_sock_table_dispatch(&eloop.writers, wfds);
545                 eloop_sock_table_dispatch(&eloop.exceptions, efds);
546         }
547
548 out:
549         os_free(rfds);
550         os_free(wfds);
551         os_free(efds);
552 }
553
554
555 void eloop_terminate(void)
556 {
557         eloop.terminate = 1;
558 }
559
560
561 void eloop_destroy(void)
562 {
563         struct eloop_timeout *timeout, *prev;
564         struct os_time now;
565
566         os_get_time(&now);
567         dl_list_for_each_safe(timeout, prev, &eloop.timeout,
568                               struct eloop_timeout, list) {
569                 int sec, usec;
570                 sec = timeout->time.sec - now.sec;
571                 usec = timeout->time.usec - now.usec;
572                 if (timeout->time.usec < now.usec) {
573                         sec--;
574                         usec += 1000000;
575                 }
576                 wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
577                            "eloop_data=%p user_data=%p handler=%p",
578                            sec, usec, timeout->eloop_data, timeout->user_data,
579                            timeout->handler);
580                 wpa_trace_dump("eloop timeout", timeout);
581                 dl_list_del(&timeout->list);
582                 eloop_remove_timeout(timeout);
583         }
584         eloop_sock_table_destroy(&eloop.readers);
585         eloop_sock_table_destroy(&eloop.writers);
586         eloop_sock_table_destroy(&eloop.exceptions);
587         os_free(eloop.signals);
588 }
589
590
591 int eloop_terminated(void)
592 {
593         return eloop.terminate;
594 }
595
596
597 void eloop_wait_for_read_sock(int sock)
598 {
599         fd_set rfds;
600
601         if (sock < 0)
602                 return;
603
604         FD_ZERO(&rfds);
605         FD_SET(sock, &rfds);
606         select(sock + 1, &rfds, NULL, NULL, NULL);
607 }