uloop: Fix race condition in SIGCHLD handling
[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 int uloop_status = 0;
62 static bool do_sigchld = false;
63
64 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
65 static int cur_fd, cur_nfds;
66 static int uloop_run_depth = 0;
67
68 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
69
70 #ifdef USE_KQUEUE
71 #include "uloop-kqueue.c"
72 #endif
73
74 #ifdef USE_EPOLL
75 #include "uloop-epoll.c"
76 #endif
77
78 static void waker_consume(struct uloop_fd *fd, unsigned int events)
79 {
80 char buf[4];
81
82 while (read(fd->fd, buf, 4) > 0)
83 ;
84 }
85
86 static int waker_pipe = -1;
87 static struct uloop_fd waker_fd = {
88 .fd = -1,
89 .cb = waker_consume,
90 };
91
92 static void waker_init_fd(int fd)
93 {
94 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
95 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
96 }
97
98 static int waker_init(void)
99 {
100 int fds[2];
101
102 if (waker_pipe >= 0)
103 return 0;
104
105 if (pipe(fds) < 0)
106 return -1;
107
108 waker_init_fd(fds[0]);
109 waker_init_fd(fds[1]);
110 waker_pipe = fds[1];
111
112 waker_fd.fd = fds[0];
113 waker_fd.cb = waker_consume;
114 uloop_fd_add(&waker_fd, ULOOP_READ);
115
116 return 0;
117 }
118
119 static void uloop_setup_signals(bool add);
120
121 int uloop_init(void)
122 {
123 if (uloop_init_pollfd() < 0)
124 return -1;
125
126 if (waker_init() < 0) {
127 uloop_done();
128 return -1;
129 }
130
131 uloop_setup_signals(true);
132
133 return 0;
134 }
135
136 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
137 {
138 struct uloop_fd_stack *cur;
139
140 /*
141 * Do not buffer events for level-triggered fds, they will keep firing.
142 * Caller needs to take care of recursion issues.
143 */
144 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
145 return false;
146
147 for (cur = fd_stack; cur; cur = cur->next) {
148 if (cur->fd != fd)
149 continue;
150
151 if (events < 0)
152 cur->fd = NULL;
153 else
154 cur->events |= events | ULOOP_EVENT_BUFFERED;
155
156 return true;
157 }
158
159 return false;
160 }
161
162 static void uloop_run_events(int timeout)
163 {
164 struct uloop_fd_event *cur;
165 struct uloop_fd *fd;
166
167 if (!cur_nfds) {
168 cur_fd = 0;
169 cur_nfds = uloop_fetch_events(timeout);
170 if (cur_nfds < 0)
171 cur_nfds = 0;
172 }
173
174 while (cur_nfds > 0) {
175 struct uloop_fd_stack stack_cur;
176 unsigned int events;
177
178 cur = &cur_fds[cur_fd++];
179 cur_nfds--;
180
181 fd = cur->fd;
182 events = cur->events;
183 if (!fd)
184 continue;
185
186 if (!fd->cb)
187 continue;
188
189 if (uloop_fd_stack_event(fd, cur->events))
190 continue;
191
192 stack_cur.next = fd_stack;
193 stack_cur.fd = fd;
194 fd_stack = &stack_cur;
195 do {
196 stack_cur.events = 0;
197 fd->cb(fd, events);
198 events = stack_cur.events & ULOOP_EVENT_MASK;
199 } while (stack_cur.fd && events);
200 fd_stack = stack_cur.next;
201
202 return;
203 }
204 }
205
206 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
207 {
208 unsigned int fl;
209 int ret;
210
211 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
212 return uloop_fd_delete(sock);
213
214 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
215 fl = fcntl(sock->fd, F_GETFL, 0);
216 fl |= O_NONBLOCK;
217 fcntl(sock->fd, F_SETFL, fl);
218 }
219
220 ret = register_poll(sock, flags);
221 if (ret < 0)
222 goto out;
223
224 sock->registered = true;
225 sock->eof = false;
226 sock->error = false;
227
228 out:
229 return ret;
230 }
231
232 int uloop_fd_delete(struct uloop_fd *fd)
233 {
234 int i;
235
236 for (i = 0; i < cur_nfds; i++) {
237 if (cur_fds[cur_fd + i].fd != fd)
238 continue;
239
240 cur_fds[cur_fd + i].fd = NULL;
241 }
242
243 if (!fd->registered)
244 return 0;
245
246 fd->registered = false;
247 uloop_fd_stack_event(fd, -1);
248 return __uloop_fd_delete(fd);
249 }
250
251 static int tv_diff(struct timeval *t1, struct timeval *t2)
252 {
253 return
254 (t1->tv_sec - t2->tv_sec) * 1000 +
255 (t1->tv_usec - t2->tv_usec) / 1000;
256 }
257
258 int uloop_timeout_add(struct uloop_timeout *timeout)
259 {
260 struct uloop_timeout *tmp;
261 struct list_head *h = &timeouts;
262
263 if (timeout->pending)
264 return -1;
265
266 list_for_each_entry(tmp, &timeouts, list) {
267 if (tv_diff(&tmp->time, &timeout->time) > 0) {
268 h = &tmp->list;
269 break;
270 }
271 }
272
273 list_add_tail(&timeout->list, h);
274 timeout->pending = true;
275
276 return 0;
277 }
278
279 static void uloop_gettime(struct timeval *tv)
280 {
281 struct timespec ts;
282
283 clock_gettime(CLOCK_MONOTONIC, &ts);
284 tv->tv_sec = ts.tv_sec;
285 tv->tv_usec = ts.tv_nsec / 1000;
286 }
287
288 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
289 {
290 struct timeval *time = &timeout->time;
291
292 if (timeout->pending)
293 uloop_timeout_cancel(timeout);
294
295 uloop_gettime(time);
296
297 time->tv_sec += msecs / 1000;
298 time->tv_usec += (msecs % 1000) * 1000;
299
300 if (time->tv_usec > 1000000) {
301 time->tv_sec++;
302 time->tv_usec -= 1000000;
303 }
304
305 return uloop_timeout_add(timeout);
306 }
307
308 int uloop_timeout_cancel(struct uloop_timeout *timeout)
309 {
310 if (!timeout->pending)
311 return -1;
312
313 list_del(&timeout->list);
314 timeout->pending = false;
315
316 return 0;
317 }
318
319 int uloop_timeout_remaining(struct uloop_timeout *timeout)
320 {
321 struct timeval now;
322
323 if (!timeout->pending)
324 return -1;
325
326 uloop_gettime(&now);
327
328 return tv_diff(&timeout->time, &now);
329 }
330
331 int uloop_process_add(struct uloop_process *p)
332 {
333 struct uloop_process *tmp;
334 struct list_head *h = &processes;
335
336 if (p->pending)
337 return -1;
338
339 list_for_each_entry(tmp, &processes, list) {
340 if (tmp->pid > p->pid) {
341 h = &tmp->list;
342 break;
343 }
344 }
345
346 list_add_tail(&p->list, h);
347 p->pending = true;
348
349 return 0;
350 }
351
352 int uloop_process_delete(struct uloop_process *p)
353 {
354 if (!p->pending)
355 return -1;
356
357 list_del(&p->list);
358 p->pending = false;
359
360 return 0;
361 }
362
363 static void uloop_handle_processes(void)
364 {
365 struct uloop_process *p, *tmp;
366 pid_t pid;
367 int ret;
368
369 do_sigchld = false;
370
371 while (1) {
372 pid = waitpid(-1, &ret, WNOHANG);
373 if (pid < 0 && errno == EINTR)
374 continue;
375
376 if (pid <= 0)
377 return;
378
379 list_for_each_entry_safe(p, tmp, &processes, list) {
380 if (p->pid < pid)
381 continue;
382
383 if (p->pid > pid)
384 break;
385
386 uloop_process_delete(p);
387 p->cb(p, ret);
388 }
389 }
390
391 }
392
393 static void uloop_signal_wake(void)
394 {
395 do {
396 if (write(waker_pipe, "w", 1) < 0) {
397 if (errno == EINTR)
398 continue;
399 }
400 break;
401 } while (1);
402 }
403
404 static void uloop_handle_sigint(int signo)
405 {
406 uloop_status = signo;
407 uloop_cancelled = true;
408 uloop_signal_wake();
409 }
410
411 static void uloop_sigchld(int signo)
412 {
413 do_sigchld = true;
414 uloop_signal_wake();
415 }
416
417 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
418 {
419 struct sigaction s;
420 struct sigaction *act;
421
422 act = NULL;
423 sigaction(signum, NULL, &s);
424
425 if (add) {
426 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
427 memcpy(old, &s, sizeof(struct sigaction));
428 s.sa_handler = handler;
429 s.sa_flags = 0;
430 act = &s;
431 }
432 }
433 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
434 act = old;
435 }
436
437 if (act != NULL)
438 sigaction(signum, act, NULL);
439 }
440
441 static void uloop_ignore_signal(int signum, bool ignore)
442 {
443 struct sigaction s;
444 void *new_handler = NULL;
445
446 sigaction(signum, NULL, &s);
447
448 if (ignore) {
449 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
450 new_handler = SIG_IGN;
451 } else {
452 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
453 new_handler = SIG_DFL;
454 }
455
456 if (new_handler) {
457 s.sa_handler = new_handler;
458 s.sa_flags = 0;
459 sigaction(signum, &s, NULL);
460 }
461 }
462
463 static void uloop_setup_signals(bool add)
464 {
465 static struct sigaction old_sigint, old_sigchld, old_sigterm;
466
467 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
468 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
469 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
470
471 uloop_ignore_signal(SIGPIPE, add);
472 }
473
474 static int uloop_get_next_timeout(struct timeval *tv)
475 {
476 struct uloop_timeout *timeout;
477 int diff;
478
479 if (list_empty(&timeouts))
480 return -1;
481
482 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
483 diff = tv_diff(&timeout->time, tv);
484 if (diff < 0)
485 return 0;
486
487 return diff;
488 }
489
490 static void uloop_process_timeouts(struct timeval *tv)
491 {
492 struct uloop_timeout *t;
493
494 while (!list_empty(&timeouts)) {
495 t = list_first_entry(&timeouts, struct uloop_timeout, list);
496
497 if (tv_diff(&t->time, tv) > 0)
498 break;
499
500 uloop_timeout_cancel(t);
501 if (t->cb)
502 t->cb(t);
503 }
504 }
505
506 static void uloop_clear_timeouts(void)
507 {
508 struct uloop_timeout *t, *tmp;
509
510 list_for_each_entry_safe(t, tmp, &timeouts, list)
511 uloop_timeout_cancel(t);
512 }
513
514 static void uloop_clear_processes(void)
515 {
516 struct uloop_process *p, *tmp;
517
518 list_for_each_entry_safe(p, tmp, &processes, list)
519 uloop_process_delete(p);
520 }
521
522 bool uloop_cancelling(void)
523 {
524 return uloop_run_depth > 0 && uloop_cancelled;
525 }
526
527 int uloop_run_timeout(int timeout)
528 {
529 int next_time = 0;
530 struct timeval tv;
531
532 uloop_run_depth++;
533
534 uloop_status = 0;
535 uloop_cancelled = false;
536 while (!uloop_cancelled)
537 {
538 uloop_gettime(&tv);
539 uloop_process_timeouts(&tv);
540
541 if (do_sigchld)
542 uloop_handle_processes();
543
544 if (uloop_cancelled)
545 break;
546
547 uloop_gettime(&tv);
548
549 next_time = uloop_get_next_timeout(&tv);
550 if (timeout >= 0 && timeout < next_time)
551 next_time = timeout;
552 uloop_run_events(next_time);
553 }
554
555 --uloop_run_depth;
556
557 return uloop_status;
558 }
559
560 void uloop_done(void)
561 {
562 uloop_setup_signals(false);
563
564 if (poll_fd >= 0) {
565 close(poll_fd);
566 poll_fd = -1;
567 }
568
569 if (waker_pipe >= 0) {
570 uloop_fd_delete(&waker_fd);
571 close(waker_pipe);
572 close(waker_fd.fd);
573 waker_pipe = -1;
574 }
575
576 uloop_clear_timeouts();
577 uloop_clear_processes();
578 }