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