lib: Clear the epoll event descriptor before use.
[ashd.git] / lib / mtio.c
CommitLineData
83723896
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>
e1cdf02e
FT
20#include <stdio.h>
21#include <fcntl.h>
83723896
FT
22#include <unistd.h>
23#include <string.h>
24#include <time.h>
25#include <errno.h>
26#include <sys/select.h>
e1cdf02e 27#include <sys/socket.h>
83723896
FT
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <log.h>
33#include <utils.h>
34#include <mt.h>
35#include <mtio.h>
36
16accb88
FT
37static struct blocker *blockers;
38
39#ifdef HAVE_EPOLL
40
41/*
42 * Support for epoll. Optimally, different I/O loops should be split
43 * into different files for greater clarity, but I'll save that fun
44 * for another day.
45 *
46 * Scroll down to #else for the normal select loop.
47 */
48
49#include <sys/epoll.h>
50
51struct blocker {
52 struct blocker *n, *p, *n2, *p2;
53 int fd, reg;
54 int ev;
55 time_t to;
56 struct muth *th;
57};
58
59static int epfd = -1, fdln = 0;
60static struct blocker **fdlist;
61
62static int regfd(struct blocker *bl)
63{
64 struct blocker *o;
65 struct epoll_event evd;
5ab220d9
FT
66
67 memset(&evd, 0, sizeof(evd));
16accb88
FT
68 evd.events = 0;
69 if(bl->ev & EV_READ)
70 evd.events |= EPOLLIN;
71 if(bl->ev & EV_WRITE)
72 evd.events |= EPOLLOUT;
73 evd.data.fd = bl->fd;
74 if(bl->fd >= fdln) {
75 if(fdlist) {
76 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
77 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
78 fdln = bl->fd + 1;
79 } else {
80 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
81 }
82 }
83 if(fdlist[bl->fd] == NULL) {
84 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
85 /* XXX?! Whatever to do, really? */
86 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
87 return(-1);
88 }
89 } else {
90 for(o = fdlist[bl->fd]; o; o = o->n2) {
91 if(o->ev & EV_READ)
92 evd.events |= EPOLLIN;
93 if(o->ev & EV_WRITE)
94 evd.events |= EPOLLOUT;
95 }
96 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
97 /* XXX?! Whatever to do, really? */
98 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
99 return(-1);
100 }
101 }
102 bl->n2 = fdlist[bl->fd];
103 bl->p2 = NULL;
104 if(fdlist[bl->fd] != NULL)
105 fdlist[bl->fd]->p2 = bl;
106 fdlist[bl->fd] = bl;
107 bl->reg = 1;
108 return(0);
109}
110
111static void remfd(struct blocker *bl)
112{
113 struct blocker *o;
114 struct epoll_event evd;
115
116 if(!bl->reg)
117 return;
118 if(bl->n2)
119 bl->n2->p2 = bl->p2;
120 if(bl->p2)
121 bl->p2->n2 = bl->n2;
122 if(bl == fdlist[bl->fd])
123 fdlist[bl->fd] = bl->n2;
124 if(fdlist[bl->fd] == NULL) {
125 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
126 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
127 } else {
5ab220d9 128 memset(&evd, 0, sizeof(evd));
16accb88
FT
129 evd.events = 0;
130 evd.data.fd = bl->fd;
131 for(o = fdlist[bl->fd]; o; o = o->n2) {
132 if(o->ev & EV_READ)
133 evd.events |= EPOLLIN;
134 if(o->ev & EV_WRITE)
135 evd.events |= EPOLLOUT;
136 }
137 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
138 /* XXX?! Whatever to do, really? */
139 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
140 }
141 }
142}
143
144int block(int fd, int ev, time_t to)
145{
146 struct blocker *bl;
147 int rv;
148
149 omalloc(bl);
150 bl->fd = fd;
151 bl->ev = ev;
152 if(to > 0)
153 bl->to = time(NULL) + to;
154 bl->th = current;
155 if((epfd >= 0) && regfd(bl)) {
156 free(bl);
157 return(-1);
158 }
159 bl->n = blockers;
160 if(blockers)
161 blockers->p = bl;
162 blockers = bl;
163 rv = yield();
164 if(bl->n)
165 bl->n->p = bl->p;
166 if(bl->p)
167 bl->p->n = bl->n;
168 if(bl == blockers)
169 blockers = bl->n;
170 remfd(bl);
171 free(bl);
172 return(rv);
173}
174
175void ioloop(void)
176{
177 struct blocker *bl, *nbl;
178 struct epoll_event evr[16];
179 int i, fd, nev, ev;
180 time_t now, timeout;
181
182 epfd = epoll_create(128);
183 for(bl = blockers; bl; bl = nbl) {
184 nbl = bl->n;
185 if(regfd(bl))
186 resume(bl->th, -1);
187 }
188 while(blockers != NULL) {
189 timeout = 0;
190 for(bl = blockers; bl; bl = bl->n) {
191 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
192 timeout = bl->to;
193 }
194 now = time(NULL);
195 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), timeout?((timeout - now) * 1000):-1);
196 if(nev < 0) {
197 if(errno != EINTR) {
198 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
199 /* To avoid CPU hogging in case it's bad, which it
200 * probably is. */
201 sleep(1);
202 }
203 continue;
204 }
205 now = time(NULL);
206 for(i = 0; i < nev; i++) {
207 fd = evr[i].data.fd;
208 ev = 0;
209 if(evr[i].events & EPOLLIN)
210 ev |= EV_READ;
211 if(evr[i].events & EPOLLOUT)
212 ev |= EV_WRITE;
213 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
214 ev = -1;
215 for(bl = fdlist[fd]; bl; bl = nbl) {
216 nbl = bl->n2;
217 if((ev < 0) || (ev & bl->ev))
218 resume(bl->th, ev);
219 else if((bl->to != 0) && (bl->to <= now))
220 resume(bl->th, 0);
221 }
222 }
223 }
224 close(epfd);
225 epfd = -1;
226}
227
228#else
229
83723896
FT
230struct blocker {
231 struct blocker *n, *p;
232 int fd;
233 int ev;
234 time_t to;
235 struct muth *th;
236};
237
83723896
FT
238int block(int fd, int ev, time_t to)
239{
240 struct blocker *bl;
241 int rv;
242
243 omalloc(bl);
244 bl->fd = fd;
245 bl->ev = ev;
246 if(to > 0)
247 bl->to = time(NULL) + to;
248 bl->th = current;
249 bl->n = blockers;
250 if(blockers)
251 blockers->p = bl;
252 blockers = bl;
253 rv = yield();
254 if(bl->n)
255 bl->n->p = bl->p;
256 if(bl->p)
257 bl->p->n = bl->n;
258 if(bl == blockers)
259 blockers = bl->n;
96fc434a 260 free(bl);
83723896
FT
261 return(rv);
262}
263
264void ioloop(void)
265{
266 int ret;
267 fd_set rfds, wfds, efds;
268 struct blocker *bl, *nbl;
269 struct timeval toval;
270 time_t now, timeout;
271 int maxfd;
272 int ev;
273
274 while(blockers != NULL) {
275 FD_ZERO(&rfds);
276 FD_ZERO(&wfds);
277 FD_ZERO(&efds);
278 maxfd = 0;
279 now = time(NULL);
280 timeout = 0;
281 for(bl = blockers; bl; bl = bl->n) {
282 if(bl->ev & EV_READ)
283 FD_SET(bl->fd, &rfds);
284 if(bl->ev & EV_WRITE)
285 FD_SET(bl->fd, &wfds);
286 FD_SET(bl->fd, &efds);
287 if(bl->fd > maxfd)
288 maxfd = bl->fd;
289 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
290 timeout = bl->to;
291 }
292 toval.tv_sec = timeout - now;
293 toval.tv_usec = 0;
294 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
295 if(ret < 0) {
296 if(errno != EINTR) {
297 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
298 /* To avoid CPU hogging in case it's bad, which it
299 * probably is. */
300 sleep(1);
301 }
302 }
303 now = time(NULL);
304 for(bl = blockers; bl; bl = nbl) {
305 nbl = bl->n;
306 ev = 0;
307 if(FD_ISSET(bl->fd, &rfds))
308 ev |= EV_READ;
309 if(FD_ISSET(bl->fd, &wfds))
310 ev |= EV_WRITE;
311 if(FD_ISSET(bl->fd, &efds))
312 ev = -1;
3dc69b45 313 if((ev < 0) || (ev & bl->ev))
83723896
FT
314 resume(bl->th, ev);
315 else if((bl->to != 0) && (bl->to <= now))
316 resume(bl->th, 0);
317 }
318 }
319}
e1cdf02e 320
16accb88
FT
321#endif
322
e1cdf02e
FT
323struct stdiofd {
324 int fd;
325 int sock;
326 int timeout;
327};
328
329static ssize_t mtread(void *cookie, char *buf, size_t len)
330{
331 struct stdiofd *d = cookie;
332 int ev;
333 ssize_t ret;
334
335 while(1) {
336 ret = read(d->fd, buf, len);
337 if((ret < 0) && (errno == EAGAIN)) {
338 ev = block(d->fd, EV_READ, d->timeout);
339 if(ev < 0) {
340 /* If we just go on, we should get the real error. */
341 continue;
342 } else if(ev == 0) {
343 errno = ETIMEDOUT;
344 return(-1);
345 } else {
346 continue;
347 }
348 } else {
349 return(ret);
350 }
351 }
352}
353
354static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
355{
356 struct stdiofd *d = cookie;
357 int ev;
9dc25d3d 358 size_t off;
e1cdf02e
FT
359 ssize_t ret;
360
9dc25d3d
FT
361 off = 0;
362 while(off < len) {
e1cdf02e 363 if(d->sock)
9dc25d3d 364 ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
e1cdf02e 365 else
9dc25d3d
FT
366 ret = write(d->fd, buf + off, len - off);
367 if(ret < 0) {
368 if(errno == EAGAIN) {
369 ev = block(d->fd, EV_WRITE, d->timeout);
370 if(ev < 0) {
371 /* If we just go on, we should get the real error. */
372 continue;
373 } else if(ev == 0) {
374 errno = ETIMEDOUT;
375 return(off);
376 } else {
377 continue;
378 }
e1cdf02e 379 } else {
9dc25d3d 380 return(off);
e1cdf02e
FT
381 }
382 } else {
9dc25d3d 383 off += ret;
e1cdf02e
FT
384 }
385 }
9dc25d3d 386 return(off);
e1cdf02e
FT
387}
388
389static int mtclose(void *cookie)
390{
391 struct stdiofd *d = cookie;
392
393 close(d->fd);
394 free(d);
395 return(0);
396}
397
398static cookie_io_functions_t iofuns = {
399 .read = mtread,
400 .write = mtwrite,
401 .close = mtclose,
402};
403
404FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
405{
406 struct stdiofd *d;
407 FILE *ret;
408
409 omalloc(d);
410 d->fd = fd;
411 d->sock = issock;
412 d->timeout = timeout;
413 ret = fopencookie(d, mode, iofuns);
414 if(!ret)
415 free(d);
416 else
417 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
418 return(ret);
419}