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/>.
21 #include <sys/epoll.h>
32 static struct blocker *blockers;
35 struct blocker *n, *p, *n2, *p2;
42 static int epfd = -1, fdln = 0;
43 static struct blocker **fdlist;
45 static int regfd(struct blocker *bl)
48 struct epoll_event evd;
50 memset(&evd, 0, sizeof(evd));
53 evd.events |= EPOLLIN;
55 evd.events |= EPOLLOUT;
59 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
60 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
63 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
66 if(fdlist[bl->fd] == NULL) {
67 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
68 /* XXX?! Whatever to do, really? */
69 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
73 for(o = fdlist[bl->fd]; o; o = o->n2) {
75 evd.events |= EPOLLIN;
77 evd.events |= EPOLLOUT;
79 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
80 /* XXX?! Whatever to do, really? */
81 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
85 bl->n2 = fdlist[bl->fd];
87 if(fdlist[bl->fd] != NULL)
88 fdlist[bl->fd]->p2 = bl;
94 static void remfd(struct blocker *bl)
97 struct epoll_event evd;
105 if(bl == fdlist[bl->fd])
106 fdlist[bl->fd] = bl->n2;
107 if(fdlist[bl->fd] == NULL) {
108 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
109 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
111 memset(&evd, 0, sizeof(evd));
113 evd.data.fd = bl->fd;
114 for(o = fdlist[bl->fd]; o; o = o->n2) {
116 evd.events |= EPOLLIN;
118 evd.events |= EPOLLOUT;
120 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
121 /* XXX?! Whatever to do, really? */
122 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
127 int block(int fd, int ev, time_t to)
136 bl->to = time(NULL) + to;
138 if((epfd >= 0) && regfd(bl)) {
160 struct blocker *bl, *nbl;
161 struct epoll_event evr[16];
162 int i, fd, nev, ev, toval;
165 epfd = epoll_create(128);
166 for(bl = blockers; bl; bl = nbl) {
171 while(blockers != NULL) {
173 for(bl = blockers; bl; bl = bl->n) {
174 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
180 else if(timeout > now)
181 toval = (timeout - now) * 1000;
184 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
187 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
188 /* To avoid CPU hogging in case it's bad, which it
194 for(i = 0; i < nev; i++) {
197 if(evr[i].events & EPOLLIN)
199 if(evr[i].events & EPOLLOUT)
201 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
203 for(bl = fdlist[fd]; bl; bl = nbl) {
205 if((ev < 0) || (ev & bl->ev))
210 for(bl = blockers; bl; bl = nbl) {
212 if((bl->to != 0) && (bl->to <= now))