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;
45 static int epfd = -1, fdln = 0;
46 static int exitstatus;
47 static struct blocker **fdlist;
49 static int regfd(struct blocker *bl)
52 struct epoll_event evd;
54 memset(&evd, 0, sizeof(evd));
57 evd.events |= EPOLLIN;
59 evd.events |= EPOLLOUT;
63 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
64 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
67 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
70 if(fdlist[bl->fd] == NULL) {
71 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
72 /* XXX?! Whatever to do, really? */
73 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
77 for(o = fdlist[bl->fd]; o; o = o->n2) {
79 evd.events |= EPOLLIN;
81 evd.events |= EPOLLOUT;
83 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
84 /* XXX?! Whatever to do, really? */
85 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
89 bl->n2 = fdlist[bl->fd];
91 if(fdlist[bl->fd] != NULL)
92 fdlist[bl->fd]->p2 = bl;
98 static void remfd(struct blocker *bl)
101 struct epoll_event evd;
109 if(bl == fdlist[bl->fd])
110 fdlist[bl->fd] = bl->n2;
111 if(fdlist[bl->fd] == NULL) {
112 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
113 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
115 memset(&evd, 0, sizeof(evd));
117 evd.data.fd = bl->fd;
118 for(o = fdlist[bl->fd]; o; o = o->n2) {
120 evd.events |= EPOLLIN;
122 evd.events |= EPOLLOUT;
124 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
125 /* XXX?! Whatever to do, really? */
126 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
132 int block(int fd, int ev, time_t to)
141 bl->to = time(NULL) + to;
143 if((epfd >= 0) && regfd(bl)) {
165 struct blocker *bl, *nbl;
166 struct epoll_event evr[16];
167 int i, fd, nev, ev, toval;
171 epfd = epoll_create(128);
172 fcntl(epfd, F_SETFD, FD_CLOEXEC);
173 for(bl = blockers; bl; bl = nbl) {
178 while(blockers != NULL) {
180 for(bl = blockers; bl; bl = bl->n) {
181 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
187 else if(timeout > now)
188 toval = (timeout - now) * 1000;
193 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
196 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
197 /* To avoid CPU hogging in case it's bad, which it
203 for(i = 0; i < nev; i++) {
206 if(evr[i].events & EPOLLIN)
208 if(evr[i].events & EPOLLOUT)
210 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
212 for(bl = fdlist[fd]; bl; bl = nbl) {
214 if((ev < 0) || (ev & bl->ev))
219 for(bl = blockers; bl; bl = nbl) {
221 if((bl->to != 0) && (bl->to <= now))
225 for(bl = blockers; bl; bl = bl->n)
232 void exitioloop(int status)