84106345fbdcb55b90e87b3e055c600839ff63d3
[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 "uloop.h"
26
27 #ifdef USE_KQUEUE
28 #include <sys/event.h>
29 #endif
30 #ifdef USE_EPOLL
31 #include <sys/epoll.h>
32 #endif
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <poll.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdbool.h>
43
44 #ifndef ARRAY_SIZE
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46 #endif
47 #define ULOOP_MAX_EVENTS 10
48
49 static struct uloop_timeout *first_timeout;
50 static int poll_fd = -1;
51 bool uloop_cancelled = false;
52
53 #ifdef USE_KQUEUE
54
55 int uloop_init(void)
56 {
57         if (poll_fd >= 0)
58                 return 0;
59
60         poll_fd = kqueue();
61         if (poll_fd < 0)
62                 return -1;
63
64         return 0;
65 }
66
67
68 static uint16_t get_flags(unsigned int flags, unsigned int mask)
69 {
70         uint16_t kflags = 0;
71
72         if (!(flags & mask))
73                 return EV_DELETE;
74
75         kflags = EV_ADD;
76         if (flags & ULOOP_EDGE_TRIGGER)
77                 kflags |= EV_CLEAR;
78
79         return kflags;
80 }
81
82 static int register_poll(struct uloop_fd *fd, unsigned int flags)
83 {
84         struct timespec timeout = { 0, 0 };
85         struct kevent ev[2];
86         unsigned int changed;
87         int nev = 0;
88
89         changed = fd->kqflags ^ flags;
90         if (changed & ULOOP_EDGE_TRIGGER)
91                 changed |= flags;
92
93         if (changed & ULOOP_READ) {
94                 uint16_t kflags = get_flags(flags, ULOOP_READ);
95                 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
96         }
97
98         if (changed & ULOOP_WRITE) {
99                 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
100                 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
101         }
102
103         if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
104                 return -1;
105
106         fd->kqflags = flags;
107         return 0;
108 }
109
110 int uloop_fd_delete(struct uloop_fd *sock)
111 {
112         sock->registered = false;
113         return register_poll(sock, 0);
114 }
115
116 static void uloop_run_events(int timeout)
117 {
118         struct kevent events[ULOOP_MAX_EVENTS];
119         struct timespec ts;
120         int nfds, n;
121
122         if (timeout > 0) {
123                 ts.tv_sec = timeout / 1000;
124                 ts.tv_nsec = timeout * 1000000;
125         }
126
127         nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL);
128         for(n = 0; n < nfds; ++n)
129         {
130                 struct uloop_fd *u = events[n].udata;
131                 unsigned int ev = 0;
132
133                 if(events[n].flags & EV_ERROR) {
134                         u->error = true;
135                         uloop_fd_delete(u);
136                 }
137
138                 if(events[n].filter == EVFILT_READ)
139                         ev |= ULOOP_READ;
140                 else if (events[n].filter == EVFILT_WRITE)
141                         ev |= ULOOP_WRITE;
142
143                 if(events[n].flags & EV_EOF)
144                         u->eof = true;
145                 else if (!ev)
146                         continue;
147
148                 if(u->cb)
149                         u->cb(u, ev);
150         }
151 }
152
153 #endif
154
155 #ifdef USE_EPOLL
156
157 /**
158  * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
159  */
160 #ifndef EPOLLRDHUP
161 #define EPOLLRDHUP 0x2000
162 #endif
163
164 int uloop_init(void)
165 {
166         if (poll_fd >= 0)
167                 return 0;
168
169         poll_fd = epoll_create(32);
170         if (poll_fd < 0)
171                 return -1;
172
173         fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
174         return 0;
175 }
176
177 static int register_poll(struct uloop_fd *fd, unsigned int flags)
178 {
179         struct epoll_event ev;
180         int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
181
182         memset(&ev, 0, sizeof(struct epoll_event));
183
184         if (flags & ULOOP_READ)
185                 ev.events |= EPOLLIN | EPOLLRDHUP;
186
187         if (flags & ULOOP_WRITE)
188                 ev.events |= EPOLLOUT;
189
190         if (flags & ULOOP_EDGE_TRIGGER)
191                 ev.events |= EPOLLET;
192
193         ev.data.fd = fd->fd;
194         ev.data.ptr = fd;
195
196         return epoll_ctl(poll_fd, op, fd->fd, &ev);
197 }
198
199 int uloop_fd_delete(struct uloop_fd *sock)
200 {
201         sock->registered = false;
202         return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
203 }
204
205 static void uloop_run_events(int timeout)
206 {
207         struct epoll_event events[ULOOP_MAX_EVENTS];
208         int nfds, n;
209
210         nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
211         for(n = 0; n < nfds; ++n)
212         {
213                 struct uloop_fd *u = events[n].data.ptr;
214                 unsigned int ev = 0;
215
216                 if(events[n].events & EPOLLERR) {
217                         u->error = true;
218                         uloop_fd_delete(u);
219                 }
220
221                 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
222                         continue;
223
224                 if(events[n].events & EPOLLRDHUP)
225                         u->eof = true;
226
227                 if(events[n].events & EPOLLIN)
228                         ev |= ULOOP_READ;
229
230                 if(events[n].events & EPOLLOUT)
231                         ev |= ULOOP_WRITE;
232
233                 if(u->cb)
234                         u->cb(u, ev);
235         }
236 }
237
238 #endif
239
240 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
241 {
242         unsigned int fl;
243         int ret;
244
245         if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
246                 fl = fcntl(sock->fd, F_GETFL, 0);
247                 fl |= O_NONBLOCK;
248                 fcntl(sock->fd, F_SETFL, fl);
249         }
250
251         ret = register_poll(sock, flags);
252         if (ret < 0)
253                 goto out;
254
255         sock->registered = true;
256         sock->eof = false;
257
258 out:
259         return ret;
260 }
261
262 static int tv_diff(struct timeval *t1, struct timeval *t2)
263 {
264         if (t1->tv_sec != t2->tv_sec)
265                 return (t1->tv_sec - t2->tv_sec) * 1000;
266         else
267                 return (t1->tv_usec - t2->tv_usec) / 1000;
268 }
269
270 int uloop_timeout_add(struct uloop_timeout *timeout)
271 {
272         struct uloop_timeout **head = &first_timeout;
273         struct uloop_timeout *prev = NULL;
274
275         if (timeout->pending)
276                 return -1;
277
278         while (*head) {
279                 if (tv_diff(&(*head)->time, &timeout->time) > 0)
280                         break;
281
282                 prev = *head;
283                 head = &(*head)->next;
284         }
285
286         timeout->prev = prev;
287         timeout->next = *head;
288         if (timeout->next)
289                 timeout->next->prev = timeout;
290         *head = timeout;
291         timeout->pending = true;
292
293         return 0;
294 }
295
296 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
297 {
298         struct timeval *time = &timeout->time;
299
300         if (timeout->pending)
301                 uloop_timeout_cancel(timeout);
302
303         gettimeofday(&timeout->time, NULL);
304
305         time->tv_sec += msecs / 1000;
306         time->tv_usec += msecs % 1000;
307
308         if (time->tv_usec > 1000000) {
309                 time->tv_sec++;
310                 time->tv_usec %= 100000;
311         }
312
313         return uloop_timeout_add(timeout);
314 }
315
316 int uloop_timeout_cancel(struct uloop_timeout *timeout)
317 {
318         if (!timeout->pending)
319                 return -1;
320
321         if (timeout->prev)
322                 timeout->prev->next = timeout->next;
323         else
324                 first_timeout = timeout->next;
325
326         if (timeout->next)
327                 timeout->next->prev = timeout->prev;
328
329         timeout->pending = false;
330
331         return 0;
332 }
333
334 static void uloop_handle_sigint(int signo)
335 {
336         uloop_cancelled = true;
337 }
338
339 static void uloop_setup_signals(void)
340 {
341         struct sigaction s;
342         memset(&s, 0, sizeof(struct sigaction));
343         s.sa_handler = uloop_handle_sigint;
344         s.sa_flags = 0;
345         sigaction(SIGINT, &s, NULL);
346 }
347
348 static int uloop_get_next_timeout(struct timeval *tv)
349 {
350         int diff;
351
352         if (!first_timeout)
353                 return -1;
354
355         diff = tv_diff(&first_timeout->time, tv);
356         if (diff < 0)
357                 return 0;
358
359         return diff;
360 }
361
362 static void uloop_process_timeouts(struct timeval *tv)
363 {
364         struct uloop_timeout *timeout;
365
366         while (first_timeout) {
367                 if (tv_diff(&first_timeout->time, tv) > 0)
368                         break;
369
370                 timeout = first_timeout;
371                 uloop_timeout_cancel(timeout);
372                 if (timeout->cb)
373                         timeout->cb(timeout);
374         }
375 }
376
377 void uloop_run(void)
378 {
379         struct timeval tv;
380
381         uloop_setup_signals();
382         while(!uloop_cancelled)
383         {
384                 gettimeofday(&tv, NULL);
385                 uloop_process_timeouts(&tv);
386                 uloop_run_events(uloop_get_next_timeout(&tv));
387         }
388 }
389
390 void uloop_done(void)
391 {
392         if (poll_fd < 0)
393                 return;
394
395         close(poll_fd);
396         poll_fd = -1;
397 }