]>
Commit | Line | Data |
---|---|---|
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 | /** @file | |
20 | * @brief Solaris /dev/poll event engine. | |
21 | * @version $Id: engine_devpoll.c,v 1.6.2.1 2005/12/30 13:28:40 entrope Exp $ | |
22 | */ | |
23 | #include "config.h" | |
24 | ||
25 | #include "ircd_events.h" | |
26 | ||
27 | #include "ircd.h" | |
28 | #include "ircd_alloc.h" | |
29 | #include "ircd_features.h" | |
30 | #include "ircd_log.h" | |
31 | #include "s_debug.h" | |
32 | ||
33 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
34 | #include <errno.h> | |
35 | #include <fcntl.h> | |
36 | #include <sys/devpoll.h> | |
37 | #include <sys/poll.h> | |
38 | #include <sys/socket.h> | |
39 | #include <sys/stat.h> | |
40 | #include <sys/types.h> | |
41 | #include <unistd.h> | |
42 | ||
43 | #define DEVPOLL_ERROR_THRESHOLD 20 /**< after 20 devpoll errors, restart */ | |
44 | #define ERROR_EXPIRE_TIME 3600 /**< expire errors after an hour */ | |
45 | ||
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) | |
51 | #elif defined(POLLIN) | |
52 | # define POLLREADFLAGS POLLIN | |
53 | #elif defined(POLLRDNORM) | |
54 | # define POLLREADFLAGS POLLRDNORM | |
55 | #endif | |
56 | ||
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 | |
64 | #endif | |
65 | ||
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; | |
72 | ||
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; | |
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 /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. | |
91 | */ | |
92 | static int | |
93 | engine_init(int max_sockets) | |
94 | { | |
95 | int i; | |
96 | ||
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 */ | |
101 | } | |
102 | ||
103 | /* allocate necessary memory */ | |
104 | sockList = (struct Socket**) MyMalloc(sizeof(struct Socket*) * max_sockets); | |
105 | ||
106 | /* initialize the data */ | |
107 | for (i = 0; i < max_sockets; i++) | |
108 | sockList[i] = 0; | |
109 | ||
110 | devpoll_max = max_sockets; /* number of sockets allocated */ | |
111 | ||
112 | return 1; | |
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 the desired events for a socket. | |
143 | * @param[in,out] sock Socket to operate on. | |
144 | * @param[in] events User-specified preferred event set. | |
145 | */ | |
146 | static void | |
147 | set_events(struct Socket* sock, unsigned int events) | |
148 | { | |
149 | struct pollfd pfd; | |
150 | ||
151 | pfd.fd = s_fd(sock); | |
152 | ||
153 | if (s_ed_int(sock)) { /* is one in /dev/poll already? */ | |
154 | pfd.events = POLLREMOVE; /* First, remove old pollfd */ | |
155 | ||
156 | Debug((DEBUG_ENGINE, "devpoll: Removing old entry for socket %d [%p]", | |
157 | s_fd(sock), sock)); | |
158 | ||
159 | if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd)) { | |
160 | event_generate(ET_ERROR, sock, errno); /* report error */ | |
161 | return; | |
162 | } | |
163 | ||
164 | s_ed_int(sock) = 0; /* mark that it's gone */ | |
165 | } | |
166 | ||
167 | if (!(events & SOCK_EVENT_MASK)) /* no events, so stop here */ | |
168 | return; | |
169 | ||
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 */ | |
175 | ||
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)))); | |
179 | ||
180 | if (write(devpoll_fd, &pfd, sizeof(pfd)) != sizeof(pfd)) { | |
181 | event_generate(ET_ERROR, sock, errno); /* report error */ | |
182 | return; | |
183 | } | |
184 | ||
185 | s_ed_int(sock) = 1; /* mark that we've added a pollfd */ | |
186 | } | |
187 | ||
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. | |
191 | */ | |
192 | static int | |
193 | engine_add(struct Socket* sock) | |
194 | { | |
195 | assert(0 != sock); | |
196 | assert(0 == sockList[s_fd(sock)]); | |
197 | ||
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), | |
202 | devpoll_max); | |
203 | return 0; | |
204 | } | |
205 | ||
206 | sockList[s_fd(sock)] = sock; /* add to list */ | |
207 | ||
208 | Debug((DEBUG_ENGINE, "devpoll: Adding socket %d [%p], state %s, to engine", | |
209 | s_fd(sock), sock, state_to_name(s_state(sock)))); | |
210 | ||
211 | /* set the correct events */ | |
212 | set_events(sock, state_to_events(s_state(sock), s_events(sock))); | |
213 | ||
214 | return 1; /* success */ | |
215 | } | |
216 | ||
217 | /** Handle state transition for a socket. | |
218 | * @param[in] sock Socket changing state. | |
219 | * @param[in] new_state New state for socket. | |
220 | */ | |
221 | static void | |
222 | engine_state(struct Socket* sock, enum SocketState new_state) | |
223 | { | |
224 | assert(0 != sock); | |
225 | assert(sock == sockList[s_fd(sock)]); | |
226 | ||
227 | Debug((DEBUG_ENGINE, "devpoll: Changing state for socket %p to %s", sock, | |
228 | state_to_name(new_state))); | |
229 | ||
230 | /* set the correct events */ | |
231 | set_events(sock, state_to_events(new_state, s_events(sock))); | |
232 | } | |
233 | ||
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. | |
237 | */ | |
238 | static void | |
239 | engine_events(struct Socket* sock, unsigned int new_events) | |
240 | { | |
241 | assert(0 != sock); | |
242 | assert(sock == sockList[s_fd(sock)]); | |
243 | ||
244 | Debug((DEBUG_ENGINE, "devpoll: Changing event mask for socket %p to [%s]", | |
245 | sock, sock_flags(new_events))); | |
246 | ||
247 | /* set the correct events */ | |
248 | set_events(sock, state_to_events(s_state(sock), new_events)); | |
249 | } | |
250 | ||
251 | /** Remove a socket from the event engine. | |
252 | * @param[in] sock Socket being destroyed. | |
253 | */ | |
254 | static void | |
255 | engine_delete(struct Socket* sock) | |
256 | { | |
257 | assert(0 != sock); | |
258 | assert(sock == sockList[s_fd(sock)]); | |
259 | ||
260 | Debug((DEBUG_ENGINE, "devpoll: Deleting socket %d [%p], state %s", | |
261 | s_fd(sock), sock, state_to_name(s_state(sock)))); | |
262 | ||
263 | set_events(sock, 0); /* get rid of the socket */ | |
264 | ||
265 | sockList[s_fd(sock)] = 0; /* zero the socket list entry */ | |
266 | } | |
267 | ||
268 | /** Run engine event loop. | |
269 | * @param[in] gen Lists of generators of various types. | |
270 | */ | |
271 | static void | |
272 | engine_loop(struct Generators* gen) | |
273 | { | |
274 | struct dvpoll dopoll; | |
275 | struct pollfd *polls; | |
276 | int polls_count; | |
277 | struct Socket* sock; | |
278 | int nfds; | |
279 | int i; | |
280 | int errcode; | |
281 | size_t codesize; | |
282 | ||
283 | if ((polls_count = feature_int(FEAT_POLLS_PER_LOOP)) < 20) | |
284 | polls_count = 20; | |
285 | polls = (struct pollfd *)MyMalloc(sizeof(struct pollfd) * polls_count); | |
286 | ||
287 | while (running) { | |
288 | if ((i = feature_int(FEAT_POLLS_PER_LOOP)) >= 20 && i != polls_count) { | |
289 | polls = (struct pollfd *)MyRealloc(polls, sizeof(struct pollfd) * i); | |
290 | polls_count = i; | |
291 | } | |
292 | ||
293 | dopoll.dp_fds = polls; /* set up the struct dvpoll */ | |
294 | dopoll.dp_nfds = polls_count; | |
295 | ||
296 | /* calculate the proper timeout */ | |
297 | dopoll.dp_timeout = timer_next(gen) ? | |
298 | (timer_next(gen) - CurrentTime) * 1000 : -1; | |
299 | ||
300 | Debug((DEBUG_INFO, "devpoll: delay: %Tu (%Tu) %d", timer_next(gen), | |
301 | CurrentTime, dopoll.dp_timeout)); | |
302 | ||
303 | /* check for active files */ | |
304 | nfds = ioctl(devpoll_fd, DP_POLL, &dopoll); | |
305 | ||
306 | CurrentTime = time(0); /* set current time... */ | |
307 | ||
308 | if (nfds < 0) { | |
309 | if (errno != EINTR) { /* ignore interrupts */ | |
310 | /* Log the poll error */ | |
311 | log_write(LS_SOCKET, L_ERROR, 0, "ioctl(DP_POLL) error: %m"); | |
312 | if (!errors++) | |
313 | timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC, | |
314 | ERROR_EXPIRE_TIME); | |
315 | else if (errors > DEVPOLL_ERROR_THRESHOLD) /* too many errors... */ | |
316 | server_restart("too many /dev/poll errors"); | |
317 | } | |
318 | /* old code did a sleep(1) here; with usage these days, | |
319 | * that may be too expensive | |
320 | */ | |
321 | continue; | |
322 | } | |
323 | ||
324 | for (i = 0; i < nfds; i++) { | |
325 | assert(-1 < polls[i].fd); | |
326 | ||
327 | sock = sockList[polls[i].fd]; | |
328 | if (!sock) /* slots may become empty while processing events */ | |
329 | continue; | |
330 | ||
331 | assert(s_fd(sock) == polls[i].fd); | |
332 | ||
333 | gen_ref_inc(sock); /* can't have it going away on us */ | |
334 | ||
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)))); | |
338 | ||
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, | |
343 | &codesize) < 0) | |
344 | errcode = errno; /* work around Solaris implementation */ | |
345 | ||
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 */ | |
351 | continue; | |
352 | } | |
353 | } | |
354 | ||
355 | assert(!(polls[i].revents & POLLERR)); | |
356 | ||
357 | #ifdef POLLHUP | |
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); | |
361 | nfds--; | |
362 | continue; | |
363 | } | |
364 | #endif /* POLLHUP */ | |
365 | ||
366 | switch (s_state(sock)) { | |
367 | case SS_CONNECTING: | |
368 | if (polls[i].revents & POLLWRITEFLAGS) { /* connection completed */ | |
369 | Debug((DEBUG_ENGINE, "devpoll: Connection completed")); | |
370 | event_generate(ET_CONNECT, sock, 0); | |
371 | } | |
372 | break; | |
373 | ||
374 | case SS_LISTENING: | |
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); | |
378 | } | |
379 | break; | |
380 | ||
381 | case SS_NOTSOCK: | |
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); | |
386 | } | |
387 | break; | |
388 | ||
389 | case SS_CONNECTED: | |
390 | if (polls[i].revents & POLLREADFLAGS) { /* data on socket */ | |
391 | char c; | |
392 | ||
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 " | |
397 | "unavailable?")); | |
398 | continue; | |
399 | } | |
400 | Debug((DEBUG_ENGINE, "devpoll: Uncaught error!")); | |
401 | event_generate(ET_ERROR, sock, errno); | |
402 | break; | |
403 | ||
404 | case 0: /* EOF from client */ | |
405 | Debug((DEBUG_ENGINE, "devpoll: EOF from client")); | |
406 | event_generate(ET_EOF, sock, 0); | |
407 | break; | |
408 | ||
409 | default: /* some data can be read */ | |
410 | Debug((DEBUG_ENGINE, "devpoll: Data to be read")); | |
411 | event_generate(ET_READ, sock, 0); | |
412 | break; | |
413 | } | |
414 | } | |
415 | if (polls[i].revents & POLLWRITEFLAGS) { /* socket writable */ | |
416 | Debug((DEBUG_ENGINE, "devpoll: Data can be written")); | |
417 | event_generate(ET_WRITE, sock, 0); | |
418 | } | |
419 | break; | |
420 | ||
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); | |
425 | } | |
426 | if (polls[i].revents & POLLWRITEFLAGS) { /* socket writable */ | |
427 | Debug((DEBUG_ENGINE, "devpoll: Datagram can be written")); | |
428 | event_generate(ET_WRITE, sock, 0); | |
429 | } | |
430 | break; | |
431 | } | |
432 | ||
433 | gen_ref_dec(sock); /* we're done with it */ | |
434 | } | |
435 | ||
436 | timer_run(); /* execute any pending timers */ | |
437 | } | |
438 | } | |
439 | ||
440 | /** Descriptor for /dev/poll event engine. */ | |
441 | struct Engine engine_devpoll = { | |
442 | "/dev/poll", /* Engine name */ | |
443 | engine_init, /* Engine initialization function */ | |
444 | 0, /* Engine signal registration function */ | |
445 | engine_add, /* Engine socket registration function */ | |
446 | engine_state, /* Engine socket state change function */ | |
447 | engine_events, /* Engine socket events mask function */ | |
448 | engine_delete, /* Engine socket deletion function */ | |
449 | engine_loop /* Core engine event loop */ | |
450 | }; |