callfcgi: Improved recvrec implementation.
[ashd.git] / lib / mtio-select.c
... / ...
CommitLineData
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>
20#include <string.h>
21#include <time.h>
22#include <unistd.h>
23#include <errno.h>
24#include <sys/select.h>
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29#include <log.h>
30#include <utils.h>
31#include <mt.h>
32#include <mtio.h>
33
34static struct blocker *blockers;
35static int exitstatus;
36
37struct blocker {
38 struct blocker *n, *p;
39 int fd;
40 int ev;
41 time_t to;
42 struct muth *th;
43};
44
45int block(int fd, int ev, time_t to)
46{
47 struct blocker *bl;
48 int rv;
49
50 if(fd >= FD_SETSIZE) {
51 flog(LOG_ERR, "tried to use more file descriptors than select() can handle: fd %i", fd);
52 errno = EMFILE;
53 return(-1);
54 }
55 omalloc(bl);
56 bl->fd = fd;
57 bl->ev = ev;
58 if(to > 0)
59 bl->to = time(NULL) + to;
60 bl->th = current;
61 bl->n = blockers;
62 if(blockers)
63 blockers->p = bl;
64 blockers = bl;
65 rv = yield();
66 if(bl->n)
67 bl->n->p = bl->p;
68 if(bl->p)
69 bl->p->n = bl->n;
70 if(bl == blockers)
71 blockers = bl->n;
72 free(bl);
73 return(rv);
74}
75
76int ioloop(void)
77{
78 int ret;
79 fd_set rfds, wfds, efds;
80 struct blocker *bl, *nbl;
81 struct timeval toval;
82 time_t now, timeout;
83 int maxfd;
84 int ev;
85
86 exitstatus = 0;
87 while(blockers != NULL) {
88 FD_ZERO(&rfds);
89 FD_ZERO(&wfds);
90 FD_ZERO(&efds);
91 maxfd = 0;
92 now = time(NULL);
93 timeout = 0;
94 for(bl = blockers; bl; bl = bl->n) {
95 if(bl->ev & EV_READ)
96 FD_SET(bl->fd, &rfds);
97 if(bl->ev & EV_WRITE)
98 FD_SET(bl->fd, &wfds);
99 FD_SET(bl->fd, &efds);
100 if(bl->fd > maxfd)
101 maxfd = bl->fd;
102 if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
103 timeout = bl->to;
104 }
105 if(exitstatus)
106 return(exitstatus);
107 toval.tv_sec = timeout - now;
108 toval.tv_usec = 0;
109 ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
110 if(ret < 0) {
111 if(errno != EINTR) {
112 flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
113 /* To avoid CPU hogging in case it's bad, which it
114 * probably is. */
115 sleep(1);
116 }
117 } else {
118 now = time(NULL);
119 for(bl = blockers; bl; bl = nbl) {
120 nbl = bl->n;
121 ev = 0;
122 if(FD_ISSET(bl->fd, &rfds))
123 ev |= EV_READ;
124 if(FD_ISSET(bl->fd, &wfds))
125 ev |= EV_WRITE;
126 if(FD_ISSET(bl->fd, &efds))
127 ev = -1;
128 if((ev < 0) || (ev & bl->ev))
129 resume(bl->th, ev);
130 else if((bl->to != 0) && (bl->to <= now))
131 resume(bl->th, 0);
132 }
133 }
134 }
135 return(0);
136}
137
138void exitioloop(int status)
139{
140 exitstatus = status;
141}