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;
45 static int qfd = -1, fdln = 0;
46 static int exitstatus;
47 static struct blocker **fdlist;
49 static int regfd(struct blocker *bl)
57 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
58 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
61 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
64 for(prev = 0, o = fdlist[bl->fd]; o; o = o->n2)
66 if((bl->ev & EV_READ) && !(prev & EV_READ)) {
67 evd = (struct kevent) {
70 .filter = EVFILT_READ,
72 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
73 /* XXX?! Whatever to do, really? */
74 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
78 if((bl->ev & EV_WRITE) && !(prev & EV_WRITE)) {
79 evd = (struct kevent) {
82 .filter = EVFILT_WRITE,
84 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
85 /* XXX?! Whatever to do, really? */
86 flog(LOG_ERR, "kevent(EV_ADD, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
90 bl->n2 = fdlist[bl->fd];
92 if(fdlist[bl->fd] != NULL)
93 fdlist[bl->fd]->p2 = bl;
99 static void remfd(struct blocker *bl)
111 if(bl == fdlist[bl->fd])
112 fdlist[bl->fd] = bl->n2;
113 for(left = 0, o = fdlist[bl->fd]; o; o = o->n2)
115 if((bl->ev & EV_READ) && !(left & EV_READ)) {
116 evd = (struct kevent) {
119 .filter = EVFILT_READ,
121 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
122 /* XXX?! Whatever to do, really? */
123 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_READ) on fd %i: %s", bl->fd, strerror(errno));
126 if((bl->ev & EV_WRITE) && !(left & EV_WRITE)) {
127 evd = (struct kevent) {
130 .filter = EVFILT_WRITE,
132 if(kevent(qfd, &evd, 1, NULL, 0, NULL) < 0) {
133 /* XXX?! Whatever to do, really? */
134 flog(LOG_ERR, "kevent(EV_DELETE, EVFILT_WRITE) on fd %i: %s", bl->fd, strerror(errno));
140 int block(int fd, int ev, time_t to)
149 bl->to = time(NULL) + to;
151 if((qfd >= 0) && regfd(bl)) {
173 struct blocker *bl, *nbl;
174 struct kevent evs[16];
177 struct timespec *toval;
181 fcntl(qfd, F_SETFD, FD_CLOEXEC);
182 for(bl = blockers; bl; bl = nbl) {
187 while(blockers != NULL) {
189 for(bl = blockers; bl; bl = bl->n) {
190 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
196 else if(timeout > now)
197 toval = &(struct timespec){.tv_sec = timeout - now};
199 toval = &(struct timespec){.tv_sec = 1};
202 nev = kevent(qfd, NULL, 0, evs, sizeof(evs) / sizeof(*evs), toval);
205 flog(LOG_CRIT, "ioloop: kevent errored out: %s", strerror(errno));
206 /* To avoid CPU hogging in case it's bad, which it
212 for(i = 0; i < nev; i++) {
213 fd = (int)evs[i].ident;
214 ev = (evs[i].filter == EVFILT_READ)?EV_READ:EV_WRITE;
215 for(bl = fdlist[fd]; bl; bl = nbl) {
222 for(bl = blockers; bl; bl = nbl) {
224 if((bl->to != 0) && (bl->to <= now))
228 for(bl = blockers; bl; bl = bl->n)
235 void exitioloop(int status)