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 for(bl = blockers; bl; bl = nbl) {
188 while(blockers != NULL) {
190 for(bl = blockers; bl; bl = bl->n) {
191 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
197 else if(timeout > now)
198 toval = (timeout - now) * 1000;
201 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
204 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
205 /* To avoid CPU hogging in case it's bad, which it
211 for(i = 0; i < nev; i++) {
214 if(evr[i].events & EPOLLIN)
216 if(evr[i].events & EPOLLOUT)
218 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
220 for(bl = fdlist[fd]; bl; bl = nbl) {
222 if((ev < 0) || (ev & bl->ev))
227 for(bl = blockers; bl; bl = nbl) {
229 if((bl->to != 0) && (bl->to <= now))
240 struct blocker *n, *p;
247 int block(int fd, int ev, time_t to)
256 bl->to = time(NULL) + to;
276 fd_set rfds, wfds, efds;
277 struct blocker *bl, *nbl;
278 struct timeval toval;
283 while(blockers != NULL) {
290 for(bl = blockers; bl; bl = bl->n) {
292 FD_SET(bl->fd, &rfds);
293 if(bl->ev & EV_WRITE)
294 FD_SET(bl->fd, &wfds);
295 FD_SET(bl->fd, &efds);
298 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
301 toval.tv_sec = timeout - now;
303 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
306 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
307 /* To avoid CPU hogging in case it's bad, which it
313 for(bl = blockers; bl; bl = nbl) {
316 if(FD_ISSET(bl->fd, &rfds))
318 if(FD_ISSET(bl->fd, &wfds))
320 if(FD_ISSET(bl->fd, &efds))
322 if((ev < 0) || (ev & bl->ev))
324 else if((bl->to != 0) && (bl->to <= now))
338 static ssize_t mtread(void *cookie, char *buf, size_t len)
340 struct stdiofd *d = cookie;
345 ret = read(d->fd, buf, len);
346 if((ret < 0) && (errno == EAGAIN)) {
347 ev = block(d->fd, EV_READ, d->timeout);
349 /* If we just go on, we should get the real error. */
363 static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
365 struct stdiofd *d = cookie;
373 ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
375 ret = write(d->fd, buf + off, len - off);
377 if(errno == EAGAIN) {
378 ev = block(d->fd, EV_WRITE, d->timeout);
380 /* If we just go on, we should get the real error. */
398 static int mtclose(void *cookie)
400 struct stdiofd *d = cookie;
407 static cookie_io_functions_t iofuns = {
413 FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
421 d->timeout = timeout;
422 ret = fopencookie(d, mode, iofuns);
426 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);