bf1721951917c0ae5688f3074a98e1de11a96a9c
[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_poll(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         uint16_t kflags;
100
101         kflags = get_flags(flags, ULOOP_READ);
102         EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
103
104         kflags = get_flags(flags, ULOOP_WRITE);
105         EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
106
107         if (!flags)
108                 fl |= EV_DELETE;
109
110         if (nev && (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1))
111                 return -1;
112
113         return 0;
114 }
115
116 int uloop_fd_delete(struct uloop_fd *sock)
117 {
118         int i;
119
120         for (i = cur_fd + 1; i < cur_nfds; i++) {
121                 if (events[i].udata != sock)
122                         continue;
123
124                 events[i].udata = NULL;
125         }
126
127         sock->registered = false;
128         return register_poll(sock, 0);
129 }
130
131 static void uloop_run_events(int timeout)
132 {
133         struct timespec ts;
134         int nfds, n;
135
136         if (timeout >= 0) {
137                 ts.tv_sec = timeout / 1000;
138                 ts.tv_nsec = (timeout % 1000) * 1000000;
139         }
140
141         nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
142         for(n = 0; n < nfds; ++n)
143         {
144                 struct uloop_fd *u = events[n].udata;
145                 unsigned int ev = 0;
146
147                 if (!u)
148                         continue;
149
150                 if (events[n].flags & EV_ERROR) {
151                         u->error = true;
152                         uloop_fd_delete(u);
153                 }
154
155                 if(events[n].filter == EVFILT_READ)
156                         ev |= ULOOP_READ;
157                 else if (events[n].filter == EVFILT_WRITE)
158                         ev |= ULOOP_WRITE;
159
160                 if (events[n].flags & EV_EOF)
161                         u->eof = true;
162                 else if (!ev)
163                         continue;
164
165                 if (u->cb) {
166                         cur_fd = n;
167                         cur_nfds = nfds;
168                         u->cb(u, ev);
169                 }
170         }
171         cur_nfds = 0;
172 }
173
174 #endif
175
176 #ifdef USE_EPOLL
177
178 /**
179  * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
180  */
181 #ifndef EPOLLRDHUP
182 #define EPOLLRDHUP 0x2000
183 #endif
184
185 int uloop_init(void)
186 {
187         if (poll_fd >= 0)
188                 return 0;
189
190         poll_fd = epoll_create(32);
191         if (poll_fd < 0)
192                 return -1;
193
194         fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
195         return 0;
196 }
197
198 static int register_poll(struct uloop_fd *fd, unsigned int flags)
199 {
200         struct epoll_event ev;
201         int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
202
203         memset(&ev, 0, sizeof(struct epoll_event));
204
205         if (flags & ULOOP_READ)
206                 ev.events |= EPOLLIN | EPOLLRDHUP;
207
208         if (flags & ULOOP_WRITE)
209                 ev.events |= EPOLLOUT;
210
211         if (flags & ULOOP_EDGE_TRIGGER)
212                 ev.events |= EPOLLET;
213
214         ev.data.fd = fd->fd;
215         ev.data.ptr = fd;
216
217         return epoll_ctl(poll_fd, op, fd->fd, &ev);
218 }
219
220 static struct epoll_event events[ULOOP_MAX_EVENTS];
221
222 int uloop_fd_delete(struct uloop_fd *sock)
223 {
224         int i;
225
226         for (i = cur_fd + 1; i < cur_nfds; i++) {
227                 if (events[i].data.ptr != sock)
228                         continue;
229
230                 events[i].data.ptr = NULL;
231         }
232         sock->registered = false;
233         return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
234 }
235
236 static void uloop_run_events(int timeout)
237 {
238         int n, nfds;
239
240         nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
241         for(n = 0; n < nfds; ++n)
242         {
243                 struct uloop_fd *u = events[n].data.ptr;
244                 unsigned int ev = 0;
245
246                 if (!u)
247                         continue;
248
249                 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
250                         u->error = true;
251                         uloop_fd_delete(u);
252                 }
253
254                 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
255                         continue;
256
257                 if(events[n].events & EPOLLRDHUP)
258                         u->eof = true;
259
260                 if(events[n].events & EPOLLIN)
261                         ev |= ULOOP_READ;
262
263                 if(events[n].events & EPOLLOUT)
264                         ev |= ULOOP_WRITE;
265
266                 if(u->cb) {
267                         cur_fd = n;
268                         cur_nfds = nfds;
269                         u->cb(u, ev);
270                 }
271         }
272         cur_nfds = 0;
273 }
274
275 #endif
276
277 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
278 {
279         unsigned int fl;
280         int ret;
281
282         if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
283                 fl = fcntl(sock->fd, F_GETFL, 0);
284                 fl |= O_NONBLOCK;
285                 fcntl(sock->fd, F_SETFL, fl);
286         }
287
288         ret = register_poll(sock, flags);
289         if (ret < 0)
290                 goto out;
291
292         sock->registered = true;
293         sock->eof = false;
294
295 out:
296         return ret;
297 }
298
299 static int tv_diff(struct timeval *t1, struct timeval *t2)
300 {
301         return
302                 (t1->tv_sec - t2->tv_sec) * 1000 +
303                 (t1->tv_usec - t2->tv_usec) / 1000;
304 }
305
306 int uloop_timeout_add(struct uloop_timeout *timeout)
307 {
308         struct uloop_timeout *tmp;
309         struct list_head *h = &timeouts;
310
311         if (timeout->pending)
312                 return -1;
313
314         list_for_each_entry(tmp, &timeouts, list) {
315                 if (tv_diff(&tmp->time, &timeout->time) > 0) {
316                         h = &tmp->list;
317                         break;
318                 }
319         }
320
321         list_add_tail(&timeout->list, h);
322         timeout->pending = true;
323
324         return 0;
325 }
326
327 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
328 {
329         struct timeval *time = &timeout->time;
330
331         if (timeout->pending)
332                 uloop_timeout_cancel(timeout);
333
334         gettimeofday(&timeout->time, NULL);
335
336         time->tv_sec += msecs / 1000;
337         time->tv_usec += (msecs % 1000) * 1000;
338
339         if (time->tv_usec > 1000000) {
340                 time->tv_sec++;
341                 time->tv_usec %= 1000000;
342         }
343
344         return uloop_timeout_add(timeout);
345 }
346
347 int uloop_timeout_cancel(struct uloop_timeout *timeout)
348 {
349         if (!timeout->pending)
350                 return -1;
351
352         list_del(&timeout->list);
353         timeout->pending = false;
354
355         return 0;
356 }
357
358 int uloop_process_add(struct uloop_process *p)
359 {
360         struct uloop_process *tmp;
361         struct list_head *h = &processes;
362
363         if (p->pending)
364                 return -1;
365
366         list_for_each_entry(tmp, &processes, list) {
367                 if (tmp->pid > p->pid) {
368                         h = &tmp->list;
369                         break;
370                 }
371         }
372
373         list_add_tail(&p->list, h);
374         p->pending = true;
375
376         return 0;
377 }
378
379 int uloop_process_delete(struct uloop_process *p)
380 {
381         if (!p->pending)
382                 return -1;
383
384         list_del(&p->list);
385         p->pending = false;
386
387         return 0;
388 }
389
390 static void uloop_handle_processes(void)
391 {
392         struct uloop_process *p, *tmp;
393         pid_t pid;
394         int ret;
395
396         do_sigchld = false;
397
398         while (1) {
399                 pid = waitpid(-1, &ret, WNOHANG);
400                 if (pid <= 0)
401                         return;
402
403                 list_for_each_entry_safe(p, tmp, &processes, list) {
404                         if (p->pid < pid)
405                                 continue;
406
407                         if (p->pid > pid)
408                                 break;
409
410                         uloop_process_delete(p);
411                         p->cb(p, ret);
412                 }
413         }
414
415 }
416
417 static void uloop_handle_sigint(int signo)
418 {
419         uloop_cancelled = true;
420 }
421
422 static void uloop_sigchld(int signo)
423 {
424         do_sigchld = true;
425 }
426
427 static void uloop_setup_signals(void)
428 {
429         struct sigaction s;
430
431         memset(&s, 0, sizeof(struct sigaction));
432         s.sa_handler = uloop_handle_sigint;
433         s.sa_flags = 0;
434         sigaction(SIGINT, &s, NULL);
435
436         if (uloop_handle_sigchld) {
437                 s.sa_handler = uloop_sigchld;
438                 sigaction(SIGCHLD, &s, NULL);
439         }
440 }
441
442 static int uloop_get_next_timeout(struct timeval *tv)
443 {
444         struct uloop_timeout *timeout;
445         int diff;
446
447         if (list_empty(&timeouts))
448                 return -1;
449
450         timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
451         diff = tv_diff(&timeout->time, tv);
452         if (diff < 0)
453                 return 0;
454
455         return diff;
456 }
457
458 static void uloop_process_timeouts(struct timeval *tv)
459 {
460         struct uloop_timeout *t;
461
462         while (!list_empty(&timeouts)) {
463                 t = list_first_entry(&timeouts, struct uloop_timeout, list);
464
465                 if (tv_diff(&t->time, tv) > 0)
466                         break;
467
468                 uloop_timeout_cancel(t);
469                 if (t->cb)
470                         t->cb(t);
471         }
472 }
473
474 static void uloop_clear_timeouts(void)
475 {
476         struct uloop_timeout *t, *tmp;
477
478         list_for_each_entry_safe(t, tmp, &timeouts, list)
479                 uloop_timeout_cancel(t);
480 }
481
482 static void uloop_clear_processes(void)
483 {
484         struct uloop_process *p, *tmp;
485
486         list_for_each_entry_safe(p, tmp, &processes, list)
487                 uloop_process_delete(p);
488 }
489
490 void uloop_run(void)
491 {
492         struct timeval tv;
493
494         uloop_setup_signals();
495         while(!uloop_cancelled)
496         {
497                 gettimeofday(&tv, NULL);
498                 uloop_process_timeouts(&tv);
499                 if (uloop_cancelled)
500                         break;
501
502                 if (do_sigchld)
503                         uloop_handle_processes();
504                 uloop_run_events(uloop_get_next_timeout(&tv));
505         }
506 }
507
508 void uloop_done(void)
509 {
510         if (poll_fd < 0)
511                 return;
512
513         close(poll_fd);
514         poll_fd = -1;
515
516         uloop_clear_timeouts();
517         uloop_clear_processes();
518 }