4636873d553957a355b4b02347684b763690ef88
[ashd.git] / lib / mtio-epoll.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <time.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/epoll.h>
25 #include <errno.h>
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <log.h>
31 #include <utils.h>
32 #include <mt.h>
33 #include <mtio.h>
34
35 static struct blocker *blockers;
36
37 struct blocker {
38     struct blocker *n, *p, *n2, *p2;
39     int fd, reg;
40     int ev, rev, id;
41     int thpos;
42     time_t to;
43     struct muth *th;
44 };
45
46 static int epfd = -1, fdln = 0;
47 static int exitstatus;
48 static struct blocker **fdlist;
49 static typedbuf(struct blocker *) timeheap;
50
51 static int regfd(struct blocker *bl)
52 {
53     struct blocker *o;
54     struct epoll_event evd;
55     
56     if(bl->fd < 0)
57         return(0);
58     memset(&evd, 0, sizeof(evd));
59     evd.events = 0;
60     if(bl->ev & EV_READ)
61         evd.events |= EPOLLIN;
62     if(bl->ev & EV_WRITE)
63         evd.events |= EPOLLOUT;
64     evd.data.fd = bl->fd;
65     if(bl->fd >= fdln) {
66         if(fdlist) {
67             fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
68             memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
69             fdln = bl->fd + 1;
70         } else {
71             fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
72         }
73     }
74     if(fdlist[bl->fd] == NULL) {
75         if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
76             /* XXX?! Whatever to do, really? */
77             flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
78             return(-1);
79         }
80     } else {
81         for(o = fdlist[bl->fd]; o; o = o->n2) {
82             if(o->ev & EV_READ)
83                 evd.events |= EPOLLIN;
84             if(o->ev & EV_WRITE)
85                 evd.events |= EPOLLOUT;
86         }
87         if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
88             /* XXX?! Whatever to do, really? */
89             flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
90             return(-1);
91         }
92     }
93     bl->n2 = fdlist[bl->fd];
94     bl->p2 = NULL;
95     if(fdlist[bl->fd] != NULL)
96         fdlist[bl->fd]->p2 = bl;
97     fdlist[bl->fd] = bl;
98     bl->reg = 1;
99     return(0);
100 }
101
102 static void remfd(struct blocker *bl)
103 {
104     struct blocker *o;
105     struct epoll_event evd;
106     
107     if(!bl->reg)
108         return;
109     if(bl->n2)
110         bl->n2->p2 = bl->p2;
111     if(bl->p2)
112         bl->p2->n2 = bl->n2;
113     if(bl == fdlist[bl->fd])
114         fdlist[bl->fd] = bl->n2;
115     if(fdlist[bl->fd] == NULL) {
116         if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
117             flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
118     } else {
119         memset(&evd, 0, sizeof(evd));
120         evd.events = 0;
121         evd.data.fd = bl->fd;
122         for(o = fdlist[bl->fd]; o; o = o->n2) {
123             if(o->ev & EV_READ)
124                 evd.events |= EPOLLIN;
125             if(o->ev & EV_WRITE)
126                 evd.events |= EPOLLOUT;
127         }
128         if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
129             /* XXX?! Whatever to do, really? */
130             flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
131         }
132     }
133     bl->reg = 0;
134 }
135
136 static void thraise(struct blocker *bl, int n)
137 {
138     int p;
139     
140     while(n > 0) {
141         p = (n - 1) >> 1;
142         if(timeheap.b[p]->to <= bl->to)
143             break;
144         timeheap.b[n] = timeheap.b[p];
145         timeheap.b[n]->thpos = n;
146         n = p;
147     }
148     timeheap.b[n] = bl;
149     bl->thpos = n;
150 }
151
152 static void thlower(struct blocker *bl, int n)
153 {
154     int c;
155     
156     while(1) {
157         c = (n << 1) + 1;
158         if(c >= timeheap.d)
159             break;
160         if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
161             c = c + 1;
162         if(timeheap.b[c]->to > bl->to)
163             break;
164         timeheap.b[n] = timeheap.b[c];
165         timeheap.b[n]->thpos = n;
166         n = c;
167     }
168     timeheap.b[n] = bl;
169     bl->thpos = n;
170 }
171
172 static void addtimeout(struct blocker *bl, time_t to)
173 {
174     sizebuf(timeheap, ++timeheap.d);
175     thraise(bl, timeheap.d - 1);
176 }
177
178 static void deltimeout(struct blocker *bl)
179 {
180     int n;
181     
182     if(bl->thpos == timeheap.d - 1) {
183         timeheap.d--;
184         return;
185     }
186     n = bl->thpos;
187     bl = timeheap.b[--timeheap.d];
188     if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
189         thraise(bl, n);
190     else
191         thlower(bl, n);
192 }
193
194 static int addblock(struct blocker *bl)
195 {
196     if((epfd >= 0) && regfd(bl))
197         return(-1);
198     bl->n = blockers;
199     if(blockers)
200         blockers->p = bl;
201     blockers = bl;
202     if(bl->to > 0)
203         addtimeout(bl, bl->to);
204     return(0);
205 }
206
207 static void remblock(struct blocker *bl)
208 {
209     if(bl->to > 0)
210         deltimeout(bl);
211     if(bl->n)
212         bl->n->p = bl->p;
213     if(bl->p)
214         bl->p->n = bl->n;
215     if(blockers == bl)
216         blockers = bl->n;
217     remfd(bl);
218 }
219
220 struct selected mblock(time_t to, int n, struct selected *spec)
221 {
222     int i, id;
223     struct blocker bls[n];
224     
225     to = (to > 0)?(time(NULL) + to):0;
226     for(i = 0; i < n; i++) {
227         bls[i] = (struct blocker) {
228             .fd = spec[i].fd,
229             .ev = spec[i].ev,
230             .id = i,
231             .to = to,
232             .th = current,
233         };
234         if(addblock(&bls[i])) {
235             for(i--; i >= 0; i--)
236                 remblock(&bls[i]);
237             return((struct selected){.fd = -1, .ev = -1});
238         }
239     }
240     id = yield();
241     for(i = 0; i < n; i++)
242         remblock(&bls[i]);
243     if(id < 0)
244         return((struct selected){.fd = -1, .ev = -1});
245     return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
246 }
247
248 int block(int fd, int ev, time_t to)
249 {
250     struct blocker bl;
251     int rv;
252     
253     bl = (struct blocker) {
254         .fd = fd,
255         .ev = ev,
256         .id = -1,
257         .to = (to > 0)?(time(NULL) + to):0,
258         .th = current,
259     };
260     if(addblock(&bl))
261         return(-1);
262     rv = yield();
263     remblock(&bl);
264     return(rv);
265 }
266
267 int ioloop(void)
268 {
269     struct blocker *bl, *nbl;
270     struct epoll_event evr[16];
271     int i, fd, nev, ev, toval;
272     time_t now;
273     
274     exitstatus = 0;
275     epfd = epoll_create(128);
276     fcntl(epfd, F_SETFD, FD_CLOEXEC);
277     for(bl = blockers; bl; bl = nbl) {
278         nbl = bl->n;
279         if(regfd(bl))
280             resume(bl->th, -1);
281     }
282     while(blockers != NULL) {
283         now = time(NULL);
284         if(timeheap.d == 0)
285             toval = -1;
286         else if(timeheap.b[0]->to > now)
287             toval = (timeheap.b[0]->to - now) * 1000;
288         else
289             toval = 1000;
290         if(exitstatus)
291             break;
292         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
293         if(nev < 0) {
294             if(errno != EINTR) {
295                 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
296                 /* To avoid CPU hogging in case it's bad, which it
297                  * probably is. */
298                 sleep(1);
299             }
300             continue;
301         }
302         for(i = 0; i < nev; i++) {
303             fd = evr[i].data.fd;
304             ev = 0;
305             if(evr[i].events & EPOLLIN)
306                 ev |= EV_READ;
307             if(evr[i].events & EPOLLOUT)
308                 ev |= EV_WRITE;
309             if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
310                 ev = -1;
311             for(bl = fdlist[fd]; bl; bl = nbl) {
312                 nbl = bl->n2;
313                 if((ev < 0) || (ev & bl->ev)) {
314                     if(bl->id < 0) {
315                         resume(bl->th, ev);
316                     } else {
317                         bl->rev = ev;
318                         resume(bl->th, bl->id);
319                     }
320                 }
321             }
322         }
323         now = time(NULL);
324         while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
325             if(bl->id < 0) {
326                 resume(bl->th, 0);
327             } else {
328                 bl->rev = 0;
329                 resume(bl->th, bl->id);
330             }
331         }
332     }
333     for(bl = blockers; bl; bl = bl->n)
334         remfd(bl);
335     close(epfd);
336     epfd = -1;
337     return(exitstatus);
338 }
339
340 void exitioloop(int status)
341 {
342     exitstatus = status;
343 }