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;
67 memset(&evd, 0, sizeof(evd));
70 evd.events |= EPOLLIN;
72 evd.events |= EPOLLOUT;
76 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
77 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
80 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
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));
90 for(o = fdlist[bl->fd]; o; o = o->n2) {
92 evd.events |= EPOLLIN;
94 evd.events |= EPOLLOUT;
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));
102 bl->n2 = fdlist[bl->fd];
104 if(fdlist[bl->fd] != NULL)
105 fdlist[bl->fd]->p2 = bl;
111 static void remfd(struct blocker *bl)
114 struct epoll_event evd;
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));
128 memset(&evd, 0, sizeof(evd));
130 evd.data.fd = bl->fd;
131 for(o = fdlist[bl->fd]; o; o = o->n2) {
133 evd.events |= EPOLLIN;
135 evd.events |= EPOLLOUT;
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));
144 int block(int fd, int ev, time_t to)
153 bl->to = time(NULL) + to;
155 if((epfd >= 0) && regfd(bl)) {
177 struct blocker *bl, *nbl;
178 struct epoll_event evr[16];
179 int i, fd, nev, ev, toval;
182 epfd = epoll_create(128);
183 fcntl(epfd, F_SETFD, FD_CLOEXEC);
184 for(bl = blockers; bl; bl = nbl) {
189 while(blockers != NULL) {
191 for(bl = blockers; bl; bl = bl->n) {
192 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
198 else if(timeout > now)
199 toval = (timeout - now) * 1000;
202 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
205 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
206 /* To avoid CPU hogging in case it's bad, which it
212 for(i = 0; i < nev; i++) {
215 if(evr[i].events & EPOLLIN)
217 if(evr[i].events & EPOLLOUT)
219 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
221 for(bl = fdlist[fd]; bl; bl = nbl) {
223 if((ev < 0) || (ev & bl->ev))
228 for(bl = blockers; bl; bl = nbl) {
230 if((bl->to != 0) && (bl->to <= now))
241 struct blocker *n, *p;
248 int block(int fd, int ev, time_t to)
257 bl->to = time(NULL) + to;
277 fd_set rfds, wfds, efds;
278 struct blocker *bl, *nbl;
279 struct timeval toval;
284 while(blockers != NULL) {
291 for(bl = blockers; bl; bl = bl->n) {
293 FD_SET(bl->fd, &rfds);
294 if(bl->ev & EV_WRITE)
295 FD_SET(bl->fd, &wfds);
296 FD_SET(bl->fd, &efds);
299 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
302 toval.tv_sec = timeout - now;
304 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
307 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
308 /* To avoid CPU hogging in case it's bad, which it
314 for(bl = blockers; bl; bl = nbl) {
317 if(FD_ISSET(bl->fd, &rfds))
319 if(FD_ISSET(bl->fd, &wfds))
321 if(FD_ISSET(bl->fd, &efds))
323 if((ev < 0) || (ev & bl->ev))
325 else if((bl->to != 0) && (bl->to <= now))
339 static ssize_t mtread(void *cookie, char *buf, size_t len)
341 struct stdiofd *d = cookie;
346 ret = read(d->fd, buf, len);
347 if((ret < 0) && (errno == EAGAIN)) {
348 ev = block(d->fd, EV_READ, d->timeout);
350 /* If we just go on, we should get the real error. */
364 static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
366 struct stdiofd *d = cookie;
374 ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
376 ret = write(d->fd, buf + off, len - off);
378 if(errno == EAGAIN) {
379 ev = block(d->fd, EV_WRITE, d->timeout);
381 /* If we just go on, we should get the real error. */
399 static int mtclose(void *cookie)
401 struct stdiofd *d = cookie;
408 static cookie_io_functions_t iofuns = {
414 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
422 d->timeout = timeout;
423 ret = fopencookie(d, mode, iofuns);
427 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);