1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
5347 |
* Copyright (c) 2005-2015 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
adx |
30 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file s_bsd_epoll.c |
23 |
|
|
* \brief Linux epoll() compatible network routines. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
stu |
908 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL |
29 |
adx |
30 |
#include "fdlist.h" |
30 |
|
|
#include "ircd.h" |
31 |
|
|
#include "s_bsd.h" |
32 |
michael |
1309 |
#include "log.h" |
33 |
adx |
30 |
#include <sys/epoll.h> |
34 |
|
|
#include <sys/syscall.h> |
35 |
|
|
|
36 |
|
|
static fde_t efd; |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
/* |
40 |
|
|
* init_netio |
41 |
|
|
* |
42 |
|
|
* This is a needed exported function which will be called to initialise |
43 |
|
|
* the network loop code. |
44 |
|
|
*/ |
45 |
|
|
void |
46 |
|
|
init_netio(void) |
47 |
|
|
{ |
48 |
|
|
int fd; |
49 |
|
|
|
50 |
|
|
if ((fd = epoll_create(hard_fdlimit)) < 0) |
51 |
|
|
{ |
52 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open epoll fd - %d: %s", |
53 |
adx |
30 |
errno, strerror(errno)); |
54 |
|
|
exit(115); /* Whee! */ |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
fd_open(&efd, fd, 0, "epoll file descriptor"); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
/* |
61 |
|
|
* comm_setselect |
62 |
|
|
* |
63 |
|
|
* This is a needed exported function which will be called to register |
64 |
|
|
* and deregister interest in a pending IO state for a given FD. |
65 |
|
|
*/ |
66 |
|
|
void |
67 |
michael |
4461 |
comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *), |
68 |
adx |
30 |
void *client_data, time_t timeout) |
69 |
|
|
{ |
70 |
|
|
int new_events, op; |
71 |
michael |
966 |
struct epoll_event ep_event = { 0, { 0 } }; |
72 |
adx |
30 |
|
73 |
|
|
if ((type & COMM_SELECT_READ)) |
74 |
|
|
{ |
75 |
|
|
F->read_handler = handler; |
76 |
|
|
F->read_data = client_data; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
if ((type & COMM_SELECT_WRITE)) |
80 |
|
|
{ |
81 |
|
|
F->write_handler = handler; |
82 |
|
|
F->write_data = client_data; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
new_events = (F->read_handler ? EPOLLIN : 0) | |
86 |
|
|
(F->write_handler ? EPOLLOUT : 0); |
87 |
|
|
|
88 |
|
|
if (timeout != 0) |
89 |
michael |
2725 |
{ |
90 |
adx |
30 |
F->timeout = CurrentTime + (timeout / 1000); |
91 |
michael |
2725 |
F->timeout_handler = handler; |
92 |
|
|
F->timeout_data = client_data; |
93 |
|
|
} |
94 |
adx |
30 |
|
95 |
|
|
if (new_events != F->evcache) |
96 |
|
|
{ |
97 |
|
|
if (new_events == 0) |
98 |
|
|
op = EPOLL_CTL_DEL; |
99 |
|
|
else if (F->evcache == 0) |
100 |
|
|
op = EPOLL_CTL_ADD; |
101 |
|
|
else |
102 |
|
|
op = EPOLL_CTL_MOD; |
103 |
|
|
|
104 |
|
|
ep_event.events = F->evcache = new_events; |
105 |
|
|
ep_event.data.fd = F->fd; |
106 |
|
|
|
107 |
|
|
if (epoll_ctl(efd.fd, op, F->fd, &ep_event) != 0) |
108 |
|
|
{ |
109 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno)); |
110 |
adx |
30 |
abort(); |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/* |
116 |
|
|
* comm_select() |
117 |
|
|
* |
118 |
|
|
* Called to do the new-style IO, courtesy of of squid (like most of this |
119 |
|
|
* new IO code). This routine handles the stuff we've hidden in |
120 |
|
|
* comm_setselect and fd_table[] and calls callbacks for IO ready |
121 |
|
|
* events. |
122 |
|
|
*/ |
123 |
|
|
void |
124 |
|
|
comm_select(void) |
125 |
|
|
{ |
126 |
|
|
struct epoll_event ep_fdlist[128]; |
127 |
|
|
int num, i; |
128 |
michael |
4461 |
void (*hdl)(fde_t *, void *); |
129 |
adx |
30 |
fde_t *F; |
130 |
|
|
|
131 |
|
|
num = epoll_wait(efd.fd, ep_fdlist, 128, SELECT_DELAY); |
132 |
|
|
|
133 |
|
|
set_time(); |
134 |
|
|
|
135 |
|
|
if (num < 0) |
136 |
|
|
{ |
137 |
michael |
6542 |
const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 }; |
138 |
|
|
nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */ |
139 |
adx |
30 |
return; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
for (i = 0; i < num; i++) |
143 |
|
|
{ |
144 |
|
|
F = lookup_fd(ep_fdlist[i].data.fd); |
145 |
|
|
if (F == NULL || !F->flags.open) |
146 |
|
|
continue; |
147 |
|
|
|
148 |
|
|
if ((ep_fdlist[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR))) |
149 |
michael |
2650 |
{ |
150 |
michael |
5902 |
if ((hdl = F->read_handler)) |
151 |
adx |
30 |
{ |
152 |
|
|
F->read_handler = NULL; |
153 |
|
|
hdl(F, F->read_data); |
154 |
michael |
2650 |
if (!F->flags.open) |
155 |
|
|
continue; |
156 |
adx |
30 |
} |
157 |
michael |
2650 |
} |
158 |
adx |
30 |
|
159 |
|
|
if ((ep_fdlist[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR))) |
160 |
michael |
2650 |
{ |
161 |
michael |
5902 |
if ((hdl = F->write_handler)) |
162 |
adx |
30 |
{ |
163 |
|
|
F->write_handler = NULL; |
164 |
|
|
hdl(F, F->write_data); |
165 |
michael |
2650 |
if (!F->flags.open) |
166 |
|
|
continue; |
167 |
adx |
30 |
} |
168 |
michael |
2650 |
} |
169 |
adx |
30 |
|
170 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
171 |
|
|
} |
172 |
|
|
} |
173 |
stu |
908 |
#endif |