]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/engine_devpoll.c
2 * IRC - Internet Relay Chat, ircd/engine_devpoll.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.
20 * @brief Solaris /dev/poll event engine.
21 * @version $Id: engine_devpoll.c,v 1.6 2004/12/11 05:13:44 klmitch Exp $
25 #include "ircd_events.h"
28 #include "ircd_alloc.h"
29 #include "ircd_features.h"
33 /* #include <assert.h> -- Now using assert in ircd_log.h */
36 #include <sys/devpoll.h>
38 #include <sys/socket.h>
40 #include <sys/types.h>
43 #define DEVPOLL_ERROR_THRESHOLD 20 /**< after 20 devpoll errors, restart */
44 #define ERROR_EXPIRE_TIME 3600 /**< expire errors after an hour */
46 /* Figure out what bits to set for read */
47 #if defined(POLLMSG) && defined(POLLIN) && defined(POLLRDNORM)
48 # define POLLREADFLAGS (POLLMSG|POLLIN|POLLRDNORM)
49 #elif defined(POLLIN) && defined(POLLRDNORM)
50 # define POLLREADFLAGS (POLLIN|POLLRDNORM)
52 # define POLLREADFLAGS POLLIN
53 #elif defined(POLLRDNORM)
54 # define POLLREADFLAGS POLLRDNORM
57 /* Figure out what bits to set for write */
58 #if defined(POLLOUT) && defined(POLLWRNORM)
59 # define POLLWRITEFLAGS (POLLOUT|POLLWRNORM)
60 #elif defined(POLLOUT)
61 # define POLLWRITEFLAGS POLLOUT
62 #elif defined(POLLWRNORM)
63 # define POLLWRITEFLAGS POLLWRNORM
66 /** Array of active Socket structures, indexed by file descriptor. */
67 static struct Socket
** sockList
;
68 /** Maximum file descriptor supported, plus one. */
69 static int devpoll_max
;
70 /** File descriptor for /dev/poll device. */
71 static int devpoll_fd
;
73 /** Number of recent errors from /dev/poll. */
74 static int errors
= 0;
75 /** Periodic timer to forget errors. */
76 static struct Timer clear_error
;
78 /** Decrement the error count (once per hour).
79 * @param[in] ev Expired timer event (ignored).
82 error_clear(struct Event
* ev
)
84 if (!--errors
) /* remove timer when error count reaches 0 */
85 timer_del(ev_timer(ev
));
88 /** Initialize the /dev/poll engine.
89 * @param[in] max_sockets Maximum number of file descriptors to support.
90 * @return Non-zero on success, or zero on failure.
93 engine_init(int max_sockets
)
97 if ((devpoll_fd
= open("/dev/poll", O_RDWR
)) < 0) {
98 log_write(LS_SYSTEM
, L_WARNING
, 0,
99 "/dev/poll engine cannot open device: %m");
100 return 0; /* engine cannot be initialized; defer */
103 /* allocate necessary memory */
104 sockList
= (struct Socket
**) MyMalloc(sizeof(struct Socket
*) * max_sockets
);
106 /* initialize the data */
107 for (i
= 0; i
< max_sockets
; i
++)
110 devpoll_max
= max_sockets
; /* number of sockets allocated */
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.
121 state_to_events(enum SocketState state
, unsigned int events
)
124 case SS_CONNECTING
: /* connecting socket */
125 return SOCK_EVENT_WRITABLE
;
128 case SS_LISTENING
: /* listening socket */
129 case SS_NOTSOCK
: /* our signal socket */
130 return SOCK_EVENT_READABLE
;
133 case SS_CONNECTED
: case SS_DATAGRAM
: case SS_CONNECTDG
:
134 return events
; /* ordinary socket */
142 /** Set the desired events for a socket.
143 * @param[in,out] sock Socket to operate on.
144 * @param[in] events User-specified preferred event set.
147 set_events(struct Socket
* sock
, unsigned int events
)
153 if (s_ed_int(sock
)) { /* is one in /dev/poll already? */
154 pfd
.events
= POLLREMOVE
; /* First, remove old pollfd */
156 Debug((DEBUG_ENGINE
, "devpoll: Removing old entry for socket %d [%p]",
159 if (write(devpoll_fd
, &pfd
, sizeof(pfd
)) != sizeof(pfd
)) {
160 event_generate(ET_ERROR
, sock
, errno
); /* report error */
164 s_ed_int(sock
) = 0; /* mark that it's gone */
167 if (!(events
& SOCK_EVENT_MASK
)) /* no events, so stop here */
170 pfd
.events
= 0; /* Now, set up new pollfd... */
171 if (events
& SOCK_EVENT_READABLE
)
172 pfd
.events
|= POLLREADFLAGS
; /* look for readable conditions */
173 if (events
& SOCK_EVENT_WRITABLE
)
174 pfd
.events
|= POLLWRITEFLAGS
; /* look for writable conditions */
176 Debug((DEBUG_ENGINE
, "devpoll: Registering interest on %d [%p] (state %s, "
177 "mask [%s])", s_fd(sock
), sock
, state_to_name(s_state(sock
)),
178 sock_flags(s_events(sock
))));
180 if (write(devpoll_fd
, &pfd
, sizeof(pfd
)) != sizeof(pfd
)) {
181 event_generate(ET_ERROR
, sock
, errno
); /* report error */
185 s_ed_int(sock
) = 1; /* mark that we've added a pollfd */
188 /** Add a socket to the event engine.
189 * @param[in] sock Socket to add to engine.
190 * @return Non-zero on success, or zero on error.
193 engine_add(struct Socket
* sock
)
196 assert(0 == sockList
[s_fd(sock
)]);
198 /* bounds-check... */
199 if (s_fd(sock
) >= devpoll_max
) {
200 log_write(LS_SYSTEM
, L_ERROR
, 0,
201 "Attempt to add socket %d (> %d) to event engine", s_fd(sock
),
206 sockList
[s_fd(sock
)] = sock
; /* add to list */
208 Debug((DEBUG_ENGINE
, "devpoll: Adding socket %d [%p], state %s, to engine",
209 s_fd(sock
), sock
, state_to_name(s_state(sock
))));
211 /* set the correct events */
212 set_events(sock
, state_to_events(s_state(sock
), s_events(sock
)));
214 return 1; /* success */
217 /** Handle state transition for a socket.
218 * @param[in] sock Socket changing state.
219 * @param[in] new_state New state for socket.
222 engine_state(struct Socket
* sock
, enum SocketState new_state
)
225 assert(sock
== sockList
[s_fd(sock
)]);
227 Debug((DEBUG_ENGINE
, "devpoll: Changing state for socket %p to %s", sock
,
228 state_to_name(new_state
)));
230 /* set the correct events */
231 set_events(sock
, state_to_events(new_state
, s_events(sock
)));
234 /** Handle change to preferred socket events.
235 * @param[in] sock Socket getting new interest list.
236 * @param[in] new_events New set of interesting events for socket.
239 engine_events(struct Socket
* sock
, unsigned int new_events
)
242 assert(sock
== sockList
[s_fd(sock
)]);
244 Debug((DEBUG_ENGINE
, "devpoll: Changing event mask for socket %p to [%s]",
245 sock
, sock_flags(new_events
)));
247 /* set the correct events */
248 set_events(sock
, state_to_events(s_state(sock
), new_events
));
251 /** Remove a socket from the event engine.
252 * @param[in] sock Socket being destroyed.
255 engine_delete(struct Socket
* sock
)
258 assert(sock
== sockList
[s_fd(sock
)]);
260 Debug((DEBUG_ENGINE
, "devpoll: Deleting socket %d [%p], state %s",
261 s_fd(sock
), sock
, state_to_name(s_state(sock
))));
263 set_events(sock
, 0); /* get rid of the socket */
265 sockList
[s_fd(sock
)] = 0; /* zero the socket list entry */
268 /** Run engine event loop.
269 * @param[in] gen Lists of generators of various types.
272 engine_loop(struct Generators
* gen
)
274 struct dvpoll dopoll
;
275 struct pollfd
*polls
;
283 if ((polls_count
= feature_int(FEAT_POLLS_PER_LOOP
)) < 20)
285 polls
= (struct pollfd
*)MyMalloc(sizeof(struct pollfd
) * polls_count
);
288 if ((i
= feature_int(FEAT_POLLS_PER_LOOP
)) >= 20 && i
!= polls_count
) {
289 polls
= (struct pollfd
*)MyRealloc(polls
, sizeof(struct pollfd
) * i
);
293 dopoll
.dp_fds
= polls
; /* set up the struct dvpoll */
294 dopoll
.dp_nfds
= polls_count
;
296 /* calculate the proper timeout */
297 dopoll
.dp_timeout
= timer_next(gen
) ?
298 (timer_next(gen
) - CurrentTime
) * 1000 : -1;
300 Debug((DEBUG_INFO
, "devpoll: delay: %Tu (%Tu) %d", timer_next(gen
),
301 CurrentTime
, dopoll
.dp_timeout
));
303 /* check for active files */
304 nfds
= ioctl(devpoll_fd
, DP_POLL
, &dopoll
);
306 CurrentTime
= time(0); /* set current time... */
309 if (errno
!= EINTR
) { /* ignore interrupts */
310 /* Log the poll error */
311 log_write(LS_SOCKET
, L_ERROR
, 0, "ioctl(DP_POLL) error: %m");
313 timer_add(timer_init(&clear_error
), error_clear
, 0, TT_PERIODIC
,
315 else if (errors
> DEVPOLL_ERROR_THRESHOLD
) /* too many errors... */
316 server_restart("too many /dev/poll errors");
318 /* old code did a sleep(1) here; with usage these days,
319 * that may be too expensive
324 for (i
= 0; i
< nfds
; i
++) {
325 assert(-1 < polls
[i
].fd
);
327 sock
= sockList
[polls
[i
].fd
];
328 if (!sock
) /* slots may become empty while processing events */
331 assert(s_fd(sock
) == polls
[i
].fd
);
333 gen_ref_inc(sock
); /* can't have it going away on us */
335 Debug((DEBUG_ENGINE
, "devpoll: Checking socket %p (fd %d) state %s, "
336 "events %s", sock
, s_fd(sock
), state_to_name(s_state(sock
)),
337 sock_flags(s_events(sock
))));
339 if (s_state(sock
) != SS_NOTSOCK
) {
340 errcode
= 0; /* check for errors on socket */
341 codesize
= sizeof(errcode
);
342 if (getsockopt(s_fd(sock
), SOL_SOCKET
, SO_ERROR
, &errcode
,
344 errcode
= errno
; /* work around Solaris implementation */
346 if (errcode
) { /* an error occurred; generate an event */
347 Debug((DEBUG_ENGINE
, "devpoll: Error %d on fd %d, socket %p",
348 errcode
, s_fd(sock
), sock
));
349 event_generate(ET_ERROR
, sock
, errcode
);
350 gen_ref_dec(sock
); /* careful not to leak reference counts */
355 assert(!(polls
[i
].revents
& POLLERR
));
358 if (polls
[i
].revents
& POLLHUP
) { /* hang-up on socket */
359 Debug((DEBUG_ENGINE
, "devpoll: EOF from client (POLLHUP)"));
360 event_generate(ET_EOF
, sock
, 0);
366 switch (s_state(sock
)) {
368 if (polls
[i
].revents
& POLLWRITEFLAGS
) { /* connection completed */
369 Debug((DEBUG_ENGINE
, "devpoll: Connection completed"));
370 event_generate(ET_CONNECT
, sock
, 0);
375 if (polls
[i
].revents
& POLLREADFLAGS
) { /* connect. to be accept. */
376 Debug((DEBUG_ENGINE
, "devpoll: Ready for accept"));
377 event_generate(ET_ACCEPT
, sock
, 0);
382 if (polls
[i
].revents
& POLLREADFLAGS
) { /* data on socket */
383 /* can't peek; it's not a socket */
384 Debug((DEBUG_ENGINE
, "devpoll: non-socket readable"));
385 event_generate(ET_READ
, sock
, 0);
390 if (polls
[i
].revents
& POLLREADFLAGS
) { /* data on socket */
393 switch (recv(s_fd(sock
), &c
, 1, MSG_PEEK
)) { /* check EOF */
394 case -1: /* error occurred?!? */
395 if (errno
== EAGAIN
) {
396 Debug((DEBUG_ENGINE
, "devpoll: Resource temporarily "
400 Debug((DEBUG_ENGINE
, "devpoll: Uncaught error!"));
401 event_generate(ET_ERROR
, sock
, errno
);
404 case 0: /* EOF from client */
405 Debug((DEBUG_ENGINE
, "devpoll: EOF from client"));
406 event_generate(ET_EOF
, sock
, 0);
409 default: /* some data can be read */
410 Debug((DEBUG_ENGINE
, "devpoll: Data to be read"));
411 event_generate(ET_READ
, sock
, 0);
415 if (polls
[i
].revents
& POLLWRITEFLAGS
) { /* socket writable */
416 Debug((DEBUG_ENGINE
, "devpoll: Data can be written"));
417 event_generate(ET_WRITE
, sock
, 0);
421 case SS_DATAGRAM
: case SS_CONNECTDG
:
422 if (polls
[i
].revents
& POLLREADFLAGS
) { /* socket readable */
423 Debug((DEBUG_ENGINE
, "devpoll: Datagram to be read"));
424 event_generate(ET_READ
, sock
, 0);
426 if (polls
[i
].revents
& POLLWRITEFLAGS
) { /* socket writable */
427 Debug((DEBUG_ENGINE
, "devpoll: Datagram can be written"));
428 event_generate(ET_WRITE
, sock
, 0);
433 assert(s_fd(sock
) == polls
[i
].fd
);
435 gen_ref_dec(sock
); /* we're done with it */
438 timer_run(); /* execute any pending timers */
442 /** Descriptor for /dev/poll event engine. */
443 struct Engine engine_devpoll
= {
444 "/dev/poll", /* Engine name */
445 engine_init
, /* Engine initialization function */
446 0, /* Engine signal registration function */
447 engine_add
, /* Engine socket registration function */
448 engine_state
, /* Engine socket state change function */
449 engine_events
, /* Engine socket events mask function */
450 engine_delete
, /* Engine socket deletion function */
451 engine_loop
/* Core engine event loop */