1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 2005-2014 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 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* 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 "memory.h" |
32 |
|
|
#include "s_bsd.h" |
33 |
michael |
1309 |
#include "log.h" |
34 |
adx |
30 |
#include <sys/epoll.h> |
35 |
|
|
#include <sys/syscall.h> |
36 |
|
|
|
37 |
|
|
static fde_t efd; |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/* |
41 |
|
|
* init_netio |
42 |
|
|
* |
43 |
|
|
* This is a needed exported function which will be called to initialise |
44 |
|
|
* the network loop code. |
45 |
|
|
*/ |
46 |
|
|
void |
47 |
|
|
init_netio(void) |
48 |
|
|
{ |
49 |
|
|
int fd; |
50 |
|
|
|
51 |
|
|
if ((fd = epoll_create(hard_fdlimit)) < 0) |
52 |
|
|
{ |
53 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open epoll fd - %d: %s", |
54 |
adx |
30 |
errno, strerror(errno)); |
55 |
|
|
exit(115); /* Whee! */ |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
fd_open(&efd, fd, 0, "epoll file descriptor"); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
/* |
62 |
|
|
* comm_setselect |
63 |
|
|
* |
64 |
|
|
* This is a needed exported function which will be called to register |
65 |
|
|
* and deregister interest in a pending IO state for a given FD. |
66 |
|
|
*/ |
67 |
|
|
void |
68 |
|
|
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
69 |
|
|
void *client_data, time_t timeout) |
70 |
|
|
{ |
71 |
|
|
int new_events, op; |
72 |
michael |
966 |
struct epoll_event ep_event = { 0, { 0 } }; |
73 |
adx |
30 |
|
74 |
|
|
if ((type & COMM_SELECT_READ)) |
75 |
|
|
{ |
76 |
|
|
F->read_handler = handler; |
77 |
|
|
F->read_data = client_data; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if ((type & COMM_SELECT_WRITE)) |
81 |
|
|
{ |
82 |
|
|
F->write_handler = handler; |
83 |
|
|
F->write_data = client_data; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
new_events = (F->read_handler ? EPOLLIN : 0) | |
87 |
|
|
(F->write_handler ? EPOLLOUT : 0); |
88 |
|
|
|
89 |
|
|
if (timeout != 0) |
90 |
michael |
2725 |
{ |
91 |
adx |
30 |
F->timeout = CurrentTime + (timeout / 1000); |
92 |
michael |
2725 |
F->timeout_handler = handler; |
93 |
|
|
F->timeout_data = client_data; |
94 |
|
|
} |
95 |
adx |
30 |
|
96 |
|
|
if (new_events != F->evcache) |
97 |
|
|
{ |
98 |
|
|
if (new_events == 0) |
99 |
|
|
op = EPOLL_CTL_DEL; |
100 |
|
|
else if (F->evcache == 0) |
101 |
|
|
op = EPOLL_CTL_ADD; |
102 |
|
|
else |
103 |
|
|
op = EPOLL_CTL_MOD; |
104 |
|
|
|
105 |
|
|
ep_event.events = F->evcache = new_events; |
106 |
|
|
ep_event.data.fd = F->fd; |
107 |
|
|
|
108 |
|
|
if (epoll_ctl(efd.fd, op, F->fd, &ep_event) != 0) |
109 |
|
|
{ |
110 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno)); |
111 |
adx |
30 |
abort(); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
* comm_select() |
118 |
|
|
* |
119 |
|
|
* Called to do the new-style IO, courtesy of of squid (like most of this |
120 |
|
|
* new IO code). This routine handles the stuff we've hidden in |
121 |
|
|
* comm_setselect and fd_table[] and calls callbacks for IO ready |
122 |
|
|
* events. |
123 |
|
|
*/ |
124 |
|
|
void |
125 |
|
|
comm_select(void) |
126 |
|
|
{ |
127 |
|
|
struct epoll_event ep_fdlist[128]; |
128 |
|
|
int num, i; |
129 |
|
|
PF *hdl; |
130 |
|
|
fde_t *F; |
131 |
|
|
|
132 |
|
|
num = epoll_wait(efd.fd, ep_fdlist, 128, SELECT_DELAY); |
133 |
|
|
|
134 |
|
|
set_time(); |
135 |
|
|
|
136 |
|
|
if (num < 0) |
137 |
|
|
{ |
138 |
|
|
#ifdef HAVE_USLEEP |
139 |
|
|
usleep(50000); /* avoid 99% CPU in comm_select */ |
140 |
|
|
#endif |
141 |
|
|
return; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
for (i = 0; i < num; i++) |
145 |
|
|
{ |
146 |
|
|
F = lookup_fd(ep_fdlist[i].data.fd); |
147 |
|
|
if (F == NULL || !F->flags.open) |
148 |
|
|
continue; |
149 |
|
|
|
150 |
|
|
if ((ep_fdlist[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR))) |
151 |
michael |
2650 |
{ |
152 |
adx |
30 |
if ((hdl = F->read_handler) != NULL) |
153 |
|
|
{ |
154 |
|
|
F->read_handler = NULL; |
155 |
|
|
hdl(F, F->read_data); |
156 |
michael |
2650 |
if (!F->flags.open) |
157 |
|
|
continue; |
158 |
adx |
30 |
} |
159 |
michael |
2650 |
} |
160 |
adx |
30 |
|
161 |
|
|
if ((ep_fdlist[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR))) |
162 |
michael |
2650 |
{ |
163 |
adx |
30 |
if ((hdl = F->write_handler) != NULL) |
164 |
|
|
{ |
165 |
|
|
F->write_handler = NULL; |
166 |
|
|
hdl(F, F->write_data); |
167 |
michael |
2650 |
if (!F->flags.open) |
168 |
|
|
continue; |
169 |
adx |
30 |
} |
170 |
michael |
2650 |
} |
171 |
adx |
30 |
|
172 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
173 |
|
|
} |
174 |
|
|
} |
175 |
stu |
908 |
#endif |