Install the library and its headers.
[ashd.git] / lib / mtio.c
CommitLineData
83723896
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>
e1cdf02e
FT
20#include <stdio.h>
21#include <fcntl.h>
83723896
FT
22#include <unistd.h>
23#include <string.h>
24#include <time.h>
25#include <errno.h>
26#include <sys/select.h>
e1cdf02e 27#include <sys/socket.h>
83723896
FT
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <log.h>
33#include <utils.h>
34#include <mt.h>
35#include <mtio.h>
36
16accb88
FT
37static struct blocker *blockers;
38
39#ifdef HAVE_EPOLL
40
41/*
42 * Support for epoll. Optimally, different I/O loops should be split
43 * into different files for greater clarity, but I'll save that fun
44 * for another day.
45 *
46 * Scroll down to #else for the normal select loop.
47 */
48
49#include <sys/epoll.h>
50
51struct blocker {
52 struct blocker *n, *p, *n2, *p2;
53 int fd, reg;
54 int ev;
55 time_t to;
56 struct muth *th;
57};
58
59static int epfd = -1, fdln = 0;
60static struct blocker **fdlist;
61
62static int regfd(struct blocker *bl)
63{
64 struct blocker *o;
65 struct epoll_event evd;
5ab220d9
FT
66
67 memset(&evd, 0, sizeof(evd));
16accb88
FT
68 evd.events = 0;
69 if(bl->ev & EV_READ)
70 evd.events |= EPOLLIN;
71 if(bl->ev & EV_WRITE)
72 evd.events |= EPOLLOUT;
73 evd.data.fd = bl->fd;
74 if(bl->fd >= fdln) {
75 if(fdlist) {
76 fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
77 memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
78 fdln = bl->fd + 1;
79 } else {
80 fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
81 }
82 }
83 if(fdlist[bl->fd] == NULL) {
84 if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
85 /* XXX?! Whatever to do, really? */
86 flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
87 return(-1);
88 }
89 } else {
90 for(o = fdlist[bl->fd]; o; o = o->n2) {
91 if(o->ev & EV_READ)
92 evd.events |= EPOLLIN;
93 if(o->ev & EV_WRITE)
94 evd.events |= EPOLLOUT;
95 }
96 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
97 /* XXX?! Whatever to do, really? */
98 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
99 return(-1);
100 }
101 }
102 bl->n2 = fdlist[bl->fd];
103 bl->p2 = NULL;
104 if(fdlist[bl->fd] != NULL)
105 fdlist[bl->fd]->p2 = bl;
106 fdlist[bl->fd] = bl;
107 bl->reg = 1;
108 return(0);
109}
110
111static void remfd(struct blocker *bl)
112{
113 struct blocker *o;
114 struct epoll_event evd;
115
116 if(!bl->reg)
117 return;
118 if(bl->n2)
119 bl->n2->p2 = bl->p2;
120 if(bl->p2)
121 bl->p2->n2 = bl->n2;
122 if(bl == fdlist[bl->fd])
123 fdlist[bl->fd] = bl->n2;
124 if(fdlist[bl->fd] == NULL) {
125 if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
126 flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
127 } else {
5ab220d9 128 memset(&evd, 0, sizeof(evd));
16accb88
FT
129 evd.events = 0;
130 evd.data.fd = bl->fd;
131 for(o = fdlist[bl->fd]; o; o = o->n2) {
132 if(o->ev & EV_READ)
133 evd.events |= EPOLLIN;
134 if(o->ev & EV_WRITE)
135 evd.events |= EPOLLOUT;
136 }
137 if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
138 /* XXX?! Whatever to do, really? */
139 flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
140 }
141 }
142}
143
144int block(int fd, int ev, time_t to)
145{
146 struct blocker *bl;
147 int rv;
148
149 omalloc(bl);
150 bl->fd = fd;
151 bl->ev = ev;
152 if(to > 0)
153 bl->to = time(NULL) + to;
154 bl->th = current;
155 if((epfd >= 0) && regfd(bl)) {
156 free(bl);
157 return(-1);
158 }
159 bl->n = blockers;
160 if(blockers)
161 blockers->p = bl;
162 blockers = bl;
163 rv = yield();
164 if(bl->n)
165 bl->n->p = bl->p;
166 if(bl->p)
167 bl->p->n = bl->n;
168 if(bl == blockers)
169 blockers = bl->n;
170 remfd(bl);
171 free(bl);
172 return(rv);
173}
174
175void ioloop(void)
176{
177 struct blocker *bl, *nbl;
178 struct epoll_event evr[16];
e0f40f83 179 int i, fd, nev, ev, toval;
16accb88
FT
180 time_t now, timeout;
181
182 epfd = epoll_create(128);
183 for(bl = blockers; bl; bl = nbl) {
184 nbl = bl->n;
185 if(regfd(bl))
186 resume(bl->th, -1);
187 }
188 while(blockers != NULL) {
189 timeout = 0;
190 for(bl = blockers; bl; bl = bl->n) {
191 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
192 timeout = bl->to;
193 }
194 now = time(NULL);
e0f40f83
FT
195 if(timeout == 0)
196 toval = -1;
197 else if(timeout > now)
198 toval = (timeout - now) * 1000;
199 else
200 toval = 1000;
201 nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
16accb88
FT
202 if(nev < 0) {
203 if(errno != EINTR) {
204 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
205 /* To avoid CPU hogging in case it's bad, which it
206 * probably is. */
207 sleep(1);
208 }
209 continue;
210 }
16accb88
FT
211 for(i = 0; i < nev; i++) {
212 fd = evr[i].data.fd;
213 ev = 0;
214 if(evr[i].events & EPOLLIN)
215 ev |= EV_READ;
216 if(evr[i].events & EPOLLOUT)
217 ev |= EV_WRITE;
218 if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
219 ev = -1;
220 for(bl = fdlist[fd]; bl; bl = nbl) {
221 nbl = bl->n2;
222 if((ev < 0) || (ev & bl->ev))
223 resume(bl->th, ev);
16accb88
FT
224 }
225 }
e0f40f83
FT
226 now = time(NULL);
227 for(bl = blockers; bl; bl = nbl) {
228 nbl = bl->n;
229 if((bl->to != 0) && (bl->to <= now))
230 resume(bl->th, 0);
231 }
16accb88
FT
232 }
233 close(epfd);
234 epfd = -1;
235}
236
237#else
238
83723896
FT
239struct blocker {
240 struct blocker *n, *p;
241 int fd;
242 int ev;
243 time_t to;
244 struct muth *th;
245};
246
83723896
FT
247int block(int fd, int ev, time_t to)
248{
249 struct blocker *bl;
250 int rv;
251
252 omalloc(bl);
253 bl->fd = fd;
254 bl->ev = ev;
255 if(to > 0)
256 bl->to = time(NULL) + to;
257 bl->th = current;
258 bl->n = blockers;
259 if(blockers)
260 blockers->p = bl;
261 blockers = bl;
262 rv = yield();
263 if(bl->n)
264 bl->n->p = bl->p;
265 if(bl->p)
266 bl->p->n = bl->n;
267 if(bl == blockers)
268 blockers = bl->n;
96fc434a 269 free(bl);
83723896
FT
270 return(rv);
271}
272
273void ioloop(void)
274{
275 int ret;
276 fd_set rfds, wfds, efds;
277 struct blocker *bl, *nbl;
278 struct timeval toval;
279 time_t now, timeout;
280 int maxfd;
281 int ev;
282
283 while(blockers != NULL) {
284 FD_ZERO(&rfds);
285 FD_ZERO(&wfds);
286 FD_ZERO(&efds);
287 maxfd = 0;
288 now = time(NULL);
289 timeout = 0;
290 for(bl = blockers; bl; bl = bl->n) {
291 if(bl->ev & EV_READ)
292 FD_SET(bl->fd, &rfds);
293 if(bl->ev & EV_WRITE)
294 FD_SET(bl->fd, &wfds);
295 FD_SET(bl->fd, &efds);
296 if(bl->fd > maxfd)
297 maxfd = bl->fd;
298 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
299 timeout = bl->to;
300 }
301 toval.tv_sec = timeout - now;
302 toval.tv_usec = 0;
303 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
304 if(ret < 0) {
305 if(errno != EINTR) {
306 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
307 /* To avoid CPU hogging in case it's bad, which it
308 * probably is. */
309 sleep(1);
310 }
311 }
312 now = time(NULL);
313 for(bl = blockers; bl; bl = nbl) {
314 nbl = bl->n;
315 ev = 0;
316 if(FD_ISSET(bl->fd, &rfds))
317 ev |= EV_READ;
318 if(FD_ISSET(bl->fd, &wfds))
319 ev |= EV_WRITE;
320 if(FD_ISSET(bl->fd, &efds))
321 ev = -1;
3dc69b45 322 if((ev < 0) || (ev & bl->ev))
83723896
FT
323 resume(bl->th, ev);
324 else if((bl->to != 0) && (bl->to <= now))
325 resume(bl->th, 0);
326 }
327 }
328}
e1cdf02e 329
16accb88
FT
330#endif
331
e1cdf02e
FT
332struct stdiofd {
333 int fd;
334 int sock;
335 int timeout;
336};
337
338static ssize_t mtread(void *cookie, char *buf, size_t len)
339{
340 struct stdiofd *d = cookie;
341 int ev;
342 ssize_t ret;
343
344 while(1) {
345 ret = read(d->fd, buf, len);
346 if((ret < 0) && (errno == EAGAIN)) {
347 ev = block(d->fd, EV_READ, d->timeout);
348 if(ev < 0) {
349 /* If we just go on, we should get the real error. */
350 continue;
351 } else if(ev == 0) {
352 errno = ETIMEDOUT;
353 return(-1);
354 } else {
355 continue;
356 }
357 } else {
358 return(ret);
359 }
360 }
361}
362
363static ssize_t mtwrite(void *cookie, const char *buf, size_t len)
364{
365 struct stdiofd *d = cookie;
366 int ev;
9dc25d3d 367 size_t off;
e1cdf02e
FT
368 ssize_t ret;
369
9dc25d3d
FT
370 off = 0;
371 while(off < len) {
e1cdf02e 372 if(d->sock)
9dc25d3d 373 ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL);
e1cdf02e 374 else
9dc25d3d
FT
375 ret = write(d->fd, buf + off, len - off);
376 if(ret < 0) {
377 if(errno == EAGAIN) {
378 ev = block(d->fd, EV_WRITE, d->timeout);
379 if(ev < 0) {
380 /* If we just go on, we should get the real error. */
381 continue;
382 } else if(ev == 0) {
383 errno = ETIMEDOUT;
384 return(off);
385 } else {
386 continue;
387 }
e1cdf02e 388 } else {
9dc25d3d 389 return(off);
e1cdf02e
FT
390 }
391 } else {
9dc25d3d 392 off += ret;
e1cdf02e
FT
393 }
394 }
9dc25d3d 395 return(off);
e1cdf02e
FT
396}
397
398static int mtclose(void *cookie)
399{
400 struct stdiofd *d = cookie;
401
402 close(d->fd);
403 free(d);
404 return(0);
405}
406
407static cookie_io_functions_t iofuns = {
408 .read = mtread,
409 .write = mtwrite,
410 .close = mtclose,
411};
412
413FILE *mtstdopen(int fd, int issock, int timeout, char *mode)
414{
415 struct stdiofd *d;
416 FILE *ret;
417
418 omalloc(d);
419 d->fd = fd;
420 d->sock = issock;
421 d->timeout = timeout;
422 ret = fopencookie(d, mode, iofuns);
423 if(!ret)
424 free(d);
425 else
426 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
427 return(ret);
428}