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 static int addblock(struct blocker *bl)
142 if((qfd >= 0) && regfd(bl))
151 static void remblock(struct blocker *bl)
162 struct selected mblock(time_t to, int n, struct selected *spec)
165 struct blocker bls[n];
167 to = (to > 0)?(time(NULL) + to):0;
168 for(i = 0; i < n; i++) {
169 bls[i] = (struct blocker) {
176 if(addblock(&bls[i])) {
177 for(i--; i >= 0; i--)
179 return((struct selected){.fd = -1, .ev = -1});
183 for(i = 0; i < n; i++)
186 return((struct selected){.fd = -1, .ev = -1});
187 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
190 int block(int fd, int ev, time_t to)
195 bl = (struct blocker) {
199 .to = (to > 0)?(time(NULL) + to):0,
210 struct blocker *bl, *nbl;
211 struct kevent evs[16];
214 struct timespec *toval;
218 fcntl(qfd, F_SETFD, FD_CLOEXEC);
219 for(bl = blockers; bl; bl = nbl) {
224 while(blockers != NULL) {
226 for(bl = blockers; bl; bl = bl->n) {
227 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
233 else if(timeout > now)
234 toval = &(struct timespec){.tv_sec = timeout - now};
236 toval = &(struct timespec){.tv_sec = 1};
239 nev = kevent(qfd, NULL, 0, evs, sizeof(evs) / sizeof(*evs), toval);
242 flog(LOG_CRIT, "ioloop: kevent errored out: %s", strerror(errno));
243 /* To avoid CPU hogging in case it's bad, which it
249 for(i = 0; i < nev; i++) {
250 fd = (int)evs[i].ident;
251 ev = (evs[i].filter == EVFILT_READ)?EV_READ:EV_WRITE;
252 for(bl = fdlist[fd]; bl; bl = nbl) {
259 resume(bl->th, bl->id);
265 /* XXX: This is inefficient and buggy, and should have the
266 * heap structure from mtio-epoll ported. */
267 for(bl = blockers; bl; bl = nbl) {
269 if((bl->to != 0) && (bl->to <= now)) {
274 resume(bl->th, bl->id);
279 for(bl = blockers; bl; bl = bl->n)
286 void exitioloop(int status)