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