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