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