2 * ircd-ratbox: A slightly useful ircd.
3 * epoll.c: Linux epoll compatible network routines.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2001 Adrian Chadd <adrian@creative.net.au>
8 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * Copyright (C) 2002 Aaron Sethman <androsyn@ratbox.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
29 #include <libratbox_config.h>
30 #include <ratbox_lib.h>
31 #include <commio-int.h>
32 #include <event-int.h>
33 #if defined(HAVE_EPOLL_CTL) && (HAVE_SYS_EPOLL_H)
36 #include <sys/epoll.h>
38 #if defined(HAVE_SIGNALFD) && (HAVE_SYS_SIGNALFD_H) && (USE_TIMER_CREATE) && (HAVE_SYS_UIO_H)
40 #include <sys/signalfd.h>
42 #define EPOLL_SCHED_EVENT 1
45 #if defined(USE_TIMERFD_CREATE)
46 #include <sys/timerfd.h>
49 #define RTSIGNAL SIGRTMIN
53 struct epoll_event
*pfd
;
57 static struct epoll_info
*ep_info
;
58 static int can_do_event
;
59 static int can_do_timerfd
;
64 * This is a needed exported function which will be called to initialise
65 * the network loop code.
68 rb_init_netio_epoll(void)
70 can_do_event
= 0; /* shut up gcc */
72 ep_info
= rb_malloc(sizeof(struct epoll_info
));
73 ep_info
->pfd_size
= getdtablesize();
74 ep_info
->ep
= epoll_create(ep_info
->pfd_size
);
79 rb_open(ep_info
->ep
, RB_FD_UNKNOWN
, "epoll file descriptor");
80 ep_info
->pfd
= rb_malloc(sizeof(struct epoll_event
) * ep_info
->pfd_size
);
86 rb_setup_fd_epoll(rb_fde_t
*F
)
95 * This is a needed exported function which will be called to register
96 * and deregister interest in a pending IO state for a given FD.
99 rb_setselect_epoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
101 struct epoll_event ep_event
;
102 int old_flags
= F
->pflags
;
105 lrb_assert(IsFDOpen(F
));
107 /* Update the list, even though we're not using it .. */
108 if(type
& RB_SELECT_READ
)
111 F
->pflags
|= EPOLLIN
;
113 F
->pflags
&= ~EPOLLIN
;
114 F
->read_handler
= handler
;
115 F
->read_data
= client_data
;
118 if(type
& RB_SELECT_WRITE
)
121 F
->pflags
|= EPOLLOUT
;
123 F
->pflags
&= ~EPOLLOUT
;
124 F
->write_handler
= handler
;
125 F
->write_data
= client_data
;
128 if(old_flags
== 0 && F
->pflags
== 0)
130 else if(F
->pflags
<= 0)
132 else if(old_flags
== 0 && F
->pflags
> 0)
134 else if(F
->pflags
!= old_flags
)
140 ep_event
.events
= F
->pflags
;
141 ep_event
.data
.ptr
= F
;
143 if(op
== EPOLL_CTL_ADD
|| op
== EPOLL_CTL_MOD
)
144 ep_event
.events
|= EPOLLET
;
146 if(epoll_ctl(ep_info
->ep
, op
, F
->fd
, &ep_event
) != 0)
148 rb_lib_log("rb_setselect_epoll(): epoll_ctl failed: %s", strerror(errno
));
158 * Called to do the new-style IO, courtesy of squid (like most of this
159 * new IO code). This routine handles the stuff we've hidden in
160 * rb_setselect and fd_table[] and calls callbacks for IO ready
165 rb_select_epoll(long delay
)
167 int num
, i
, flags
, old_flags
, op
;
168 struct epoll_event ep_event
;
172 num
= epoll_wait(ep_info
->ep
, ep_info
->pfd
, ep_info
->pfd_size
, delay
);
174 /* save errno as rb_set_time() will likely clobber it */
179 if(num
< 0 && !rb_ignore_errno(o_errno
))
185 for(i
= 0; i
< num
; i
++)
188 rb_fde_t
*F
= ep_info
->pfd
[i
].data
.ptr
;
189 old_flags
= F
->pflags
;
190 if(ep_info
->pfd
[i
].events
& (EPOLLIN
| EPOLLHUP
| EPOLLERR
))
192 hdl
= F
->read_handler
;
194 F
->read_handler
= NULL
;
204 if(ep_info
->pfd
[i
].events
& (EPOLLOUT
| EPOLLHUP
| EPOLLERR
))
206 hdl
= F
->write_handler
;
207 data
= F
->write_data
;
208 F
->write_handler
= NULL
;
209 F
->write_data
= NULL
;
222 if(F
->read_handler
!= NULL
)
224 if(F
->write_handler
!= NULL
)
227 if(old_flags
!= flags
)
233 F
->pflags
= ep_event
.events
= flags
;
234 ep_event
.data
.ptr
= F
;
235 if(op
== EPOLL_CTL_MOD
|| op
== EPOLL_CTL_ADD
)
236 ep_event
.events
|= EPOLLET
;
238 if(epoll_ctl(ep_info
->ep
, op
, F
->fd
, &ep_event
) != 0)
240 rb_lib_log("rb_select_epoll(): epoll_ctl failed: %s",
249 #ifdef EPOLL_SCHED_EVENT
251 rb_epoll_supports_event(void)
253 /* try to detect at runtime if everything we need actually works */
259 if(can_do_event
== 1)
261 if(can_do_event
== -1)
264 #ifdef USE_TIMERFD_CREATE
265 if((fd
= timerfd_create(CLOCK_REALTIME
, 0)) >= 0)
274 ev
.sigev_signo
= SIGVTALRM
;
275 ev
.sigev_notify
= SIGEV_SIGNAL
;
276 if(timer_create(CLOCK_REALTIME
, &ev
, &timer
) != 0)
283 fd
= signalfd(-1, &set
, 0);
295 /* bleh..work around a glibc header bug on 32bit systems */
296 struct our_signalfd_siginfo
318 #define SIGFDIOV_COUNT 16
320 signalfd_handler(rb_fde_t
*F
, void *data
)
322 static struct our_signalfd_siginfo fdsig
[SIGFDIOV_COUNT
];
323 static struct iovec iov
[SIGFDIOV_COUNT
];
327 for(x
= 0; x
< SIGFDIOV_COUNT
; x
++)
329 iov
[x
].iov_base
= &fdsig
[x
];
330 iov
[x
].iov_len
= sizeof(struct our_signalfd_siginfo
);
335 ret
= readv(rb_get_fd(F
), iov
, SIGFDIOV_COUNT
);
337 if(ret
== 0 || (ret
< 0 && !rb_ignore_errno(errno
)))
340 rb_epoll_init_event();
346 rb_setselect(F
, RB_SELECT_READ
, signalfd_handler
, NULL
);
349 for(x
= 0; x
< ret
/ (int)sizeof(struct our_signalfd_siginfo
); x
++)
351 #if __WORDSIZE == 32 && defined(__sparc__)
352 uint32_t *q
= (uint32_t *)&fdsig
[x
].svptr
;
353 ev
= (struct ev_entry
*)q
[0];
355 ev
= (struct ev_entry
*)(uintptr_t)(fdsig
[x
].svptr
);
366 rb_epoll_init_event(void)
372 rb_epoll_supports_event();
376 sigaddset(&ss
, RTSIGNAL
);
377 sigprocmask(SIG_BLOCK
, &ss
, 0);
379 sigaddset(&ss
, RTSIGNAL
);
380 sfd
= signalfd(-1, &ss
, 0);
386 F
= rb_open(sfd
, RB_FD_UNKNOWN
, "signalfd");
388 signalfd_handler(F
, NULL
);
393 rb_epoll_sched_event_signalfd(struct ev_entry
*event
, int when
)
397 struct itimerspec ts
;
399 memset(&ev
, 0, sizeof(&ev
));
400 event
->comm_ptr
= rb_malloc(sizeof(timer_t
));
401 id
= event
->comm_ptr
;
402 ev
.sigev_notify
= SIGEV_SIGNAL
;
403 ev
.sigev_signo
= RTSIGNAL
;
404 ev
.sigev_value
.sival_ptr
= event
;
406 if(timer_create(CLOCK_REALTIME
, &ev
, id
) < 0)
408 rb_lib_log("timer_create: %s\n", strerror(errno
));
411 memset(&ts
, 0, sizeof(ts
));
412 ts
.it_value
.tv_sec
= when
;
413 ts
.it_value
.tv_nsec
= 0;
414 if(event
->frequency
!= 0)
415 ts
.it_interval
= ts
.it_value
;
417 if(timer_settime(*id
, 0, &ts
, NULL
) < 0)
419 rb_lib_log("timer_settime: %s\n", strerror(errno
));
425 #ifdef USE_TIMERFD_CREATE
427 rb_read_timerfd(rb_fde_t
*F
, void *data
)
429 struct ev_entry
*event
= (struct ev_entry
*)data
;
439 retlen
= rb_read(F
, &count
, sizeof(count
));
441 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
444 rb_lib_log("rb_read_timerfd: timerfd[%s] closed on error: %s", event
->name
,
448 rb_setselect(F
, RB_SELECT_READ
, rb_read_timerfd
, event
);
454 rb_epoll_sched_event_timerfd(struct ev_entry
*event
, int when
)
456 struct itimerspec ts
;
457 static char buf
[FD_DESC_SZ
+ 8];
461 if((fd
= timerfd_create(CLOCK_REALTIME
, 0)) < 0)
463 rb_lib_log("timerfd_create: %s\n", strerror(errno
));
467 memset(&ts
, 0, sizeof(ts
));
468 ts
.it_value
.tv_sec
= when
;
469 ts
.it_value
.tv_nsec
= 0;
470 if(event
->frequency
!= 0)
471 ts
.it_interval
= ts
.it_value
;
473 if(timerfd_settime(fd
, 0, &ts
, NULL
) < 0)
475 rb_lib_log("timerfd_settime: %s\n", strerror(errno
));
479 rb_snprintf(buf
, sizeof(buf
), "timerfd: %s", event
->name
);
480 F
= rb_open(fd
, RB_FD_UNKNOWN
, buf
);
483 rb_setselect(F
, RB_SELECT_READ
, rb_read_timerfd
, event
);
491 rb_epoll_sched_event(struct ev_entry
*event
, int when
)
493 #ifdef USE_TIMERFD_CREATE
496 return rb_epoll_sched_event_timerfd(event
, when
);
499 return rb_epoll_sched_event_signalfd(event
, when
);
503 rb_epoll_unsched_event(struct ev_entry
*event
)
505 #ifdef USE_TIMERFD_CREATE
508 rb_close((rb_fde_t
*)event
->comm_ptr
);
509 event
->comm_ptr
= NULL
;
513 timer_delete(*((timer_t
*) event
->comm_ptr
));
514 rb_free(event
->comm_ptr
);
515 event
->comm_ptr
= NULL
;
517 #endif /* EPOLL_SCHED_EVENT */
519 #else /* epoll not supported here */
521 rb_init_netio_epoll(void)
527 rb_setselect_epoll(rb_fde_t
*F
, unsigned int type
, PF
* handler
, void *client_data
)
534 rb_select_epoll(long delay
)
541 rb_setup_fd_epoll(rb_fde_t
*F
)
550 #if !defined(USING_EPOLL) || !defined(EPOLL_SCHED_EVENT)
552 rb_epoll_init_event(void)
558 rb_epoll_sched_event(struct ev_entry
*event
, int when
)
565 rb_epoll_unsched_event(struct ev_entry
*event
)
571 rb_epoll_supports_event(void)
576 #endif /* !USING_EPOLL || !EPOLL_SCHED_EVENT */