ecf26ec185db87fadb0d2aa2cadacf6b1b4a0ae0
[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 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
355 {
356         struct timeval *time = &timeout->time;
357
358         if (timeout->pending)
359                 uloop_timeout_cancel(timeout);
360
361         gettimeofday(&timeout->time, NULL);
362
363         time->tv_sec += msecs / 1000;
364         time->tv_usec += (msecs % 1000) * 1000;
365
366         if (time->tv_usec > 1000000) {
367                 time->tv_sec++;
368                 time->tv_usec %= 1000000;
369         }
370
371         return uloop_timeout_add(timeout);
372 }
373
374 int uloop_timeout_cancel(struct uloop_timeout *timeout)
375 {
376         if (!timeout->pending)
377                 return -1;
378
379         list_del(&timeout->list);
380         timeout->pending = false;
381
382         return 0;
383 }
384
385 int uloop_process_add(struct uloop_process *p)
386 {
387         struct uloop_process *tmp;
388         struct list_head *h = &processes;
389
390         if (p->pending)
391                 return -1;
392
393         list_for_each_entry(tmp, &processes, list) {
394                 if (tmp->pid > p->pid) {
395                         h = &tmp->list;
396                         break;
397                 }
398         }
399
400         list_add_tail(&p->list, h);
401         p->pending = true;
402
403         return 0;
404 }
405
406 int uloop_process_delete(struct uloop_process *p)
407 {
408         if (!p->pending)
409                 return -1;
410
411         list_del(&p->list);
412         p->pending = false;
413
414         return 0;
415 }
416
417 static void uloop_handle_processes(void)
418 {
419         struct uloop_process *p, *tmp;
420         pid_t pid;
421         int ret;
422
423         do_sigchld = false;
424
425         while (1) {
426                 pid = waitpid(-1, &ret, WNOHANG);
427                 if (pid <= 0)
428                         return;
429
430                 list_for_each_entry_safe(p, tmp, &processes, list) {
431                         if (p->pid < pid)
432                                 continue;
433
434                         if (p->pid > pid)
435                                 break;
436
437                         uloop_process_delete(p);
438                         p->cb(p, ret);
439                 }
440         }
441
442 }
443
444 static void uloop_handle_sigint(int signo)
445 {
446         uloop_cancelled = true;
447 }
448
449 static void uloop_sigchld(int signo)
450 {
451         do_sigchld = true;
452 }
453
454 static void uloop_setup_signals(void)
455 {
456         struct sigaction s;
457
458         memset(&s, 0, sizeof(struct sigaction));
459         s.sa_handler = uloop_handle_sigint;
460         s.sa_flags = 0;
461         sigaction(SIGINT, &s, NULL);
462
463         if (uloop_handle_sigchld) {
464                 s.sa_handler = uloop_sigchld;
465                 sigaction(SIGCHLD, &s, NULL);
466         }
467 }
468
469 static int uloop_get_next_timeout(struct timeval *tv)
470 {
471         struct uloop_timeout *timeout;
472         int diff;
473
474         if (list_empty(&timeouts))
475                 return -1;
476
477         timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
478         diff = tv_diff(&timeout->time, tv);
479         if (diff < 0)
480                 return 0;
481
482         return diff;
483 }
484
485 static void uloop_process_timeouts(struct timeval *tv)
486 {
487         struct uloop_timeout *t;
488
489         while (!list_empty(&timeouts)) {
490                 t = list_first_entry(&timeouts, struct uloop_timeout, list);
491
492                 if (tv_diff(&t->time, tv) > 0)
493                         break;
494
495                 uloop_timeout_cancel(t);
496                 if (t->cb)
497                         t->cb(t);
498         }
499 }
500
501 static void uloop_clear_timeouts(void)
502 {
503         struct uloop_timeout *t, *tmp;
504
505         list_for_each_entry_safe(t, tmp, &timeouts, list)
506                 uloop_timeout_cancel(t);
507 }
508
509 static void uloop_clear_processes(void)
510 {
511         struct uloop_process *p, *tmp;
512
513         list_for_each_entry_safe(p, tmp, &processes, list)
514                 uloop_process_delete(p);
515 }
516
517 void uloop_run(void)
518 {
519         struct timeval tv;
520
521         uloop_setup_signals();
522         while(!uloop_cancelled)
523         {
524                 gettimeofday(&tv, NULL);
525                 uloop_process_timeouts(&tv);
526                 if (uloop_cancelled)
527                         break;
528
529                 if (do_sigchld)
530                         uloop_handle_processes();
531                 uloop_run_events(uloop_get_next_timeout(&tv));
532         }
533 }
534
535 void uloop_done(void)
536 {
537         if (poll_fd < 0)
538                 return;
539
540         close(poll_fd);
541         poll_fd = -1;
542
543         uloop_clear_timeouts();
544         uloop_clear_processes();
545 }