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;
46 static int epfd = -1, fdln = 0;
47 static int exitstatus;
48 static struct blocker **fdlist;
49 static typedbuf(struct blocker *) timeheap;
51 static int regfd(struct blocker *bl)
54 struct epoll_event evd;
56 memset(&evd, 0, sizeof(evd));
59 evd.events |= EPOLLIN;
61 evd.events |= EPOLLOUT;
65 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
66 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
69 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
72 if(fdlist[bl->fd] == NULL) {
73 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
74 /* XXX?! Whatever to do, really? */
75 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
79 for(o = fdlist[bl->fd]; o; o = o->n2) {
81 evd.events |= EPOLLIN;
83 evd.events |= EPOLLOUT;
85 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
86 /* XXX?! Whatever to do, really? */
87 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
91 bl->n2 = fdlist[bl->fd];
93 if(fdlist[bl->fd] != NULL)
94 fdlist[bl->fd]->p2 = bl;
100 static void remfd(struct blocker *bl)
103 struct epoll_event evd;
111 if(bl == fdlist[bl->fd])
112 fdlist[bl->fd] = bl->n2;
113 if(fdlist[bl->fd] == NULL) {
114 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
115 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
117 memset(&evd, 0, sizeof(evd));
119 evd.data.fd = bl->fd;
120 for(o = fdlist[bl->fd]; o; o = o->n2) {
122 evd.events |= EPOLLIN;
124 evd.events |= EPOLLOUT;
126 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
127 /* XXX?! Whatever to do, really? */
128 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
134 static void thraise(struct blocker *bl, int n)
140 if(timeheap.b[p]->to <= bl->to)
142 timeheap.b[n] = timeheap.b[p];
143 timeheap.b[n]->thpos = n;
150 static void thlower(struct blocker *bl, int n)
158 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
160 if(timeheap.b[c]->to > bl->to)
162 timeheap.b[n] = timeheap.b[c];
163 timeheap.b[n]->thpos = n;
170 static void addtimeout(struct blocker *bl, time_t to)
172 sizebuf(timeheap, ++timeheap.d);
173 thraise(bl, timeheap.d - 1);
176 static void deltimeout(struct blocker *bl)
180 if(bl->thpos == timeheap.d - 1) {
185 bl = timeheap.b[--timeheap.d];
186 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
192 static int addblock(struct blocker *bl)
194 if((epfd >= 0) && regfd(bl))
201 addtimeout(bl, bl->to);
205 static void remblock(struct blocker *bl)
218 struct selected mblock(time_t to, int n, struct selected *spec)
221 struct blocker bls[n];
223 to = (to > 0)?(time(NULL) + to):0;
224 for(i = 0; i < n; i++) {
225 bls[i] = (struct blocker) {
232 if(addblock(&bls[i])) {
233 for(i--; i >= 0; i--)
235 return((struct selected){.fd = -1, .ev = -1});
239 for(i = 0; i < n; i++)
242 return((struct selected){.fd = -1, .ev = -1});
243 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
246 int block(int fd, int ev, time_t to)
251 bl = (struct blocker) {
255 .to = (to > 0)?(time(NULL) + to):0,
267 struct blocker *bl, *nbl;
268 struct epoll_event evr[16];
269 int i, fd, nev, ev, toval;
273 epfd = epoll_create(128);
274 fcntl(epfd, F_SETFD, FD_CLOEXEC);
275 for(bl = blockers; bl; bl = nbl) {
280 while(blockers != NULL) {
284 else if(timeheap.b[0]->to > now)
285 toval = (timeheap.b[0]->to - now) * 1000;
290 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
293 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
294 /* To avoid CPU hogging in case it's bad, which it
300 for(i = 0; i < nev; i++) {
303 if(evr[i].events & EPOLLIN)
305 if(evr[i].events & EPOLLOUT)
307 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
309 for(bl = fdlist[fd]; bl; bl = nbl) {
311 if((ev < 0) || (ev & bl->ev)) {
316 resume(bl->th, bl->id);
322 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
327 resume(bl->th, bl->id);
331 for(bl = blockers; bl; bl = bl->n)
338 void exitioloop(int status)