LCOV - code coverage report
Current view: top level - src/utils - eloop.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1426431149 Lines: 281 324 86.7 %
Date: 2015-03-15 Functions: 29 32 90.6 %

          Line data    Source code
       1             : /*
       2             :  * Event loop based on select() loop
       3             :  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : #include <assert.h>
      11             : 
      12             : #include "common.h"
      13             : #include "trace.h"
      14             : #include "list.h"
      15             : #include "eloop.h"
      16             : 
      17             : #if defined(CONFIG_ELOOP_POLL) && defined(CONFIG_ELOOP_EPOLL)
      18             : #error Do not define both of poll and epoll
      19             : #endif
      20             : 
      21             : #if !defined(CONFIG_ELOOP_POLL) && !defined(CONFIG_ELOOP_EPOLL)
      22             : #define CONFIG_ELOOP_SELECT
      23             : #endif
      24             : 
      25             : #ifdef CONFIG_ELOOP_POLL
      26             : #include <poll.h>
      27             : #endif /* CONFIG_ELOOP_POLL */
      28             : 
      29             : #ifdef CONFIG_ELOOP_EPOLL
      30             : #include <sys/epoll.h>
      31             : #endif /* CONFIG_ELOOP_EPOLL */
      32             : 
      33             : struct eloop_sock {
      34             :         int sock;
      35             :         void *eloop_data;
      36             :         void *user_data;
      37             :         eloop_sock_handler handler;
      38             :         WPA_TRACE_REF(eloop);
      39             :         WPA_TRACE_REF(user);
      40             :         WPA_TRACE_INFO
      41             : };
      42             : 
      43             : struct eloop_timeout {
      44             :         struct dl_list list;
      45             :         struct os_reltime time;
      46             :         void *eloop_data;
      47             :         void *user_data;
      48             :         eloop_timeout_handler handler;
      49             :         WPA_TRACE_REF(eloop);
      50             :         WPA_TRACE_REF(user);
      51             :         WPA_TRACE_INFO
      52             : };
      53             : 
      54             : struct eloop_signal {
      55             :         int sig;
      56             :         void *user_data;
      57             :         eloop_signal_handler handler;
      58             :         int signaled;
      59             : };
      60             : 
      61             : struct eloop_sock_table {
      62             :         int count;
      63             :         struct eloop_sock *table;
      64             : #ifdef CONFIG_ELOOP_EPOLL
      65             :         eloop_event_type type;
      66             : #else /* CONFIG_ELOOP_EPOLL */
      67             :         int changed;
      68             : #endif /* CONFIG_ELOOP_EPOLL */
      69             : };
      70             : 
      71             : struct eloop_data {
      72             :         int max_sock;
      73             : 
      74             :         int count; /* sum of all table counts */
      75             : #ifdef CONFIG_ELOOP_POLL
      76             :         int max_pollfd_map; /* number of pollfds_map currently allocated */
      77             :         int max_poll_fds; /* number of pollfds currently allocated */
      78             :         struct pollfd *pollfds;
      79             :         struct pollfd **pollfds_map;
      80             : #endif /* CONFIG_ELOOP_POLL */
      81             : #ifdef CONFIG_ELOOP_EPOLL
      82             :         int epollfd;
      83             :         int epoll_max_event_num;
      84             :         int epoll_max_fd;
      85             :         struct eloop_sock *epoll_table;
      86             :         struct epoll_event *epoll_events;
      87             : #endif /* CONFIG_ELOOP_EPOLL */
      88             :         struct eloop_sock_table readers;
      89             :         struct eloop_sock_table writers;
      90             :         struct eloop_sock_table exceptions;
      91             : 
      92             :         struct dl_list timeout;
      93             : 
      94             :         int signal_count;
      95             :         struct eloop_signal *signals;
      96             :         int signaled;
      97             :         int pending_terminate;
      98             : 
      99             :         int terminate;
     100             : };
     101             : 
     102             : static struct eloop_data eloop;
     103             : 
     104             : 
     105             : #ifdef WPA_TRACE
     106             : 
     107           0 : static void eloop_sigsegv_handler(int sig)
     108             : {
     109           0 :         wpa_trace_show("eloop SIGSEGV");
     110           0 :         abort();
     111             : }
     112             : 
     113       30672 : static void eloop_trace_sock_add_ref(struct eloop_sock_table *table)
     114             : {
     115             :         int i;
     116       30672 :         if (table == NULL || table->table == NULL)
     117       30672 :                 return;
     118      545354 :         for (i = 0; i < table->count; i++) {
     119      514682 :                 wpa_trace_add_ref(&table->table[i], eloop,
     120             :                                   table->table[i].eloop_data);
     121      514682 :                 wpa_trace_add_ref(&table->table[i], user,
     122             :                                   table->table[i].user_data);
     123             :         }
     124             : }
     125             : 
     126             : 
     127       30672 : static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
     128             : {
     129             :         int i;
     130       30672 :         if (table == NULL || table->table == NULL)
     131       30717 :                 return;
     132      545309 :         for (i = 0; i < table->count; i++) {
     133      514682 :                 wpa_trace_remove_ref(&table->table[i], eloop,
     134             :                                      table->table[i].eloop_data);
     135      514682 :                 wpa_trace_remove_ref(&table->table[i], user,
     136             :                                      table->table[i].user_data);
     137             :         }
     138             : }
     139             : 
     140             : #else /* WPA_TRACE */
     141             : 
     142             : #define eloop_trace_sock_add_ref(table) do { } while (0)
     143             : #define eloop_trace_sock_remove_ref(table) do { } while (0)
     144             : 
     145             : #endif /* WPA_TRACE */
     146             : 
     147             : 
     148          65 : int eloop_init(void)
     149             : {
     150          65 :         os_memset(&eloop, 0, sizeof(eloop));
     151          65 :         dl_list_init(&eloop.timeout);
     152             : #ifdef CONFIG_ELOOP_EPOLL
     153             :         eloop.epollfd = epoll_create1(0);
     154             :         if (eloop.epollfd < 0) {
     155             :                 wpa_printf(MSG_ERROR, "%s: epoll_create1 failed. %s\n",
     156             :                            __func__, strerror(errno));
     157             :                 return -1;
     158             :         }
     159             :         eloop.readers.type = EVENT_TYPE_READ;
     160             :         eloop.writers.type = EVENT_TYPE_WRITE;
     161             :         eloop.exceptions.type = EVENT_TYPE_EXCEPTION;
     162             : #endif /* CONFIG_ELOOP_EPOLL */
     163             : #ifdef WPA_TRACE
     164          65 :         signal(SIGSEGV, eloop_sigsegv_handler);
     165             : #endif /* WPA_TRACE */
     166          65 :         return 0;
     167             : }
     168             : 
     169             : 
     170       15338 : static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
     171             :                                      int sock, eloop_sock_handler handler,
     172             :                                      void *eloop_data, void *user_data)
     173             : {
     174             : #ifdef CONFIG_ELOOP_EPOLL
     175             :         struct eloop_sock *temp_table;
     176             :         struct epoll_event ev, *temp_events;
     177             :         int next;
     178             : #endif /* CONFIG_ELOOP_EPOLL */
     179             :         struct eloop_sock *tmp;
     180             :         int new_max_sock;
     181             : 
     182       15338 :         if (sock > eloop.max_sock)
     183         957 :                 new_max_sock = sock;
     184             :         else
     185       14381 :                 new_max_sock = eloop.max_sock;
     186             : 
     187       15338 :         if (table == NULL)
     188           0 :                 return -1;
     189             : 
     190             : #ifdef CONFIG_ELOOP_POLL
     191             :         if (new_max_sock >= eloop.max_pollfd_map) {
     192             :                 struct pollfd **nmap;
     193             :                 nmap = os_realloc_array(eloop.pollfds_map, new_max_sock + 50,
     194             :                                         sizeof(struct pollfd *));
     195             :                 if (nmap == NULL)
     196             :                         return -1;
     197             : 
     198             :                 eloop.max_pollfd_map = new_max_sock + 50;
     199             :                 eloop.pollfds_map = nmap;
     200             :         }
     201             : 
     202             :         if (eloop.count + 1 > eloop.max_poll_fds) {
     203             :                 struct pollfd *n;
     204             :                 int nmax = eloop.count + 1 + 50;
     205             :                 n = os_realloc_array(eloop.pollfds, nmax,
     206             :                                      sizeof(struct pollfd));
     207             :                 if (n == NULL)
     208             :                         return -1;
     209             : 
     210             :                 eloop.max_poll_fds = nmax;
     211             :                 eloop.pollfds = n;
     212             :         }
     213             : #endif /* CONFIG_ELOOP_POLL */
     214             : #ifdef CONFIG_ELOOP_EPOLL
     215             :         if (new_max_sock >= eloop.epoll_max_fd) {
     216             :                 next = eloop.epoll_max_fd == 0 ? 16 : eloop.epoll_max_fd * 2;
     217             :                 temp_table = os_realloc_array(eloop.epoll_table, next,
     218             :                                               sizeof(struct eloop_sock));
     219             :                 if (temp_table == NULL)
     220             :                         return -1;
     221             : 
     222             :                 eloop.epoll_max_fd = next;
     223             :                 eloop.epoll_table = temp_table;
     224             :         }
     225             : 
     226             :         if (eloop.count + 1 > eloop.epoll_max_event_num) {
     227             :                 next = eloop.epoll_max_event_num == 0 ? 8 :
     228             :                         eloop.epoll_max_event_num * 2;
     229             :                 temp_events = os_realloc_array(eloop.epoll_events, next,
     230             :                                                sizeof(struct epoll_event));
     231             :                 if (temp_events == NULL) {
     232             :                         wpa_printf(MSG_ERROR, "%s: malloc for epoll failed. "
     233             :                                    "%s\n", __func__, strerror(errno));
     234             :                         return -1;
     235             :                 }
     236             : 
     237             :                 eloop.epoll_max_event_num = next;
     238             :                 eloop.epoll_events = temp_events;
     239             :         }
     240             : #endif /* CONFIG_ELOOP_EPOLL */
     241             : 
     242       15338 :         eloop_trace_sock_remove_ref(table);
     243       15338 :         tmp = os_realloc_array(table->table, table->count + 1,
     244             :                                sizeof(struct eloop_sock));
     245       15338 :         if (tmp == NULL) {
     246           4 :                 eloop_trace_sock_add_ref(table);
     247           4 :                 return -1;
     248             :         }
     249             : 
     250       15334 :         tmp[table->count].sock = sock;
     251       15334 :         tmp[table->count].eloop_data = eloop_data;
     252       15334 :         tmp[table->count].user_data = user_data;
     253       15334 :         tmp[table->count].handler = handler;
     254       15334 :         wpa_trace_record(&tmp[table->count]);
     255       15334 :         table->count++;
     256       15334 :         table->table = tmp;
     257       15334 :         eloop.max_sock = new_max_sock;
     258       15334 :         eloop.count++;
     259             : #ifndef CONFIG_ELOOP_EPOLL
     260       15334 :         table->changed = 1;
     261             : #endif /* CONFIG_ELOOP_EPOLL */
     262       15334 :         eloop_trace_sock_add_ref(table);
     263             : 
     264             : #ifdef CONFIG_ELOOP_EPOLL
     265             :         os_memset(&ev, 0, sizeof(ev));
     266             :         switch (table->type) {
     267             :         case EVENT_TYPE_READ:
     268             :                 ev.events = EPOLLIN;
     269             :                 break;
     270             :         case EVENT_TYPE_WRITE:
     271             :                 ev.events = EPOLLOUT;
     272             :                 break;
     273             :         /*
     274             :          * Exceptions are always checked when using epoll, but I suppose it's
     275             :          * possible that someone registered a socket *only* for exception
     276             :          * handling.
     277             :          */
     278             :         case EVENT_TYPE_EXCEPTION:
     279             :                 ev.events = EPOLLERR | EPOLLHUP;
     280             :                 break;
     281             :         }
     282             :         ev.data.fd = sock;
     283             :         if (epoll_ctl(eloop.epollfd, EPOLL_CTL_ADD, sock, &ev) < 0) {
     284             :                 wpa_printf(MSG_ERROR, "%s: epoll_ctl(ADD) for fd=%d "
     285             :                            "failed. %s\n", __func__, sock, strerror(errno));
     286             :                 return -1;
     287             :         }
     288             :         os_memcpy(&eloop.epoll_table[sock], &table->table[table->count - 1],
     289             :                   sizeof(struct eloop_sock));
     290             : #endif /* CONFIG_ELOOP_EPOLL */
     291       15334 :         return 0;
     292             : }
     293             : 
     294             : 
     295       15833 : static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
     296             :                                          int sock)
     297             : {
     298             :         int i;
     299             : 
     300       15833 :         if (table == NULL || table->table == NULL || table->count == 0)
     301         166 :                 return;
     302             : 
     303      246981 :         for (i = 0; i < table->count; i++) {
     304      246648 :                 if (table->table[i].sock == sock)
     305       15334 :                         break;
     306             :         }
     307       15667 :         if (i == table->count)
     308         333 :                 return;
     309       15334 :         eloop_trace_sock_remove_ref(table);
     310       15334 :         if (i != table->count - 1) {
     311        6166 :                 os_memmove(&table->table[i], &table->table[i + 1],
     312             :                            (table->count - i - 1) *
     313             :                            sizeof(struct eloop_sock));
     314             :         }
     315       15334 :         table->count--;
     316       15334 :         eloop.count--;
     317             : #ifndef CONFIG_ELOOP_EPOLL
     318       15334 :         table->changed = 1;
     319             : #endif /* CONFIG_ELOOP_EPOLL */
     320       15334 :         eloop_trace_sock_add_ref(table);
     321             : #ifdef CONFIG_ELOOP_EPOLL
     322             :         if (epoll_ctl(eloop.epollfd, EPOLL_CTL_DEL, sock, NULL) < 0) {
     323             :                 wpa_printf(MSG_ERROR, "%s: epoll_ctl(DEL) for fd=%d "
     324             :                            "failed. %s\n", __func__, sock, strerror(errno));
     325             :                 return;
     326             :         }
     327             :         os_memset(&eloop.epoll_table[sock], 0, sizeof(struct eloop_sock));
     328             : #endif /* CONFIG_ELOOP_EPOLL */
     329             : }
     330             : 
     331             : 
     332             : #ifdef CONFIG_ELOOP_POLL
     333             : 
     334             : static struct pollfd * find_pollfd(struct pollfd **pollfds_map, int fd, int mx)
     335             : {
     336             :         if (fd < mx && fd >= 0)
     337             :                 return pollfds_map[fd];
     338             :         return NULL;
     339             : }
     340             : 
     341             : 
     342             : static int eloop_sock_table_set_fds(struct eloop_sock_table *readers,
     343             :                                     struct eloop_sock_table *writers,
     344             :                                     struct eloop_sock_table *exceptions,
     345             :                                     struct pollfd *pollfds,
     346             :                                     struct pollfd **pollfds_map,
     347             :                                     int max_pollfd_map)
     348             : {
     349             :         int i;
     350             :         int nxt = 0;
     351             :         int fd;
     352             :         struct pollfd *pfd;
     353             : 
     354             :         /* Clear pollfd lookup map. It will be re-populated below. */
     355             :         os_memset(pollfds_map, 0, sizeof(struct pollfd *) * max_pollfd_map);
     356             : 
     357             :         if (readers && readers->table) {
     358             :                 for (i = 0; i < readers->count; i++) {
     359             :                         fd = readers->table[i].sock;
     360             :                         assert(fd >= 0 && fd < max_pollfd_map);
     361             :                         pollfds[nxt].fd = fd;
     362             :                         pollfds[nxt].events = POLLIN;
     363             :                         pollfds[nxt].revents = 0;
     364             :                         pollfds_map[fd] = &(pollfds[nxt]);
     365             :                         nxt++;
     366             :                 }
     367             :         }
     368             : 
     369             :         if (writers && writers->table) {
     370             :                 for (i = 0; i < writers->count; i++) {
     371             :                         /*
     372             :                          * See if we already added this descriptor, update it
     373             :                          * if so.
     374             :                          */
     375             :                         fd = writers->table[i].sock;
     376             :                         assert(fd >= 0 && fd < max_pollfd_map);
     377             :                         pfd = pollfds_map[fd];
     378             :                         if (!pfd) {
     379             :                                 pfd = &(pollfds[nxt]);
     380             :                                 pfd->events = 0;
     381             :                                 pfd->fd = fd;
     382             :                                 pollfds[i].revents = 0;
     383             :                                 pollfds_map[fd] = pfd;
     384             :                                 nxt++;
     385             :                         }
     386             :                         pfd->events |= POLLOUT;
     387             :                 }
     388             :         }
     389             : 
     390             :         /*
     391             :          * Exceptions are always checked when using poll, but I suppose it's
     392             :          * possible that someone registered a socket *only* for exception
     393             :          * handling. Set the POLLIN bit in this case.
     394             :          */
     395             :         if (exceptions && exceptions->table) {
     396             :                 for (i = 0; i < exceptions->count; i++) {
     397             :                         /*
     398             :                          * See if we already added this descriptor, just use it
     399             :                          * if so.
     400             :                          */
     401             :                         fd = exceptions->table[i].sock;
     402             :                         assert(fd >= 0 && fd < max_pollfd_map);
     403             :                         pfd = pollfds_map[fd];
     404             :                         if (!pfd) {
     405             :                                 pfd = &(pollfds[nxt]);
     406             :                                 pfd->events = POLLIN;
     407             :                                 pfd->fd = fd;
     408             :                                 pollfds[i].revents = 0;
     409             :                                 pollfds_map[fd] = pfd;
     410             :                                 nxt++;
     411             :                         }
     412             :                 }
     413             :         }
     414             : 
     415             :         return nxt;
     416             : }
     417             : 
     418             : 
     419             : static int eloop_sock_table_dispatch_table(struct eloop_sock_table *table,
     420             :                                            struct pollfd **pollfds_map,
     421             :                                            int max_pollfd_map,
     422             :                                            short int revents)
     423             : {
     424             :         int i;
     425             :         struct pollfd *pfd;
     426             : 
     427             :         if (!table || !table->table)
     428             :                 return 0;
     429             : 
     430             :         table->changed = 0;
     431             :         for (i = 0; i < table->count; i++) {
     432             :                 pfd = find_pollfd(pollfds_map, table->table[i].sock,
     433             :                                   max_pollfd_map);
     434             :                 if (!pfd)
     435             :                         continue;
     436             : 
     437             :                 if (!(pfd->revents & revents))
     438             :                         continue;
     439             : 
     440             :                 table->table[i].handler(table->table[i].sock,
     441             :                                         table->table[i].eloop_data,
     442             :                                         table->table[i].user_data);
     443             :                 if (table->changed)
     444             :                         return 1;
     445             :         }
     446             : 
     447             :         return 0;
     448             : }
     449             : 
     450             : 
     451             : static void eloop_sock_table_dispatch(struct eloop_sock_table *readers,
     452             :                                       struct eloop_sock_table *writers,
     453             :                                       struct eloop_sock_table *exceptions,
     454             :                                       struct pollfd **pollfds_map,
     455             :                                       int max_pollfd_map)
     456             : {
     457             :         if (eloop_sock_table_dispatch_table(readers, pollfds_map,
     458             :                                             max_pollfd_map, POLLIN | POLLERR |
     459             :                                             POLLHUP))
     460             :                 return; /* pollfds may be invalid at this point */
     461             : 
     462             :         if (eloop_sock_table_dispatch_table(writers, pollfds_map,
     463             :                                             max_pollfd_map, POLLOUT))
     464             :                 return; /* pollfds may be invalid at this point */
     465             : 
     466             :         eloop_sock_table_dispatch_table(exceptions, pollfds_map,
     467             :                                         max_pollfd_map, POLLERR | POLLHUP);
     468             : }
     469             : 
     470             : #endif /* CONFIG_ELOOP_POLL */
     471             : 
     472             : #ifdef CONFIG_ELOOP_SELECT
     473             : 
     474     1445634 : static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
     475             :                                      fd_set *fds)
     476             : {
     477             :         int i;
     478             : 
     479     1445634 :         FD_ZERO(fds);
     480             : 
     481     1445634 :         if (table->table == NULL)
     482     2152691 :                 return;
     483             : 
     484     5510208 :         for (i = 0; i < table->count; i++) {
     485     4771631 :                 assert(table->table[i].sock >= 0);
     486     4771631 :                 FD_SET(table->table[i].sock, fds);
     487             :         }
     488             : }
     489             : 
     490             : 
     491     1297965 : static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
     492             :                                       fd_set *fds)
     493             : {
     494             :         int i;
     495             : 
     496     1297965 :         if (table == NULL || table->table == NULL)
     497     1945888 :                 return;
     498             : 
     499      650042 :         table->changed = 0;
     500     4692757 :         for (i = 0; i < table->count; i++) {
     501     4051557 :                 if (FD_ISSET(table->table[i].sock, fds)) {
     502     1549935 :                         table->table[i].handler(table->table[i].sock,
     503      516645 :                                                 table->table[i].eloop_data,
     504      516645 :                                                 table->table[i].user_data);
     505      516645 :                         if (table->changed)
     506        8842 :                                 break;
     507             :                 }
     508             :         }
     509             : }
     510             : 
     511             : #endif /* CONFIG_ELOOP_SELECT */
     512             : 
     513             : 
     514             : #ifdef CONFIG_ELOOP_EPOLL
     515             : static void eloop_sock_table_dispatch(struct epoll_event *events, int nfds)
     516             : {
     517             :         struct eloop_sock *table;
     518             :         int i;
     519             : 
     520             :         for (i = 0; i < nfds; i++) {
     521             :                 table = &eloop.epoll_table[events[i].data.fd];
     522             :                 if (table->handler == NULL)
     523             :                         continue;
     524             :                 table->handler(table->sock, table->eloop_data,
     525             :                                table->user_data);
     526             :         }
     527             : }
     528             : #endif /* CONFIG_ELOOP_EPOLL */
     529             : 
     530             : 
     531         195 : static void eloop_sock_table_destroy(struct eloop_sock_table *table)
     532             : {
     533         195 :         if (table) {
     534             :                 int i;
     535         195 :                 for (i = 0; i < table->count && table->table; i++) {
     536           0 :                         wpa_printf(MSG_INFO, "ELOOP: remaining socket: "
     537             :                                    "sock=%d eloop_data=%p user_data=%p "
     538             :                                    "handler=%p",
     539           0 :                                    table->table[i].sock,
     540           0 :                                    table->table[i].eloop_data,
     541           0 :                                    table->table[i].user_data,
     542           0 :                                    table->table[i].handler);
     543           0 :                         wpa_trace_dump_funcname("eloop unregistered socket "
     544             :                                                 "handler",
     545           0 :                                                 table->table[i].handler);
     546           0 :                         wpa_trace_dump("eloop sock", &table->table[i]);
     547             :                 }
     548         195 :                 os_free(table->table);
     549             :         }
     550         195 : }
     551             : 
     552             : 
     553       14775 : int eloop_register_read_sock(int sock, eloop_sock_handler handler,
     554             :                              void *eloop_data, void *user_data)
     555             : {
     556       14775 :         return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
     557             :                                    eloop_data, user_data);
     558             : }
     559             : 
     560             : 
     561       14775 : void eloop_unregister_read_sock(int sock)
     562             : {
     563       14775 :         eloop_unregister_sock(sock, EVENT_TYPE_READ);
     564       14775 : }
     565             : 
     566             : 
     567       31171 : static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
     568             : {
     569       31171 :         switch (type) {
     570             :         case EVENT_TYPE_READ:
     571       30679 :                 return &eloop.readers;
     572             :         case EVENT_TYPE_WRITE:
     573         477 :                 return &eloop.writers;
     574             :         case EVENT_TYPE_EXCEPTION:
     575          15 :                 return &eloop.exceptions;
     576             :         }
     577             : 
     578           0 :         return NULL;
     579             : }
     580             : 
     581             : 
     582       15338 : int eloop_register_sock(int sock, eloop_event_type type,
     583             :                         eloop_sock_handler handler,
     584             :                         void *eloop_data, void *user_data)
     585             : {
     586             :         struct eloop_sock_table *table;
     587             : 
     588       15338 :         assert(sock >= 0);
     589       15338 :         table = eloop_get_sock_table(type);
     590       15338 :         return eloop_sock_table_add_sock(table, sock, handler,
     591             :                                          eloop_data, user_data);
     592             : }
     593             : 
     594             : 
     595       15833 : void eloop_unregister_sock(int sock, eloop_event_type type)
     596             : {
     597             :         struct eloop_sock_table *table;
     598             : 
     599       15833 :         table = eloop_get_sock_table(type);
     600       15833 :         eloop_sock_table_remove_sock(table, sock);
     601       15833 : }
     602             : 
     603             : 
     604      117165 : int eloop_register_timeout(unsigned int secs, unsigned int usecs,
     605             :                            eloop_timeout_handler handler,
     606             :                            void *eloop_data, void *user_data)
     607             : {
     608             :         struct eloop_timeout *timeout, *tmp;
     609             :         os_time_t now_sec;
     610             : 
     611      117165 :         timeout = os_zalloc(sizeof(*timeout));
     612      117165 :         if (timeout == NULL)
     613          39 :                 return -1;
     614      117126 :         if (os_get_reltime(&timeout->time) < 0) {
     615           0 :                 os_free(timeout);
     616           0 :                 return -1;
     617             :         }
     618      117126 :         now_sec = timeout->time.sec;
     619      117126 :         timeout->time.sec += secs;
     620      117126 :         if (timeout->time.sec < now_sec) {
     621             :                 /*
     622             :                  * Integer overflow - assume long enough timeout to be assumed
     623             :                  * to be infinite, i.e., the timeout would never happen.
     624             :                  */
     625           0 :                 wpa_printf(MSG_DEBUG, "ELOOP: Too long timeout (secs=%u) to "
     626             :                            "ever happen - ignore it", secs);
     627           0 :                 os_free(timeout);
     628           0 :                 return 0;
     629             :         }
     630      117126 :         timeout->time.usec += usecs;
     631      239287 :         while (timeout->time.usec >= 1000000) {
     632        5035 :                 timeout->time.sec++;
     633        5035 :                 timeout->time.usec -= 1000000;
     634             :         }
     635      117126 :         timeout->eloop_data = eloop_data;
     636      117126 :         timeout->user_data = user_data;
     637      117126 :         timeout->handler = handler;
     638      117126 :         wpa_trace_add_ref(timeout, eloop, eloop_data);
     639      117126 :         wpa_trace_add_ref(timeout, user, user_data);
     640      117126 :         wpa_trace_record(timeout);
     641             : 
     642             :         /* Maintain timeouts in order of increasing time */
     643      370385 :         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
     644      357652 :                 if (os_reltime_before(&timeout->time, &tmp->time)) {
     645      104393 :                         dl_list_add(tmp->list.prev, &timeout->list);
     646      104393 :                         return 0;
     647             :                 }
     648             :         }
     649       12733 :         dl_list_add_tail(&eloop.timeout, &timeout->list);
     650             : 
     651       12733 :         return 0;
     652             : }
     653             : 
     654             : 
     655      117126 : static void eloop_remove_timeout(struct eloop_timeout *timeout)
     656             : {
     657      117126 :         dl_list_del(&timeout->list);
     658      117126 :         wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
     659      117126 :         wpa_trace_remove_ref(timeout, user, timeout->user_data);
     660      117126 :         os_free(timeout);
     661      117126 : }
     662             : 
     663             : 
     664      300292 : int eloop_cancel_timeout(eloop_timeout_handler handler,
     665             :                          void *eloop_data, void *user_data)
     666             : {
     667             :         struct eloop_timeout *timeout, *prev;
     668      300292 :         int removed = 0;
     669             : 
     670     2563732 :         dl_list_for_each_safe(timeout, prev, &eloop.timeout,
     671             :                               struct eloop_timeout, list) {
     672     2363558 :                 if (timeout->handler == handler &&
     673      112860 :                     (timeout->eloop_data == eloop_data ||
     674       87390 :                      eloop_data == ELOOP_ALL_CTX) &&
     675      121748 :                     (timeout->user_data == user_data ||
     676             :                      user_data == ELOOP_ALL_CTX)) {
     677       53045 :                         eloop_remove_timeout(timeout);
     678       53045 :                         removed++;
     679             :                 }
     680             :         }
     681             : 
     682      300292 :         return removed;
     683             : }
     684             : 
     685             : 
     686          19 : int eloop_cancel_timeout_one(eloop_timeout_handler handler,
     687             :                              void *eloop_data, void *user_data,
     688             :                              struct os_reltime *remaining)
     689             : {
     690             :         struct eloop_timeout *timeout, *prev;
     691          19 :         int removed = 0;
     692             :         struct os_reltime now;
     693             : 
     694          19 :         os_get_reltime(&now);
     695          19 :         remaining->sec = remaining->usec = 0;
     696             : 
     697          49 :         dl_list_for_each_safe(timeout, prev, &eloop.timeout,
     698             :                               struct eloop_timeout, list) {
     699          42 :                 if (timeout->handler == handler &&
     700          12 :                     (timeout->eloop_data == eloop_data) &&
     701           6 :                     (timeout->user_data == user_data)) {
     702           6 :                         removed = 1;
     703           6 :                         if (os_reltime_before(&now, &timeout->time))
     704           6 :                                 os_reltime_sub(&timeout->time, &now, remaining);
     705           6 :                         eloop_remove_timeout(timeout);
     706           6 :                         break;
     707             :                 }
     708             :         }
     709          19 :         return removed;
     710             : }
     711             : 
     712             : 
     713       33120 : int eloop_is_timeout_registered(eloop_timeout_handler handler,
     714             :                                 void *eloop_data, void *user_data)
     715             : {
     716             :         struct eloop_timeout *tmp;
     717             : 
     718      162490 :         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
     719      160462 :                 if (tmp->handler == handler &&
     720       34211 :                     tmp->eloop_data == eloop_data &&
     721       17105 :                     tmp->user_data == user_data)
     722       13986 :                         return 1;
     723             :         }
     724             : 
     725       19134 :         return 0;
     726             : }
     727             : 
     728             : 
     729        4813 : int eloop_deplete_timeout(unsigned int req_secs, unsigned int req_usecs,
     730             :                           eloop_timeout_handler handler, void *eloop_data,
     731             :                           void *user_data)
     732             : {
     733             :         struct os_reltime now, requested, remaining;
     734             :         struct eloop_timeout *tmp;
     735             : 
     736       35107 :         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
     737       31160 :                 if (tmp->handler == handler &&
     738         866 :                     tmp->eloop_data == eloop_data &&
     739         433 :                     tmp->user_data == user_data) {
     740         433 :                         requested.sec = req_secs;
     741         433 :                         requested.usec = req_usecs;
     742         433 :                         os_get_reltime(&now);
     743         433 :                         os_reltime_sub(&tmp->time, &now, &remaining);
     744         433 :                         if (os_reltime_before(&requested, &remaining)) {
     745         123 :                                 eloop_cancel_timeout(handler, eloop_data,
     746             :                                                      user_data);
     747         123 :                                 eloop_register_timeout(requested.sec,
     748         123 :                                                        requested.usec,
     749             :                                                        handler, eloop_data,
     750             :                                                        user_data);
     751         123 :                                 return 1;
     752             :                         }
     753         310 :                         return 0;
     754             :                 }
     755             :         }
     756             : 
     757        4380 :         return -1;
     758             : }
     759             : 
     760             : 
     761           9 : int eloop_replenish_timeout(unsigned int req_secs, unsigned int req_usecs,
     762             :                             eloop_timeout_handler handler, void *eloop_data,
     763             :                             void *user_data)
     764             : {
     765             :         struct os_reltime now, requested, remaining;
     766             :         struct eloop_timeout *tmp;
     767             : 
     768          17 :         dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
     769          24 :                 if (tmp->handler == handler &&
     770          16 :                     tmp->eloop_data == eloop_data &&
     771           8 :                     tmp->user_data == user_data) {
     772           8 :                         requested.sec = req_secs;
     773           8 :                         requested.usec = req_usecs;
     774           8 :                         os_get_reltime(&now);
     775           8 :                         os_reltime_sub(&tmp->time, &now, &remaining);
     776           8 :                         if (os_reltime_before(&remaining, &requested)) {
     777           8 :                                 eloop_cancel_timeout(handler, eloop_data,
     778             :                                                      user_data);
     779           8 :                                 eloop_register_timeout(requested.sec,
     780           8 :                                                        requested.usec,
     781             :                                                        handler, eloop_data,
     782             :                                                        user_data);
     783           8 :                                 return 1;
     784             :                         }
     785           0 :                         return 0;
     786             :                 }
     787             :         }
     788             : 
     789           1 :         return -1;
     790             : }
     791             : 
     792             : 
     793             : #ifndef CONFIG_NATIVE_WINDOWS
     794           0 : static void eloop_handle_alarm(int sig)
     795             : {
     796           0 :         wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
     797             :                    "two seconds. Looks like there\n"
     798             :                    "is a bug that ends up in a busy loop that "
     799             :                    "prevents clean shutdown.\n"
     800             :                    "Killing program forcefully.\n");
     801           0 :         exit(1);
     802             : }
     803             : #endif /* CONFIG_NATIVE_WINDOWS */
     804             : 
     805             : 
     806        1317 : static void eloop_handle_signal(int sig)
     807             : {
     808             :         int i;
     809             : 
     810             : #ifndef CONFIG_NATIVE_WINDOWS
     811        1317 :         if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
     812             :                 /* Use SIGALRM to break out from potential busy loops that
     813             :                  * would not allow the program to be killed. */
     814          30 :                 eloop.pending_terminate = 1;
     815          30 :                 signal(SIGALRM, eloop_handle_alarm);
     816          30 :                 alarm(2);
     817             :         }
     818             : #endif /* CONFIG_NATIVE_WINDOWS */
     819             : 
     820        1317 :         eloop.signaled++;
     821        1372 :         for (i = 0; i < eloop.signal_count; i++) {
     822        1372 :                 if (eloop.signals[i].sig == sig) {
     823        1317 :                         eloop.signals[i].signaled++;
     824        1317 :                         break;
     825             :                 }
     826             :         }
     827        1317 : }
     828             : 
     829             : 
     830      481878 : static void eloop_process_pending_signals(void)
     831             : {
     832             :         int i;
     833             : 
     834      481878 :         if (eloop.signaled == 0)
     835      962444 :                 return;
     836        1312 :         eloop.signaled = 0;
     837             : 
     838        1312 :         if (eloop.pending_terminate) {
     839             : #ifndef CONFIG_NATIVE_WINDOWS
     840          30 :                 alarm(0);
     841             : #endif /* CONFIG_NATIVE_WINDOWS */
     842          30 :                 eloop.pending_terminate = 0;
     843             :         }
     844             : 
     845        6545 :         for (i = 0; i < eloop.signal_count; i++) {
     846        5233 :                 if (eloop.signals[i].signaled) {
     847        1313 :                         eloop.signals[i].signaled = 0;
     848        2626 :                         eloop.signals[i].handler(eloop.signals[i].sig,
     849        1313 :                                                  eloop.signals[i].user_data);
     850             :                 }
     851             :         }
     852             : }
     853             : 
     854             : 
     855         145 : int eloop_register_signal(int sig, eloop_signal_handler handler,
     856             :                           void *user_data)
     857             : {
     858             :         struct eloop_signal *tmp;
     859             : 
     860         145 :         tmp = os_realloc_array(eloop.signals, eloop.signal_count + 1,
     861             :                                sizeof(struct eloop_signal));
     862         145 :         if (tmp == NULL)
     863           0 :                 return -1;
     864             : 
     865         145 :         tmp[eloop.signal_count].sig = sig;
     866         145 :         tmp[eloop.signal_count].user_data = user_data;
     867         145 :         tmp[eloop.signal_count].handler = handler;
     868         145 :         tmp[eloop.signal_count].signaled = 0;
     869         145 :         eloop.signal_count++;
     870         145 :         eloop.signals = tmp;
     871         145 :         signal(sig, eloop_handle_signal);
     872             : 
     873         145 :         return 0;
     874             : }
     875             : 
     876             : 
     877          50 : int eloop_register_signal_terminate(eloop_signal_handler handler,
     878             :                                     void *user_data)
     879             : {
     880          50 :         int ret = eloop_register_signal(SIGINT, handler, user_data);
     881          50 :         if (ret == 0)
     882          50 :                 ret = eloop_register_signal(SIGTERM, handler, user_data);
     883          50 :         return ret;
     884             : }
     885             : 
     886             : 
     887          20 : int eloop_register_signal_reconfig(eloop_signal_handler handler,
     888             :                                    void *user_data)
     889             : {
     890             : #ifdef CONFIG_NATIVE_WINDOWS
     891             :         return 0;
     892             : #else /* CONFIG_NATIVE_WINDOWS */
     893          20 :         return eloop_register_signal(SIGHUP, handler, user_data);
     894             : #endif /* CONFIG_NATIVE_WINDOWS */
     895             : }
     896             : 
     897             : 
     898          30 : void eloop_run(void)
     899             : {
     900             : #ifdef CONFIG_ELOOP_POLL
     901             :         int num_poll_fds;
     902             :         int timeout_ms = 0;
     903             : #endif /* CONFIG_ELOOP_POLL */
     904             : #ifdef CONFIG_ELOOP_SELECT
     905             :         fd_set *rfds, *wfds, *efds;
     906             :         struct timeval _tv;
     907             : #endif /* CONFIG_ELOOP_SELECT */
     908             : #ifdef CONFIG_ELOOP_EPOLL
     909             :         int timeout_ms = -1;
     910             : #endif /* CONFIG_ELOOP_EPOLL */
     911             :         int res;
     912             :         struct os_reltime tv, now;
     913             : 
     914             : #ifdef CONFIG_ELOOP_SELECT
     915          30 :         rfds = os_malloc(sizeof(*rfds));
     916          30 :         wfds = os_malloc(sizeof(*wfds));
     917          30 :         efds = os_malloc(sizeof(*efds));
     918          30 :         if (rfds == NULL || wfds == NULL || efds == NULL)
     919             :                 goto out;
     920             : #endif /* CONFIG_ELOOP_SELECT */
     921             : 
     922      963816 :         while (!eloop.terminate &&
     923      567790 :                (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
     924           0 :                 eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
     925             :                 struct eloop_timeout *timeout;
     926      481878 :                 timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
     927             :                                         list);
     928      481878 :                 if (timeout) {
     929      395966 :                         os_get_reltime(&now);
     930      395966 :                         if (os_reltime_before(&now, &timeout->time))
     931      347066 :                                 os_reltime_sub(&timeout->time, &now, &tv);
     932             :                         else
     933       48900 :                                 tv.sec = tv.usec = 0;
     934             : #if defined(CONFIG_ELOOP_POLL) || defined(CONFIG_ELOOP_EPOLL)
     935             :                         timeout_ms = tv.sec * 1000 + tv.usec / 1000;
     936             : #endif /* defined(CONFIG_ELOOP_POLL) || defined(CONFIG_ELOOP_EPOLL) */
     937             : #ifdef CONFIG_ELOOP_SELECT
     938      395966 :                         _tv.tv_sec = tv.sec;
     939      395966 :                         _tv.tv_usec = tv.usec;
     940             : #endif /* CONFIG_ELOOP_SELECT */
     941             :                 }
     942             : 
     943             : #ifdef CONFIG_ELOOP_POLL
     944             :                 num_poll_fds = eloop_sock_table_set_fds(
     945             :                         &eloop.readers, &eloop.writers, &eloop.exceptions,
     946             :                         eloop.pollfds, eloop.pollfds_map,
     947             :                         eloop.max_pollfd_map);
     948             :                 res = poll(eloop.pollfds, num_poll_fds,
     949             :                            timeout ? timeout_ms : -1);
     950             : #endif /* CONFIG_ELOOP_POLL */
     951             : #ifdef CONFIG_ELOOP_SELECT
     952      481878 :                 eloop_sock_table_set_fds(&eloop.readers, rfds);
     953      481878 :                 eloop_sock_table_set_fds(&eloop.writers, wfds);
     954      481878 :                 eloop_sock_table_set_fds(&eloop.exceptions, efds);
     955      481878 :                 res = select(eloop.max_sock + 1, rfds, wfds, efds,
     956             :                              timeout ? &_tv : NULL);
     957             : #endif /* CONFIG_ELOOP_SELECT */
     958             : #ifdef CONFIG_ELOOP_EPOLL
     959             :                 if (eloop.count == 0) {
     960             :                         res = 0;
     961             :                 } else {
     962             :                         res = epoll_wait(eloop.epollfd, eloop.epoll_events,
     963             :                                          eloop.count, timeout_ms);
     964             :                 }
     965             : #endif /* CONFIG_ELOOP_EPOLL */
     966      481878 :                 if (res < 0 && errno != EINTR && errno != 0) {
     967           0 :                         wpa_printf(MSG_ERROR, "eloop: %s: %s",
     968             : #ifdef CONFIG_ELOOP_POLL
     969             :                                    "poll"
     970             : #endif /* CONFIG_ELOOP_POLL */
     971             : #ifdef CONFIG_ELOOP_SELECT
     972             :                                    "select"
     973             : #endif /* CONFIG_ELOOP_SELECT */
     974             : #ifdef CONFIG_ELOOP_EPOLL
     975             :                                    "epoll"
     976             : #endif /* CONFIG_ELOOP_EPOLL */
     977           0 :                                    , strerror(errno));
     978           0 :                         goto out;
     979             :                 }
     980      481878 :                 eloop_process_pending_signals();
     981             : 
     982             :                 /* check if some registered timeouts have occurred */
     983      481878 :                 timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
     984             :                                         list);
     985      481878 :                 if (timeout) {
     986      395966 :                         os_get_reltime(&now);
     987      395966 :                         if (!os_reltime_before(&now, &timeout->time)) {
     988       64075 :                                 void *eloop_data = timeout->eloop_data;
     989       64075 :                                 void *user_data = timeout->user_data;
     990       64075 :                                 eloop_timeout_handler handler =
     991             :                                         timeout->handler;
     992       64075 :                                 eloop_remove_timeout(timeout);
     993       64075 :                                 handler(eloop_data, user_data);
     994             :                         }
     995             : 
     996             :                 }
     997             : 
     998      481878 :                 if (res <= 0)
     999       49223 :                         continue;
    1000             : 
    1001             : #ifdef CONFIG_ELOOP_POLL
    1002             :                 eloop_sock_table_dispatch(&eloop.readers, &eloop.writers,
    1003             :                                           &eloop.exceptions, eloop.pollfds_map,
    1004             :                                           eloop.max_pollfd_map);
    1005             : #endif /* CONFIG_ELOOP_POLL */
    1006             : #ifdef CONFIG_ELOOP_SELECT
    1007      432655 :                 eloop_sock_table_dispatch(&eloop.readers, rfds);
    1008      432655 :                 eloop_sock_table_dispatch(&eloop.writers, wfds);
    1009      432655 :                 eloop_sock_table_dispatch(&eloop.exceptions, efds);
    1010             : #endif /* CONFIG_ELOOP_SELECT */
    1011             : #ifdef CONFIG_ELOOP_EPOLL
    1012             :                 eloop_sock_table_dispatch(eloop.epoll_events, res);
    1013             : #endif /* CONFIG_ELOOP_EPOLL */
    1014             :         }
    1015             : 
    1016          30 :         eloop.terminate = 0;
    1017             : out:
    1018             : #ifdef CONFIG_ELOOP_SELECT
    1019          30 :         os_free(rfds);
    1020          30 :         os_free(wfds);
    1021          30 :         os_free(efds);
    1022             : #endif /* CONFIG_ELOOP_SELECT */
    1023          30 :         return;
    1024             : }
    1025             : 
    1026             : 
    1027          30 : void eloop_terminate(void)
    1028             : {
    1029          30 :         eloop.terminate = 1;
    1030          30 : }
    1031             : 
    1032             : 
    1033          65 : void eloop_destroy(void)
    1034             : {
    1035             :         struct eloop_timeout *timeout, *prev;
    1036             :         struct os_reltime now;
    1037             : 
    1038          65 :         os_get_reltime(&now);
    1039          65 :         dl_list_for_each_safe(timeout, prev, &eloop.timeout,
    1040             :                               struct eloop_timeout, list) {
    1041             :                 int sec, usec;
    1042           0 :                 sec = timeout->time.sec - now.sec;
    1043           0 :                 usec = timeout->time.usec - now.usec;
    1044           0 :                 if (timeout->time.usec < now.usec) {
    1045           0 :                         sec--;
    1046           0 :                         usec += 1000000;
    1047             :                 }
    1048           0 :                 wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
    1049             :                            "eloop_data=%p user_data=%p handler=%p",
    1050             :                            sec, usec, timeout->eloop_data, timeout->user_data,
    1051             :                            timeout->handler);
    1052           0 :                 wpa_trace_dump_funcname("eloop unregistered timeout handler",
    1053           0 :                                         timeout->handler);
    1054           0 :                 wpa_trace_dump("eloop timeout", timeout);
    1055           0 :                 eloop_remove_timeout(timeout);
    1056             :         }
    1057          65 :         eloop_sock_table_destroy(&eloop.readers);
    1058          65 :         eloop_sock_table_destroy(&eloop.writers);
    1059          65 :         eloop_sock_table_destroy(&eloop.exceptions);
    1060          65 :         os_free(eloop.signals);
    1061             : 
    1062             : #ifdef CONFIG_ELOOP_POLL
    1063             :         os_free(eloop.pollfds);
    1064             :         os_free(eloop.pollfds_map);
    1065             : #endif /* CONFIG_ELOOP_POLL */
    1066             : #ifdef CONFIG_ELOOP_EPOLL
    1067             :         os_free(eloop.epoll_table);
    1068             :         os_free(eloop.epoll_events);
    1069             :         close(eloop.epollfd);
    1070             : #endif /* CONFIG_ELOOP_EPOLL */
    1071          65 : }
    1072             : 
    1073             : 
    1074        4483 : int eloop_terminated(void)
    1075             : {
    1076        4483 :         return eloop.terminate;
    1077             : }
    1078             : 
    1079             : 
    1080           0 : void eloop_wait_for_read_sock(int sock)
    1081             : {
    1082             : #ifdef CONFIG_ELOOP_POLL
    1083             :         struct pollfd pfd;
    1084             : 
    1085             :         if (sock < 0)
    1086             :                 return;
    1087             : 
    1088             :         os_memset(&pfd, 0, sizeof(pfd));
    1089             :         pfd.fd = sock;
    1090             :         pfd.events = POLLIN;
    1091             : 
    1092             :         poll(&pfd, 1, -1);
    1093             : #endif /* CONFIG_ELOOP_POLL */
    1094             : #if defined(CONFIG_ELOOP_SELECT) || defined(CONFIG_ELOOP_EPOLL)
    1095             :         /*
    1096             :          * We can use epoll() here. But epoll() requres 4 system calls.
    1097             :          * epoll_create1(), epoll_ctl() for ADD, epoll_wait, and close() for
    1098             :          * epoll fd. So select() is better for performance here.
    1099             :          */
    1100             :         fd_set rfds;
    1101             : 
    1102           0 :         if (sock < 0)
    1103           0 :                 return;
    1104             : 
    1105           0 :         FD_ZERO(&rfds);
    1106           0 :         FD_SET(sock, &rfds);
    1107           0 :         select(sock + 1, &rfds, NULL, NULL, NULL);
    1108             : #endif /* defined(CONFIG_ELOOP_SELECT) || defined(CONFIG_ELOOP_EPOLL) */
    1109             : }
    1110             : 
    1111             : #ifdef CONFIG_ELOOP_SELECT
    1112             : #undef CONFIG_ELOOP_SELECT
    1113             : #endif /* CONFIG_ELOOP_SELECT */

Generated by: LCOV version 1.10