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