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/event.h>
35 static struct blocker *blockers;
38 struct blocker *n, *p, *n2, *p2;
46 static int qfd = -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)
61 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
62 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
65 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
68 for(prev = 0, o = fdlist[bl->fd]; o; o = o->n2)
70 if((bl->ev & EV_READ) && !(prev & EV_READ)) {
71 evd = (struct kevent) {
74 .filter = EVFILT_READ,
76 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
77 /* XXX?! Whatever to do, really? */
78 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
82 if((bl->ev & EV_WRITE) && !(prev & EV_WRITE)) {
83 evd = (struct kevent) {
86 .filter = EVFILT_WRITE,
88 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
89 /* XXX?! Whatever to do, really? */
90 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
94 bl->n2 = fdlist[bl->fd];
96 if(fdlist[bl->fd] != NULL)
97 fdlist[bl->fd]->p2 = bl;
103 static void remfd(struct blocker *bl)
115 if(bl == fdlist[bl->fd])
116 fdlist[bl->fd] = bl->n2;
117 for(left = 0, o = fdlist[bl->fd]; o; o = o->n2)
119 if((bl->ev & EV_READ) && !(left & EV_READ)) {
120 evd = (struct kevent) {
123 .filter = EVFILT_READ,
125 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
126 /* XXX?! Whatever to do, really? */
127 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
130 if((bl->ev & EV_WRITE) && !(left & EV_WRITE)) {
131 evd = (struct kevent) {
134 .filter = EVFILT_WRITE,
136 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
137 /* XXX?! Whatever to do, really? */
138 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
144 static void thraise(struct blocker *bl, int n)
150 if(timeheap.b[p]->to <= bl->to)
152 timeheap.b[n] = timeheap.b[p];
153 timeheap.b[n]->thpos = n;
160 static void thlower(struct blocker *bl, int n)
168 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
170 if(timeheap.b[c]->to > bl->to)
172 timeheap.b[n] = timeheap.b[c];
173 timeheap.b[n]->thpos = n;
180 static void addtimeout(struct blocker *bl, time_t to)
182 sizebuf(timeheap, ++timeheap.d);
183 thraise(bl, timeheap.d - 1);
186 static void deltimeout(struct blocker *bl)
190 if(bl->thpos == timeheap.d - 1) {
195 bl = timeheap.b[--timeheap.d];
196 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
202 static int addblock(struct blocker *bl)
204 if((qfd >= 0) && regfd(bl))
211 addtimeout(bl, bl->to);
215 static void remblock(struct blocker *bl)
228 struct selected mblock(time_t to, int n, struct selected *spec)
231 struct blocker bls[n];
233 to = (to > 0)?(time(NULL) + to):0;
234 for(i = 0; i < n; i++) {
235 bls[i] = (struct blocker) {
242 if(addblock(&bls[i])) {
243 for(i--; i >= 0; i--)
245 return((struct selected){.fd = -1, .ev = -1});
249 for(i = 0; i < n; i++)
252 return((struct selected){.fd = -1, .ev = -1});
253 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
256 int block(int fd, int ev, time_t to)
261 bl = (struct blocker) {
265 .to = (to > 0)?(time(NULL) + to):0,
276 struct blocker *bl, *nbl;
277 struct kevent evs[16];
280 struct timespec *toval;
284 fcntl(qfd, F_SETFD, FD_CLOEXEC);
285 for(bl = blockers; bl; bl = nbl) {
290 while(blockers != NULL) {
292 toval = &(struct timespec){};
295 else if(timeheap.b[0]->to > now)
296 *toval = (struct timespec){.tv_sec = timeheap.b[0]->to - now};
298 *toval = (struct timespec){.tv_sec = 1};
301 nev = kevent(qfd, NULL, 0, evs, sizeof(evs) / sizeof(*evs), toval);
304 flog(LOG_CRIT, "ioloop: kevent errored out: %s", strerror(errno));
305 /* To avoid CPU hogging in case it's bad, which it
311 for(i = 0; i < nev; i++) {
312 fd = (int)evs[i].ident;
313 ev = (evs[i].filter == EVFILT_READ)?EV_READ:EV_WRITE;
314 for(bl = fdlist[fd]; bl; bl = nbl) {
321 resume(bl->th, bl->id);
327 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
332 resume(bl->th, bl->id);
336 for(bl = blockers; bl; bl = bl->n)
343 void exitioloop(int status)