blobmsg: allow BLOBMSG_TYPE_UNSPEC attributes, treat them as null for JSON conversion
[project/libubox.git] / uloop.c
1 /*
2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2010 Steven Barth <steven@midlink.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
22 #include <sys/time.h>
23 #include <sys/types.h>
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <poll.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <stdbool.h>
33
34 #include "uloop.h"
35 #include "utils.h"
36
37 #ifdef USE_KQUEUE
38 #include <sys/event.h>
39 #endif
40 #ifdef USE_EPOLL
41 #include <sys/epoll.h>
42 #endif
43 #include <sys/wait.h>
44
45 #define ULOOP_MAX_EVENTS 10
46
47 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
48 static struct list_head processes = LIST_HEAD_INIT(processes);
49
50 static int poll_fd = -1;
51 bool uloop_cancelled = false;
52 bool uloop_handle_sigchld = true;
53 static bool do_sigchld = false;
54 static int cur_fd, cur_nfds;
55
56 #ifdef USE_KQUEUE
57
58 int uloop_init(void)
59 {
60 struct timespec timeout = { 0, 0 };
61 struct kevent ev = {};
62
63 if (poll_fd >= 0)
64 return 0;
65
66 poll_fd = kqueue();
67 if (poll_fd < 0)
68 return -1;
69
70 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
71 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
72
73 return 0;
74 }
75
76
77 static uint16_t get_flags(unsigned int flags, unsigned int mask)
78 {
79 uint16_t kflags = 0;
80
81 if (!(flags & mask))
82 return EV_DELETE;
83
84 kflags = EV_ADD;
85 if (flags & ULOOP_EDGE_TRIGGER)
86 kflags |= EV_CLEAR;
87
88 return kflags;
89 }
90
91 static struct kevent events[ULOOP_MAX_EVENTS];
92
93 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
94 {
95 struct timespec timeout = { 0, 0 };
96 struct kevent ev[2];
97 int nev = 0;
98 unsigned int fl = 0;
99 unsigned int changed;
100 uint16_t kflags;
101
102 if (flags & ULOOP_EDGE_DEFER)
103 flags &= ~ULOOP_EDGE_TRIGGER;
104
105 changed = flags ^ fd->flags;
106 if (changed & ULOOP_EDGE_TRIGGER)
107 changed |= flags;
108
109 if (changed & ULOOP_READ) {
110 kflags = get_flags(flags, ULOOP_READ);
111 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
112 }
113
114 if (changed & ULOOP_WRITE) {
115 kflags = get_flags(flags, ULOOP_WRITE);
116 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
117 }
118
119 if (!flags)
120 fl |= EV_DELETE;
121
122 fd->flags = flags;
123 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
124 return -1;
125
126 return 0;
127 }
128
129 static int register_poll(struct uloop_fd *fd, unsigned int flags)
130 {
131 if (flags & ULOOP_EDGE_TRIGGER)
132 flags |= ULOOP_EDGE_DEFER;
133 else
134 flags &= ~ULOOP_EDGE_DEFER;
135
136 return register_kevent(fd, flags);
137 }
138
139 int uloop_fd_delete(struct uloop_fd *sock)
140 {
141 int i;
142
143 for (i = cur_fd + 1; i < cur_nfds; i++) {
144 if (events[i].udata != sock)
145 continue;
146
147 events[i].udata = NULL;
148 }
149
150 sock->registered = false;
151 return register_poll(sock, 0);
152 }
153
154 static void uloop_run_events(int timeout)
155 {
156 struct timespec ts;
157 int nfds, n;
158
159 if (timeout >= 0) {
160 ts.tv_sec = timeout / 1000;
161 ts.tv_nsec = (timeout % 1000) * 1000000;
162 }
163
164 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
165 for(n = 0; n < nfds; ++n)
166 {
167 struct uloop_fd *u = events[n].udata;
168 unsigned int ev = 0;
169
170 if (!u)
171 continue;
172
173 if (events[n].flags & EV_ERROR) {
174 u->error = true;
175 uloop_fd_delete(u);
176 }
177
178 if(events[n].filter == EVFILT_READ)
179 ev |= ULOOP_READ;
180 else if (events[n].filter == EVFILT_WRITE)
181 ev |= ULOOP_WRITE;
182
183 if (events[n].flags & EV_EOF)
184 u->eof = true;
185 else if (!ev)
186 continue;
187
188 if (u->cb) {
189 cur_fd = n;
190 cur_nfds = nfds;
191 u->cb(u, ev);
192 if (u->flags & ULOOP_EDGE_DEFER) {
193 u->flags &= ~ULOOP_EDGE_DEFER;
194 register_kevent(u, u->flags);
195 }
196 }
197 }
198 cur_nfds = 0;
199 }
200
201 #endif
202
203 #ifdef USE_EPOLL
204
205 /**
206 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
207 */
208 #ifndef EPOLLRDHUP
209 #define EPOLLRDHUP 0x2000
210 #endif
211
212 int uloop_init(void)
213 {
214 if (poll_fd >= 0)
215 return 0;
216
217 poll_fd = epoll_create(32);
218 if (poll_fd < 0)
219 return -1;
220
221 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
222 return 0;
223 }
224
225 static int register_poll(struct uloop_fd *fd, unsigned int flags)
226 {
227 struct epoll_event ev;
228 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
229
230 memset(&ev, 0, sizeof(struct epoll_event));
231
232 if (flags & ULOOP_READ)
233 ev.events |= EPOLLIN | EPOLLRDHUP;
234
235 if (flags & ULOOP_WRITE)
236 ev.events |= EPOLLOUT;
237
238 if (flags & ULOOP_EDGE_TRIGGER)
239 ev.events |= EPOLLET;
240
241 ev.data.fd = fd->fd;
242 ev.data.ptr = fd;
243
244 return epoll_ctl(poll_fd, op, fd->fd, &ev);
245 }
246
247 static struct epoll_event events[ULOOP_MAX_EVENTS];
248
249 int uloop_fd_delete(struct uloop_fd *sock)
250 {
251 int i;
252
253 for (i = cur_fd + 1; i < cur_nfds; i++) {
254 if (events[i].data.ptr != sock)
255 continue;
256
257 events[i].data.ptr = NULL;
258 }
259 sock->registered = false;
260 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
261 }
262
263 static void uloop_run_events(int timeout)
264 {
265 int n, nfds;
266
267 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
268 for(n = 0; n < nfds; ++n)
269 {
270 struct uloop_fd *u = events[n].data.ptr;
271 unsigned int ev = 0;
272
273 if (!u)
274 continue;
275
276 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
277 u->error = true;
278 uloop_fd_delete(u);
279 }
280
281 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
282 continue;
283
284 if(events[n].events & EPOLLRDHUP)
285 u->eof = true;
286
287 if(events[n].events & EPOLLIN)
288 ev |= ULOOP_READ;
289
290 if(events[n].events & EPOLLOUT)
291 ev |= ULOOP_WRITE;
292
293 if(u->cb) {
294 cur_fd = n;
295 cur_nfds = nfds;
296 u->cb(u, ev);
297 }
298 }
299 cur_nfds = 0;
300 }
301
302 #endif
303
304 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
305 {
306 unsigned int fl;
307 int ret;
308
309 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
310 fl = fcntl(sock->fd, F_GETFL, 0);
311 fl |= O_NONBLOCK;
312 fcntl(sock->fd, F_SETFL, fl);
313 }
314
315 ret = register_poll(sock, flags);
316 if (ret < 0)
317 goto out;
318
319 sock->registered = true;
320 sock->eof = false;
321
322 out:
323 return ret;
324 }
325
326 static int tv_diff(struct timeval *t1, struct timeval *t2)
327 {
328 return
329 (t1->tv_sec - t2->tv_sec) * 1000 +
330 (t1->tv_usec - t2->tv_usec) / 1000;
331 }
332
333 int uloop_timeout_add(struct uloop_timeout *timeout)
334 {
335 struct uloop_timeout *tmp;
336 struct list_head *h = &timeouts;
337
338 if (timeout->pending)
339 return -1;
340
341 list_for_each_entry(tmp, &timeouts, list) {
342 if (tv_diff(&tmp->time, &timeout->time) > 0) {
343 h = &tmp->list;
344 break;
345 }
346 }
347
348 list_add_tail(&timeout->list, h);
349 timeout->pending = true;
350
351 return 0;
352 }
353
354 static void uloop_gettime(struct timeval *tv)
355 {
356 struct timespec ts;
357
358 clock_gettime(CLOCK_MONOTONIC, &ts);
359 tv->tv_sec = ts.tv_sec;
360 tv->tv_usec = ts.tv_nsec / 1000;
361 }
362
363 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
364 {
365 struct timeval *time = &timeout->time;
366
367 if (timeout->pending)
368 uloop_timeout_cancel(timeout);
369
370 uloop_gettime(&timeout->time);
371
372 time->tv_sec += msecs / 1000;
373 time->tv_usec += (msecs % 1000) * 1000;
374
375 if (time->tv_usec > 1000000) {
376 time->tv_sec++;
377 time->tv_usec %= 1000000;
378 }
379
380 return uloop_timeout_add(timeout);
381 }
382
383 int uloop_timeout_cancel(struct uloop_timeout *timeout)
384 {
385 if (!timeout->pending)
386 return -1;
387
388 list_del(&timeout->list);
389 timeout->pending = false;
390
391 return 0;
392 }
393
394 int uloop_process_add(struct uloop_process *p)
395 {
396 struct uloop_process *tmp;
397 struct list_head *h = &processes;
398
399 if (p->pending)
400 return -1;
401
402 list_for_each_entry(tmp, &processes, list) {
403 if (tmp->pid > p->pid) {
404 h = &tmp->list;
405 break;
406 }
407 }
408
409 list_add_tail(&p->list, h);
410 p->pending = true;
411
412 return 0;
413 }
414
415 int uloop_process_delete(struct uloop_process *p)
416 {
417 if (!p->pending)
418 return -1;
419
420 list_del(&p->list);
421 p->pending = false;
422
423 return 0;
424 }
425
426 static void uloop_handle_processes(void)
427 {
428 struct uloop_process *p, *tmp;
429 pid_t pid;
430 int ret;
431
432 do_sigchld = false;
433
434 while (1) {
435 pid = waitpid(-1, &ret, WNOHANG);
436 if (pid <= 0)
437 return;
438
439 list_for_each_entry_safe(p, tmp, &processes, list) {
440 if (p->pid < pid)
441 continue;
442
443 if (p->pid > pid)
444 break;
445
446 uloop_process_delete(p);
447 p->cb(p, ret);
448 }
449 }
450
451 }
452
453 static void uloop_handle_sigint(int signo)
454 {
455 uloop_cancelled = true;
456 }
457
458 static void uloop_sigchld(int signo)
459 {
460 do_sigchld = true;
461 }
462
463 static void uloop_setup_signals(void)
464 {
465 struct sigaction s;
466
467 memset(&s, 0, sizeof(struct sigaction));
468 s.sa_handler = uloop_handle_sigint;
469 s.sa_flags = 0;
470 sigaction(SIGINT, &s, NULL);
471
472 if (uloop_handle_sigchld) {
473 s.sa_handler = uloop_sigchld;
474 sigaction(SIGCHLD, &s, NULL);
475 }
476 }
477
478 static int uloop_get_next_timeout(struct timeval *tv)
479 {
480 struct uloop_timeout *timeout;
481 int diff;
482
483 if (list_empty(&timeouts))
484 return -1;
485
486 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
487 diff = tv_diff(&timeout->time, tv);
488 if (diff < 0)
489 return 0;
490
491 return diff;
492 }
493
494 static void uloop_process_timeouts(struct timeval *tv)
495 {
496 struct uloop_timeout *t;
497
498 while (!list_empty(&timeouts)) {
499 t = list_first_entry(&timeouts, struct uloop_timeout, list);
500
501 if (tv_diff(&t->time, tv) > 0)
502 break;
503
504 uloop_timeout_cancel(t);
505 if (t->cb)
506 t->cb(t);
507 }
508 }
509
510 static void uloop_clear_timeouts(void)
511 {
512 struct uloop_timeout *t, *tmp;
513
514 list_for_each_entry_safe(t, tmp, &timeouts, list)
515 uloop_timeout_cancel(t);
516 }
517
518 static void uloop_clear_processes(void)
519 {
520 struct uloop_process *p, *tmp;
521
522 list_for_each_entry_safe(p, tmp, &processes, list)
523 uloop_process_delete(p);
524 }
525
526 void uloop_run(void)
527 {
528 struct timeval tv;
529
530 uloop_setup_signals();
531 while(!uloop_cancelled)
532 {
533 uloop_gettime(&tv);
534 uloop_process_timeouts(&tv);
535 if (uloop_cancelled)
536 break;
537
538 if (do_sigchld)
539 uloop_handle_processes();
540 uloop_run_events(uloop_get_next_timeout(&tv));
541 }
542 }
543
544 void uloop_done(void)
545 {
546 if (poll_fd < 0)
547 return;
548
549 close(poll_fd);
550 poll_fd = -1;
551
552 uloop_clear_timeouts();
553 uloop_clear_processes();
554 }