2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
26 #include <sys/select.h>
27 #include <sys/socket.h>
37 static struct blocker *blockers;
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
46 * Scroll down to #else for the normal select loop.
49 #include <sys/epoll.h>
52 struct blocker *n, *p, *n2, *p2;
59 static int epfd = -1, fdln = 0;
60 static struct blocker **fdlist;
62 static int regfd(struct blocker *bl)
65 struct epoll_event evd;
69 evd.events |= EPOLLIN;
71 evd.events |= EPOLLOUT;
75 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
76 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
79 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
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));
89 for(o = fdlist[bl->fd]; o; o = o->n2) {
91 evd.events |= EPOLLIN;
93 evd.events |= EPOLLOUT;
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));
101 bl->n2 = fdlist[bl->fd];
103 if(fdlist[bl->fd] != NULL)
104 fdlist[bl->fd]->p2 = bl;
110 static void remfd(struct blocker *bl)
113 struct epoll_event evd;
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));
128 evd.data.fd = bl->fd;
129 for(o = fdlist[bl->fd]; o; o = o->n2) {
131 evd.events |= EPOLLIN;
133 evd.events |= EPOLLOUT;
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));
142 int block(int fd, int ev, time_t to)
151 bl->to = time(NULL) + to;
153 if((epfd >= 0) && regfd(bl)) {
175 struct blocker *bl, *nbl;
176 struct epoll_event evr[16];
180 epfd = epoll_create(128);
181 for(bl = blockers; bl; bl = nbl) {
186 while(blockers != NULL) {
188 for(bl = blockers; bl; bl = bl->n) {
189 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
193 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), timeout?((timeout - now) * 1000):-1);
196 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
197 /* To avoid CPU hogging in case it's bad, which it
204 for(i = 0; i < nev; i++) {
207 if(evr[i].events & EPOLLIN)
209 if(evr[i].events & EPOLLOUT)
211 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
213 for(bl = fdlist[fd]; bl; bl = nbl) {
215 if((ev < 0) || (ev & bl->ev))
217 else if((bl->to != 0) && (bl->to <= now))
229 struct blocker *n, *p;
236 int block(int fd, int ev, time_t to)
245 bl->to = time(NULL) + to;
265 fd_set rfds, wfds, efds;
266 struct blocker *bl, *nbl;
267 struct timeval toval;
272 while(blockers != NULL) {
279 for(bl = blockers; bl; bl = bl->n) {
281 FD_SET(bl->fd, &rfds);
282 if(bl->ev & EV_WRITE)
283 FD_SET(bl->fd, &wfds);
284 FD_SET(bl->fd, &efds);
287 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
290 toval.tv_sec = timeout - now;
292 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
295 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
296 /* To avoid CPU hogging in case it's bad, which it
302 for(bl = blockers; bl; bl = nbl) {
305 if(FD_ISSET(bl->fd, &rfds))
307 if(FD_ISSET(bl->fd, &wfds))
309 if(FD_ISSET(bl->fd, &efds))
311 if((ev < 0) || (ev & bl->ev))
313 else if((bl->to != 0) && (bl->to <= now))
327 static ssize_t mtread(void *cookie, char *buf, size_t len)
329 struct stdiofd *d = cookie;
334 ret = read(d->fd, buf, len);
335 if((ret < 0) && (errno == EAGAIN)) {
336 ev = block(d->fd, EV_READ, d->timeout);
338 /* If we just go on, we should get the real error. */
352 static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
354 struct stdiofd *d = cookie;
362 ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
364 ret = write(d->fd, buf + off, len - off);
366 if(errno == EAGAIN) {
367 ev = block(d->fd, EV_WRITE, d->timeout);
369 /* If we just go on, we should get the real error. */
387 static int mtclose(void *cookie)
389 struct stdiofd *d = cookie;
396 static cookie_io_functions_t iofuns = {
402 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
410 d->timeout = timeout;
411 ret = fopencookie(d, mode, iofuns);
415 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);