uloop: add missing waker_pipe initialization
[project/libubox.git] / uloop.c
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/time.h>
19 #include <sys/types.h>
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29
30 #include "uloop.h"
31 #include "utils.h"
32
33 #ifdef USE_KQUEUE
34 #include <sys/event.h>
35 #endif
36 #ifdef USE_EPOLL
37 #include <sys/epoll.h>
38 #endif
39 #include <sys/wait.h>
40
41 struct uloop_fd_event {
42 struct uloop_fd *fd;
43 unsigned int events;
44 };
45
46 struct uloop_fd_stack {
47 struct uloop_fd_stack *next;
48 struct uloop_fd *fd;
49 unsigned int events;
50 };
51
52 static struct uloop_fd_stack *fd_stack = NULL;
53
54 #define ULOOP_MAX_EVENTS 10
55
56 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
57 static struct list_head processes = LIST_HEAD_INIT(processes);
58
59 static int poll_fd = -1;
60 bool uloop_cancelled = false;
61 static bool do_sigchld = false;
62
63 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
64 static int cur_fd, cur_nfds;
65
66 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
67
68 #ifdef USE_KQUEUE
69 #include "uloop-kqueue.c"
70 #endif
71
72 #ifdef USE_EPOLL
73 #include "uloop-epoll.c"
74 #endif
75
76 static void waker_consume(struct uloop_fd *fd, unsigned int events)
77 {
78 char buf[4];
79
80 while (read(fd->fd, buf, 4) > 0)
81 ;
82 }
83
84 static int waker_pipe = -1;
85 static struct uloop_fd waker_fd = {
86 .fd = -1,
87 .cb = waker_consume,
88 };
89
90 static void waker_init_fd(int fd)
91 {
92 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
93 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
94 }
95
96 static int waker_init(void)
97 {
98 int fds[2];
99
100 if (waker_pipe >= 0)
101 return 0;
102
103 if (pipe(fds) < 0)
104 return -1;
105
106 waker_init_fd(fds[0]);
107 waker_init_fd(fds[1]);
108 waker_pipe = fds[1];
109
110 waker_fd.fd = fds[0];
111 waker_fd.cb = waker_consume;
112 uloop_fd_add(&waker_fd, ULOOP_READ);
113
114 return 0;
115 }
116
117 int uloop_init(void)
118 {
119 if (uloop_init_pollfd() < 0)
120 return -1;
121
122 if (waker_init() < 0) {
123 uloop_done();
124 return -1;
125 }
126
127 return 0;
128 }
129
130 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
131 {
132 struct uloop_fd_stack *cur;
133
134 /*
135 * Do not buffer events for level-triggered fds, they will keep firing.
136 * Caller needs to take care of recursion issues.
137 */
138 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
139 return false;
140
141 for (cur = fd_stack; cur; cur = cur->next) {
142 if (cur->fd != fd)
143 continue;
144
145 if (events < 0)
146 cur->fd = NULL;
147 else
148 cur->events |= events | ULOOP_EVENT_BUFFERED;
149
150 return true;
151 }
152
153 return false;
154 }
155
156 static void uloop_run_events(int timeout)
157 {
158 struct uloop_fd_event *cur;
159 struct uloop_fd *fd;
160
161 if (!cur_nfds) {
162 cur_fd = 0;
163 cur_nfds = uloop_fetch_events(timeout);
164 if (cur_nfds < 0)
165 cur_nfds = 0;
166 }
167
168 while (cur_nfds > 0) {
169 struct uloop_fd_stack stack_cur;
170 unsigned int events;
171
172 cur = &cur_fds[cur_fd++];
173 cur_nfds--;
174
175 fd = cur->fd;
176 events = cur->events;
177 if (!fd)
178 continue;
179
180 if (!fd->cb)
181 continue;
182
183 if (uloop_fd_stack_event(fd, cur->events))
184 continue;
185
186 stack_cur.next = fd_stack;
187 stack_cur.fd = fd;
188 fd_stack = &stack_cur;
189 do {
190 stack_cur.events = 0;
191 fd->cb(fd, events);
192 events = stack_cur.events & ULOOP_EVENT_MASK;
193 } while (stack_cur.fd && events);
194 fd_stack = stack_cur.next;
195
196 return;
197 }
198 }
199
200 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
201 {
202 unsigned int fl;
203 int ret;
204
205 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
206 return uloop_fd_delete(sock);
207
208 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
209 fl = fcntl(sock->fd, F_GETFL, 0);
210 fl |= O_NONBLOCK;
211 fcntl(sock->fd, F_SETFL, fl);
212 }
213
214 ret = register_poll(sock, flags);
215 if (ret < 0)
216 goto out;
217
218 sock->registered = true;
219 sock->eof = false;
220 sock->error = false;
221
222 out:
223 return ret;
224 }
225
226 int uloop_fd_delete(struct uloop_fd *fd)
227 {
228 int i;
229
230 for (i = 0; i < cur_nfds; i++) {
231 if (cur_fds[cur_fd + i].fd != fd)
232 continue;
233
234 cur_fds[cur_fd + i].fd = NULL;
235 }
236
237 if (!fd->registered)
238 return 0;
239
240 fd->registered = false;
241 uloop_fd_stack_event(fd, -1);
242 return __uloop_fd_delete(fd);
243 }
244
245 static int tv_diff(struct timeval *t1, struct timeval *t2)
246 {
247 return
248 (t1->tv_sec - t2->tv_sec) * 1000 +
249 (t1->tv_usec - t2->tv_usec) / 1000;
250 }
251
252 int uloop_timeout_add(struct uloop_timeout *timeout)
253 {
254 struct uloop_timeout *tmp;
255 struct list_head *h = &timeouts;
256
257 if (timeout->pending)
258 return -1;
259
260 list_for_each_entry(tmp, &timeouts, list) {
261 if (tv_diff(&tmp->time, &timeout->time) > 0) {
262 h = &tmp->list;
263 break;
264 }
265 }
266
267 list_add_tail(&timeout->list, h);
268 timeout->pending = true;
269
270 return 0;
271 }
272
273 static void uloop_gettime(struct timeval *tv)
274 {
275 struct timespec ts;
276
277 clock_gettime(CLOCK_MONOTONIC, &ts);
278 tv->tv_sec = ts.tv_sec;
279 tv->tv_usec = ts.tv_nsec / 1000;
280 }
281
282 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
283 {
284 struct timeval *time = &timeout->time;
285
286 if (timeout->pending)
287 uloop_timeout_cancel(timeout);
288
289 uloop_gettime(time);
290
291 time->tv_sec += msecs / 1000;
292 time->tv_usec += (msecs % 1000) * 1000;
293
294 if (time->tv_usec > 1000000) {
295 time->tv_sec++;
296 time->tv_usec -= 1000000;
297 }
298
299 return uloop_timeout_add(timeout);
300 }
301
302 int uloop_timeout_cancel(struct uloop_timeout *timeout)
303 {
304 if (!timeout->pending)
305 return -1;
306
307 list_del(&timeout->list);
308 timeout->pending = false;
309
310 return 0;
311 }
312
313 int uloop_timeout_remaining(struct uloop_timeout *timeout)
314 {
315 struct timeval now;
316
317 if (!timeout->pending)
318 return -1;
319
320 uloop_gettime(&now);
321
322 return tv_diff(&timeout->time, &now);
323 }
324
325 int uloop_process_add(struct uloop_process *p)
326 {
327 struct uloop_process *tmp;
328 struct list_head *h = &processes;
329
330 if (p->pending)
331 return -1;
332
333 list_for_each_entry(tmp, &processes, list) {
334 if (tmp->pid > p->pid) {
335 h = &tmp->list;
336 break;
337 }
338 }
339
340 list_add_tail(&p->list, h);
341 p->pending = true;
342
343 return 0;
344 }
345
346 int uloop_process_delete(struct uloop_process *p)
347 {
348 if (!p->pending)
349 return -1;
350
351 list_del(&p->list);
352 p->pending = false;
353
354 return 0;
355 }
356
357 static void uloop_handle_processes(void)
358 {
359 struct uloop_process *p, *tmp;
360 pid_t pid;
361 int ret;
362
363 do_sigchld = false;
364
365 while (1) {
366 pid = waitpid(-1, &ret, WNOHANG);
367 if (pid < 0 && errno == EINTR)
368 continue;
369
370 if (pid <= 0)
371 return;
372
373 list_for_each_entry_safe(p, tmp, &processes, list) {
374 if (p->pid < pid)
375 continue;
376
377 if (p->pid > pid)
378 break;
379
380 uloop_process_delete(p);
381 p->cb(p, ret);
382 }
383 }
384
385 }
386
387 static void uloop_signal_wake(void)
388 {
389 write(waker_pipe, "w", 1);
390 }
391
392 static void uloop_handle_sigint(int signo)
393 {
394 uloop_cancelled = true;
395 uloop_signal_wake();
396 }
397
398 static void uloop_sigchld(int signo)
399 {
400 do_sigchld = true;
401 uloop_signal_wake();
402 }
403
404 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
405 {
406 struct sigaction s;
407 struct sigaction *act;
408
409 act = NULL;
410 sigaction(signum, NULL, &s);
411
412 if (add) {
413 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
414 memcpy(old, &s, sizeof(struct sigaction));
415 s.sa_handler = handler;
416 s.sa_flags = 0;
417 act = &s;
418 }
419 }
420 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
421 act = old;
422 }
423
424 if (act != NULL)
425 sigaction(signum, act, NULL);
426 }
427
428 static void uloop_ignore_signal(int signum, bool ignore)
429 {
430 struct sigaction s;
431 void *new_handler = NULL;
432
433 sigaction(signum, NULL, &s);
434
435 if (ignore) {
436 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
437 new_handler = SIG_IGN;
438 } else {
439 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
440 new_handler = SIG_DFL;
441 }
442
443 if (new_handler) {
444 s.sa_handler = new_handler;
445 s.sa_flags = 0;
446 sigaction(signum, &s, NULL);
447 }
448 }
449
450 static void uloop_setup_signals(bool add)
451 {
452 static struct sigaction old_sigint, old_sigchld, old_sigterm;
453
454 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
455 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
456 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
457
458 uloop_ignore_signal(SIGPIPE, add);
459 }
460
461 static int uloop_get_next_timeout(struct timeval *tv)
462 {
463 struct uloop_timeout *timeout;
464 int diff;
465
466 if (list_empty(&timeouts))
467 return -1;
468
469 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
470 diff = tv_diff(&timeout->time, tv);
471 if (diff < 0)
472 return 0;
473
474 return diff;
475 }
476
477 static void uloop_process_timeouts(struct timeval *tv)
478 {
479 struct uloop_timeout *t;
480
481 while (!list_empty(&timeouts)) {
482 t = list_first_entry(&timeouts, struct uloop_timeout, list);
483
484 if (tv_diff(&t->time, tv) > 0)
485 break;
486
487 uloop_timeout_cancel(t);
488 if (t->cb)
489 t->cb(t);
490 }
491 }
492
493 static void uloop_clear_timeouts(void)
494 {
495 struct uloop_timeout *t, *tmp;
496
497 list_for_each_entry_safe(t, tmp, &timeouts, list)
498 uloop_timeout_cancel(t);
499 }
500
501 static void uloop_clear_processes(void)
502 {
503 struct uloop_process *p, *tmp;
504
505 list_for_each_entry_safe(p, tmp, &processes, list)
506 uloop_process_delete(p);
507 }
508
509 void uloop_run(void)
510 {
511 static int recursive_calls = 0;
512 struct timeval tv;
513
514 /*
515 * Handlers are only updated for the first call to uloop_run() (and restored
516 * when this call is done).
517 */
518 if (!recursive_calls++)
519 uloop_setup_signals(true);
520
521 uloop_cancelled = false;
522 while(!uloop_cancelled)
523 {
524 uloop_gettime(&tv);
525 uloop_process_timeouts(&tv);
526
527 if (do_sigchld)
528 uloop_handle_processes();
529
530 if (uloop_cancelled)
531 break;
532
533 uloop_gettime(&tv);
534 uloop_run_events(uloop_get_next_timeout(&tv));
535 }
536
537 if (!--recursive_calls)
538 uloop_setup_signals(false);
539 }
540
541 void uloop_done(void)
542 {
543 if (poll_fd >= 0) {
544 close(poll_fd);
545 poll_fd = -1;
546 }
547
548 if (waker_pipe >= 0) {
549 uloop_fd_delete(&waker_fd);
550 close(waker_pipe);
551 close(waker_fd.fd);
552 waker_pipe = -1;
553 }
554
555 uloop_clear_timeouts();
556 uloop_clear_processes();
557 }