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;
58 memset(&evd, 0, sizeof(evd));
61 evd.events |= EPOLLIN;
63 evd.events |= EPOLLOUT;
67 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
68 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
71 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
74 if(fdlist[bl->fd] == NULL) {
75 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
76 /* XXX?! Whatever to do, really? */
77 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
81 for(o = fdlist[bl->fd]; o; o = o->n2) {
83 evd.events |= EPOLLIN;
85 evd.events |= EPOLLOUT;
87 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
88 /* XXX?! Whatever to do, really? */
89 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
93 bl->n2 = fdlist[bl->fd];
95 if(fdlist[bl->fd] != NULL)
96 fdlist[bl->fd]->p2 = bl;
102 static void remfd(struct blocker *bl)
105 struct epoll_event evd;
113 if(bl == fdlist[bl->fd])
114 fdlist[bl->fd] = bl->n2;
115 if(fdlist[bl->fd] == NULL) {
116 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
117 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
119 memset(&evd, 0, sizeof(evd));
121 evd.data.fd = bl->fd;
122 for(o = fdlist[bl->fd]; o; o = o->n2) {
124 evd.events |= EPOLLIN;
126 evd.events |= EPOLLOUT;
128 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
129 /* XXX?! Whatever to do, really? */
130 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
136 static void thraise(struct blocker *bl, int n)
142 if(timeheap.b[p]->to <= bl->to)
144 timeheap.b[n] = timeheap.b[p];
145 timeheap.b[n]->thpos = n;
152 static void thlower(struct blocker *bl, int n)
160 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
162 if(timeheap.b[c]->to > bl->to)
164 timeheap.b[n] = timeheap.b[c];
165 timeheap.b[n]->thpos = n;
172 static void addtimeout(struct blocker *bl, time_t to)
174 sizebuf(timeheap, ++timeheap.d);
175 thraise(bl, timeheap.d - 1);
178 static void deltimeout(struct blocker *bl)
182 if(bl->thpos == timeheap.d - 1) {
187 bl = timeheap.b[--timeheap.d];
188 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
194 static int addblock(struct blocker *bl)
196 if((epfd >= 0) && regfd(bl))
203 addtimeout(bl, bl->to);
207 static void remblock(struct blocker *bl)
220 struct selected mblock(time_t to, int n, struct selected *spec)
223 struct blocker bls[n];
225 to = (to > 0)?(time(NULL) + to):0;
226 for(i = 0; i < n; i++) {
227 bls[i] = (struct blocker) {
234 if(addblock(&bls[i])) {
235 for(i--; i >= 0; i--)
237 return((struct selected){.fd = -1, .ev = -1});
241 for(i = 0; i < n; i++)
244 return((struct selected){.fd = -1, .ev = -1});
245 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
248 int block(int fd, int ev, time_t to)
253 bl = (struct blocker) {
257 .to = (to > 0)?(time(NULL) + to):0,
269 struct blocker *bl, *nbl;
270 struct epoll_event evr[16];
271 int i, fd, nev, ev, toval;
275 epfd = epoll_create(128);
276 fcntl(epfd, F_SETFD, FD_CLOEXEC);
277 for(bl = blockers; bl; bl = nbl) {
282 while(blockers != NULL) {
286 else if(timeheap.b[0]->to > now)
287 toval = (timeheap.b[0]->to - now) * 1000;
292 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
295 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
296 /* To avoid CPU hogging in case it's bad, which it
302 for(i = 0; i < nev; i++) {
305 if(evr[i].events & EPOLLIN)
307 if(evr[i].events & EPOLLOUT)
309 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
311 for(bl = fdlist[fd]; bl; bl = nbl) {
313 if((ev < 0) || (ev & bl->ev)) {
318 resume(bl->th, bl->id);
324 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
329 resume(bl->th, bl->id);
333 for(bl = blockers; bl; bl = bl->n)
340 void exitioloop(int status)