]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* |
2 | * IRC - Internet Relay Chat, ircd/engine_select.c | |
3 | * Copyright (C) 2001 Kevin L. Mitchell <klmitch@mit.edu> | |
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 1, or (at your option) | |
8 | * 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, write to the Free Software | |
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | /** @file | |
20 | * @brief BSD sockets select() event engine. | |
21 | * @version $Id: engine_select.c,v 1.8 2005/07/12 03:10:59 entrope Exp $ | |
22 | */ | |
23 | #include "config.h" | |
24 | ||
25 | /* On BSD, define FD_SETSIZE to what we want before including sys/types.h */ | |
26 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__bsdi__) | |
27 | # if !defined(FD_SETSIZE) | |
28 | # define FD_SETSIZE ((MAXCONNECTIONS)+4) | |
29 | # endif | |
30 | #endif | |
31 | ||
32 | #include "ircd_events.h" | |
33 | ||
34 | #include "ircd.h" | |
35 | #include "ircd_log.h" | |
36 | #include "s_debug.h" | |
37 | ||
38 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
39 | #include <errno.h> | |
40 | #include <string.h> /* needed for bzero() on OS X */ | |
41 | #include <sys/socket.h> | |
42 | #include <sys/time.h> | |
43 | #include <sys/types.h> | |
44 | #include <time.h> | |
45 | #include <unistd.h> | |
46 | ||
47 | #if FD_SETSIZE < (MAXCONNECTIONS + 4) | |
48 | /* | |
49 | * Sanity check | |
50 | * | |
51 | * All operating systems work when MAXCONNECTIONS <= 252. | |
52 | * Most operating systems work when MAXCONNECTIONS <= 1020 and FD_SETSIZE is | |
53 | * updated correctly in the system headers (on BSD systems sys/types.h might | |
54 | * have abruptly redefined it so the check is still done), you might | |
55 | * already need to recompile your kernel. | |
56 | * For larger FD_SETSIZE your mileage may vary (kernel patches may be needed). | |
57 | */ | |
58 | # error FD_SETSIZE is too small or MAXCONNECTIONS too large. | |
59 | #endif | |
60 | ||
61 | #define SELECT_ERROR_THRESHOLD 20 /**< after 20 select errors, restart */ | |
62 | #define ERROR_EXPIRE_TIME 3600 /**< expire errors after an hour */ | |
63 | ||
64 | /** Array of active Socket structures, indexed by file descriptor. */ | |
65 | static struct Socket* sockList[FD_SETSIZE]; | |
66 | /** Maximum file descriptor currently used. */ | |
67 | static int highest_fd; | |
68 | /** Global read event interest bitmap. */ | |
69 | static fd_set global_read_set; | |
70 | /** Global write event interest bitmap. */ | |
71 | static fd_set global_write_set; | |
72 | ||
73 | /** Number of recent errors from select(). */ | |
74 | static int errors = 0; | |
75 | /** Periodic timer to forget errors. */ | |
76 | static struct Timer clear_error; | |
77 | ||
78 | /** Decrement the error count (once per hour). | |
79 | * @param[in] ev Expired timer event (ignored). | |
80 | */ | |
81 | static void | |
82 | error_clear(struct Event* ev) | |
83 | { | |
84 | if (!--errors) /* remove timer when error count reaches 0 */ | |
85 | timer_del(ev_timer(ev)); | |
86 | } | |
87 | ||
88 | /** Initialize the select() engine. | |
89 | * @param[in] max_sockets Maximum number of file descriptors to support. | |
90 | * @return Non-zero on success, or zero on failure. | |
91 | */ | |
92 | static int | |
93 | engine_init(int max_sockets) | |
94 | { | |
95 | int i; | |
96 | ||
97 | if (max_sockets > FD_SETSIZE) { /* too many sockets */ | |
98 | log_write(LS_SYSTEM, L_WARNING, 0, | |
99 | "select() engine cannot handle %d sockets (> %d)", | |
100 | max_sockets, FD_SETSIZE); | |
101 | return 0; | |
102 | } | |
103 | ||
104 | FD_ZERO(&global_read_set); /* zero the global fd sets */ | |
105 | FD_ZERO(&global_write_set); | |
106 | ||
107 | for (i = 0; i < FD_SETSIZE; i++) /* zero the sockList */ | |
108 | sockList[i] = 0; | |
109 | ||
110 | highest_fd = -1; /* No fds in set */ | |
111 | ||
112 | return 1; /* initialization successful */ | |
113 | } | |
114 | ||
115 | /** Figure out what events go with a given state. | |
116 | * @param[in] state %Socket state to consider. | |
117 | * @param[in] events User-specified preferred event set. | |
118 | * @return Actual set of preferred events. | |
119 | */ | |
120 | static unsigned int | |
121 | state_to_events(enum SocketState state, unsigned int events) | |
122 | { | |
123 | switch (state) { | |
124 | case SS_CONNECTING: /* connecting socket */ | |
125 | return SOCK_EVENT_WRITABLE; | |
126 | break; | |
127 | ||
128 | case SS_LISTENING: /* listening socket */ | |
129 | case SS_NOTSOCK: /* our signal socket */ | |
130 | return SOCK_EVENT_READABLE; | |
131 | break; | |
132 | ||
133 | case SS_CONNECTED: case SS_DATAGRAM: case SS_CONNECTDG: | |
134 | return events; /* ordinary socket */ | |
135 | break; | |
136 | } | |
137 | ||
138 | /*NOTREACHED*/ | |
139 | return 0; | |
140 | } | |
141 | ||
142 | /** Set interest events in #global_read_set and #global_write_set as appropriate. | |
143 | * @param[in] fd File descriptor to operate on. | |
144 | * @param[in] clear Set of interest events to clear from socket. | |
145 | * @param[in] set Set of interest events to set on socket. | |
146 | */ | |
147 | static void | |
148 | set_or_clear(int fd, unsigned int clear, unsigned int set) | |
149 | { | |
150 | if ((clear ^ set) & SOCK_EVENT_READABLE) { /* readable has changed */ | |
151 | if (set & SOCK_EVENT_READABLE) /* it's set */ | |
152 | FD_SET(fd, &global_read_set); | |
153 | else /* clear it */ | |
154 | FD_CLR(fd, &global_read_set); | |
155 | } | |
156 | ||
157 | if ((clear ^ set) & SOCK_EVENT_WRITABLE) { /* writable has changed */ | |
158 | if (set & SOCK_EVENT_WRITABLE) /* it's set */ | |
159 | FD_SET(fd, &global_write_set); | |
160 | else /* clear it */ | |
161 | FD_CLR(fd, &global_write_set); | |
162 | } | |
163 | } | |
164 | ||
165 | /** Add a socket to the event engine. | |
166 | * @param[in] sock Socket to add to engine. | |
167 | * @return Non-zero on success, or zero on error. | |
168 | */ | |
169 | static int | |
170 | engine_add(struct Socket* sock) | |
171 | { | |
172 | assert(0 != sock); | |
173 | assert(0 == sockList[s_fd(sock)]); | |
174 | ||
175 | /* bounds-check... */ | |
176 | if (s_fd(sock) >= FD_SETSIZE) { | |
177 | log_write(LS_SYSTEM, L_ERROR, 0, | |
178 | "Attempt to add socket %d (> %d) to event engine", s_fd(sock), | |
179 | FD_SETSIZE); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | sockList[s_fd(sock)] = sock; /* add to list */ | |
184 | ||
185 | if (s_fd(sock) >= highest_fd) /* update highest_fd */ | |
186 | highest_fd = s_fd(sock); | |
187 | ||
188 | Debug((DEBUG_ENGINE, "select: Adding socket %d to engine [%p], state %s", | |
189 | s_fd(sock), sock, state_to_name(s_state(sock)))); | |
190 | ||
191 | /* set the fd set bits */ | |
192 | set_or_clear(s_fd(sock), 0, state_to_events(s_state(sock), s_events(sock))); | |
193 | ||
194 | return 1; /* success */ | |
195 | } | |
196 | ||
197 | /** Handle state transition for a socket. | |
198 | * @param[in] sock Socket changing state. | |
199 | * @param[in] new_state New state for socket. | |
200 | */ | |
201 | static void | |
202 | engine_state(struct Socket* sock, enum SocketState new_state) | |
203 | { | |
204 | assert(0 != sock); | |
205 | assert(sock == sockList[s_fd(sock)]); | |
206 | ||
207 | Debug((DEBUG_ENGINE, "select: Changing state for socket %p to %s", sock, | |
208 | state_to_name(new_state))); | |
209 | ||
210 | /* set the correct events */ | |
211 | set_or_clear(s_fd(sock), | |
212 | state_to_events(s_state(sock), s_events(sock)), /* old state */ | |
213 | state_to_events(new_state, s_events(sock))); /* new state */ | |
214 | } | |
215 | ||
216 | /** Handle change to preferred socket events. | |
217 | * @param[in] sock Socket getting new interest list. | |
218 | * @param[in] new_events New set of interesting events for socket. | |
219 | */ | |
220 | static void | |
221 | engine_events(struct Socket* sock, unsigned int new_events) | |
222 | { | |
223 | assert(0 != sock); | |
224 | assert(sock == sockList[s_fd(sock)]); | |
225 | ||
226 | Debug((DEBUG_ENGINE, "select: Changing event mask for socket %p to [%s]", | |
227 | sock, sock_flags(new_events))); | |
228 | ||
229 | /* set the correct events */ | |
230 | set_or_clear(s_fd(sock), | |
231 | state_to_events(s_state(sock), s_events(sock)), /* old events */ | |
232 | state_to_events(s_state(sock), new_events)); /* new events */ | |
233 | } | |
234 | ||
235 | /** Remove a socket from the event engine. | |
236 | * @param[in] sock Socket being destroyed. | |
237 | */ | |
238 | static void | |
239 | engine_delete(struct Socket* sock) | |
240 | { | |
241 | assert(0 != sock); | |
242 | assert(sock == sockList[s_fd(sock)]); | |
243 | ||
244 | Debug((DEBUG_ENGINE, "select: Deleting socket %d [%p], state %s", s_fd(sock), | |
245 | sock, state_to_name(s_state(sock)))); | |
246 | ||
247 | FD_CLR(s_fd(sock), &global_read_set); /* clear event set bits */ | |
248 | FD_CLR(s_fd(sock), &global_write_set); | |
249 | ||
250 | sockList[s_fd(sock)] = 0; /* zero the socket list entry */ | |
251 | ||
252 | while (highest_fd > -1 && sockList[highest_fd] == 0) /* update highest_fd */ | |
253 | highest_fd--; | |
254 | } | |
255 | ||
256 | /** Run engine event loop. | |
257 | * @param[in] gen Lists of generators of various types. | |
258 | */ | |
259 | static void | |
260 | engine_loop(struct Generators* gen) | |
261 | { | |
262 | struct timeval wait; | |
263 | fd_set read_set; | |
264 | fd_set write_set; | |
265 | int nfds; | |
266 | int i; | |
267 | int errcode; | |
268 | size_t codesize; | |
269 | struct Socket *sock; | |
270 | ||
271 | while (running) { | |
272 | read_set = global_read_set; /* all hail structure copy!! */ | |
273 | write_set = global_write_set; | |
274 | ||
275 | /* set up the sleep time */ | |
276 | wait.tv_sec = timer_next(gen) ? (timer_next(gen) - CurrentTime) : -1; | |
277 | wait.tv_usec = 0; | |
278 | ||
279 | Debug((DEBUG_INFO, "select: delay: %Tu (%Tu) %Tu", timer_next(gen), | |
280 | CurrentTime, wait.tv_sec)); | |
281 | ||
282 | /* check for active files */ | |
283 | nfds = select(highest_fd + 1, &read_set, &write_set, 0, | |
284 | wait.tv_sec < 0 ? 0 : &wait); | |
285 | ||
286 | CurrentTime = time(0); /* set current time... */ | |
287 | ||
288 | if (nfds < 0) { | |
289 | if (errno != EINTR) { /* ignore select interrupts */ | |
290 | /* Log the select error */ | |
291 | log_write(LS_SOCKET, L_ERROR, 0, "select() error: %m"); | |
292 | if (!errors++) | |
293 | timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC, | |
294 | ERROR_EXPIRE_TIME); | |
295 | else if (errors > SELECT_ERROR_THRESHOLD) /* too many errors... */ | |
296 | server_restart("too many select errors"); | |
297 | } | |
298 | /* old code did a sleep(1) here; with usage these days, | |
299 | * that may be too expensive | |
300 | */ | |
301 | continue; | |
302 | } | |
303 | ||
304 | for (i = 0; nfds && i <= highest_fd; i++) { | |
305 | if (!(sock = sockList[i])) /* skip empty socket elements */ | |
306 | continue; | |
307 | ||
308 | assert(s_fd(sock) == i); | |
309 | ||
310 | gen_ref_inc(sock); /* can't have it going away on us */ | |
311 | ||
312 | Debug((DEBUG_ENGINE, "select: Checking socket %p (fd %d) state %s, " | |
313 | "events %s", sock, i, state_to_name(s_state(sock)), | |
314 | sock_flags(s_events(sock)))); | |
315 | ||
316 | if (s_state(sock) != SS_NOTSOCK) { | |
317 | errcode = 0; /* check for errors on socket */ | |
318 | codesize = sizeof(errcode); | |
319 | if (getsockopt(i, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0) | |
320 | errcode = errno; /* work around Solaris implementation */ | |
321 | ||
322 | if (errcode) { /* an error occurred; generate an event */ | |
323 | Debug((DEBUG_ENGINE, "select: Error %d on fd %d, socket %p", errcode, | |
324 | i, sock)); | |
325 | event_generate(ET_ERROR, sock, errcode); | |
326 | gen_ref_dec(sock); /* careful not to leak reference counts */ | |
327 | continue; | |
328 | } | |
329 | } | |
330 | ||
331 | switch (s_state(sock)) { | |
332 | case SS_CONNECTING: | |
333 | if (FD_ISSET(i, &write_set)) { /* connection completed */ | |
334 | Debug((DEBUG_ENGINE, "select: Connection completed")); | |
335 | event_generate(ET_CONNECT, sock, 0); | |
336 | nfds--; | |
337 | continue; | |
338 | } | |
339 | break; | |
340 | ||
341 | case SS_LISTENING: | |
342 | if (FD_ISSET(i, &read_set)) { /* connection to be accepted */ | |
343 | Debug((DEBUG_ENGINE, "select: Ready for accept")); | |
344 | event_generate(ET_ACCEPT, sock, 0); | |
345 | nfds--; | |
346 | } | |
347 | break; | |
348 | ||
349 | case SS_NOTSOCK: | |
350 | if (FD_ISSET(i, &read_set)) { /* data on socket */ | |
351 | /* can't peek; it's not a socket */ | |
352 | Debug((DEBUG_ENGINE, "select: non-socket readable")); | |
353 | event_generate(ET_READ, sock, 0); | |
354 | nfds--; | |
355 | } | |
356 | break; | |
357 | ||
358 | case SS_CONNECTED: | |
359 | if (FD_ISSET(i, &read_set)) { /* data to be read from socket */ | |
360 | char c; | |
361 | ||
362 | switch (recv(i, &c, 1, MSG_PEEK)) { /* check for EOF */ | |
363 | case -1: /* error occurred?!? */ | |
364 | if (errno == EAGAIN) { | |
365 | Debug((DEBUG_ENGINE, "select: Resource temporarily " | |
366 | "unavailable?")); | |
367 | continue; | |
368 | } | |
369 | Debug((DEBUG_ENGINE, "select: Uncaught error!")); | |
370 | event_generate(ET_ERROR, sock, errno); | |
371 | break; | |
372 | ||
373 | case 0: /* EOF from client */ | |
374 | Debug((DEBUG_ENGINE, "select: EOF from client")); | |
375 | event_generate(ET_EOF, sock, 0); | |
376 | break; | |
377 | ||
378 | default: /* some data can be read */ | |
379 | Debug((DEBUG_ENGINE, "select: Data to be read")); | |
380 | event_generate(ET_READ, sock, 0); | |
381 | break; | |
382 | } | |
383 | } | |
384 | if (FD_ISSET(i, &write_set)) { /* data can be written to socket */ | |
385 | Debug((DEBUG_ENGINE, "select: Data can be written")); | |
386 | event_generate(ET_WRITE, sock, 0); | |
387 | } | |
388 | if (FD_ISSET(i, &read_set) || FD_ISSET(i, &write_set)) | |
389 | nfds--; | |
390 | break; | |
391 | ||
392 | case SS_DATAGRAM: case SS_CONNECTDG: | |
393 | if (FD_ISSET(i, &read_set)) { /* data to be read from socket */ | |
394 | Debug((DEBUG_ENGINE, "select: Datagram to be read")); | |
395 | event_generate(ET_READ, sock, 0); | |
396 | } | |
397 | if (FD_ISSET(i, &write_set)) { /* data can be written to socket */ | |
398 | Debug((DEBUG_ENGINE, "select: Datagram can be written")); | |
399 | event_generate(ET_WRITE, sock, 0); | |
400 | } | |
401 | if (FD_ISSET(i, &read_set) || FD_ISSET(i, &write_set)) | |
402 | nfds--; | |
403 | break; | |
404 | } | |
405 | ||
406 | assert(s_fd(sock) == i); | |
407 | ||
408 | gen_ref_dec(sock); /* we're done with it */ | |
409 | } | |
410 | ||
411 | timer_run(); /* execute any pending timers */ | |
412 | } | |
413 | } | |
414 | ||
415 | /** Descriptor for select() event engine. */ | |
416 | struct Engine engine_select = { | |
417 | "select()", /* Engine name */ | |
418 | engine_init, /* Engine initialization function */ | |
419 | 0, /* Engine signal registration function (none) */ | |
420 | engine_add, /* Engine socket registration function */ | |
421 | engine_state, /* Engine socket state change function */ | |
422 | engine_events, /* Engine socket events mask function */ | |
423 | engine_delete, /* Engine socket deletion function */ | |
424 | engine_loop /* Core engine event loop */ | |
425 | }; |