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 static int addblock(struct blocker *bl)
134 if((epfd >= 0) && regfd(bl))
143 static void remblock(struct blocker *bl)
154 struct selected mblock(time_t to, int n, struct selected *spec)
157 struct blocker bls[n];
159 to = (to > 0)?(time(NULL) + to):0;
160 for(i = 0; i < n; i++) {
161 bls[i] = (struct blocker) {
168 if(addblock(&bls[i])) {
169 for(i--; i >= 0; i++)
171 return((struct selected){.fd = -1, .ev = -1});
175 for(i = 0; i < n; i++)
178 return((struct selected){.fd = -1, .ev = -1});
179 return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev});
182 int block(int fd, int ev, time_t to)
187 bl = (struct blocker) {
191 .to = (to > 0)?(time(NULL) + to):0,
203 struct blocker *bl, *nbl;
204 struct epoll_event evr[16];
205 int i, fd, nev, ev, toval;
209 epfd = epoll_create(128);
210 fcntl(epfd, F_SETFD, FD_CLOEXEC);
211 for(bl = blockers; bl; bl = nbl) {
216 while(blockers != NULL) {
218 for(bl = blockers; bl; bl = bl->n) {
219 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
225 else if(timeout > now)
226 toval = (timeout - now) * 1000;
231 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
234 flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno));
235 /* To avoid CPU hogging in case it's bad, which it
241 for(i = 0; i < nev; i++) {
244 if(evr[i].events & EPOLLIN)
246 if(evr[i].events & EPOLLOUT)
248 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
250 for(bl = fdlist[fd]; bl; bl = nbl) {
252 if((ev < 0) || (ev & bl->ev)) {
257 resume(bl->th, bl->id);
263 for(bl = blockers; bl; bl = nbl) {
265 if((bl->to != 0) && (bl->to <= now)) {
270 resume(bl->th, bl->id);
275 for(bl = blockers; bl; bl = bl->n)
282 void exitioloop(int status)