]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * kqueue.c: FreeBSD kqueue compatible network routines. | |
4 | * | |
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 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
24 | * | |
3611dcf7 | 25 | * $Id: kqueue.c 3358 2007-04-03 09:34:38Z nenolod $ |
212380e3 AC |
26 | */ |
27 | ||
28 | #include "stdinc.h" | |
29 | #include <sys/event.h> | |
30 | ||
31 | #include "libcharybdis.h" | |
32 | ||
3611dcf7 AC |
33 | #define KE_LENGTH 128 |
34 | ||
212380e3 AC |
35 | /* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */ |
36 | ||
37 | #ifndef EV_SET | |
38 | #define EV_SET(kevp, a, b, c, d, e, f) do { \ | |
39 | (kevp)->ident = (a); \ | |
40 | (kevp)->filter = (b); \ | |
41 | (kevp)->flags = (c); \ | |
42 | (kevp)->fflags = (d); \ | |
43 | (kevp)->data = (e); \ | |
44 | (kevp)->udata = (f); \ | |
45 | } while(0) | |
46 | #endif | |
47 | ||
48 | static void kq_update_events(fde_t *, short, PF *); | |
49 | static int kq; | |
50 | static struct timespec zero_timespec; | |
51 | ||
52 | static struct kevent *kqlst; /* kevent buffer */ | |
53 | static int kqmax; /* max structs to buffer */ | |
54 | static int kqoff; /* offset into the buffer */ | |
55 | ||
56 | ||
57 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ | |
58 | /* Private functions */ | |
59 | ||
60 | void | |
61 | kq_update_events(fde_t * F, short filter, PF * handler) | |
62 | { | |
63 | PF *cur_handler; | |
64 | int kep_flags; | |
65 | ||
66 | switch (filter) | |
67 | { | |
68 | case EVFILT_READ: | |
69 | cur_handler = F->read_handler; | |
70 | break; | |
71 | case EVFILT_WRITE: | |
72 | cur_handler = F->write_handler; | |
73 | break; | |
74 | default: | |
75 | /* XXX bad! -- adrian */ | |
76 | return; | |
77 | break; | |
78 | } | |
79 | ||
80 | if((cur_handler == NULL && handler != NULL) || (cur_handler != NULL && handler == NULL)) | |
81 | { | |
82 | struct kevent *kep; | |
83 | ||
84 | kep = kqlst + kqoff; | |
85 | ||
86 | if(handler != NULL) | |
87 | { | |
88 | if(filter == EVFILT_WRITE) | |
89 | kep_flags = (EV_ADD | EV_ENABLE | EV_ONESHOT); | |
90 | else | |
91 | kep_flags = (EV_ADD | EV_ENABLE); | |
92 | } | |
93 | else | |
94 | { | |
95 | /* lets definately not poll stuff that isn't real -- | |
96 | * some kqueue implementations hate doing this... and | |
97 | * it's intended to delete AND disable at the same time. | |
98 | * | |
99 | * don't believe me? read kevent(4). --nenolod | |
100 | */ | |
101 | kep_flags = (EV_DELETE | EV_DISABLE); | |
102 | } | |
103 | ||
104 | EV_SET(kep, (uintptr_t) F->fd, filter, kep_flags, 0, 0, (void *) F); | |
105 | ||
c8ff7ca9 | 106 | if(++kqoff == kqmax) |
212380e3 AC |
107 | { |
108 | int ret; | |
109 | ||
110 | ret = kevent(kq, kqlst, kqoff, NULL, 0, &zero_timespec); | |
111 | /* jdc -- someone needs to do error checking... */ | |
112 | if(ret == -1) | |
113 | { | |
114 | libcharybdis_log("kq_update_events(): kevent(): %s", strerror(errno)); | |
115 | return; | |
116 | } | |
117 | kqoff = 0; | |
118 | } | |
212380e3 AC |
119 | } |
120 | } | |
121 | ||
122 | ||
123 | ||
124 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ | |
125 | /* Public functions */ | |
126 | ||
127 | ||
128 | /* | |
129 | * init_netio | |
130 | * | |
131 | * This is a needed exported function which will be called to initialise | |
132 | * the network loop code. | |
133 | */ | |
134 | void | |
135 | init_netio(void) | |
136 | { | |
137 | kq = kqueue(); | |
138 | if(kq < 0) | |
139 | { | |
140 | libcharybdis_log("init_netio: Couldn't open kqueue fd!\n"); | |
141 | exit(115); /* Whee! */ | |
142 | } | |
143 | kqmax = getdtablesize(); | |
144 | kqlst = MyMalloc(sizeof(struct kevent) * kqmax); | |
145 | zero_timespec.tv_sec = 0; | |
146 | zero_timespec.tv_nsec = 0; | |
147 | } | |
148 | ||
149 | /* | |
150 | * comm_setselect | |
151 | * | |
152 | * This is a needed exported function which will be called to register | |
153 | * and deregister interest in a pending IO state for a given FD. | |
154 | */ | |
155 | void | |
156 | comm_setselect(int fd, fdlist_t list, unsigned int type, PF * handler, | |
157 | void *client_data, time_t timeout) | |
158 | { | |
e70f8e92 | 159 | fde_t *F = comm_locate_fd(fd); |
212380e3 AC |
160 | s_assert(fd >= 0); |
161 | s_assert(F->flags.open); | |
162 | ||
163 | /* Update the list, even though we're not using it .. */ | |
164 | F->list = list; | |
165 | ||
166 | if(type & COMM_SELECT_READ) | |
167 | { | |
168 | kq_update_events(F, EVFILT_READ, handler); | |
169 | F->read_handler = handler; | |
170 | F->read_data = client_data; | |
171 | } | |
172 | if(type & COMM_SELECT_WRITE) | |
173 | { | |
174 | kq_update_events(F, EVFILT_WRITE, handler); | |
175 | F->write_handler = handler; | |
176 | F->write_data = client_data; | |
177 | } | |
178 | if(timeout) | |
179 | F->timeout = CurrentTime + (timeout / 1000); | |
180 | ||
181 | } | |
182 | ||
183 | /* | |
184 | * Check all connections for new connections and input data that is to be | |
185 | * processed. Also check for connections with data queued and whether we can | |
186 | * write it out. | |
187 | */ | |
188 | ||
189 | /* | |
190 | * comm_select | |
191 | * | |
192 | * Called to do the new-style IO, courtesy of squid (like most of this | |
193 | * new IO code). This routine handles the stuff we've hidden in | |
194 | * comm_setselect and fd_table[] and calls callbacks for IO ready | |
195 | * events. | |
196 | */ | |
197 | ||
198 | int | |
199 | comm_select(unsigned long delay) | |
200 | { | |
201 | int num, i; | |
3611dcf7 | 202 | static struct kevent ke[KE_LENGTH]; |
212380e3 AC |
203 | struct timespec poll_time; |
204 | ||
205 | /* | |
206 | * remember we are doing NANOseconds here, not micro/milli. God knows | |
207 | * why jlemon used a timespec, but hey, he wrote the interface, not I | |
208 | * -- Adrian | |
209 | */ | |
210 | ||
211 | poll_time.tv_sec = delay / 1000; | |
212 | ||
213 | poll_time.tv_nsec = (delay % 1000) * 1000000; | |
214 | ||
215 | for (;;) | |
216 | { | |
217 | num = kevent(kq, kqlst, kqoff, ke, KE_LENGTH, &poll_time); | |
218 | kqoff = 0; | |
219 | ||
220 | if(num >= 0) | |
221 | break; | |
222 | ||
223 | if(ignoreErrno(errno)) | |
224 | break; | |
225 | ||
226 | set_time(); | |
227 | ||
228 | return COMM_ERROR; | |
229 | ||
230 | /* NOTREACHED */ | |
231 | } | |
232 | ||
233 | set_time(); | |
234 | ||
235 | if(num == 0) | |
236 | return COMM_OK; /* No error.. */ | |
237 | ||
238 | for (i = 0; i < num; i++) | |
239 | { | |
240 | int fd = (int) ke[i].ident; | |
241 | PF *hdl = NULL; | |
e70f8e92 | 242 | fde_t *F = comm_locate_fd(fd); |
212380e3 AC |
243 | |
244 | if(ke[i].flags & EV_ERROR) | |
245 | { | |
246 | errno = (int) ke[i].data; | |
247 | /* XXX error == bad! -- adrian */ | |
248 | continue; /* XXX! */ | |
249 | } | |
51b5f335 JT |
250 | if (F == NULL) |
251 | { | |
252 | /* XXX this is because of our "queueing" of | |
253 | * kqueue changes so we may get ones for fds | |
254 | * we have already closed? -- jilles */ | |
255 | continue; | |
256 | } | |
212380e3 AC |
257 | |
258 | switch (ke[i].filter) | |
259 | { | |
260 | ||
261 | case EVFILT_READ: | |
262 | ||
263 | if((hdl = F->read_handler) != NULL) | |
264 | { | |
265 | F->read_handler = NULL; | |
266 | hdl(fd, F->read_data); | |
267 | } | |
268 | ||
269 | break; | |
270 | ||
271 | case EVFILT_WRITE: | |
272 | ||
273 | if((hdl = F->write_handler) != NULL) | |
274 | { | |
275 | F->write_handler = NULL; | |
276 | hdl(fd, F->write_data); | |
277 | } | |
278 | break; | |
279 | ||
280 | default: | |
281 | /* Bad! -- adrian */ | |
282 | break; | |
283 | } | |
284 | } | |
285 | return COMM_OK; | |
286 | } | |
287 |