Author: Ghostwolf <foxxe@wtfs.net>
[ircu2.10.12-pk.git] / ircd / engine_devpoll.c
1 /*
2  * IRC - Internet Relay Chat, ircd/engine_devpoll.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_alloc.h"
27 #include "ircd_features.h"
28 #include "ircd_log.h"
29 #include "s_debug.h"
30
31 #include <assert.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <sys/devpoll.h>
35 #include <sys/poll.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #define DEVPOLL_ERROR_THRESHOLD 20      /* after 20 devpoll errors, restart */
42 #define ERROR_EXPIRE_TIME       3600    /* expire errors after an hour */
43
44 /* Figure out what bits to set for read */
45 #if defined(POLLMSG) && defined(POLLIN) && defined(POLLRDNORM)
46 #  define POLLREADFLAGS (POLLMSG|POLLIN|POLLRDNORM)
47 #elif defined(POLLIN) && defined(POLLRDNORM)
48 #  define POLLREADFLAGS (POLLIN|POLLRDNORM)
49 #elif defined(POLLIN)
50 #  define POLLREADFLAGS POLLIN
51 #elif defined(POLLRDNORM)
52 #  define POLLREADFLAGS POLLRDNORM
53 #endif
54
55 /* Figure out what bits to set for write */
56 #if defined(POLLOUT) && defined(POLLWRNORM)
57 #  define POLLWRITEFLAGS (POLLOUT|POLLWRNORM)
58 #elif defined(POLLOUT)
59 #  define POLLWRITEFLAGS POLLOUT
60 #elif defined(POLLWRNORM)
61 #  define POLLWRITEFLAGS POLLWRNORM
62 #endif
63
64 static struct Socket** sockList;
65 static int devpoll_max;
66 static int devpoll_fd;
67
68 static int errors = 0;
69 static struct Timer clear_error;
70
71 /* decrements the error count once per hour */
72 static void
73 error_clear(struct Event* ev)
74 {
75   if (!--errors) /* remove timer when error count reaches 0 */
76     timer_del(ev_timer(ev));
77 }
78
79 /* initialize the devpoll engine */
80 static int
81 engine_init(int max_sockets)
82 {
83   int i;
84
85   if ((devpoll_fd = open("/dev/poll", O_RDWR)) < 0) {
86     log_write(LS_SYSTEM, L_WARNING, 0,
87               "/dev/poll engine cannot open device: %m");
88     return 0; /* engine cannot be initialized; defer */
89   }
90
91   /* allocate necessary memory */
92   sockList = (struct Socket**) MyMalloc(sizeof(struct Socket*) * max_sockets);
93
94   /* initialize the data */
95   for (i = 0; i < max_sockets; i++)
96     sockList[i] = 0;
97
98   devpoll_max = max_sockets; /* number of sockets allocated */
99
100   return 1;
101 }
102
103 /* Figure out what events go with a given state */
104 static unsigned int
105 state_to_events(enum SocketState state, unsigned int events)
106 {
107   switch (state) {
108   case SS_CONNECTING: /* connecting socket */
109     return SOCK_EVENT_WRITABLE;
110     break;
111
112   case SS_LISTENING: /* listening socket */
113   case SS_NOTSOCK: /* our signal socket */
114     return SOCK_EVENT_READABLE;
115     break;
116
117   case SS_CONNECTED: case SS_DATAGRAM: case SS_CONNECTDG:
118     return events; /* ordinary socket */
119     break;
120   }
121
122   /*NOTREACHED*/
123   return 0;
124 }
125
126 /* Reset the desired events */
127 static void
128 set_events(struct Socket* sock, unsigned int events)
129 {
130   struct pollfd pfd;
131
132   pfd.fd = s_fd(sock);
133
134   if (s_ed_int(sock)) { /* is one in /dev/poll already? */
135     pfd.events = POLLREMOVE; /* First, remove old pollfd */
136
137     Debug((DEBUG_ENGINE, "devpoll: Removing old entry for socket %d [%p]",
138            s_fd(sock), sock));
139
140     if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd)) {
141       event_generate(ET_ERROR, sock, errno); /* report error */
142       return;
143     }
144
145     s_ed_int(sock) = 0; /* mark that it's gone */
146   }
147
148   if (!(events & SOCK_EVENT_MASK)) /* no events, so stop here */
149     return;
150
151   pfd.events = 0; /* Now, set up new pollfd... */
152   if (events & SOCK_EVENT_READABLE)
153     pfd.events |= POLLREADFLAGS; /* look for readable conditions */
154   if (events & SOCK_EVENT_WRITABLE)
155     pfd.events |= POLLWRITEFLAGS; /* look for writable conditions */
156
157   Debug((DEBUG_ENGINE, "devpoll: Registering interest on %d [%p] (state %s, "
158          "mask [%s])", s_fd(sock), sock, state_to_name(s_state(sock)),
159          sock_flags(s_events(sock))));
160
161   if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd)) {
162     event_generate(ET_ERROR, sock, errno); /* report error */
163     return;
164   }
165
166   s_ed_int(sock) = 1; /* mark that we've added a pollfd */
167 }
168
169 /* add a socket to be listened on */
170 static int
171 engine_add(struct Socket* sock)
172 {
173   assert(0 != sock);
174   assert(0 == sockList[s_fd(sock)]);
175
176   /* bounds-check... */
177   if (s_fd(sock) >= devpoll_max) {
178     log_write(LS_SYSTEM, L_ERROR, 0,
179               "Attempt to add socket %d (> %d) to event engine", s_fd(sock),
180               devpoll_max);
181     return 0;
182   }
183
184   sockList[s_fd(sock)] = sock; /* add to list */
185
186   Debug((DEBUG_ENGINE, "devpoll: Adding socket %d [%p], state %s, to engine",
187          s_fd(sock), sock, state_to_name(s_state(sock))));
188
189   /* set the correct events */
190   set_events(sock, state_to_events(s_state(sock), s_events(sock)));
191
192   return 1; /* success */
193 }
194
195 /* socket switching to new state */
196 static void
197 engine_state(struct Socket* sock, enum SocketState new_state)
198 {
199   assert(0 != sock);
200   assert(sock == sockList[s_fd(sock)]);
201
202   Debug((DEBUG_ENGINE, "devpoll: Changing state for socket %p to %s", sock,
203          state_to_name(new_state)));
204
205   /* set the correct events */
206   set_events(sock, state_to_events(new_state, s_events(sock)));
207 }
208
209 /* socket events changing */
210 static void
211 engine_events(struct Socket* sock, unsigned int new_events)
212 {
213   assert(0 != sock);
214   assert(sock == sockList[s_fd(sock)]);
215
216   Debug((DEBUG_ENGINE, "devpoll: Changing event mask for socket %p to [%s]",
217          sock, sock_flags(new_events)));
218
219   /* set the correct events */
220   set_events(sock, state_to_events(s_state(sock), new_events));
221 }
222
223 /* socket going away */
224 static void
225 engine_delete(struct Socket* sock)
226 {
227   assert(0 != sock);
228   assert(sock == sockList[s_fd(sock)]);
229
230   Debug((DEBUG_ENGINE, "devpoll: Deleting socket %d [%p], state %s",
231          s_fd(sock), sock, state_to_name(s_state(sock))));
232
233   set_events(sock, 0); /* get rid of the socket */
234
235   sockList[s_fd(sock)] = 0; /* zero the socket list entry */
236 }
237
238 /* engine event loop */
239 static void
240 engine_loop(struct Generators* gen)
241 {
242   struct dvpoll dopoll;
243   struct pollfd *polls;
244   int polls_count;
245   struct Socket* sock;
246   int nfds;
247   int i;
248   int errcode;
249   size_t codesize;
250
251   if ((polls_count = feature_int(FEAT_POLLS_PER_LOOP)) < 20)
252     polls_count = 20;
253   polls = (struct pollfd *)MyMalloc(sizeof(struct pollfd) * polls_count);
254
255   while (running) {
256     if ((i = feature_int(FEAT_POLLS_PER_LOOP)) >= 20 && i != polls_count) {
257       polls = (struct pollfd *)MyRealloc(polls, sizeof(struct pollfd) * i);
258       polls_count = i;
259     }
260
261     dopoll.dp_fds = polls; /* set up the struct dvpoll */
262     dopoll.dp_nfds = polls_count;
263
264     /* calculate the proper timeout */
265     dopoll.dp_timeout = timer_next(gen) ?
266       (timer_next(gen) - CurrentTime) * 1000 : -1;
267
268     Debug((DEBUG_INFO, "devpoll: delay: %Tu (%Tu) %d", timer_next(gen),
269            CurrentTime, dopoll.dp_timeout));
270
271     /* check for active files */
272     nfds = ioctl(devpoll_fd, DP_POLL, &dopoll);
273
274     CurrentTime = time(0); /* set current time... */
275
276     if (nfds < 0) {
277       if (errno != EINTR) { /* ignore interrupts */
278         /* Log the poll error */
279         log_write(LS_SOCKET, L_ERROR, 0, "ioctl(DP_POLL) error: %m");
280         if (!errors++)
281           timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC,
282                     ERROR_EXPIRE_TIME);
283         else if (errors > DEVPOLL_ERROR_THRESHOLD) /* too many errors... */
284           server_restart("too many /dev/poll errors");
285       }
286       /* old code did a sleep(1) here; with usage these days,
287        * that may be too expensive
288        */
289       continue;
290     }
291
292     for (i = 0; i < nfds; i++) {
293       assert(-1 < polls[i].fd);
294
295       sock = sockList[polls[i].fd];
296       if (!sock) /* slots may become empty while processing events */
297         continue;
298
299       assert(s_fd(sock) == polls[i].fd);
300
301       gen_ref_inc(sock); /* can't have it going away on us */
302
303       Debug((DEBUG_ENGINE, "devpoll: Checking socket %p (fd %d) state %s, "
304              "events %s", sock, s_fd(sock), state_to_name(s_state(sock)),
305              sock_flags(s_events(sock))));
306
307       if (s_state(sock) != SS_NOTSOCK) {
308         errcode = 0; /* check for errors on socket */
309         codesize = sizeof(errcode);
310         if (getsockopt(s_fd(sock), SOL_SOCKET, SO_ERROR, &errcode,
311                        &codesize) < 0)
312           errcode = errno; /* work around Solaris implementation */
313
314         if (errcode) { /* an error occurred; generate an event */
315           Debug((DEBUG_ENGINE, "devpoll: Error %d on fd %d, socket %p",
316                  errcode, s_fd(sock), sock));
317           event_generate(ET_ERROR, sock, errcode);
318           gen_ref_dec(sock); /* careful not to leak reference counts */
319           continue;
320         }
321       }
322
323       assert(!(polls[i].revents & POLLERR));
324
325 #ifdef POLLHUP
326       if (polls[i].revents & POLLHUP) { /* hang-up on socket */
327         Debug((DEBUG_ENGINE, "devpoll: EOF from client (POLLHUP)"));
328         event_generate(ET_EOF, sock, 0);
329         nfds--;
330         continue;
331       }
332 #endif /* POLLHUP */
333
334       switch (s_state(sock)) {
335       case SS_CONNECTING:
336         if (polls[i].revents & POLLWRITEFLAGS) { /* connection completed */
337           Debug((DEBUG_ENGINE, "devpoll: Connection completed"));
338           event_generate(ET_CONNECT, sock, 0);
339         }
340         break;
341
342       case SS_LISTENING:
343         if (polls[i].revents & POLLREADFLAGS) { /* connect. to be accept. */
344           Debug((DEBUG_ENGINE, "devpoll: Ready for accept"));
345           event_generate(ET_ACCEPT, sock, 0);
346         }
347         break;
348
349       case SS_NOTSOCK:
350         if (polls[i].revents & POLLREADFLAGS) { /* data on socket */
351           /* can't peek; it's not a socket */
352           Debug((DEBUG_ENGINE, "devpoll: non-socket readable"));
353           event_generate(ET_READ, sock, 0);
354         }
355         break;
356
357       case SS_CONNECTED:
358         if (polls[i].revents & POLLREADFLAGS) { /* data on socket */
359           char c;
360
361           switch (recv(s_fd(sock), &c, 1, MSG_PEEK)) { /* check EOF */
362           case -1: /* error occurred?!? */
363             if (errno == EAGAIN) {
364               Debug((DEBUG_ENGINE, "devpoll: Resource temporarily "
365                      "unavailable?"));
366               continue;
367             }
368             Debug((DEBUG_ENGINE, "devpoll: Uncaught error!"));
369             event_generate(ET_ERROR, sock, errno);
370             break;
371
372           case 0: /* EOF from client */
373             Debug((DEBUG_ENGINE, "devpoll: EOF from client"));
374             event_generate(ET_EOF, sock, 0);
375             break;
376
377           default: /* some data can be read */
378             Debug((DEBUG_ENGINE, "devpoll: Data to be read"));
379             event_generate(ET_READ, sock, 0);
380             break;
381           }
382         }
383         if (polls[i].revents & POLLWRITEFLAGS) { /* socket writable */
384           Debug((DEBUG_ENGINE, "devpoll: Data can be written"));
385           event_generate(ET_WRITE, sock, 0);
386         }
387         break;
388
389       case SS_DATAGRAM: case SS_CONNECTDG:
390         if (polls[i].revents & POLLREADFLAGS) { /* socket readable */
391           Debug((DEBUG_ENGINE, "devpoll: Datagram to be read"));
392           event_generate(ET_READ, sock, 0);
393         }
394         if (polls[i].revents & POLLWRITEFLAGS) { /* socket writable */
395           Debug((DEBUG_ENGINE, "devpoll: Datagram can be written"));
396           event_generate(ET_WRITE, sock, 0);
397         }
398         break;
399       }
400
401       assert(s_fd(sock) == polls[i].fd);
402
403       gen_ref_dec(sock); /* we're done with it */
404     }
405
406     timer_run(); /* execute any pending timers */
407   }
408 }
409
410 struct Engine engine_devpoll = {
411   "/dev/poll",          /* Engine name */
412   engine_init,          /* Engine initialization function */
413   0,                    /* Engine signal registration function */
414   engine_add,           /* Engine socket registration function */
415   engine_state,         /* Engine socket state change function */
416   engine_events,        /* Engine socket events mask function */
417   engine_delete,        /* Engine socket deletion function */
418   engine_loop           /* Core engine event loop */
419 };