call[fs]cgi: Handle serialized child connections asynchronously.
[ashd.git] / lib / mtio-epoll.c
CommitLineData
a6cda4dd
FT
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>
4f6d772e
FT
20#include <unistd.h>
21#include <time.h>
be078ac9 22#include <fcntl.h>
a6cda4dd
FT
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
35static struct blocker *blockers;
36
37struct blocker {
38 struct blocker *n, *p, *n2, *p2;
39 int fd, reg;
205ee933 40 int ev, rev, id;
589987f8 41 int thpos;
a6cda4dd
FT
42 time_t to;
43 struct muth *th;
44};
45
46static int epfd = -1, fdln = 0;
9d32586e 47static int exitstatus;
a6cda4dd 48static struct blocker **fdlist;
bb073004 49static typedbuf(struct blocker *) timeheap;
a6cda4dd
FT
50
51static int regfd(struct blocker *bl)
52{
53 struct blocker *o;
54 struct epoll_event evd;
55
99587986
FT
56 if(bl->fd < 0)
57 return(0);
a6cda4dd
FT
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
102static 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 }
9d32586e 133 bl->reg = 0;
a6cda4dd
FT
134}
135
bb073004 136static void thraise(struct blocker *bl, int n)
589987f8
FT
137{
138 int p;
139
140 while(n > 0) {
141 p = (n - 1) >> 1;
bb073004 142 if(timeheap.b[p]->to <= bl->to)
589987f8
FT
143 break;
144 timeheap.b[n] = timeheap.b[p];
bb073004 145 timeheap.b[n]->thpos = n;
589987f8
FT
146 n = p;
147 }
bb073004
FT
148 timeheap.b[n] = bl;
149 bl->thpos = n;
589987f8
FT
150}
151
bb073004 152static void thlower(struct blocker *bl, int n)
589987f8
FT
153{
154 int c;
155
156 while(1) {
157 c = (n << 1) + 1;
158 if(c >= timeheap.d)
159 break;
bb073004 160 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
589987f8 161 c = c + 1;
bb073004 162 if(timeheap.b[c]->to > bl->to)
589987f8
FT
163 break;
164 timeheap.b[n] = timeheap.b[c];
bb073004 165 timeheap.b[n]->thpos = n;
589987f8
FT
166 n = c;
167 }
bb073004
FT
168 timeheap.b[n] = bl;
169 bl->thpos = n;
589987f8
FT
170}
171
172static void addtimeout(struct blocker *bl, time_t to)
173{
174 sizebuf(timeheap, ++timeheap.d);
bb073004 175 thraise(bl, timeheap.d - 1);
589987f8
FT
176}
177
178static void deltimeout(struct blocker *bl)
179{
589987f8
FT
180 int n;
181
182 if(bl->thpos == timeheap.d - 1) {
183 timeheap.d--;
184 return;
185 }
186 n = bl->thpos;
bb073004
FT
187 bl = timeheap.b[--timeheap.d];
188 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
189 thraise(bl, n);
589987f8 190 else
bb073004 191 thlower(bl, n);
589987f8
FT
192}
193
205ee933 194static int addblock(struct blocker *bl)
a6cda4dd 195{
205ee933 196 if((epfd >= 0) && regfd(bl))
a6cda4dd 197 return(-1);
a6cda4dd
FT
198 bl->n = blockers;
199 if(blockers)
200 blockers->p = bl;
201 blockers = bl;
bcad6b0c
FT
202 if(bl->to > 0)
203 addtimeout(bl, bl->to);
205ee933
FT
204 return(0);
205}
206
207static void remblock(struct blocker *bl)
208{
589987f8
FT
209 if(bl->to > 0)
210 deltimeout(bl);
a6cda4dd
FT
211 if(bl->n)
212 bl->n->p = bl->p;
213 if(bl->p)
214 bl->p->n = bl->n;
205ee933 215 if(blockers == bl)
a6cda4dd
FT
216 blockers = bl->n;
217 remfd(bl);
205ee933
FT
218}
219
220struct 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])) {
4f5bec84 235 for(i--; i >= 0; i--)
205ee933
FT
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
248int 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);
a6cda4dd
FT
264 return(rv);
265}
266
9d32586e 267int ioloop(void)
a6cda4dd
FT
268{
269 struct blocker *bl, *nbl;
270 struct epoll_event evr[16];
271 int i, fd, nev, ev, toval;
589987f8 272 time_t now;
a6cda4dd 273
9d32586e 274 exitstatus = 0;
a6cda4dd 275 epfd = epoll_create(128);
be078ac9 276 fcntl(epfd, F_SETFD, FD_CLOEXEC);
a6cda4dd
FT
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) {
a6cda4dd 283 now = time(NULL);
589987f8 284 if(timeheap.d == 0)
a6cda4dd 285 toval = -1;
bb073004
FT
286 else if(timeheap.b[0]->to > now)
287 toval = (timeheap.b[0]->to - now) * 1000;
a6cda4dd
FT
288 else
289 toval = 1000;
9d32586e
FT
290 if(exitstatus)
291 break;
a6cda4dd
FT
292 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
293 if(nev < 0) {
294 if(errno != EINTR) {
f43decce 295 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
a6cda4dd
FT
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;
205ee933
FT
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 }
a6cda4dd
FT
321 }
322 }
323 now = time(NULL);
bb073004 324 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
bcad6b0c 325 if(bl->id < 0) {
bb073004 326 resume(bl->th, 0);
bcad6b0c
FT
327 } else {
328 bl->rev = 0;
329 resume(bl->th, bl->id);
205ee933 330 }
a6cda4dd
FT
331 }
332 }
9d32586e
FT
333 for(bl = blockers; bl; bl = bl->n)
334 remfd(bl);
a6cda4dd
FT
335 close(epfd);
336 epfd = -1;
9d32586e
FT
337 return(exitstatus);
338}
339
340void exitioloop(int status)
341{
342 exitstatus = status;
a6cda4dd 343}