Fixes to improve portability (especially to OS X, Solaris, OpenBSD).
[ircu2.10.12-pk.git] / ircd / engine_select.c
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  * $Id$
20  */
21 #include "config.h"
22
23 #include "ircd_events.h"
24
25 #include "ircd.h"
26 #include "ircd_log.h"
27 #include "s_debug.h"
28
29 /* On BSD, define FD_SETSIZE to what we want before including sys/types.h */
30 #if  defined(__FreeBSD__) || defined(__NetBSD__) || defined(__bsdi__)
31 # if !defined(FD_SETSIZE)
32 #  define FD_SETSIZE    MAXCONNECTIONS
33 # endif
34 #endif
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <string.h> /* needed for bzero() on OS X */
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 #define SELECT_ERROR_THRESHOLD  20      /* after 20 select errors, restart */
46 #define ERROR_EXPIRE_TIME       3600    /* expire errors after an hour */
47
48 static struct Socket* sockList[FD_SETSIZE];
49 static int highest_fd;
50 static fd_set global_read_set;
51 static fd_set global_write_set;
52
53 static int errors = 0;
54 static struct Timer clear_error;
55
56 /* decrements the error count once per hour */
57 static void
58 error_clear(struct Event* ev)
59 {
60   if (!--errors) /* remove timer when error count reaches 0 */
61     timer_del(ev_timer(ev));
62 }
63
64 /* initialize the select engine */
65 static int
66 engine_init(int max_sockets)
67 {
68   int i;
69
70   if (max_sockets > FD_SETSIZE) { /* too many sockets */
71     log_write(LS_SYSTEM, L_WARNING, 0,
72               "select() engine cannot handle %d sockets (> %d)",
73               max_sockets, FD_SETSIZE);
74     return 0;
75   }
76
77   FD_ZERO(&global_read_set); /* zero the global fd sets */
78   FD_ZERO(&global_write_set);
79
80   for (i = 0; i < FD_SETSIZE; i++) /* zero the sockList */
81     sockList[i] = 0;
82
83   highest_fd = -1; /* No fds in set */
84
85   return 1; /* initialization successful */
86 }
87
88 /* Figure out what events go with a given state */
89 static unsigned int
90 state_to_events(enum SocketState state, unsigned int events)
91 {
92   switch (state) {
93   case SS_CONNECTING: /* connecting socket */
94     return SOCK_EVENT_WRITABLE;
95     break;
96
97   case SS_LISTENING: /* listening socket */
98   case SS_NOTSOCK: /* our signal socket */
99     return SOCK_EVENT_READABLE;
100     break;
101
102   case SS_CONNECTED: case SS_DATAGRAM: case SS_CONNECTDG:
103     return events; /* ordinary socket */
104     break;
105   }
106
107   /*NOTREACHED*/
108   return 0;
109 }
110
111 /* Toggle bits in the global fd sets appropriately */
112 static void
113 set_or_clear(int fd, unsigned int clear, unsigned int set)
114 {
115   if ((clear ^ set) & SOCK_EVENT_READABLE) { /* readable has changed */
116     if (set & SOCK_EVENT_READABLE) /* it's set */
117       FD_SET(fd, &global_read_set);
118     else /* clear it */
119       FD_CLR(fd, &global_read_set);
120   }
121
122   if ((clear ^ set) & SOCK_EVENT_WRITABLE) { /* writable has changed */
123     if (set & SOCK_EVENT_WRITABLE) /* it's set */
124       FD_SET(fd, &global_write_set);
125     else /* clear it */
126       FD_CLR(fd, &global_write_set);
127   }
128 }
129
130 /* add a socket to be listened on */
131 static int
132 engine_add(struct Socket* sock)
133 {
134   assert(0 != sock);
135   assert(0 == sockList[s_fd(sock)]);
136
137   /* bounds-check... */
138   if (s_fd(sock) >= FD_SETSIZE) {
139     log_write(LS_SYSTEM, L_ERROR, 0,
140               "Attempt to add socket %d (> %d) to event engine", s_fd(sock),
141               FD_SETSIZE);
142     return 0;
143   }
144
145   sockList[s_fd(sock)] = sock; /* add to list */
146
147   if (s_fd(sock) >= highest_fd) /* update highest_fd */
148     highest_fd = s_fd(sock);
149
150   Debug((DEBUG_ENGINE, "select: Adding socket %d to engine [%p], state %s",
151          s_fd(sock), sock, state_to_name(s_state(sock))));
152
153   /* set the fd set bits */
154   set_or_clear(s_fd(sock), 0, state_to_events(s_state(sock), s_events(sock)));
155
156   return 1; /* success */
157 }
158
159 /* socket switching to new state */
160 static void
161 engine_state(struct Socket* sock, enum SocketState new_state)
162 {
163   assert(0 != sock);
164   assert(sock == sockList[s_fd(sock)]);
165
166   Debug((DEBUG_ENGINE, "select: Changing state for socket %p to %s", sock,
167          state_to_name(new_state)));
168
169   /* set the correct events */
170   set_or_clear(s_fd(sock),
171                state_to_events(s_state(sock), s_events(sock)), /* old state */
172                state_to_events(new_state, s_events(sock))); /* new state */
173 }
174
175 /* socket events changing */
176 static void
177 engine_events(struct Socket* sock, unsigned int new_events)
178 {
179   assert(0 != sock);
180   assert(sock == sockList[s_fd(sock)]);
181
182   Debug((DEBUG_ENGINE, "select: Changing event mask for socket %p to [%s]",
183          sock, sock_flags(new_events)));
184
185   /* set the correct events */
186   set_or_clear(s_fd(sock),
187                state_to_events(s_state(sock), s_events(sock)), /* old events */
188                state_to_events(s_state(sock), new_events)); /* new events */
189 }
190
191 /* socket going away */
192 static void
193 engine_delete(struct Socket* sock)
194 {
195   assert(0 != sock);
196   assert(sock == sockList[s_fd(sock)]);
197
198   Debug((DEBUG_ENGINE, "select: Deleting socket %d [%p], state %s", s_fd(sock),
199          sock, state_to_name(s_state(sock))));
200
201   FD_CLR(s_fd(sock), &global_read_set); /* clear event set bits */
202   FD_CLR(s_fd(sock), &global_write_set);
203
204   sockList[s_fd(sock)] = 0; /* zero the socket list entry */
205
206   while (highest_fd > -1 && sockList[highest_fd] == 0) /* update highest_fd */
207     highest_fd--;
208 }
209
210 /* engine event loop */
211 static void
212 engine_loop(struct Generators* gen)
213 {
214   struct timeval wait;
215   fd_set read_set;
216   fd_set write_set;
217   int nfds;
218   int i;
219   int errcode;
220   size_t codesize;
221   struct Socket *sock;
222
223   while (running) {
224     read_set = global_read_set; /* all hail structure copy!! */
225     write_set = global_write_set;
226
227     /* set up the sleep time */
228     wait.tv_sec = timer_next(gen) ? (timer_next(gen) - CurrentTime) : -1;
229     wait.tv_usec = 0;
230
231     Debug((DEBUG_INFO, "select: delay: %Tu (%Tu) %Tu", timer_next(gen),
232            CurrentTime, wait.tv_sec));
233
234     /* check for active files */
235     nfds = select(highest_fd + 1, &read_set, &write_set, 0,
236                   wait.tv_sec < 0 ? 0 : &wait);
237
238     CurrentTime = time(0); /* set current time... */
239
240     if (nfds < 0) {
241       if (errno != EINTR) { /* ignore select interrupts */
242         /* Log the select error */
243         log_write(LS_SOCKET, L_ERROR, 0, "select() error: %m");
244         if (!errors++)
245           timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC,
246                     ERROR_EXPIRE_TIME);
247         else if (errors > SELECT_ERROR_THRESHOLD) /* too many errors... */
248           server_restart("too many select errors");
249       }
250       /* old code did a sleep(1) here; with usage these days,
251        * that may be too expensive
252        */
253       continue;
254     }
255
256     for (i = 0; nfds && i <= highest_fd; i++) {
257       if (!(sock = sockList[i])) /* skip empty socket elements */
258         continue;
259
260       assert(s_fd(sock) == i);
261
262       gen_ref_inc(sock); /* can't have it going away on us */
263
264       Debug((DEBUG_ENGINE, "select: Checking socket %p (fd %d) state %s, "
265              "events %s", sock, i, state_to_name(s_state(sock)),
266              sock_flags(s_events(sock))));
267
268       if (s_state(sock) != SS_NOTSOCK) {
269         errcode = 0; /* check for errors on socket */
270         codesize = sizeof(errcode);
271         if (getsockopt(i, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
272           errcode = errno; /* work around Solaris implementation */
273
274         if (errcode) { /* an error occurred; generate an event */
275           Debug((DEBUG_ENGINE, "select: Error %d on fd %d, socket %p", errcode,
276                  i, sock));
277           event_generate(ET_ERROR, sock, errcode);
278           gen_ref_dec(sock); /* careful not to leak reference counts */
279           continue;
280         }
281       }
282
283       switch (s_state(sock)) {
284       case SS_CONNECTING:
285         if (FD_ISSET(i, &write_set)) { /* connection completed */
286           Debug((DEBUG_ENGINE, "select: Connection completed"));
287           event_generate(ET_CONNECT, sock, 0);
288           nfds--;
289           continue;
290         }
291         break;
292
293       case SS_LISTENING:
294         if (FD_ISSET(i, &read_set)) { /* connection to be accepted */
295           Debug((DEBUG_ENGINE, "select: Ready for accept"));
296           event_generate(ET_ACCEPT, sock, 0);
297           nfds--;
298         }
299         break;
300
301       case SS_NOTSOCK:
302         if (FD_ISSET(i, &read_set)) { /* data on socket */
303           /* can't peek; it's not a socket */
304           Debug((DEBUG_ENGINE, "select: non-socket readable"));
305           event_generate(ET_READ, sock, 0);
306           nfds--;
307         }
308         break;
309
310       case SS_CONNECTED:
311         if (FD_ISSET(i, &read_set)) { /* data to be read from socket */
312           char c;
313
314           switch (recv(i, &c, 1, MSG_PEEK)) { /* check for EOF */
315           case -1: /* error occurred?!? */
316             if (errno == EAGAIN) {
317               Debug((DEBUG_ENGINE, "select: Resource temporarily "
318                      "unavailable?"));
319               continue;
320             }
321             Debug((DEBUG_ENGINE, "select: Uncaught error!"));
322             event_generate(ET_ERROR, sock, errno);
323             break;
324
325           case 0: /* EOF from client */
326             Debug((DEBUG_ENGINE, "select: EOF from client"));
327             event_generate(ET_EOF, sock, 0);
328             break;
329
330           default: /* some data can be read */
331             Debug((DEBUG_ENGINE, "select: Data to be read"));
332             event_generate(ET_READ, sock, 0);
333             break;
334           }
335         }
336         if (FD_ISSET(i, &write_set)) { /* data can be written to socket */
337           Debug((DEBUG_ENGINE, "select: Data can be written"));
338           event_generate(ET_WRITE, sock, 0);
339         }
340         if (FD_ISSET(i, &read_set) || FD_ISSET(i, &write_set))
341           nfds--;
342         break;
343
344       case SS_DATAGRAM: case SS_CONNECTDG:
345         if (FD_ISSET(i, &read_set)) { /* data to be read from socket */
346           Debug((DEBUG_ENGINE, "select: Datagram to be read"));
347           event_generate(ET_READ, sock, 0);
348         }
349         if (FD_ISSET(i, &write_set)) { /* data can be written to socket */
350           Debug((DEBUG_ENGINE, "select: Datagram can be written"));
351           event_generate(ET_WRITE, sock, 0);
352         }
353         if (FD_ISSET(i, &read_set) || FD_ISSET(i, &write_set))
354           nfds--;
355         break;
356       }
357
358       assert(s_fd(sock) == i);
359
360       gen_ref_dec(sock); /* we're done with it */
361     }
362
363     timer_run(); /* execute any pending timers */
364   }
365 }
366
367 struct Engine engine_select = {
368   "select()",           /* Engine name */
369   engine_init,          /* Engine initialization function */
370   0,                    /* Engine signal registration function (none) */
371   engine_add,           /* Engine socket registration function */
372   engine_state,         /* Engine socket state change function */
373   engine_events,        /* Engine socket events mask function */
374   engine_delete,        /* Engine socket deletion function */
375   engine_loop           /* Core engine event loop */
376 };