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;
51 static int epfd = -1, fdln = 0;
52 static int exitstatus;
53 static struct blocker **fdlist;
54 static typedbuf(struct timeentry) timeheap;
56 static int regfd(struct blocker *bl)
59 struct epoll_event evd;
61 memset(&evd, 0, sizeof(evd));
64 evd.events |= EPOLLIN;
66 evd.events |= EPOLLOUT;
70 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
71 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
74 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
77 if(fdlist[bl->fd] == NULL) {
78 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
79 /* XXX?! Whatever to do, really? */
80 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
84 for(o = fdlist[bl->fd]; o; o = o->n2) {
86 evd.events |= EPOLLIN;
88 evd.events |= EPOLLOUT;
90 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
91 /* XXX?! Whatever to do, really? */
92 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
96 bl->n2 = fdlist[bl->fd];
98 if(fdlist[bl->fd] != NULL)
99 fdlist[bl->fd]->p2 = bl;
105 static void remfd(struct blocker *bl)
108 struct epoll_event evd;
116 if(bl == fdlist[bl->fd])
117 fdlist[bl->fd] = bl->n2;
118 if(fdlist[bl->fd] == NULL) {
119 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
120 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
122 memset(&evd, 0, sizeof(evd));
124 evd.data.fd = bl->fd;
125 for(o = fdlist[bl->fd]; o; o = o->n2) {
127 evd.events |= EPOLLIN;
129 evd.events |= EPOLLOUT;
131 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
132 /* XXX?! Whatever to do, really? */
133 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
139 static void thraise(struct timeentry ent, int n)
145 if(timeheap.b[p].to <= ent.to)
147 timeheap.b[n] = timeheap.b[p];
148 timeheap.b[n].bl->thpos = n;
155 static void thlower(struct timeentry ent, int n)
163 if((c + 1 < timeheap.d) && (timeheap.b[c + 1].to < timeheap.b[c].to))
165 if(timeheap.b[c].to > ent.to)
167 timeheap.b[n] = timeheap.b[c];
168 timeheap.b[n].bl->thpos = n;
175 static void addtimeout(struct blocker *bl, time_t to)
177 sizebuf(timeheap, ++timeheap.d);
178 thraise((struct timeentry){.to = to, .bl = bl}, timeheap.d - 1);
181 static void deltimeout(struct blocker *bl)
183 struct timeentry ent;
186 if(bl->thpos == timeheap.d - 1) {
191 ent = timeheap.b[--timeheap.d];
192 if((n > 0) && (timeheap.b[(n - 1) >> 1].to > ent.to))
198 int block(int fd, int ev, time_t to)
207 if((epfd >= 0) && regfd(bl)) {
216 addtimeout(bl, bl->to = (time(NULL) + to));
233 struct blocker *bl, *nbl;
234 struct epoll_event evr[16];
235 int i, fd, nev, ev, toval;
239 epfd = epoll_create(128);
240 fcntl(epfd, F_SETFD, FD_CLOEXEC);
242 for(bl = blockers; bl; bl = nbl) {
247 while(blockers != NULL) {
251 else if(timeheap.b[0].to > now)
252 toval = (timeheap.b[0].to - now) * 1000;
257 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
260 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
261 /* To avoid CPU hogging in case it's bad, which it
267 for(i = 0; i < nev; i++) {
270 if(evr[i].events & EPOLLIN)
272 if(evr[i].events & EPOLLOUT)
274 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
276 for(bl = fdlist[fd]; bl; bl = nbl) {
278 if((ev < 0) || (ev & bl->ev))
283 while((timeheap.d > 0) && (timeheap.b[0].to <= now))
284 resume(timeheap.b[0].bl->th, 0);
286 for(bl = blockers; bl; bl = bl->n)
294 void exitioloop(int status)