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/>.
24 #include <sys/epoll.h>
35 static struct blocker *blockers;
38 struct blocker *n, *p, *n2, *p2;
45 static int epfd = -1, fdln = 0;
46 static struct blocker **fdlist;
48 static int regfd(struct blocker *bl)
51 struct epoll_event evd;
53 memset(&evd, 0, sizeof(evd));
56 evd.events |= EPOLLIN;
58 evd.events |= EPOLLOUT;
62 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
63 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
66 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
69 if(fdlist[bl->fd] == NULL) {
70 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
71 /* XXX?! Whatever to do, really? */
72 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
76 for(o = fdlist[bl->fd]; o; o = o->n2) {
78 evd.events |= EPOLLIN;
80 evd.events |= EPOLLOUT;
82 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
83 /* XXX?! Whatever to do, really? */
84 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
88 bl->n2 = fdlist[bl->fd];
90 if(fdlist[bl->fd] != NULL)
91 fdlist[bl->fd]->p2 = bl;
97 static void remfd(struct blocker *bl)
100 struct epoll_event evd;
108 if(bl == fdlist[bl->fd])
109 fdlist[bl->fd] = bl->n2;
110 if(fdlist[bl->fd] == NULL) {
111 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
112 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
114 memset(&evd, 0, sizeof(evd));
116 evd.data.fd = bl->fd;
117 for(o = fdlist[bl->fd]; o; o = o->n2) {
119 evd.events |= EPOLLIN;
121 evd.events |= EPOLLOUT;
123 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
124 /* XXX?! Whatever to do, really? */
125 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
130 int block(int fd, int ev, time_t to)
139 bl->to = time(NULL) + to;
141 if((epfd >= 0) && regfd(bl)) {
163 struct blocker *bl, *nbl;
164 struct epoll_event evr[16];
165 int i, fd, nev, ev, toval;
168 epfd = epoll_create(128);
169 fcntl(epfd, F_SETFD, FD_CLOEXEC);
170 for(bl = blockers; bl; bl = nbl) {
175 while(blockers != NULL) {
177 for(bl = blockers; bl; bl = bl->n) {
178 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
184 else if(timeout > now)
185 toval = (timeout - now) * 1000;
188 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
191 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
192 /* To avoid CPU hogging in case it's bad, which it
198 for(i = 0; i < nev; i++) {
201 if(evr[i].events & EPOLLIN)
203 if(evr[i].events & EPOLLOUT)
205 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
207 for(bl = fdlist[fd]; bl; bl = nbl) {
209 if((ev < 0) || (ev & bl->ev))
214 for(bl = blockers; bl; bl = nbl) {
216 if((bl->to != 0) && (bl->to <= now))