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)
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 for(prev = 0, o = fdlist[bl->fd]; o; o = o->n2)
68 if((bl->ev & EV_READ) && !(prev & EV_READ)) {
69 evd = (struct kevent) {
72 .filter = EVFILT_READ,
74 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
75 /* XXX?! Whatever to do, really? */
76 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
80 if((bl->ev & EV_WRITE) && !(prev & EV_WRITE)) {
81 evd = (struct kevent) {
84 .filter = EVFILT_WRITE,
86 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
87 /* XXX?! Whatever to do, really? */
88 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
92 bl->n2 = fdlist[bl->fd];
94 if(fdlist[bl->fd] != NULL)
95 fdlist[bl->fd]->p2 = bl;
101 static void remfd(struct blocker *bl)
113 if(bl == fdlist[bl->fd])
114 fdlist[bl->fd] = bl->n2;
115 for(left = 0, o = fdlist[bl->fd]; o; o = o->n2)
117 if((bl->ev & EV_READ) && !(left & EV_READ)) {
118 evd = (struct kevent) {
121 .filter = EVFILT_READ,
123 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
124 /* XXX?! Whatever to do, really? */
125 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
128 if((bl->ev & EV_WRITE) && !(left & EV_WRITE)) {
129 evd = (struct kevent) {
132 .filter = EVFILT_WRITE,
134 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
135 /* XXX?! Whatever to do, really? */
136 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
142 static void thraise(struct blocker *bl, int n)
148 if(timeheap.b[p]->to <= bl->to)
150 timeheap.b[n] = timeheap.b[p];
151 timeheap.b[n]->thpos = n;
158 static void thlower(struct blocker *bl, int n)
166 if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to))
168 if(timeheap.b[c]->to > bl->to)
170 timeheap.b[n] = timeheap.b[c];
171 timeheap.b[n]->thpos = n;
178 static void addtimeout(struct blocker *bl, time_t to)
180 sizebuf(timeheap, ++timeheap.d);
181 thraise(bl, timeheap.d - 1);
184 static void deltimeout(struct blocker *bl)
188 if(bl->thpos == timeheap.d - 1) {
193 bl = timeheap.b[--timeheap.d];
194 if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to))
200 static int addblock(struct blocker *bl)
202 if((qfd >= 0) && regfd(bl))
209 addtimeout(bl, bl->to);
213 static void remblock(struct blocker *bl)
226 struct selected mblock(time_t to, int n, struct selected *spec)
229 struct blocker bls[n];
231 to = (to > 0)?(time(NULL) + to):0;
232 for(i = 0; i < n; i++) {
233 bls[i] = (struct blocker) {
240 if(addblock(&bls[i])) {
241 for(i--; i >= 0; i--)
243 return((struct selected){.fd = -1, .ev = -1});
247 for(i = 0; i < n; i++)
250 return((struct selected){.fd = -1, .ev = -1});
251 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
254 int block(int fd, int ev, time_t to)
259 bl = (struct blocker) {
263 .to = (to > 0)?(time(NULL) + to):0,
274 struct blocker *bl, *nbl;
275 struct kevent evs[16];
278 struct timespec *toval;
282 fcntl(qfd, F_SETFD, FD_CLOEXEC);
283 for(bl = blockers; bl; bl = nbl) {
288 while(blockers != NULL) {
290 toval = &(struct timespec){};
293 else if(timeheap.b[0]->to > now)
294 *toval = (struct timespec){.tv_sec = timeheap.b[0]->to - now};
296 *toval = (struct timespec){.tv_sec = 1};
299 nev = kevent(qfd, NULL, 0, evs, sizeof(evs) / sizeof(*evs), toval);
302 flog(LOG_CRIT, "ioloop: kevent errored out: %s", strerror(errno));
303 /* To avoid CPU hogging in case it's bad, which it
309 for(i = 0; i < nev; i++) {
310 fd = (int)evs[i].ident;
311 ev = (evs[i].filter == EVFILT_READ)?EV_READ:EV_WRITE;
312 for(bl = fdlist[fd]; bl; bl = nbl) {
319 resume(bl->th, bl->id);
325 while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) {
330 resume(bl->th, bl->id);
334 for(bl = blockers; bl; bl = bl->n)
341 void exitioloop(int status)