xref: /freebsd/contrib/wpa/src/utils/eloop.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
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_KQUEUE)
22 #error Do not define both of poll and kqueue
23 #endif
24 
25 #if !defined(CONFIG_ELOOP_POLL) && !defined(CONFIG_ELOOP_EPOLL) && \
26     !defined(CONFIG_ELOOP_KQUEUE)
27 #define CONFIG_ELOOP_SELECT
28 #endif
29 
30 #ifdef CONFIG_ELOOP_POLL
31 #include <poll.h>
32 #endif /* CONFIG_ELOOP_POLL */
33 
34 #ifdef CONFIG_ELOOP_EPOLL
35 #include <sys/epoll.h>
36 #endif /* CONFIG_ELOOP_EPOLL */
37 
38 #ifdef CONFIG_ELOOP_KQUEUE
39 #include <sys/event.h>
40 #endif /* CONFIG_ELOOP_KQUEUE */
41 
42 struct eloop_sock {
43 	int sock;
44 	void *eloop_data;
45 	void *user_data;
46 	eloop_sock_handler handler;
47 	WPA_TRACE_REF(eloop);
48 	WPA_TRACE_REF(user);
49 	WPA_TRACE_INFO
50 };
51 
52 struct eloop_timeout {
53 	struct dl_list list;
54 	struct os_reltime time;
55 	void *eloop_data;
56 	void *user_data;
57 	eloop_timeout_handler handler;
58 	WPA_TRACE_REF(eloop);
59 	WPA_TRACE_REF(user);
60 	WPA_TRACE_INFO
61 };
62 
63 struct eloop_signal {
64 	int sig;
65 	void *user_data;
66 	eloop_signal_handler handler;
67 	int signaled;
68 };
69 
70 struct eloop_sock_table {
71 	size_t count;
72 	struct eloop_sock *table;
73 	eloop_event_type type;
74 	int changed;
75 };
76 
77 struct eloop_data {
78 	int max_sock;
79 
80 	size_t count; /* sum of all table counts */
81 #ifdef CONFIG_ELOOP_POLL
82 	size_t max_pollfd_map; /* number of pollfds_map currently allocated */
83 	size_t max_poll_fds; /* number of pollfds currently allocated */
84 	struct pollfd *pollfds;
85 	struct pollfd **pollfds_map;
86 #endif /* CONFIG_ELOOP_POLL */
87 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
88 	int max_fd;
89 	struct eloop_sock *fd_table;
90 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
91 #ifdef CONFIG_ELOOP_EPOLL
92 	int epollfd;
93 	size_t epoll_max_event_num;
94 	struct epoll_event *epoll_events;
95 #endif /* CONFIG_ELOOP_EPOLL */
96 #ifdef CONFIG_ELOOP_KQUEUE
97 	int kqueuefd;
98 	size_t kqueue_nevents;
99 	struct kevent *kqueue_events;
100 #endif /* CONFIG_ELOOP_KQUEUE */
101 	struct eloop_sock_table readers;
102 	struct eloop_sock_table writers;
103 	struct eloop_sock_table exceptions;
104 
105 	struct dl_list timeout;
106 
107 	size_t signal_count;
108 	struct eloop_signal *signals;
109 	int signaled;
110 	int pending_terminate;
111 
112 	int terminate;
113 };
114 
115 static struct eloop_data eloop;
116 
117 
118 #ifdef WPA_TRACE
119 
eloop_sigsegv_handler(int sig)120 static void eloop_sigsegv_handler(int sig)
121 {
122 	wpa_trace_show("eloop SIGSEGV");
123 	abort();
124 }
125 
eloop_trace_sock_add_ref(struct eloop_sock_table * table)126 static void eloop_trace_sock_add_ref(struct eloop_sock_table *table)
127 {
128 	size_t i;
129 
130 	if (table == NULL || table->table == NULL)
131 		return;
132 	for (i = 0; i < table->count; i++) {
133 		wpa_trace_add_ref(&table->table[i], eloop,
134 				  table->table[i].eloop_data);
135 		wpa_trace_add_ref(&table->table[i], user,
136 				  table->table[i].user_data);
137 	}
138 }
139 
140 
eloop_trace_sock_remove_ref(struct eloop_sock_table * table)141 static void eloop_trace_sock_remove_ref(struct eloop_sock_table *table)
142 {
143 	size_t i;
144 
145 	if (table == NULL || table->table == NULL)
146 		return;
147 	for (i = 0; i < table->count; i++) {
148 		wpa_trace_remove_ref(&table->table[i], eloop,
149 				     table->table[i].eloop_data);
150 		wpa_trace_remove_ref(&table->table[i], user,
151 				     table->table[i].user_data);
152 	}
153 }
154 
155 #else /* WPA_TRACE */
156 
157 #define eloop_trace_sock_add_ref(table) do { } while (0)
158 #define eloop_trace_sock_remove_ref(table) do { } while (0)
159 
160 #endif /* WPA_TRACE */
161 
162 
eloop_init(void)163 int eloop_init(void)
164 {
165 	os_memset(&eloop, 0, sizeof(eloop));
166 	dl_list_init(&eloop.timeout);
167 #ifdef CONFIG_ELOOP_EPOLL
168 	eloop.epollfd = epoll_create1(0);
169 	if (eloop.epollfd < 0) {
170 		wpa_printf(MSG_ERROR, "%s: epoll_create1 failed. %s",
171 			   __func__, strerror(errno));
172 		return -1;
173 	}
174 #endif /* CONFIG_ELOOP_EPOLL */
175 #ifdef CONFIG_ELOOP_KQUEUE
176 	eloop.kqueuefd = kqueue();
177 	if (eloop.kqueuefd < 0) {
178 		wpa_printf(MSG_ERROR, "%s: kqueue failed: %s",
179 			   __func__, strerror(errno));
180 		return -1;
181 	}
182 #endif /* CONFIG_ELOOP_KQUEUE */
183 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
184 	eloop.readers.type = EVENT_TYPE_READ;
185 	eloop.writers.type = EVENT_TYPE_WRITE;
186 	eloop.exceptions.type = EVENT_TYPE_EXCEPTION;
187 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
188 #ifdef WPA_TRACE
189 	signal(SIGSEGV, eloop_sigsegv_handler);
190 #endif /* WPA_TRACE */
191 	return 0;
192 }
193 
194 
195 #ifdef CONFIG_ELOOP_EPOLL
eloop_sock_queue(int sock,eloop_event_type type)196 static int eloop_sock_queue(int sock, eloop_event_type type)
197 {
198 	struct epoll_event ev;
199 
200 	os_memset(&ev, 0, sizeof(ev));
201 	switch (type) {
202 	case EVENT_TYPE_READ:
203 		ev.events = EPOLLIN;
204 		break;
205 	case EVENT_TYPE_WRITE:
206 		ev.events = EPOLLOUT;
207 		break;
208 	/*
209 	 * Exceptions are always checked when using epoll, but I suppose it's
210 	 * possible that someone registered a socket *only* for exception
211 	 * handling.
212 	 */
213 	case EVENT_TYPE_EXCEPTION:
214 		ev.events = EPOLLERR | EPOLLHUP;
215 		break;
216 	}
217 	ev.data.fd = sock;
218 	if (epoll_ctl(eloop.epollfd, EPOLL_CTL_ADD, sock, &ev) < 0) {
219 		wpa_printf(MSG_ERROR, "%s: epoll_ctl(ADD) for fd=%d failed: %s",
220 			   __func__, sock, strerror(errno));
221 		return -1;
222 	}
223 	return 0;
224 }
225 #endif /* CONFIG_ELOOP_EPOLL */
226 
227 
228 #ifdef CONFIG_ELOOP_KQUEUE
229 
event_type_kevent_filter(eloop_event_type type)230 static short event_type_kevent_filter(eloop_event_type type)
231 {
232 	switch (type) {
233 	case EVENT_TYPE_READ:
234 		return EVFILT_READ;
235 	case EVENT_TYPE_WRITE:
236 		return EVFILT_WRITE;
237 	default:
238 		return 0;
239 	}
240 }
241 
242 
eloop_sock_queue(int sock,eloop_event_type type)243 static int eloop_sock_queue(int sock, eloop_event_type type)
244 {
245 	struct kevent ke;
246 
247 	EV_SET(&ke, sock, event_type_kevent_filter(type), EV_ADD, 0, 0, 0);
248 	if (kevent(eloop.kqueuefd, &ke, 1, NULL, 0, NULL) == -1) {
249 		wpa_printf(MSG_ERROR, "%s: kevent(ADD) for fd=%d failed: %s",
250 			   __func__, sock, strerror(errno));
251 		return -1;
252 	}
253 	return 0;
254 }
255 
256 #endif /* CONFIG_ELOOP_KQUEUE */
257 
258 
eloop_sock_table_add_sock(struct eloop_sock_table * table,int sock,eloop_sock_handler handler,void * eloop_data,void * user_data)259 static int eloop_sock_table_add_sock(struct eloop_sock_table *table,
260                                      int sock, eloop_sock_handler handler,
261                                      void *eloop_data, void *user_data)
262 {
263 #ifdef CONFIG_ELOOP_EPOLL
264 	struct epoll_event *temp_events;
265 #endif /* CONFIG_ELOOP_EPOLL */
266 #ifdef CONFIG_ELOOP_KQUEUE
267 	struct kevent *temp_events;
268 #endif /* CONFIG_ELOOP_EPOLL */
269 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
270 	struct eloop_sock *temp_table;
271 	size_t next;
272 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
273 	struct eloop_sock *tmp;
274 	int new_max_sock;
275 
276 	if (sock > eloop.max_sock)
277 		new_max_sock = sock;
278 	else
279 		new_max_sock = eloop.max_sock;
280 
281 	if (table == NULL)
282 		return -1;
283 
284 #ifdef CONFIG_ELOOP_POLL
285 	if ((size_t) new_max_sock >= eloop.max_pollfd_map) {
286 		struct pollfd **nmap;
287 		nmap = os_realloc_array(eloop.pollfds_map, new_max_sock + 50,
288 					sizeof(struct pollfd *));
289 		if (nmap == NULL)
290 			return -1;
291 
292 		eloop.max_pollfd_map = new_max_sock + 50;
293 		eloop.pollfds_map = nmap;
294 	}
295 
296 	if (eloop.count + 1 > eloop.max_poll_fds) {
297 		struct pollfd *n;
298 		size_t nmax = eloop.count + 1 + 50;
299 
300 		n = os_realloc_array(eloop.pollfds, nmax,
301 				     sizeof(struct pollfd));
302 		if (n == NULL)
303 			return -1;
304 
305 		eloop.max_poll_fds = nmax;
306 		eloop.pollfds = n;
307 	}
308 #endif /* CONFIG_ELOOP_POLL */
309 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
310 	if (new_max_sock >= eloop.max_fd) {
311 		next = new_max_sock + 16;
312 		temp_table = os_realloc_array(eloop.fd_table, next,
313 					      sizeof(struct eloop_sock));
314 		if (temp_table == NULL)
315 			return -1;
316 
317 		eloop.max_fd = next;
318 		eloop.fd_table = temp_table;
319 	}
320 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
321 
322 #ifdef CONFIG_ELOOP_EPOLL
323 	if (eloop.count + 1 > eloop.epoll_max_event_num) {
324 		next = eloop.epoll_max_event_num == 0 ? 8 :
325 			eloop.epoll_max_event_num * 2;
326 		temp_events = os_realloc_array(eloop.epoll_events, next,
327 					       sizeof(struct epoll_event));
328 		if (temp_events == NULL) {
329 			wpa_printf(MSG_ERROR, "%s: malloc for epoll failed: %s",
330 				   __func__, strerror(errno));
331 			return -1;
332 		}
333 
334 		eloop.epoll_max_event_num = next;
335 		eloop.epoll_events = temp_events;
336 	}
337 #endif /* CONFIG_ELOOP_EPOLL */
338 #ifdef CONFIG_ELOOP_KQUEUE
339 	if (eloop.count + 1 > eloop.kqueue_nevents) {
340 		next = eloop.kqueue_nevents == 0 ? 8 : eloop.kqueue_nevents * 2;
341 		temp_events = os_malloc(next * sizeof(*temp_events));
342 		if (!temp_events) {
343 			wpa_printf(MSG_ERROR,
344 				   "%s: malloc for kqueue failed: %s",
345 				   __func__, strerror(errno));
346 			return -1;
347 		}
348 
349 		os_free(eloop.kqueue_events);
350 		eloop.kqueue_events = temp_events;
351 		eloop.kqueue_nevents = next;
352 	}
353 #endif /* CONFIG_ELOOP_KQUEUE */
354 
355 	eloop_trace_sock_remove_ref(table);
356 	tmp = os_realloc_array(table->table, table->count + 1,
357 			       sizeof(struct eloop_sock));
358 	if (tmp == NULL) {
359 		eloop_trace_sock_add_ref(table);
360 		return -1;
361 	}
362 
363 	tmp[table->count].sock = sock;
364 	tmp[table->count].eloop_data = eloop_data;
365 	tmp[table->count].user_data = user_data;
366 	tmp[table->count].handler = handler;
367 	wpa_trace_record(&tmp[table->count]);
368 	table->count++;
369 	table->table = tmp;
370 	eloop.max_sock = new_max_sock;
371 	eloop.count++;
372 	table->changed = 1;
373 	eloop_trace_sock_add_ref(table);
374 
375 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
376 	if (eloop_sock_queue(sock, table->type) < 0)
377 		return -1;
378 	os_memcpy(&eloop.fd_table[sock], &table->table[table->count - 1],
379 		  sizeof(struct eloop_sock));
380 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
381 	return 0;
382 }
383 
384 
eloop_sock_table_remove_sock(struct eloop_sock_table * table,int sock)385 static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
386                                          int sock)
387 {
388 #ifdef CONFIG_ELOOP_KQUEUE
389 	struct kevent ke;
390 #endif /* CONFIG_ELOOP_KQUEUE */
391 	size_t i;
392 
393 	if (table == NULL || table->table == NULL || table->count == 0)
394 		return;
395 
396 	for (i = 0; i < table->count; i++) {
397 		if (table->table[i].sock == sock)
398 			break;
399 	}
400 	if (i == table->count)
401 		return;
402 	eloop_trace_sock_remove_ref(table);
403 	if (i != table->count - 1) {
404 		os_memmove(&table->table[i], &table->table[i + 1],
405 			   (table->count - i - 1) *
406 			   sizeof(struct eloop_sock));
407 	}
408 	table->count--;
409 	eloop.count--;
410 	table->changed = 1;
411 	eloop_trace_sock_add_ref(table);
412 #ifdef CONFIG_ELOOP_EPOLL
413 	if (epoll_ctl(eloop.epollfd, EPOLL_CTL_DEL, sock, NULL) < 0) {
414 		wpa_printf(MSG_ERROR, "%s: epoll_ctl(DEL) for fd=%d failed: %s",
415 			   __func__, sock, strerror(errno));
416 		return;
417 	}
418 	os_memset(&eloop.fd_table[sock], 0, sizeof(struct eloop_sock));
419 #endif /* CONFIG_ELOOP_EPOLL */
420 #ifdef CONFIG_ELOOP_KQUEUE
421 	EV_SET(&ke, sock, event_type_kevent_filter(table->type), EV_DELETE, 0,
422 	       0, 0);
423 	if (kevent(eloop.kqueuefd, &ke, 1, NULL, 0, NULL) < 0) {
424 		wpa_printf(MSG_ERROR, "%s: kevent(DEL) for fd=%d failed: %s",
425 			   __func__, sock, strerror(errno));
426 		return;
427 	}
428 	os_memset(&eloop.fd_table[sock], 0, sizeof(struct eloop_sock));
429 #endif /* CONFIG_ELOOP_KQUEUE */
430 }
431 
432 
433 #ifdef CONFIG_ELOOP_POLL
434 
find_pollfd(struct pollfd ** pollfds_map,int fd,int mx)435 static struct pollfd * find_pollfd(struct pollfd **pollfds_map, int fd, int mx)
436 {
437 	if (fd < mx && fd >= 0)
438 		return pollfds_map[fd];
439 	return NULL;
440 }
441 
442 
eloop_sock_table_set_fds(struct eloop_sock_table * readers,struct eloop_sock_table * writers,struct eloop_sock_table * exceptions,struct pollfd * pollfds,struct pollfd ** pollfds_map,int max_pollfd_map)443 static int eloop_sock_table_set_fds(struct eloop_sock_table *readers,
444 				    struct eloop_sock_table *writers,
445 				    struct eloop_sock_table *exceptions,
446 				    struct pollfd *pollfds,
447 				    struct pollfd **pollfds_map,
448 				    int max_pollfd_map)
449 {
450 	size_t i;
451 	int nxt = 0;
452 	int fd;
453 	struct pollfd *pfd;
454 
455 	/* Clear pollfd lookup map. It will be re-populated below. */
456 	os_memset(pollfds_map, 0, sizeof(struct pollfd *) * max_pollfd_map);
457 
458 	if (readers && readers->table) {
459 		for (i = 0; i < readers->count; i++) {
460 			fd = readers->table[i].sock;
461 			assert(fd >= 0 && fd < max_pollfd_map);
462 			pollfds[nxt].fd = fd;
463 			pollfds[nxt].events = POLLIN;
464 			pollfds[nxt].revents = 0;
465 			pollfds_map[fd] = &(pollfds[nxt]);
466 			nxt++;
467 		}
468 	}
469 
470 	if (writers && writers->table) {
471 		for (i = 0; i < writers->count; i++) {
472 			/*
473 			 * See if we already added this descriptor, update it
474 			 * if so.
475 			 */
476 			fd = writers->table[i].sock;
477 			assert(fd >= 0 && fd < max_pollfd_map);
478 			pfd = pollfds_map[fd];
479 			if (!pfd) {
480 				pfd = &(pollfds[nxt]);
481 				pfd->events = 0;
482 				pfd->fd = fd;
483 				pollfds[i].revents = 0;
484 				pollfds_map[fd] = pfd;
485 				nxt++;
486 			}
487 			pfd->events |= POLLOUT;
488 		}
489 	}
490 
491 	/*
492 	 * Exceptions are always checked when using poll, but I suppose it's
493 	 * possible that someone registered a socket *only* for exception
494 	 * handling. Set the POLLIN bit in this case.
495 	 */
496 	if (exceptions && exceptions->table) {
497 		for (i = 0; i < exceptions->count; i++) {
498 			/*
499 			 * See if we already added this descriptor, just use it
500 			 * if so.
501 			 */
502 			fd = exceptions->table[i].sock;
503 			assert(fd >= 0 && fd < max_pollfd_map);
504 			pfd = pollfds_map[fd];
505 			if (!pfd) {
506 				pfd = &(pollfds[nxt]);
507 				pfd->events = POLLIN;
508 				pfd->fd = fd;
509 				pollfds[i].revents = 0;
510 				pollfds_map[fd] = pfd;
511 				nxt++;
512 			}
513 		}
514 	}
515 
516 	return nxt;
517 }
518 
519 
eloop_sock_table_dispatch_table(struct eloop_sock_table * table,struct pollfd ** pollfds_map,int max_pollfd_map,short int revents)520 static int eloop_sock_table_dispatch_table(struct eloop_sock_table *table,
521 					   struct pollfd **pollfds_map,
522 					   int max_pollfd_map,
523 					   short int revents)
524 {
525 	size_t i;
526 	struct pollfd *pfd;
527 
528 	if (!table || !table->table)
529 		return 0;
530 
531 	table->changed = 0;
532 	for (i = 0; i < table->count; i++) {
533 		pfd = find_pollfd(pollfds_map, table->table[i].sock,
534 				  max_pollfd_map);
535 		if (!pfd)
536 			continue;
537 
538 		if (!(pfd->revents & revents))
539 			continue;
540 
541 		table->table[i].handler(table->table[i].sock,
542 					table->table[i].eloop_data,
543 					table->table[i].user_data);
544 		if (table->changed)
545 			return 1;
546 	}
547 
548 	return 0;
549 }
550 
551 
eloop_sock_table_dispatch(struct eloop_sock_table * readers,struct eloop_sock_table * writers,struct eloop_sock_table * exceptions,struct pollfd ** pollfds_map,int max_pollfd_map)552 static void eloop_sock_table_dispatch(struct eloop_sock_table *readers,
553 				      struct eloop_sock_table *writers,
554 				      struct eloop_sock_table *exceptions,
555 				      struct pollfd **pollfds_map,
556 				      int max_pollfd_map)
557 {
558 	if (eloop_sock_table_dispatch_table(readers, pollfds_map,
559 					    max_pollfd_map, POLLIN | POLLERR |
560 					    POLLHUP))
561 		return; /* pollfds may be invalid at this point */
562 
563 	if (eloop_sock_table_dispatch_table(writers, pollfds_map,
564 					    max_pollfd_map, POLLOUT))
565 		return; /* pollfds may be invalid at this point */
566 
567 	eloop_sock_table_dispatch_table(exceptions, pollfds_map,
568 					max_pollfd_map, POLLERR | POLLHUP);
569 }
570 
571 #endif /* CONFIG_ELOOP_POLL */
572 
573 #ifdef CONFIG_ELOOP_SELECT
574 
eloop_sock_table_set_fds(struct eloop_sock_table * table,fd_set * fds)575 static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
576 				     fd_set *fds)
577 {
578 	size_t i;
579 
580 	FD_ZERO(fds);
581 
582 	if (table->table == NULL)
583 		return;
584 
585 	for (i = 0; i < table->count; i++) {
586 		assert(table->table[i].sock >= 0);
587 		FD_SET(table->table[i].sock, fds);
588 	}
589 }
590 
591 
eloop_sock_table_dispatch(struct eloop_sock_table * table,fd_set * fds)592 static void eloop_sock_table_dispatch(struct eloop_sock_table *table,
593 				      fd_set *fds)
594 {
595 	size_t i;
596 
597 	if (table == NULL || table->table == NULL)
598 		return;
599 
600 	table->changed = 0;
601 	for (i = 0; i < table->count; i++) {
602 		if (FD_ISSET(table->table[i].sock, fds)) {
603 			table->table[i].handler(table->table[i].sock,
604 						table->table[i].eloop_data,
605 						table->table[i].user_data);
606 			if (table->changed)
607 				break;
608 		}
609 	}
610 }
611 
612 #endif /* CONFIG_ELOOP_SELECT */
613 
614 
615 #ifdef CONFIG_ELOOP_EPOLL
eloop_sock_table_dispatch(struct epoll_event * events,int nfds)616 static void eloop_sock_table_dispatch(struct epoll_event *events, int nfds)
617 {
618 	struct eloop_sock *table;
619 	int i;
620 
621 	for (i = 0; i < nfds; i++) {
622 		table = &eloop.fd_table[events[i].data.fd];
623 		if (table->handler == NULL)
624 			continue;
625 		table->handler(table->sock, table->eloop_data,
626 			       table->user_data);
627 		if (eloop.readers.changed ||
628 		    eloop.writers.changed ||
629 		    eloop.exceptions.changed)
630 			break;
631 	}
632 }
633 #endif /* CONFIG_ELOOP_EPOLL */
634 
635 
636 #ifdef CONFIG_ELOOP_KQUEUE
637 
eloop_sock_table_dispatch(struct kevent * events,int nfds)638 static void eloop_sock_table_dispatch(struct kevent *events, int nfds)
639 {
640 	struct eloop_sock *table;
641 	int i;
642 
643 	for (i = 0; i < nfds; i++) {
644 		table = &eloop.fd_table[events[i].ident];
645 		if (table->handler == NULL)
646 			continue;
647 		table->handler(table->sock, table->eloop_data,
648 			       table->user_data);
649 		if (eloop.readers.changed ||
650 		    eloop.writers.changed ||
651 		    eloop.exceptions.changed)
652 			break;
653 	}
654 }
655 
656 
eloop_sock_table_requeue(struct eloop_sock_table * table)657 static int eloop_sock_table_requeue(struct eloop_sock_table *table)
658 {
659 	size_t i;
660 	int r;
661 
662 	r = 0;
663 	for (i = 0; i < table->count && table->table; i++) {
664 		if (eloop_sock_queue(table->table[i].sock, table->type) == -1)
665 			r = -1;
666 	}
667 	return r;
668 }
669 
670 #endif /* CONFIG_ELOOP_KQUEUE */
671 
672 
eloop_sock_requeue(void)673 int eloop_sock_requeue(void)
674 {
675 	int r = 0;
676 
677 #ifdef CONFIG_ELOOP_KQUEUE
678 	close(eloop.kqueuefd);
679 	eloop.kqueuefd = kqueue();
680 	if (eloop.kqueuefd < 0) {
681 		wpa_printf(MSG_ERROR, "%s: kqueue failed: %s",
682 			   __func__, strerror(errno));
683 		return -1;
684 	}
685 
686 	if (eloop_sock_table_requeue(&eloop.readers) < 0)
687 		r = -1;
688 	if (eloop_sock_table_requeue(&eloop.writers) < 0)
689 		r = -1;
690 	if (eloop_sock_table_requeue(&eloop.exceptions) < 0)
691 		r = -1;
692 #endif /* CONFIG_ELOOP_KQUEUE */
693 
694 	return r;
695 }
696 
697 
eloop_sock_table_destroy(struct eloop_sock_table * table)698 static void eloop_sock_table_destroy(struct eloop_sock_table *table)
699 {
700 	if (table) {
701 		size_t i;
702 
703 		for (i = 0; i < table->count && table->table; i++) {
704 			wpa_printf(MSG_INFO, "ELOOP: remaining socket: "
705 				   "sock=%d eloop_data=%p user_data=%p "
706 				   "handler=%p",
707 				   table->table[i].sock,
708 				   table->table[i].eloop_data,
709 				   table->table[i].user_data,
710 				   table->table[i].handler);
711 			wpa_trace_dump_funcname("eloop unregistered socket "
712 						"handler",
713 						table->table[i].handler);
714 			wpa_trace_dump("eloop sock", &table->table[i]);
715 		}
716 		eloop_trace_sock_remove_ref(table);
717 		os_free(table->table);
718 	}
719 }
720 
721 
eloop_register_read_sock(int sock,eloop_sock_handler handler,void * eloop_data,void * user_data)722 int eloop_register_read_sock(int sock, eloop_sock_handler handler,
723 			     void *eloop_data, void *user_data)
724 {
725 	return eloop_register_sock(sock, EVENT_TYPE_READ, handler,
726 				   eloop_data, user_data);
727 }
728 
729 
eloop_unregister_read_sock(int sock)730 void eloop_unregister_read_sock(int sock)
731 {
732 	eloop_unregister_sock(sock, EVENT_TYPE_READ);
733 }
734 
735 
eloop_get_sock_table(eloop_event_type type)736 static struct eloop_sock_table *eloop_get_sock_table(eloop_event_type type)
737 {
738 	switch (type) {
739 	case EVENT_TYPE_READ:
740 		return &eloop.readers;
741 	case EVENT_TYPE_WRITE:
742 		return &eloop.writers;
743 	case EVENT_TYPE_EXCEPTION:
744 		return &eloop.exceptions;
745 	}
746 
747 	return NULL;
748 }
749 
750 
eloop_register_sock(int sock,eloop_event_type type,eloop_sock_handler handler,void * eloop_data,void * user_data)751 int eloop_register_sock(int sock, eloop_event_type type,
752 			eloop_sock_handler handler,
753 			void *eloop_data, void *user_data)
754 {
755 	struct eloop_sock_table *table;
756 
757 	assert(sock >= 0);
758 	table = eloop_get_sock_table(type);
759 	return eloop_sock_table_add_sock(table, sock, handler,
760 					 eloop_data, user_data);
761 }
762 
763 
eloop_unregister_sock(int sock,eloop_event_type type)764 void eloop_unregister_sock(int sock, eloop_event_type type)
765 {
766 	struct eloop_sock_table *table;
767 
768 	table = eloop_get_sock_table(type);
769 	eloop_sock_table_remove_sock(table, sock);
770 }
771 
772 
eloop_register_timeout(unsigned int secs,unsigned int usecs,eloop_timeout_handler handler,void * eloop_data,void * user_data)773 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
774 			   eloop_timeout_handler handler,
775 			   void *eloop_data, void *user_data)
776 {
777 	struct eloop_timeout *timeout, *tmp;
778 	os_time_t now_sec;
779 
780 	timeout = os_zalloc(sizeof(*timeout));
781 	if (timeout == NULL)
782 		return -1;
783 	if (os_get_reltime(&timeout->time) < 0) {
784 		os_free(timeout);
785 		return -1;
786 	}
787 	now_sec = timeout->time.sec;
788 	timeout->time.sec += secs;
789 	if (timeout->time.sec < now_sec)
790 		goto overflow;
791 	timeout->time.usec += usecs;
792 	while (timeout->time.usec >= 1000000) {
793 		timeout->time.sec++;
794 		timeout->time.usec -= 1000000;
795 	}
796 	if (timeout->time.sec < now_sec)
797 		goto overflow;
798 	timeout->eloop_data = eloop_data;
799 	timeout->user_data = user_data;
800 	timeout->handler = handler;
801 	wpa_trace_add_ref(timeout, eloop, eloop_data);
802 	wpa_trace_add_ref(timeout, user, user_data);
803 	wpa_trace_record(timeout);
804 
805 	/* Maintain timeouts in order of increasing time */
806 	dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
807 		if (os_reltime_before(&timeout->time, &tmp->time)) {
808 			dl_list_add(tmp->list.prev, &timeout->list);
809 			return 0;
810 		}
811 	}
812 	dl_list_add_tail(&eloop.timeout, &timeout->list);
813 
814 	return 0;
815 
816 overflow:
817 	/*
818 	 * Integer overflow - assume long enough timeout to be assumed
819 	 * to be infinite, i.e., the timeout would never happen.
820 	 */
821 	wpa_printf(MSG_DEBUG,
822 		   "ELOOP: Too long timeout (secs=%u usecs=%u) to ever happen - ignore it",
823 		   secs,usecs);
824 	os_free(timeout);
825 	return 0;
826 }
827 
828 
eloop_remove_timeout(struct eloop_timeout * timeout)829 static void eloop_remove_timeout(struct eloop_timeout *timeout)
830 {
831 	dl_list_del(&timeout->list);
832 	wpa_trace_remove_ref(timeout, eloop, timeout->eloop_data);
833 	wpa_trace_remove_ref(timeout, user, timeout->user_data);
834 	os_free(timeout);
835 }
836 
837 
eloop_cancel_timeout(eloop_timeout_handler handler,void * eloop_data,void * user_data)838 int eloop_cancel_timeout(eloop_timeout_handler handler,
839 			 void *eloop_data, void *user_data)
840 {
841 	struct eloop_timeout *timeout, *prev;
842 	int removed = 0;
843 
844 	dl_list_for_each_safe(timeout, prev, &eloop.timeout,
845 			      struct eloop_timeout, list) {
846 		if (timeout->handler == handler &&
847 		    (timeout->eloop_data == eloop_data ||
848 		     eloop_data == ELOOP_ALL_CTX) &&
849 		    (timeout->user_data == user_data ||
850 		     user_data == ELOOP_ALL_CTX)) {
851 			eloop_remove_timeout(timeout);
852 			removed++;
853 		}
854 	}
855 
856 	return removed;
857 }
858 
859 
eloop_cancel_timeout_one(eloop_timeout_handler handler,void * eloop_data,void * user_data,struct os_reltime * remaining)860 int eloop_cancel_timeout_one(eloop_timeout_handler handler,
861 			     void *eloop_data, void *user_data,
862 			     struct os_reltime *remaining)
863 {
864 	struct eloop_timeout *timeout, *prev;
865 	int removed = 0;
866 	struct os_reltime now;
867 
868 	os_get_reltime(&now);
869 	remaining->sec = remaining->usec = 0;
870 
871 	dl_list_for_each_safe(timeout, prev, &eloop.timeout,
872 			      struct eloop_timeout, list) {
873 		if (timeout->handler == handler &&
874 		    (timeout->eloop_data == eloop_data) &&
875 		    (timeout->user_data == user_data)) {
876 			removed = 1;
877 			if (os_reltime_before(&now, &timeout->time))
878 				os_reltime_sub(&timeout->time, &now, remaining);
879 			eloop_remove_timeout(timeout);
880 			break;
881 		}
882 	}
883 	return removed;
884 }
885 
886 
eloop_is_timeout_registered(eloop_timeout_handler handler,void * eloop_data,void * user_data)887 int eloop_is_timeout_registered(eloop_timeout_handler handler,
888 				void *eloop_data, void *user_data)
889 {
890 	struct eloop_timeout *tmp;
891 
892 	dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
893 		if (tmp->handler == handler &&
894 		    tmp->eloop_data == eloop_data &&
895 		    tmp->user_data == user_data)
896 			return 1;
897 	}
898 
899 	return 0;
900 }
901 
902 
eloop_deplete_timeout(unsigned int req_secs,unsigned int req_usecs,eloop_timeout_handler handler,void * eloop_data,void * user_data)903 int eloop_deplete_timeout(unsigned int req_secs, unsigned int req_usecs,
904 			  eloop_timeout_handler handler, void *eloop_data,
905 			  void *user_data)
906 {
907 	struct os_reltime now, requested, remaining;
908 	struct eloop_timeout *tmp;
909 
910 	dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
911 		if (tmp->handler == handler &&
912 		    tmp->eloop_data == eloop_data &&
913 		    tmp->user_data == user_data) {
914 			requested.sec = req_secs;
915 			requested.usec = req_usecs;
916 			os_get_reltime(&now);
917 			os_reltime_sub(&tmp->time, &now, &remaining);
918 			if (os_reltime_before(&requested, &remaining)) {
919 				eloop_cancel_timeout(handler, eloop_data,
920 						     user_data);
921 				eloop_register_timeout(requested.sec,
922 						       requested.usec,
923 						       handler, eloop_data,
924 						       user_data);
925 				return 1;
926 			}
927 			return 0;
928 		}
929 	}
930 
931 	return -1;
932 }
933 
934 
eloop_replenish_timeout(unsigned int req_secs,unsigned int req_usecs,eloop_timeout_handler handler,void * eloop_data,void * user_data)935 int eloop_replenish_timeout(unsigned int req_secs, unsigned int req_usecs,
936 			    eloop_timeout_handler handler, void *eloop_data,
937 			    void *user_data)
938 {
939 	struct os_reltime now, requested, remaining;
940 	struct eloop_timeout *tmp;
941 
942 	dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
943 		if (tmp->handler == handler &&
944 		    tmp->eloop_data == eloop_data &&
945 		    tmp->user_data == user_data) {
946 			requested.sec = req_secs;
947 			requested.usec = req_usecs;
948 			os_get_reltime(&now);
949 			os_reltime_sub(&tmp->time, &now, &remaining);
950 			if (os_reltime_before(&remaining, &requested)) {
951 				eloop_cancel_timeout(handler, eloop_data,
952 						     user_data);
953 				eloop_register_timeout(requested.sec,
954 						       requested.usec,
955 						       handler, eloop_data,
956 						       user_data);
957 				return 1;
958 			}
959 			return 0;
960 		}
961 	}
962 
963 	return -1;
964 }
965 
966 
967 #ifndef CONFIG_NATIVE_WINDOWS
eloop_handle_alarm(int sig)968 static void eloop_handle_alarm(int sig)
969 {
970 	wpa_printf(MSG_ERROR, "eloop: could not process SIGINT or SIGTERM in "
971 		   "two seconds. Looks like there\n"
972 		   "is a bug that ends up in a busy loop that "
973 		   "prevents clean shutdown.\n"
974 		   "Killing program forcefully.\n");
975 #ifdef WPA_TRACE
976 	wpa_trace_show("eloop: could not process SIGINT or SIGTERM in two seconds");
977 #endif /* WPA_TRACE */
978 	exit(1);
979 }
980 #endif /* CONFIG_NATIVE_WINDOWS */
981 
982 
eloop_handle_signal(int sig)983 static void eloop_handle_signal(int sig)
984 {
985 	size_t i;
986 
987 #ifndef CONFIG_NATIVE_WINDOWS
988 	if ((sig == SIGINT || sig == SIGTERM) && !eloop.pending_terminate) {
989 		/* Use SIGALRM to break out from potential busy loops that
990 		 * would not allow the program to be killed. */
991 		eloop.pending_terminate = 1;
992 		signal(SIGALRM, eloop_handle_alarm);
993 		alarm(2);
994 	}
995 #endif /* CONFIG_NATIVE_WINDOWS */
996 
997 	eloop.signaled++;
998 	for (i = 0; i < eloop.signal_count; i++) {
999 		if (eloop.signals[i].sig == sig) {
1000 			eloop.signals[i].signaled++;
1001 			break;
1002 		}
1003 	}
1004 }
1005 
1006 
eloop_process_pending_signals(void)1007 static void eloop_process_pending_signals(void)
1008 {
1009 	size_t i;
1010 
1011 	if (eloop.signaled == 0)
1012 		return;
1013 	eloop.signaled = 0;
1014 
1015 	if (eloop.pending_terminate) {
1016 #ifndef CONFIG_NATIVE_WINDOWS
1017 		alarm(0);
1018 #endif /* CONFIG_NATIVE_WINDOWS */
1019 		eloop.pending_terminate = 0;
1020 	}
1021 
1022 	for (i = 0; i < eloop.signal_count; i++) {
1023 		if (eloop.signals[i].signaled) {
1024 			eloop.signals[i].signaled = 0;
1025 			eloop.signals[i].handler(eloop.signals[i].sig,
1026 						 eloop.signals[i].user_data);
1027 		}
1028 	}
1029 }
1030 
1031 
eloop_register_signal(int sig,eloop_signal_handler handler,void * user_data)1032 int eloop_register_signal(int sig, eloop_signal_handler handler,
1033 			  void *user_data)
1034 {
1035 	struct eloop_signal *tmp;
1036 
1037 	tmp = os_realloc_array(eloop.signals, eloop.signal_count + 1,
1038 			       sizeof(struct eloop_signal));
1039 	if (tmp == NULL)
1040 		return -1;
1041 
1042 	tmp[eloop.signal_count].sig = sig;
1043 	tmp[eloop.signal_count].user_data = user_data;
1044 	tmp[eloop.signal_count].handler = handler;
1045 	tmp[eloop.signal_count].signaled = 0;
1046 	eloop.signal_count++;
1047 	eloop.signals = tmp;
1048 	signal(sig, eloop_handle_signal);
1049 
1050 	return 0;
1051 }
1052 
1053 
eloop_register_signal_terminate(eloop_signal_handler handler,void * user_data)1054 int eloop_register_signal_terminate(eloop_signal_handler handler,
1055 				    void *user_data)
1056 {
1057 	int ret = eloop_register_signal(SIGINT, handler, user_data);
1058 	if (ret == 0)
1059 		ret = eloop_register_signal(SIGTERM, handler, user_data);
1060 	return ret;
1061 }
1062 
1063 
eloop_register_signal_reconfig(eloop_signal_handler handler,void * user_data)1064 int eloop_register_signal_reconfig(eloop_signal_handler handler,
1065 				   void *user_data)
1066 {
1067 #ifdef CONFIG_NATIVE_WINDOWS
1068 	return 0;
1069 #else /* CONFIG_NATIVE_WINDOWS */
1070 	return eloop_register_signal(SIGHUP, handler, user_data);
1071 #endif /* CONFIG_NATIVE_WINDOWS */
1072 }
1073 
1074 
eloop_run(void)1075 void eloop_run(void)
1076 {
1077 #ifdef CONFIG_ELOOP_POLL
1078 	int num_poll_fds;
1079 	int timeout_ms = 0;
1080 #endif /* CONFIG_ELOOP_POLL */
1081 #ifdef CONFIG_ELOOP_SELECT
1082 	fd_set *rfds, *wfds, *efds;
1083 	struct timeval _tv;
1084 #endif /* CONFIG_ELOOP_SELECT */
1085 #ifdef CONFIG_ELOOP_EPOLL
1086 	int timeout_ms = -1;
1087 #endif /* CONFIG_ELOOP_EPOLL */
1088 #ifdef CONFIG_ELOOP_KQUEUE
1089 	struct timespec ts;
1090 #endif /* CONFIG_ELOOP_KQUEUE */
1091 	int res;
1092 	struct os_reltime tv, now;
1093 
1094 #ifdef CONFIG_ELOOP_SELECT
1095 	rfds = os_malloc(sizeof(*rfds));
1096 	wfds = os_malloc(sizeof(*wfds));
1097 	efds = os_malloc(sizeof(*efds));
1098 	if (rfds == NULL || wfds == NULL || efds == NULL)
1099 		goto out;
1100 #endif /* CONFIG_ELOOP_SELECT */
1101 
1102 	while (!eloop.terminate &&
1103 	       (!dl_list_empty(&eloop.timeout) || eloop.readers.count > 0 ||
1104 		eloop.writers.count > 0 || eloop.exceptions.count > 0)) {
1105 		struct eloop_timeout *timeout;
1106 
1107 		if (eloop.pending_terminate) {
1108 			/*
1109 			 * This may happen in some corner cases where a signal
1110 			 * is received during a blocking operation. We need to
1111 			 * process the pending signals and exit if requested to
1112 			 * avoid hitting the SIGALRM limit if the blocking
1113 			 * operation took more than two seconds.
1114 			 */
1115 			eloop_process_pending_signals();
1116 			if (eloop.terminate)
1117 				break;
1118 		}
1119 
1120 		timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
1121 					list);
1122 		if (timeout) {
1123 			os_get_reltime(&now);
1124 			if (os_reltime_before(&now, &timeout->time))
1125 				os_reltime_sub(&timeout->time, &now, &tv);
1126 			else
1127 				tv.sec = tv.usec = 0;
1128 #if defined(CONFIG_ELOOP_POLL) || defined(CONFIG_ELOOP_EPOLL)
1129 			timeout_ms = tv.sec * 1000 + tv.usec / 1000;
1130 #endif /* defined(CONFIG_ELOOP_POLL) || defined(CONFIG_ELOOP_EPOLL) */
1131 #ifdef CONFIG_ELOOP_SELECT
1132 			_tv.tv_sec = tv.sec;
1133 			_tv.tv_usec = tv.usec;
1134 #endif /* CONFIG_ELOOP_SELECT */
1135 #ifdef CONFIG_ELOOP_KQUEUE
1136 			ts.tv_sec = tv.sec;
1137 			ts.tv_nsec = tv.usec * 1000L;
1138 #endif /* CONFIG_ELOOP_KQUEUE */
1139 		}
1140 
1141 #ifdef CONFIG_ELOOP_POLL
1142 		num_poll_fds = eloop_sock_table_set_fds(
1143 			&eloop.readers, &eloop.writers, &eloop.exceptions,
1144 			eloop.pollfds, eloop.pollfds_map,
1145 			eloop.max_pollfd_map);
1146 		res = poll(eloop.pollfds, num_poll_fds,
1147 			   timeout ? timeout_ms : -1);
1148 #endif /* CONFIG_ELOOP_POLL */
1149 #ifdef CONFIG_ELOOP_SELECT
1150 		eloop_sock_table_set_fds(&eloop.readers, rfds);
1151 		eloop_sock_table_set_fds(&eloop.writers, wfds);
1152 		eloop_sock_table_set_fds(&eloop.exceptions, efds);
1153 		res = select(eloop.max_sock + 1, rfds, wfds, efds,
1154 			     timeout ? &_tv : NULL);
1155 #endif /* CONFIG_ELOOP_SELECT */
1156 #ifdef CONFIG_ELOOP_EPOLL
1157 		if (eloop.count == 0) {
1158 			res = 0;
1159 		} else {
1160 			res = epoll_wait(eloop.epollfd, eloop.epoll_events,
1161 					 eloop.count, timeout_ms);
1162 		}
1163 #endif /* CONFIG_ELOOP_EPOLL */
1164 #ifdef CONFIG_ELOOP_KQUEUE
1165 		if (eloop.count == 0) {
1166 			res = 0;
1167 		} else {
1168 			res = kevent(eloop.kqueuefd, NULL, 0,
1169 				     eloop.kqueue_events, eloop.kqueue_nevents,
1170 				     timeout ? &ts : NULL);
1171 		}
1172 #endif /* CONFIG_ELOOP_KQUEUE */
1173 		if (res < 0 && errno != EINTR && errno != 0) {
1174 			wpa_printf(MSG_ERROR, "eloop: %s: %s",
1175 #ifdef CONFIG_ELOOP_POLL
1176 				   "poll"
1177 #endif /* CONFIG_ELOOP_POLL */
1178 #ifdef CONFIG_ELOOP_SELECT
1179 				   "select"
1180 #endif /* CONFIG_ELOOP_SELECT */
1181 #ifdef CONFIG_ELOOP_EPOLL
1182 				   "epoll"
1183 #endif /* CONFIG_ELOOP_EPOLL */
1184 #ifdef CONFIG_ELOOP_KQUEUE
1185 				   "kqueue"
1186 #endif /* CONFIG_ELOOP_EKQUEUE */
1187 
1188 				   , strerror(errno));
1189 			goto out;
1190 		}
1191 
1192 		eloop.readers.changed = 0;
1193 		eloop.writers.changed = 0;
1194 		eloop.exceptions.changed = 0;
1195 
1196 		eloop_process_pending_signals();
1197 
1198 
1199 		/* check if some registered timeouts have occurred */
1200 		timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
1201 					list);
1202 		if (timeout) {
1203 			os_get_reltime(&now);
1204 			if (!os_reltime_before(&now, &timeout->time)) {
1205 				void *eloop_data = timeout->eloop_data;
1206 				void *user_data = timeout->user_data;
1207 				eloop_timeout_handler handler =
1208 					timeout->handler;
1209 				eloop_remove_timeout(timeout);
1210 				handler(eloop_data, user_data);
1211 			}
1212 
1213 		}
1214 
1215 		if (res <= 0)
1216 			continue;
1217 
1218 		if (eloop.readers.changed ||
1219 		    eloop.writers.changed ||
1220 		    eloop.exceptions.changed) {
1221 			 /*
1222 			  * Sockets may have been closed and reopened with the
1223 			  * same FD in the signal or timeout handlers, so we
1224 			  * must skip the previous results and check again
1225 			  * whether any of the currently registered sockets have
1226 			  * events.
1227 			  */
1228 			continue;
1229 		}
1230 
1231 #ifdef CONFIG_ELOOP_POLL
1232 		eloop_sock_table_dispatch(&eloop.readers, &eloop.writers,
1233 					  &eloop.exceptions, eloop.pollfds_map,
1234 					  eloop.max_pollfd_map);
1235 #endif /* CONFIG_ELOOP_POLL */
1236 #ifdef CONFIG_ELOOP_SELECT
1237 		eloop_sock_table_dispatch(&eloop.readers, rfds);
1238 		eloop_sock_table_dispatch(&eloop.writers, wfds);
1239 		eloop_sock_table_dispatch(&eloop.exceptions, efds);
1240 #endif /* CONFIG_ELOOP_SELECT */
1241 #ifdef CONFIG_ELOOP_EPOLL
1242 		eloop_sock_table_dispatch(eloop.epoll_events, res);
1243 #endif /* CONFIG_ELOOP_EPOLL */
1244 #ifdef CONFIG_ELOOP_KQUEUE
1245 		eloop_sock_table_dispatch(eloop.kqueue_events, res);
1246 #endif /* CONFIG_ELOOP_KQUEUE */
1247 	}
1248 
1249 	eloop.terminate = 0;
1250 out:
1251 #ifdef CONFIG_ELOOP_SELECT
1252 	os_free(rfds);
1253 	os_free(wfds);
1254 	os_free(efds);
1255 #endif /* CONFIG_ELOOP_SELECT */
1256 	return;
1257 }
1258 
1259 
eloop_terminate(void)1260 void eloop_terminate(void)
1261 {
1262 	eloop.terminate = 1;
1263 }
1264 
1265 
eloop_destroy(void)1266 void eloop_destroy(void)
1267 {
1268 	struct eloop_timeout *timeout, *prev;
1269 	struct os_reltime now;
1270 	size_t i;
1271 
1272 	os_get_reltime(&now);
1273 	dl_list_for_each_safe(timeout, prev, &eloop.timeout,
1274 			      struct eloop_timeout, list) {
1275 		int sec, usec;
1276 		sec = timeout->time.sec - now.sec;
1277 		usec = timeout->time.usec - now.usec;
1278 		if (timeout->time.usec < now.usec) {
1279 			sec--;
1280 			usec += 1000000;
1281 		}
1282 		wpa_printf(MSG_INFO, "ELOOP: remaining timeout: %d.%06d "
1283 			   "eloop_data=%p user_data=%p handler=%p",
1284 			   sec, usec, timeout->eloop_data, timeout->user_data,
1285 			   timeout->handler);
1286 		wpa_trace_dump_funcname("eloop unregistered timeout handler",
1287 					timeout->handler);
1288 		wpa_trace_dump("eloop timeout", timeout);
1289 		eloop_remove_timeout(timeout);
1290 	}
1291 	eloop_sock_table_destroy(&eloop.readers);
1292 	eloop_sock_table_destroy(&eloop.writers);
1293 	eloop_sock_table_destroy(&eloop.exceptions);
1294 
1295 	for (i = 0; i < eloop.signal_count; i++)
1296 		signal(eloop.signals[i].sig, SIG_DFL);
1297 
1298 	os_free(eloop.signals);
1299 
1300 #ifdef CONFIG_ELOOP_POLL
1301 	os_free(eloop.pollfds);
1302 	os_free(eloop.pollfds_map);
1303 #endif /* CONFIG_ELOOP_POLL */
1304 #if defined(CONFIG_ELOOP_EPOLL) || defined(CONFIG_ELOOP_KQUEUE)
1305 	os_free(eloop.fd_table);
1306 #endif /* CONFIG_ELOOP_EPOLL || CONFIG_ELOOP_KQUEUE */
1307 #ifdef CONFIG_ELOOP_EPOLL
1308 	os_free(eloop.epoll_events);
1309 	close(eloop.epollfd);
1310 #endif /* CONFIG_ELOOP_EPOLL */
1311 #ifdef CONFIG_ELOOP_KQUEUE
1312 	os_free(eloop.kqueue_events);
1313 	close(eloop.kqueuefd);
1314 #endif /* CONFIG_ELOOP_KQUEUE */
1315 }
1316 
1317 
eloop_terminated(void)1318 int eloop_terminated(void)
1319 {
1320 	return eloop.terminate || eloop.pending_terminate;
1321 }
1322 
1323 
eloop_wait_for_read_sock(int sock)1324 void eloop_wait_for_read_sock(int sock)
1325 {
1326 #ifdef CONFIG_ELOOP_POLL
1327 	struct pollfd pfd;
1328 
1329 	if (sock < 0)
1330 		return;
1331 
1332 	os_memset(&pfd, 0, sizeof(pfd));
1333 	pfd.fd = sock;
1334 	pfd.events = POLLIN;
1335 
1336 	poll(&pfd, 1, -1);
1337 #endif /* CONFIG_ELOOP_POLL */
1338 #if defined(CONFIG_ELOOP_SELECT) || defined(CONFIG_ELOOP_EPOLL)
1339 	/*
1340 	 * We can use epoll() here. But epoll() requres 4 system calls.
1341 	 * epoll_create1(), epoll_ctl() for ADD, epoll_wait, and close() for
1342 	 * epoll fd. So select() is better for performance here.
1343 	 */
1344 	fd_set rfds;
1345 
1346 	if (sock < 0)
1347 		return;
1348 
1349 	FD_ZERO(&rfds);
1350 	FD_SET(sock, &rfds);
1351 	select(sock + 1, &rfds, NULL, NULL, NULL);
1352 #endif /* defined(CONFIG_ELOOP_SELECT) || defined(CONFIG_ELOOP_EPOLL) */
1353 #ifdef CONFIG_ELOOP_KQUEUE
1354 	int kfd;
1355 	struct kevent ke1, ke2;
1356 
1357 	kfd = kqueue();
1358 	if (kfd == -1)
1359 		return;
1360 	EV_SET(&ke1, sock, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
1361 	kevent(kfd, &ke1, 1, &ke2, 1, NULL);
1362 	close(kfd);
1363 #endif /* CONFIG_ELOOP_KQUEUE */
1364 }
1365 
1366 #ifdef CONFIG_ELOOP_SELECT
1367 #undef CONFIG_ELOOP_SELECT
1368 #endif /* CONFIG_ELOOP_SELECT */
1369