Try to use FD_CLOEXEC instead of mass-closing everywhere.
[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     memset(&evd, 0, sizeof(evd));
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
111 static 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 {
128         memset(&evd, 0, sizeof(evd));
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
144 int 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
175 void ioloop(void)
176 {
177     struct blocker *bl, *nbl;
178     struct epoll_event evr[16];
179     int i, fd, nev, ev, toval;
180     time_t now, timeout;
181     
182     epfd = epoll_create(128);
183     fcntl(epfd, F_SETFD, FD_CLOEXEC);
184     for(bl = blockers; bl; bl = nbl) {
185         nbl = bl->n;
186         if(regfd(bl))
187             resume(bl->th, -1);
188     }
189     while(blockers != NULL) {
190         timeout = 0;
191         for(bl = blockers; bl; bl = bl->n) {
192             if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
193                 timeout = bl->to;
194         }
195         now = time(NULL);
196         if(timeout == 0)
197             toval = -1;
198         else if(timeout > now)
199             toval = (timeout - now) * 1000;
200         else
201             toval = 1000;
202         nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
203         if(nev < 0) {
204             if(errno != EINTR) {
205                 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
206                 /* To avoid CPU hogging in case it's bad, which it
207                  * probably is. */
208                 sleep(1);
209             }
210             continue;
211         }
212         for(i = 0; i < nev; i++) {
213             fd = evr[i].data.fd;
214             ev = 0;
215             if(evr[i].events & EPOLLIN)
216                 ev |= EV_READ;
217             if(evr[i].events & EPOLLOUT)
218                 ev |= EV_WRITE;
219             if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
220                 ev = -1;
221             for(bl = fdlist[fd]; bl; bl = nbl) {
222                 nbl = bl->n2;
223                 if((ev < 0) || (ev & bl->ev))
224                     resume(bl->th, ev);
225             }
226         }
227         now = time(NULL);
228         for(bl = blockers; bl; bl = nbl) {
229             nbl = bl->n;
230             if((bl->to != 0) && (bl->to <= now))
231                 resume(bl->th, 0);
232         }
233     }
234     close(epfd);
235     epfd = -1;
236 }
237
238 #else
239
240 struct blocker {
241     struct blocker *n, *p;
242     int fd;
243     int ev;
244     time_t to;
245     struct muth *th;
246 };
247
248 int block(int fd, int ev, time_t to)
249 {
250     struct blocker *bl;
251     int rv;
252     
253     omalloc(bl);
254     bl->fd = fd;
255     bl->ev = ev;
256     if(to > 0)
257         bl->to = time(NULL) + to;
258     bl->th = current;
259     bl->n = blockers;
260     if(blockers)
261         blockers->p = bl;
262     blockers = bl;
263     rv = yield();
264     if(bl->n)
265         bl->n->p = bl->p;
266     if(bl->p)
267         bl->p->n = bl->n;
268     if(bl == blockers)
269         blockers = bl->n;
270     free(bl);
271     return(rv);
272 }
273
274 void ioloop(void)
275 {
276     int ret;
277     fd_set rfds, wfds, efds;
278     struct blocker *bl, *nbl;
279     struct timeval toval;
280     time_t now, timeout;
281     int maxfd;
282     int ev;
283     
284     while(blockers != NULL) {
285         FD_ZERO(&rfds);
286         FD_ZERO(&wfds);
287         FD_ZERO(&efds);
288         maxfd = 0;
289         now = time(NULL);
290         timeout = 0;
291         for(bl = blockers; bl; bl = bl->n) {
292             if(bl->ev & EV_READ)
293                 FD_SET(bl->fd, &rfds);
294             if(bl->ev & EV_WRITE)
295                 FD_SET(bl->fd, &wfds);
296             FD_SET(bl->fd, &efds);
297             if(bl->fd > maxfd)
298                 maxfd = bl->fd;
299             if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
300                 timeout = bl->to;
301         }
302         toval.tv_sec = timeout - now;
303         toval.tv_usec = 0;
304         ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
305         if(ret < 0) {
306             if(errno != EINTR) {
307                 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
308                 /* To avoid CPU hogging in case it's bad, which it
309                  * probably is. */
310                 sleep(1);
311             }
312         }
313         now = time(NULL);
314         for(bl = blockers; bl; bl = nbl) {
315             nbl = bl->n;
316             ev = 0;
317             if(FD_ISSET(bl->fd, &rfds))
318                 ev |= EV_READ;
319             if(FD_ISSET(bl->fd, &wfds))
320                 ev |= EV_WRITE;
321             if(FD_ISSET(bl->fd, &efds))
322                 ev = -1;
323             if((ev < 0) || (ev & bl->ev))
324                 resume(bl->th, ev);
325             else if((bl->to != 0) && (bl->to <= now))
326                 resume(bl->th, 0);
327         }
328     }
329 }
330
331 #endif
332
333 struct stdiofd {
334     int fd;
335     int sock;
336     int timeout;
337 };
338
339 static ssize_t mtread(void *cookie, char *buf, size_t len)
340 {
341     struct stdiofd *d = cookie;
342     int ev;
343     ssize_t ret;
344     
345     while(1) {
346         ret = read(d->fd, buf, len);
347         if((ret < 0) && (errno == EAGAIN)) {
348             ev = block(d->fd, EV_READ, d->timeout);
349             if(ev < 0) {
350                 /* If we just go on, we should get the real error. */
351                 continue;
352             } else if(ev == 0) {
353                 errno = ETIMEDOUT;
354                 return(-1);
355             } else {
356                 continue;
357             }
358         } else {
359             return(ret);
360         }
361     }
362 }
363
364 static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
365 {
366     struct stdiofd *d = cookie;
367     int ev;
368     size_t off;
369     ssize_t ret;
370     
371     off = 0;
372     while(off < len) {
373         if(d->sock)
374             ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
375         else
376             ret = write(d->fd, buf + off, len - off);
377         if(ret < 0) {
378             if(errno == EAGAIN) {
379                 ev = block(d->fd, EV_WRITE, d->timeout);
380                 if(ev < 0) {
381                     /* If we just go on, we should get the real error. */
382                     continue;
383                 } else if(ev == 0) {
384                     errno = ETIMEDOUT;
385                     return(off);
386                 } else {
387                     continue;
388                 }
389             } else {
390                 return(off);
391             }
392         } else {
393             off += ret;
394         }
395     }
396     return(off);
397 }
398
399 static int mtclose(void *cookie)
400 {
401     struct stdiofd *d = cookie;
402     
403     close(d->fd);
404     free(d);
405     return(0);
406 }
407
408 static cookie_io_functions_t iofuns = {
409     .read = mtread,
410     .write = mtwrite,
411     .close = mtclose,
412 };
413
414 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
415 {
416     struct stdiofd *d;
417     FILE *ret;
418     
419     omalloc(d);
420     d->fd = fd;
421     d->sock = issock;
422     d->timeout = timeout;
423     ret = fopencookie(d, mode, iofuns);
424     if(!ret)
425         free(d);
426     else
427         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
428     return(ret);
429 }