Commit | Line | Data |
---|---|---|
a6cda4dd FT |
1 | /* |
2 | ashd - A Sane HTTP Daemon | |
3 | Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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/>. | |
17 | */ | |
18 | ||
19 | #include <stdlib.h> | |
4f6d772e FT |
20 | #include <unistd.h> |
21 | #include <time.h> | |
be078ac9 | 22 | #include <fcntl.h> |
a6cda4dd FT |
23 | #include <string.h> |
24 | #include <sys/epoll.h> | |
25 | #include <errno.h> | |
26 | ||
27 | #ifdef HAVE_CONFIG_H | |
28 | #include <config.h> | |
29 | #endif | |
30 | #include <log.h> | |
31 | #include <utils.h> | |
32 | #include <mt.h> | |
33 | #include <mtio.h> | |
34 | ||
35 | static struct blocker *blockers; | |
36 | ||
37 | struct blocker { | |
38 | struct blocker *n, *p, *n2, *p2; | |
39 | int fd, reg; | |
205ee933 | 40 | int ev, rev, id; |
589987f8 | 41 | int thpos; |
a6cda4dd FT |
42 | time_t to; |
43 | struct muth *th; | |
44 | }; | |
45 | ||
46 | static int epfd = -1, fdln = 0; | |
9d32586e | 47 | static int exitstatus; |
a6cda4dd | 48 | static struct blocker **fdlist; |
bb073004 | 49 | static typedbuf(struct blocker *) timeheap; |
a6cda4dd FT |
50 | |
51 | static int regfd(struct blocker *bl) | |
52 | { | |
53 | struct blocker *o; | |
54 | struct epoll_event evd; | |
55 | ||
56 | memset(&evd, 0, sizeof(evd)); | |
57 | evd.events = 0; | |
58 | if(bl->ev & EV_READ) | |
59 | evd.events |= EPOLLIN; | |
60 | if(bl->ev & EV_WRITE) | |
61 | evd.events |= EPOLLOUT; | |
62 | evd.data.fd = bl->fd; | |
63 | if(bl->fd >= fdln) { | |
64 | if(fdlist) { | |
65 | fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1)); | |
66 | memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln)); | |
67 | fdln = bl->fd + 1; | |
68 | } else { | |
69 | fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1))); | |
70 | } | |
71 | } | |
72 | if(fdlist[bl->fd] == NULL) { | |
73 | if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) { | |
74 | /* XXX?! Whatever to do, really? */ | |
75 | flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno)); | |
76 | return(-1); | |
77 | } | |
78 | } else { | |
79 | for(o = fdlist[bl->fd]; o; o = o->n2) { | |
80 | if(o->ev & EV_READ) | |
81 | evd.events |= EPOLLIN; | |
82 | if(o->ev & EV_WRITE) | |
83 | evd.events |= EPOLLOUT; | |
84 | } | |
85 | if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) { | |
86 | /* XXX?! Whatever to do, really? */ | |
87 | flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno)); | |
88 | return(-1); | |
89 | } | |
90 | } | |
91 | bl->n2 = fdlist[bl->fd]; | |
92 | bl->p2 = NULL; | |
93 | if(fdlist[bl->fd] != NULL) | |
94 | fdlist[bl->fd]->p2 = bl; | |
95 | fdlist[bl->fd] = bl; | |
96 | bl->reg = 1; | |
97 | return(0); | |
98 | } | |
99 | ||
100 | static void remfd(struct blocker *bl) | |
101 | { | |
102 | struct blocker *o; | |
103 | struct epoll_event evd; | |
104 | ||
105 | if(!bl->reg) | |
106 | return; | |
107 | if(bl->n2) | |
108 | bl->n2->p2 = bl->p2; | |
109 | if(bl->p2) | |
110 | bl->p2->n2 = bl->n2; | |
111 | if(bl == fdlist[bl->fd]) | |
112 | fdlist[bl->fd] = bl->n2; | |
113 | if(fdlist[bl->fd] == NULL) { | |
114 | if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL)) | |
115 | flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno)); | |
116 | } else { | |
117 | memset(&evd, 0, sizeof(evd)); | |
118 | evd.events = 0; | |
119 | evd.data.fd = bl->fd; | |
120 | for(o = fdlist[bl->fd]; o; o = o->n2) { | |
121 | if(o->ev & EV_READ) | |
122 | evd.events |= EPOLLIN; | |
123 | if(o->ev & EV_WRITE) | |
124 | evd.events |= EPOLLOUT; | |
125 | } | |
126 | if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) { | |
127 | /* XXX?! Whatever to do, really? */ | |
128 | flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno)); | |
129 | } | |
130 | } | |
9d32586e | 131 | bl->reg = 0; |
a6cda4dd FT |
132 | } |
133 | ||
bb073004 | 134 | static void thraise(struct blocker *bl, int n) |
589987f8 FT |
135 | { |
136 | int p; | |
137 | ||
138 | while(n > 0) { | |
139 | p = (n - 1) >> 1; | |
bb073004 | 140 | if(timeheap.b[p]->to <= bl->to) |
589987f8 FT |
141 | break; |
142 | timeheap.b[n] = timeheap.b[p]; | |
bb073004 | 143 | timeheap.b[n]->thpos = n; |
589987f8 FT |
144 | n = p; |
145 | } | |
bb073004 FT |
146 | timeheap.b[n] = bl; |
147 | bl->thpos = n; | |
589987f8 FT |
148 | } |
149 | ||
bb073004 | 150 | static void thlower(struct blocker *bl, int n) |
589987f8 FT |
151 | { |
152 | int c; | |
153 | ||
154 | while(1) { | |
155 | c = (n << 1) + 1; | |
156 | if(c >= timeheap.d) | |
157 | break; | |
bb073004 | 158 | if((c + 1 < timeheap.d) && (timeheap.b[c + 1]->to < timeheap.b[c]->to)) |
589987f8 | 159 | c = c + 1; |
bb073004 | 160 | if(timeheap.b[c]->to > bl->to) |
589987f8 FT |
161 | break; |
162 | timeheap.b[n] = timeheap.b[c]; | |
bb073004 | 163 | timeheap.b[n]->thpos = n; |
589987f8 FT |
164 | n = c; |
165 | } | |
bb073004 FT |
166 | timeheap.b[n] = bl; |
167 | bl->thpos = n; | |
589987f8 FT |
168 | } |
169 | ||
170 | static void addtimeout(struct blocker *bl, time_t to) | |
171 | { | |
172 | sizebuf(timeheap, ++timeheap.d); | |
bb073004 | 173 | thraise(bl, timeheap.d - 1); |
589987f8 FT |
174 | } |
175 | ||
176 | static void deltimeout(struct blocker *bl) | |
177 | { | |
589987f8 FT |
178 | int n; |
179 | ||
180 | if(bl->thpos == timeheap.d - 1) { | |
181 | timeheap.d--; | |
182 | return; | |
183 | } | |
184 | n = bl->thpos; | |
bb073004 FT |
185 | bl = timeheap.b[--timeheap.d]; |
186 | if((n > 0) && (timeheap.b[(n - 1) >> 1]->to > bl->to)) | |
187 | thraise(bl, n); | |
589987f8 | 188 | else |
bb073004 | 189 | thlower(bl, n); |
589987f8 FT |
190 | } |
191 | ||
205ee933 | 192 | static int addblock(struct blocker *bl) |
a6cda4dd | 193 | { |
205ee933 | 194 | if((epfd >= 0) && regfd(bl)) |
a6cda4dd | 195 | return(-1); |
a6cda4dd FT |
196 | bl->n = blockers; |
197 | if(blockers) | |
198 | blockers->p = bl; | |
199 | blockers = bl; | |
bcad6b0c FT |
200 | if(bl->to > 0) |
201 | addtimeout(bl, bl->to); | |
205ee933 FT |
202 | return(0); |
203 | } | |
204 | ||
205 | static void remblock(struct blocker *bl) | |
206 | { | |
589987f8 FT |
207 | if(bl->to > 0) |
208 | deltimeout(bl); | |
a6cda4dd FT |
209 | if(bl->n) |
210 | bl->n->p = bl->p; | |
211 | if(bl->p) | |
212 | bl->p->n = bl->n; | |
205ee933 | 213 | if(blockers == bl) |
a6cda4dd FT |
214 | blockers = bl->n; |
215 | remfd(bl); | |
205ee933 FT |
216 | } |
217 | ||
218 | struct selected mblock(time_t to, int n, struct selected *spec) | |
219 | { | |
220 | int i, id; | |
221 | struct blocker bls[n]; | |
222 | ||
223 | to = (to > 0)?(time(NULL) + to):0; | |
224 | for(i = 0; i < n; i++) { | |
225 | bls[i] = (struct blocker) { | |
226 | .fd = spec[i].fd, | |
227 | .ev = spec[i].ev, | |
228 | .id = i, | |
229 | .to = to, | |
230 | .th = current, | |
231 | }; | |
232 | if(addblock(&bls[i])) { | |
4f5bec84 | 233 | for(i--; i >= 0; i--) |
205ee933 FT |
234 | remblock(&bls[i]); |
235 | return((struct selected){.fd = -1, .ev = -1}); | |
236 | } | |
237 | } | |
238 | id = yield(); | |
239 | for(i = 0; i < n; i++) | |
240 | remblock(&bls[i]); | |
241 | if(id < 0) | |
242 | return((struct selected){.fd = -1, .ev = -1}); | |
243 | return((struct selected){.fd = bls[id].fd, .ev = bls[id].rev}); | |
244 | } | |
245 | ||
246 | int block(int fd, int ev, time_t to) | |
247 | { | |
248 | struct blocker bl; | |
249 | int rv; | |
250 | ||
251 | bl = (struct blocker) { | |
252 | .fd = fd, | |
253 | .ev = ev, | |
254 | .id = -1, | |
255 | .to = (to > 0)?(time(NULL) + to):0, | |
256 | .th = current, | |
257 | }; | |
258 | if(addblock(&bl)) | |
259 | return(-1); | |
260 | rv = yield(); | |
261 | remblock(&bl); | |
a6cda4dd FT |
262 | return(rv); |
263 | } | |
264 | ||
9d32586e | 265 | int ioloop(void) |
a6cda4dd FT |
266 | { |
267 | struct blocker *bl, *nbl; | |
268 | struct epoll_event evr[16]; | |
269 | int i, fd, nev, ev, toval; | |
589987f8 | 270 | time_t now; |
a6cda4dd | 271 | |
9d32586e | 272 | exitstatus = 0; |
a6cda4dd | 273 | epfd = epoll_create(128); |
be078ac9 | 274 | fcntl(epfd, F_SETFD, FD_CLOEXEC); |
a6cda4dd FT |
275 | for(bl = blockers; bl; bl = nbl) { |
276 | nbl = bl->n; | |
277 | if(regfd(bl)) | |
278 | resume(bl->th, -1); | |
279 | } | |
280 | while(blockers != NULL) { | |
a6cda4dd | 281 | now = time(NULL); |
589987f8 | 282 | if(timeheap.d == 0) |
a6cda4dd | 283 | toval = -1; |
bb073004 FT |
284 | else if(timeheap.b[0]->to > now) |
285 | toval = (timeheap.b[0]->to - now) * 1000; | |
a6cda4dd FT |
286 | else |
287 | toval = 1000; | |
9d32586e FT |
288 | if(exitstatus) |
289 | break; | |
a6cda4dd FT |
290 | nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval); |
291 | if(nev < 0) { | |
292 | if(errno != EINTR) { | |
f43decce | 293 | flog(LOG_CRIT, "ioloop: epoll_wait errored out: %s", strerror(errno)); |
a6cda4dd FT |
294 | /* To avoid CPU hogging in case it's bad, which it |
295 | * probably is. */ | |
296 | sleep(1); | |
297 | } | |
298 | continue; | |
299 | } | |
300 | for(i = 0; i < nev; i++) { | |
301 | fd = evr[i].data.fd; | |
302 | ev = 0; | |
303 | if(evr[i].events & EPOLLIN) | |
304 | ev |= EV_READ; | |
305 | if(evr[i].events & EPOLLOUT) | |
306 | ev |= EV_WRITE; | |
307 | if(evr[i].events & ~(EPOLLIN | EPOLLOUT)) | |
308 | ev = -1; | |
309 | for(bl = fdlist[fd]; bl; bl = nbl) { | |
310 | nbl = bl->n2; | |
205ee933 FT |
311 | if((ev < 0) || (ev & bl->ev)) { |
312 | if(bl->id < 0) { | |
313 | resume(bl->th, ev); | |
314 | } else { | |
315 | bl->rev = ev; | |
316 | resume(bl->th, bl->id); | |
317 | } | |
318 | } | |
a6cda4dd FT |
319 | } |
320 | } | |
321 | now = time(NULL); | |
bb073004 | 322 | while((timeheap.d > 0) && ((bl = timeheap.b[0])->to <= now)) { |
bcad6b0c | 323 | if(bl->id < 0) { |
bb073004 | 324 | resume(bl->th, 0); |
bcad6b0c FT |
325 | } else { |
326 | bl->rev = 0; | |
327 | resume(bl->th, bl->id); | |
205ee933 | 328 | } |
a6cda4dd FT |
329 | } |
330 | } | |
9d32586e FT |
331 | for(bl = blockers; bl; bl = bl->n) |
332 | remfd(bl); | |
a6cda4dd FT |
333 | close(epfd); |
334 | epfd = -1; | |
9d32586e FT |
335 | return(exitstatus); |
336 | } | |
337 | ||
338 | void exitioloop(int status) | |
339 | { | |
340 | exitstatus = status; | |
a6cda4dd | 341 | } |