2 * IRC - Internet Relay Chat, ircd/engine_poll.c
3 * Copyright (C) 2001 Kevin L. Mitchell <klmitch@mit.edu>
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)
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.
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.
23 #include "ircd_events.h"
26 #include "ircd_alloc.h"
33 #include <sys/socket.h>
34 #include <sys/types.h>
38 #define POLL_ERROR_THRESHOLD 20 /* after 20 poll errors, restart */
39 #define ERROR_EXPIRE_TIME 3600 /* expire errors after an hour */
41 /* Figure out what bits to set for read */
42 #if defined(POLLMSG) && defined(POLLIN) && defined(POLLRDNORM)
43 # define POLLREADFLAGS (POLLMSG|POLLIN|POLLRDNORM)
44 #elif defined(POLLIN) && defined(POLLRDNORM)
45 # define POLLREADFLAGS (POLLIN|POLLRDNORM)
47 # define POLLREADFLAGS POLLIN
48 #elif defined(POLLRDNORM)
49 # define POLLREADFLAGS POLLRDNORM
52 /* Figure out what bits to set for write */
53 #if defined(POLLOUT) && defined(POLLWRNORM)
54 # define POLLWRITEFLAGS (POLLOUT|POLLWRNORM)
55 #elif defined(POLLOUT)
56 # define POLLWRITEFLAGS POLLOUT
57 #elif defined(POLLWRNORM)
58 # define POLLWRITEFLAGS POLLWRNORM
61 /* Figure out what bits indicate errors */
63 # define POLLERRORS (POLLHUP|POLLERR)
65 # define POLLERRORS POLLERR
68 static struct Socket** sockList;
69 static struct pollfd* pollfdList;
70 static unsigned int poll_count;
71 static unsigned int poll_max;
73 static int errors = 0;
74 static struct Timer clear_error;
76 /* decrements the error count once per hour */
78 error_clear(struct Event* ev)
80 if (!--errors) /* remove timer when error count reaches 0 */
81 timer_del(ev_timer(ev));
84 /* initialize the poll engine */
86 engine_init(int max_sockets)
90 /* allocate necessary memory */
91 sockList = (struct Socket**) MyMalloc(sizeof(struct Socket*) * max_sockets);
92 pollfdList = (struct pollfd*) MyMalloc(sizeof(struct pollfd) * max_sockets);
94 /* initialize the data */
95 for (i = 0; i < max_sockets; i++) {
97 pollfdList[i].fd = -1;
98 pollfdList[i].events = 0;
99 pollfdList[i].revents = 0;
102 poll_count = 0; /* nothing in set */
103 poll_max = max_sockets; /* number of sockets allocated */
108 /* Figure out what events go with a given state */
110 state_to_events(enum SocketState state, unsigned int events)
113 case SS_CONNECTING: /* connecting socket */
114 return SOCK_EVENT_WRITABLE;
117 case SS_LISTENING: /* listening socket */
118 case SS_NOTSOCK: /* our signal socket */
119 return SOCK_EVENT_READABLE;
122 case SS_CONNECTED: case SS_DATAGRAM: case SS_CONNECTDG:
123 return events; /* ordinary socket */
131 /* Toggle bits in the pollfd structs correctly */
133 set_or_clear(int idx, unsigned int clear, unsigned int set)
135 if ((clear ^ set) & SOCK_EVENT_READABLE) { /* readable has changed */
136 if (set & SOCK_EVENT_READABLE) /* it's set */
137 pollfdList[idx].events |= POLLREADFLAGS;
139 pollfdList[idx].events &= ~POLLREADFLAGS;
142 if ((clear ^ set) & SOCK_EVENT_WRITABLE) { /* writable has changed */
143 if (set & SOCK_EVENT_WRITABLE) /* it's set */
144 pollfdList[idx].events |= POLLWRITEFLAGS;
146 pollfdList[idx].events &= ~POLLWRITEFLAGS;
150 /* add a socket to be listened on */
152 engine_add(struct Socket* sock)
158 for (i = 0; sockList[i] && i < poll_count; i++) /* Find an empty slot */
161 Debug((DEBUG_ENGINE, "poll: Looking at slot %d, contents %p", i,
164 if (i >= poll_count) { /* ok, need to allocate another off the list */
165 if (poll_count >= poll_max) { /* bounds-check... */
166 log_write(LS_SYSTEM, L_ERROR, 0,
167 "Attempt to add socket %d (> %d) to event engine", sock->s_fd,
173 Debug((DEBUG_ENGINE, "poll: Allocating a new slot: %d", i));
176 s_ed_int(sock) = i; /* set engine data */
177 sockList[i] = sock; /* enter socket into data structures */
178 pollfdList[i].fd = s_fd(sock);
180 Debug((DEBUG_ENGINE, "poll: Adding socket %d to engine on %d [%p], state %s",
181 s_fd(sock), s_ed_int(sock), sock, state_to_name(s_state(sock))));
183 /* set the appropriate bits */
184 set_or_clear(i, 0, state_to_events(s_state(sock), s_events(sock)));
186 return 1; /* success */
189 /* socket switching to new state */
191 engine_state(struct Socket* sock, enum SocketState new_state)
194 assert(sock == sockList[s_ed_int(sock)]);
195 assert(s_fd(sock) == pollfdList[s_ed_int(sock)].fd);
197 Debug((DEBUG_ENGINE, "poll: Changing state for socket %p to %s", sock,
198 state_to_name(new_state)));
200 /* set the correct events */
201 set_or_clear(s_ed_int(sock),
202 state_to_events(s_state(sock), s_events(sock)), /* old state */
203 state_to_events(new_state, s_events(sock))); /* new state */
206 /* socket events changing */
208 engine_events(struct Socket* sock, unsigned int new_events)
211 assert(sock == sockList[s_ed_int(sock)]);
212 assert(s_fd(sock) == pollfdList[s_ed_int(sock)].fd);
214 Debug((DEBUG_ENGINE, "poll: Changing event mask for socket %p to [%s]", sock,
215 sock_flags(new_events)));
217 /* set the correct events */
218 set_or_clear(s_ed_int(sock),
219 state_to_events(s_state(sock), s_events(sock)), /* old events */
220 state_to_events(s_state(sock), new_events)); /* new events */
223 /* socket going away */
225 engine_delete(struct Socket* sock)
228 assert(sock == sockList[s_ed_int(sock)]);
229 assert(s_fd(sock) == pollfdList[s_ed_int(sock)].fd);
231 Debug((DEBUG_ENGINE, "poll: Deleting socket %d (%d) [%p], state %s",
232 s_fd(sock), s_ed_int(sock), sock, state_to_name(s_state(sock))));
234 /* clear the events */
235 pollfdList[s_ed_int(sock)].fd = -1;
236 pollfdList[s_ed_int(sock)].events = 0;
238 /* zero the socket list entry */
239 sockList[s_ed_int(sock)] = 0;
241 /* update poll_count */
242 while (poll_count > 0 && sockList[poll_count - 1] == 0)
246 /* socket event loop */
248 engine_loop(struct Generators* gen)
258 wait = timer_next(gen) ? (timer_next(gen) - CurrentTime) * 1000 : -1;
260 Debug((DEBUG_INFO, "poll: delay: %Tu (%Tu) %d", timer_next(gen),
263 /* check for active files */
264 nfds = poll(pollfdList, poll_count, wait);
266 CurrentTime = time(0); /* set current time... */
269 if (errno != EINTR) { /* ignore poll interrupts */
270 /* Log the poll error */
271 log_write(LS_SOCKET, L_ERROR, 0, "poll() error: %m");
273 timer_add(&clear_error, error_clear, 0, TT_PERIODIC,
275 else if (errors > POLL_ERROR_THRESHOLD) /* too many errors... */
276 server_restart("too many poll errors");
278 /* old code did a sleep(1) here; with usage these days,
279 * that may be too expensive
284 for (i = 0; nfds && i < poll_count; i++) {
285 if (!(sock = sockList[i])) /* skip empty socket elements */
288 assert(s_fd(sock) == pollfdList[i].fd);
290 gen_ref_inc(sock); /* can't have it going away on us */
292 Debug((DEBUG_ENGINE, "poll: Checking socket %p (fd %d, index %d, "
293 "state %s, events %s", sock, s_fd(sock), i,
294 state_to_name(s_state(sock)), sock_flags(s_events(sock))));
296 if (s_state(sock) != SS_NOTSOCK) {
297 errcode = 0; /* check for errors on socket */
298 codesize = sizeof(errcode);
299 if (getsockopt(s_fd(sock), SOL_SOCKET, SO_ERROR, &errcode,
301 errcode = errno; /* work around Solaris implementation */
303 if (errcode) { /* an error occurred; generate an event */
304 Debug((DEBUG_ENGINE, "poll: Error %d on fd %d (index %d), socket %p",
305 errcode, s_fd(sock), i, sock));
306 event_generate(ET_ERROR, sock, errcode);
307 gen_ref_dec(sock); /* careful not to leak ref counts */
313 switch (s_state(sock)) {
315 if (pollfdList[i].revents & POLLWRITEFLAGS) { /* connect completed */
316 Debug((DEBUG_ENGINE, "poll: Connection completed"));
317 event_generate(ET_CONNECT, sock, 0);
323 if (pollfdList[i].revents & POLLREADFLAGS) { /* ready for accept */
324 Debug((DEBUG_ENGINE, "poll: Ready for accept"));
325 event_generate(ET_ACCEPT, sock, 0);
331 if (pollfdList[i].revents & POLLREADFLAGS) { /* data on socket */
332 /* can't peek; it's not a socket */
333 Debug((DEBUG_ENGINE, "poll: non-socket readable"));
334 event_generate(ET_READ, sock, 0);
340 if (pollfdList[i].revents & POLLREADFLAGS) { /* data on socket */
343 switch (recv(s_fd(sock), &c, 1, MSG_PEEK)) { /* check EOF */
344 case -1: /* error occurred?!? */
345 if (errno == EAGAIN) {
346 Debug((DEBUG_ENGINE, "poll: Resource temporarily unavailable?"));
349 Debug((DEBUG_ENGINE, "poll: Uncaught error!"));
350 event_generate(ET_ERROR, sock, errno);
353 case 0: /* EOF from client */
354 Debug((DEBUG_ENGINE, "poll: EOF from client"));
355 event_generate(ET_EOF, sock, 0);
358 default: /* some data can be read */
359 Debug((DEBUG_ENGINE, "poll: Data to be read"));
360 event_generate(ET_READ, sock, 0);
364 if (pollfdList[i].revents & POLLWRITEFLAGS) { /* socket writable */
365 Debug((DEBUG_ENGINE, "poll: Data can be written"));
366 event_generate(ET_WRITE, sock, 0);
368 if (pollfdList[i].revents & (POLLREADFLAGS | POLLWRITEFLAGS))
372 case SS_DATAGRAM: case SS_CONNECTDG:
373 if (pollfdList[i].revents & POLLREADFLAGS) { /* socket readable */
374 Debug((DEBUG_ENGINE, "poll: Datagram to be read"));
375 event_generate(ET_READ, sock, 0);
377 if (pollfdList[i].revents & POLLWRITEFLAGS) { /* socket writable */
378 Debug((DEBUG_ENGINE, "poll: Datagram can be written"));
379 event_generate(ET_WRITE, sock, 0);
381 if (pollfdList[i].revents & (POLLREADFLAGS | POLLWRITEFLAGS))
386 gen_ref_dec(sock); /* we're done with it */
389 timer_run(); /* execute any pending timers */
393 struct Engine engine_poll = {
394 "poll()", /* Engine name */
395 engine_init, /* Engine initialization function */
396 0, /* Engine signal registration function */
397 engine_add, /* Engine socket registration function */
398 engine_state, /* Engine socket state change function */
399 engine_events, /* Engine socket events mask function */
400 engine_delete, /* Engine socket deletion function */
401 engine_loop /* Core engine event loop */