1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file s_bsd_select.c |
23 |
|
|
* \brief select() compatible network routines. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
stu |
908 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_SELECT |
29 |
michael |
1021 |
#include "list.h" |
30 |
adx |
30 |
#include "fdlist.h" |
31 |
|
|
#include "hook.h" |
32 |
|
|
#include "ircd.h" |
33 |
|
|
#include "s_bsd.h" |
34 |
michael |
1309 |
#include "log.h" |
35 |
adx |
30 |
|
36 |
|
|
/* |
37 |
|
|
* Note that this is only a single list - multiple lists is kinda pointless |
38 |
|
|
* under select because the list size is a function of the highest FD :-) |
39 |
|
|
* -- adrian |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
static fd_set select_readfds, tmpreadfds; |
43 |
|
|
static fd_set select_writefds, tmpwritefds; |
44 |
|
|
static int highest_fd = -1; |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
/* |
48 |
|
|
* init_netio |
49 |
|
|
* |
50 |
|
|
* This is a needed exported function which will be called to initialise |
51 |
|
|
* the network loop code. |
52 |
|
|
*/ |
53 |
|
|
void |
54 |
|
|
init_netio(void) |
55 |
|
|
{ |
56 |
|
|
FD_ZERO(&select_readfds); |
57 |
|
|
FD_ZERO(&select_writefds); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
/* |
61 |
|
|
* comm_setselect |
62 |
|
|
* |
63 |
|
|
* This is a needed exported function which will be called to register |
64 |
|
|
* and deregister interest in a pending IO state for a given FD. |
65 |
|
|
*/ |
66 |
|
|
void |
67 |
|
|
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
68 |
|
|
void *client_data, time_t timeout) |
69 |
|
|
{ |
70 |
|
|
int new_events; |
71 |
|
|
|
72 |
|
|
if ((type & COMM_SELECT_READ)) |
73 |
|
|
{ |
74 |
|
|
F->read_handler = handler; |
75 |
|
|
F->read_data = client_data; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
if ((type & COMM_SELECT_WRITE)) |
79 |
|
|
{ |
80 |
|
|
F->write_handler = handler; |
81 |
|
|
F->write_data = client_data; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
85 |
michael |
2650 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
86 |
adx |
30 |
|
87 |
|
|
if (timeout != 0) |
88 |
michael |
2725 |
{ |
89 |
adx |
30 |
F->timeout = CurrentTime + (timeout / 1000); |
90 |
michael |
2725 |
F->timeout_handler = handler; |
91 |
|
|
F->timeout_data = client_data; |
92 |
|
|
} |
93 |
adx |
30 |
|
94 |
|
|
if (new_events != F->evcache) |
95 |
|
|
{ |
96 |
|
|
if ((new_events & COMM_SELECT_READ)) |
97 |
|
|
FD_SET(F->fd, &select_readfds); |
98 |
|
|
else |
99 |
|
|
{ |
100 |
|
|
FD_CLR(F->fd, &select_readfds); |
101 |
|
|
FD_CLR(F->fd, &tmpreadfds); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
if ((new_events & COMM_SELECT_WRITE)) |
105 |
|
|
FD_SET(F->fd, &select_writefds); |
106 |
|
|
else |
107 |
|
|
{ |
108 |
|
|
FD_CLR(F->fd, &select_writefds); |
109 |
|
|
FD_CLR(F->fd, &tmpwritefds); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
if (new_events == 0) |
113 |
|
|
{ |
114 |
|
|
if (highest_fd == F->fd) |
115 |
|
|
while (highest_fd >= 0 && (FD_ISSET(highest_fd, &select_readfds) || |
116 |
michael |
2650 |
FD_ISSET(highest_fd, &select_writefds))) |
117 |
adx |
30 |
highest_fd--; |
118 |
|
|
} |
119 |
|
|
else if (F->evcache == 0) |
120 |
|
|
if (F->fd > highest_fd) |
121 |
|
|
highest_fd = F->fd; |
122 |
|
|
|
123 |
|
|
F->evcache = new_events; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/* |
128 |
|
|
* comm_select |
129 |
|
|
* |
130 |
|
|
* Called to do the new-style IO, courtesy of squid (like most of this |
131 |
|
|
* new IO code). This routine handles the stuff we've hidden in |
132 |
|
|
* comm_setselect and fd_table[] and calls callbacks for IO ready |
133 |
|
|
* events. |
134 |
|
|
*/ |
135 |
|
|
void |
136 |
|
|
comm_select(void) |
137 |
|
|
{ |
138 |
|
|
struct timeval to; |
139 |
|
|
int num, fd; |
140 |
|
|
fde_t *F; |
141 |
|
|
PF *hdl; |
142 |
|
|
|
143 |
|
|
/* Copy over the read/write sets so we don't have to rebuild em */ |
144 |
|
|
memcpy(&tmpreadfds, &select_readfds, sizeof(fd_set)); |
145 |
|
|
memcpy(&tmpwritefds, &select_writefds, sizeof(fd_set)); |
146 |
|
|
|
147 |
|
|
to.tv_sec = 0; |
148 |
|
|
to.tv_usec = SELECT_DELAY * 1000; |
149 |
|
|
num = select(highest_fd + 1, &tmpreadfds, &tmpwritefds, NULL, &to); |
150 |
|
|
|
151 |
|
|
set_time(); |
152 |
|
|
|
153 |
|
|
if (num < 0) |
154 |
|
|
{ |
155 |
|
|
#ifdef HAVE_USLEEP |
156 |
|
|
usleep(50000); |
157 |
|
|
#endif |
158 |
|
|
return; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
for (fd = 0; fd <= highest_fd && num > 0; fd++) |
162 |
michael |
2650 |
{ |
163 |
adx |
30 |
if (FD_ISSET(fd, &tmpreadfds) || FD_ISSET(fd, &tmpwritefds)) |
164 |
|
|
{ |
165 |
|
|
num--; |
166 |
|
|
|
167 |
|
|
F = lookup_fd(fd); |
168 |
|
|
if (F == NULL || !F->flags.open) |
169 |
|
|
continue; |
170 |
|
|
|
171 |
|
|
if (FD_ISSET(fd, &tmpreadfds)) |
172 |
michael |
2650 |
{ |
173 |
adx |
30 |
if ((hdl = F->read_handler) != NULL) |
174 |
|
|
{ |
175 |
|
|
F->read_handler = NULL; |
176 |
|
|
hdl(F, F->read_data); |
177 |
|
|
if (!F->flags.open) |
178 |
|
|
continue; |
179 |
|
|
} |
180 |
michael |
2650 |
} |
181 |
adx |
30 |
|
182 |
|
|
if (FD_ISSET(fd, &tmpwritefds)) |
183 |
michael |
2650 |
{ |
184 |
adx |
30 |
if ((hdl = F->write_handler) != NULL) |
185 |
|
|
{ |
186 |
|
|
F->write_handler = NULL; |
187 |
|
|
hdl(F, F->write_data); |
188 |
|
|
if (!F->flags.open) |
189 |
|
|
continue; |
190 |
|
|
} |
191 |
michael |
2650 |
} |
192 |
adx |
30 |
|
193 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
194 |
|
|
} |
195 |
michael |
2650 |
} |
196 |
adx |
30 |
} |
197 |
stu |
908 |
#endif |