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
26 * $Id: epoll.c 25038 2008-01-23 16:03:08Z androsyn $
30 #include <libratbox_config.h>
31 #include <ratbox_lib.h>
32 #include <commio-int.h>
33 #include <event-int.h>
35 #if defined(HAVE_EPOLL_CTL) && (HAVE_SYS_EPOLL_H)
38 #include <sys/epoll.h>
40 #if defined(HAVE_SIGNALFD) && (HAVE_SYS_SIGNALFD_H) && (USE_TIMER_CREATE) && (HAVE_SYS_UIO_H)
42 #include <sys/signalfd.h>
44 #define EPOLL_SCHED_EVENT 1
47 #define RTSIGNAL SIGRTMIN
51 struct epoll_event
*pfd
;
55 static struct epoll_info
*ep_info
;
56 static int can_do_event
;
58 //static void setup_signalfd(void);
63 * This is a needed exported function which will be called to initialise
64 * the network loop code.
67 rb_init_netio_epoll(void)
69 can_do_event
= 0; /* shut up gcc */
70 ep_info
= rb_malloc(sizeof(struct epoll_info
));
71 ep_info
->pfd_size
= getdtablesize();
72 ep_info
->ep
= epoll_create(ep_info
->pfd_size
);
77 rb_open(ep_info
->ep
, RB_FD_UNKNOWN
, "epoll file descriptor");
78 ep_info
->pfd
= rb_malloc(sizeof(struct epoll_event
) * ep_info
->pfd_size
);
84 rb_setup_fd_epoll(rb_fde_t
* F
)
93 * This is a needed exported function which will be called to register
94 * and deregister interest in a pending IO state for a given FD.
97 rb_setselect_epoll(rb_fde_t
* F
, unsigned int type
, PF
* handler
, void *client_data
)
99 struct epoll_event ep_event
;
100 int old_flags
= F
->pflags
;
103 lrb_assert(IsFDOpen(F
));
105 /* Update the list, even though we're not using it .. */
106 if(type
& RB_SELECT_READ
)
109 F
->pflags
|= EPOLLIN
;
111 F
->pflags
&= ~EPOLLIN
;
112 F
->read_handler
= handler
;
113 F
->read_data
= client_data
;
116 if(type
& RB_SELECT_WRITE
)
119 F
->pflags
|= EPOLLOUT
;
121 F
->pflags
&= ~EPOLLOUT
;
122 F
->write_handler
= handler
;
123 F
->write_data
= client_data
;
126 if(old_flags
== 0 && F
->pflags
== 0)
128 else if(F
->pflags
<= 0)
130 else if(old_flags
== 0 && F
->pflags
> 0)
132 else if(F
->pflags
!= old_flags
)
138 ep_event
.events
= F
->pflags
;
139 ep_event
.data
.ptr
= F
;
141 if(op
== EPOLL_CTL_ADD
|| op
== EPOLL_CTL_MOD
)
142 ep_event
.events
|= EPOLLET
;
144 if(epoll_ctl(ep_info
->ep
, op
, F
->fd
, &ep_event
) != 0)
146 rb_lib_log("rb_setselect_epoll(): epoll_ctl failed: %s", strerror(errno
));
156 * Called to do the new-style IO, courtesy of squid (like most of this
157 * new IO code). This routine handles the stuff we've hidden in
158 * rb_setselect and fd_table[] and calls callbacks for IO ready
163 rb_select_epoll(long delay
)
165 int num
, i
, flags
, old_flags
, op
;
166 struct epoll_event ep_event
;
170 num
= epoll_wait(ep_info
->ep
, ep_info
->pfd
, ep_info
->pfd_size
, delay
);
172 /* save errno as rb_set_time() will likely clobber it */
177 if(num
< 0 && !rb_ignore_errno(o_errno
))
183 for (i
= 0; i
< num
; i
++)
186 rb_fde_t
*F
= ep_info
->pfd
[i
].data
.ptr
;
187 old_flags
= F
->pflags
;
188 if(ep_info
->pfd
[i
].events
& (EPOLLIN
| EPOLLHUP
| EPOLLERR
))
190 hdl
= F
->read_handler
;
192 F
->read_handler
= NULL
;
202 if(ep_info
->pfd
[i
].events
& (EPOLLOUT
| EPOLLHUP
| EPOLLERR
))
204 hdl
= F
->write_handler
;
205 data
= F
->write_data
;
206 F
->write_handler
= NULL
;
207 F
->write_data
= NULL
;
220 if(F
->read_handler
!= NULL
)
222 if(F
->write_handler
!= NULL
)
225 if(old_flags
!= flags
)
231 F
->pflags
= ep_event
.events
= flags
;
232 ep_event
.data
.ptr
= F
;
233 if(op
== EPOLL_CTL_MOD
|| op
== EPOLL_CTL_ADD
)
234 ep_event
.events
|= EPOLLET
;
236 if(epoll_ctl(ep_info
->ep
, op
, F
->fd
, &ep_event
) != 0)
238 rb_lib_log("rb_select_epoll(): epoll_ctl failed: %s",
247 #ifdef EPOLL_SCHED_EVENT
249 rb_epoll_supports_event(void)
251 /* try to detect at runtime if everything we need actually works */
257 if(can_do_event
== 1)
259 if(can_do_event
== -1)
262 ev
.sigev_signo
= SIGVTALRM
;
263 ev
.sigev_notify
= SIGEV_SIGNAL
;
264 if(timer_create(CLOCK_REALTIME
, &ev
, &timer
) != 0)
271 fd
= signalfd(-1, &set
, 0);
283 /* bleh..work around a glibc header bug on 32bit systems */
284 struct our_signalfd_siginfo
{
305 #define SIGFDIOV_COUNT 16
307 signalfd_handler(rb_fde_t
*F
, void *data
)
309 static struct our_signalfd_siginfo fdsig
[SIGFDIOV_COUNT
];
310 static struct iovec iov
[SIGFDIOV_COUNT
];
314 for(x
= 0; x
< SIGFDIOV_COUNT
; x
++)
316 iov
[x
].iov_base
= &fdsig
[x
];
317 iov
[x
].iov_len
= sizeof(struct our_signalfd_siginfo
);
322 ret
= readv(rb_get_fd(F
), iov
, SIGFDIOV_COUNT
);
323 if(ret
== 0 || (ret
< 0 && !rb_ignore_errno(errno
)))
326 rb_epoll_init_event();
332 rb_setselect(F
, RB_SELECT_READ
, signalfd_handler
, NULL
);
335 for(x
= 0; x
< ret
/ (int)sizeof(struct signalfd_siginfo
); x
++)
337 ev
= (struct ev_entry
*)fdsig
[x
].svptr
;
346 rb_epoll_init_event(void)
352 sigaddset(&ss
, RTSIGNAL
);
353 sigprocmask(SIG_BLOCK
, &ss
, 0);
355 sigaddset(&ss
, RTSIGNAL
);
356 sfd
= signalfd(-1, &ss
, 0);
361 F
= rb_open(sfd
, RB_FD_UNKNOWN
, "signalfd");
363 signalfd_handler(F
, NULL
);
367 rb_epoll_sched_event(struct ev_entry
*event
, int when
)
371 struct itimerspec ts
;
373 memset(&ev
, 0, sizeof(&ev
));
374 event
->comm_ptr
= rb_malloc(sizeof(timer_t
));
375 id
= event
->comm_ptr
;
376 ev
.sigev_notify
= SIGEV_SIGNAL
;
377 ev
.sigev_signo
= RTSIGNAL
;
378 ev
.sigev_value
.sival_ptr
= event
;
380 if (timer_create(CLOCK_REALTIME
, &ev
, id
) < 0)
382 rb_lib_log("timer_create: %s\n", strerror(errno
));
385 memset(&ts
, 0, sizeof(ts
));
386 ts
.it_value
.tv_sec
= when
;
387 ts
.it_value
.tv_nsec
= 0;
388 if(event
->frequency
!= 0)
389 ts
.it_interval
= ts
.it_value
;
391 if(timer_settime(*id
, 0, &ts
, NULL
) < 0)
393 rb_lib_log("timer_settime: %s\n", strerror(errno
));
400 rb_epoll_unsched_event(struct ev_entry
*event
)
402 timer_delete(*((timer_t
*)event
->comm_ptr
));
403 rb_free(event
->comm_ptr
);
404 event
->comm_ptr
= NULL
;
406 #endif /* EPOLL_SCHED_EVENT */
408 #else /* epoll not supported here */
410 rb_init_netio_epoll(void)
416 rb_setselect_epoll(rb_fde_t
* F
, unsigned int type
, PF
* handler
, void *client_data
)
423 rb_select_epoll(long delay
)
430 rb_setup_fd_epoll(rb_fde_t
* F
)
439 #if !defined(USING_EPOLL) || !defined(EPOLL_SCHED_EVENT)
440 void rb_epoll_init_event(void)
446 rb_epoll_sched_event(struct ev_entry
*event
, int when
)
453 rb_epoll_unsched_event(struct ev_entry
*event
)
459 rb_epoll_supports_event(void)
464 #endif /* !USING_EPOLL || !EPOLL_SCHED_EVENT */