/src/samba/lib/tevent/tevent_epoll.c
Line | Count | Source |
1 | | /* |
2 | | Unix SMB/CIFS implementation. |
3 | | |
4 | | main select loop and event handling - epoll implementation |
5 | | |
6 | | Copyright (C) Andrew Tridgell 2003-2005 |
7 | | Copyright (C) Stefan Metzmacher 2005-2013 |
8 | | Copyright (C) Jeremy Allison 2013 |
9 | | |
10 | | ** NOTE! The following LGPL license applies to the tevent |
11 | | ** library. This does NOT imply that all of Samba is released |
12 | | ** under the LGPL |
13 | | |
14 | | This library is free software; you can redistribute it and/or |
15 | | modify it under the terms of the GNU Lesser General Public |
16 | | License as published by the Free Software Foundation; either |
17 | | version 3 of the License, or (at your option) any later version. |
18 | | |
19 | | This library is distributed in the hope that it will be useful, |
20 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 | | Lesser General Public License for more details. |
23 | | |
24 | | You should have received a copy of the GNU Lesser General Public |
25 | | License along with this library; if not, see <http://www.gnu.org/licenses/>. |
26 | | */ |
27 | | |
28 | | #include "replace.h" |
29 | | #include "system/filesys.h" |
30 | | #include "system/select.h" |
31 | | #include "tevent.h" |
32 | | #include "tevent_internal.h" |
33 | | #include "tevent_util.h" |
34 | | |
35 | | struct epoll_event_context { |
36 | | /* a pointer back to the generic event_context */ |
37 | | struct tevent_context *ev; |
38 | | |
39 | | /* when using epoll this is the handle from epoll_create1(2) */ |
40 | | int epoll_fd; |
41 | | |
42 | | pid_t pid; |
43 | | |
44 | | bool panic_force_replay; |
45 | | bool *panic_state; |
46 | | bool (*panic_fallback)(struct tevent_context *ev, bool replay); |
47 | | }; |
48 | | |
49 | 0 | #define EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT (1<<0) |
50 | 0 | #define EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR (1<<1) |
51 | | |
52 | | #ifdef TEST_PANIC_FALLBACK |
53 | | |
54 | | static int epoll_create1_panic_fallback(struct epoll_event_context *epoll_ev, |
55 | | int flags) |
56 | | { |
57 | | if (epoll_ev->panic_fallback == NULL) { |
58 | | return epoll_create1(flags); |
59 | | } |
60 | | |
61 | | /* 50% of the time, fail... */ |
62 | | if ((random() % 2) == 0) { |
63 | | errno = EINVAL; |
64 | | return -1; |
65 | | } |
66 | | |
67 | | return epoll_create1(flags); |
68 | | } |
69 | | |
70 | | static int epoll_ctl_panic_fallback(struct epoll_event_context *epoll_ev, |
71 | | int epfd, int op, int fd, |
72 | | struct epoll_event *event) |
73 | | { |
74 | | if (epoll_ev->panic_fallback == NULL) { |
75 | | return epoll_ctl(epfd, op, fd, event); |
76 | | } |
77 | | |
78 | | /* 50% of the time, fail... */ |
79 | | if ((random() % 2) == 0) { |
80 | | errno = EINVAL; |
81 | | return -1; |
82 | | } |
83 | | |
84 | | return epoll_ctl(epfd, op, fd, event); |
85 | | } |
86 | | |
87 | | static int epoll_wait_panic_fallback(struct epoll_event_context *epoll_ev, |
88 | | int epfd, |
89 | | struct epoll_event *events, |
90 | | int maxevents, |
91 | | int timeout) |
92 | | { |
93 | | if (epoll_ev->panic_fallback == NULL) { |
94 | | return epoll_wait(epfd, events, maxevents, timeout); |
95 | | } |
96 | | |
97 | | /* 50% of the time, fail... */ |
98 | | if ((random() % 2) == 0) { |
99 | | errno = EINVAL; |
100 | | return -1; |
101 | | } |
102 | | |
103 | | return epoll_wait(epfd, events, maxevents, timeout); |
104 | | } |
105 | | |
106 | | #define epoll_create1(_flags) \ |
107 | | epoll_create1_panic_fallback(epoll_ev, _flags) |
108 | | #define epoll_ctl(_epfd, _op, _fd, _event) \ |
109 | | epoll_ctl_panic_fallback(epoll_ev,_epfd, _op, _fd, _event) |
110 | | #define epoll_wait(_epfd, _events, _maxevents, _timeout) \ |
111 | | epoll_wait_panic_fallback(epoll_ev, _epfd, _events, _maxevents, _timeout) |
112 | | #endif |
113 | | |
114 | | /* |
115 | | called to set the panic fallback function. |
116 | | */ |
117 | | _PRIVATE_ void tevent_epoll_set_panic_fallback(struct tevent_context *ev, |
118 | | bool (*panic_fallback)(struct tevent_context *ev, |
119 | | bool replay)) |
120 | 0 | { |
121 | 0 | struct epoll_event_context *epoll_ev = |
122 | 0 | talloc_get_type_abort(ev->additional_data, |
123 | 0 | struct epoll_event_context); |
124 | |
|
125 | 0 | epoll_ev->panic_fallback = panic_fallback; |
126 | 0 | } |
127 | | |
128 | | /* |
129 | | called when a epoll call fails |
130 | | */ |
131 | | static void epoll_panic(struct epoll_event_context *epoll_ev, |
132 | | const char *reason, bool replay) |
133 | 0 | { |
134 | 0 | struct tevent_context *ev = epoll_ev->ev; |
135 | 0 | bool (*panic_fallback)(struct tevent_context *ev, bool replay); |
136 | |
|
137 | 0 | panic_fallback = epoll_ev->panic_fallback; |
138 | |
|
139 | 0 | if (epoll_ev->panic_state != NULL) { |
140 | 0 | *epoll_ev->panic_state = true; |
141 | 0 | } |
142 | |
|
143 | 0 | if (epoll_ev->panic_force_replay) { |
144 | 0 | replay = true; |
145 | 0 | } |
146 | |
|
147 | 0 | TALLOC_FREE(ev->additional_data); |
148 | |
|
149 | 0 | if (panic_fallback == NULL) { |
150 | 0 | tevent_debug(ev, TEVENT_DEBUG_FATAL, |
151 | 0 | "%s (%s) replay[%u] - calling abort()\n", |
152 | 0 | reason, strerror(errno), (unsigned)replay); |
153 | 0 | abort(); |
154 | 0 | } |
155 | | |
156 | 0 | tevent_debug(ev, TEVENT_DEBUG_ERROR, |
157 | 0 | "%s (%s) replay[%u] - calling panic_fallback\n", |
158 | 0 | reason, strerror(errno), (unsigned)replay); |
159 | |
|
160 | 0 | if (!panic_fallback(ev, replay)) { |
161 | | /* Fallback failed. */ |
162 | 0 | tevent_debug(ev, TEVENT_DEBUG_FATAL, |
163 | 0 | "%s (%s) replay[%u] - calling abort()\n", |
164 | 0 | reason, strerror(errno), (unsigned)replay); |
165 | 0 | abort(); |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | map from TEVENT_FD_* to EPOLLIN/EPOLLOUT |
171 | | */ |
172 | | static uint32_t epoll_map_flags(uint16_t flags) |
173 | 0 | { |
174 | 0 | uint32_t ret = 0; |
175 | | |
176 | | /* |
177 | | * we do not need to specify EPOLLERR | EPOLLHUP |
178 | | * they are always reported. |
179 | | */ |
180 | |
|
181 | 0 | if (flags & TEVENT_FD_READ) { |
182 | | /* |
183 | | * Note that EPOLLRDHUP always |
184 | | * returns EPOLLIN in addition, |
185 | | * so EPOLLRDHUP is not strictly needed, |
186 | | * but we want to make it explicit. |
187 | | */ |
188 | 0 | ret |= EPOLLIN | EPOLLRDHUP; |
189 | 0 | } |
190 | 0 | if (flags & TEVENT_FD_WRITE) { |
191 | 0 | ret |= EPOLLOUT; |
192 | 0 | } |
193 | 0 | if (flags & TEVENT_FD_ERROR) { |
194 | 0 | ret |= EPOLLRDHUP; |
195 | 0 | } |
196 | 0 | return ret; |
197 | 0 | } |
198 | | |
199 | | /* |
200 | | free the epoll fd |
201 | | */ |
202 | | static int epoll_ctx_destructor(struct epoll_event_context *epoll_ev) |
203 | 0 | { |
204 | 0 | close(epoll_ev->epoll_fd); |
205 | 0 | epoll_ev->epoll_fd = -1; |
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | | /* |
210 | | init the epoll fd |
211 | | */ |
212 | | static int epoll_init_ctx(struct epoll_event_context *epoll_ev) |
213 | 0 | { |
214 | 0 | epoll_ev->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
215 | 0 | if (epoll_ev->epoll_fd == -1) { |
216 | 0 | tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL, |
217 | 0 | "Failed to create epoll handle (%s).\n", |
218 | 0 | strerror(errno)); |
219 | 0 | return -1; |
220 | 0 | } |
221 | | |
222 | 0 | epoll_ev->pid = tevent_cached_getpid(); |
223 | 0 | talloc_set_destructor(epoll_ev, epoll_ctx_destructor); |
224 | |
|
225 | 0 | return 0; |
226 | 0 | } |
227 | | |
228 | | static void epoll_update_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde); |
229 | | |
230 | | /* |
231 | | reopen the epoll handle when our pid changes |
232 | | see http://junkcode.samba.org/ftp/unpacked/junkcode/epoll_fork.c for an |
233 | | demonstration of why this is needed |
234 | | */ |
235 | | static void epoll_check_reopen(struct epoll_event_context *epoll_ev) |
236 | 0 | { |
237 | 0 | struct tevent_fd *fde; |
238 | 0 | bool *caller_panic_state = epoll_ev->panic_state; |
239 | 0 | bool panic_triggered = false; |
240 | 0 | pid_t pid = tevent_cached_getpid(); |
241 | |
|
242 | 0 | if (epoll_ev->pid == pid) { |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | 0 | close(epoll_ev->epoll_fd); |
247 | 0 | epoll_ev->epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
248 | 0 | if (epoll_ev->epoll_fd == -1) { |
249 | 0 | epoll_panic(epoll_ev, "epoll_create() failed", false); |
250 | 0 | return; |
251 | 0 | } |
252 | | |
253 | 0 | epoll_ev->pid = pid; |
254 | 0 | epoll_ev->panic_state = &panic_triggered; |
255 | 0 | for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) { |
256 | | /* |
257 | | * We leave the mpx mappings alive |
258 | | * so that we'll just re-add events for |
259 | | * the existing primary events in the loop |
260 | | * below. |
261 | | */ |
262 | 0 | fde->additional_flags &= ~EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
263 | 0 | } |
264 | 0 | for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) { |
265 | 0 | epoll_update_event(epoll_ev, fde); |
266 | |
|
267 | 0 | if (panic_triggered) { |
268 | 0 | if (caller_panic_state != NULL) { |
269 | 0 | *caller_panic_state = true; |
270 | 0 | } |
271 | 0 | return; |
272 | 0 | } |
273 | 0 | } |
274 | 0 | epoll_ev->panic_state = NULL; |
275 | 0 | } |
276 | | |
277 | | /* |
278 | | epoll cannot add the same file descriptor twice, once |
279 | | with read, once with write which is allowed by the |
280 | | tevent poll backend. Multiplex the existing fde, flag it |
281 | | as such so we can search for the correct fde on |
282 | | event triggering. |
283 | | */ |
284 | | |
285 | | static int epoll_add_multiplex_fd(struct epoll_event_context *epoll_ev, |
286 | | struct tevent_fd *add_fde) |
287 | 0 | { |
288 | 0 | struct tevent_fd *primary = NULL; |
289 | 0 | uint16_t effective_flags; |
290 | 0 | struct epoll_event event; |
291 | 0 | uint64_t clear_flags = 0; |
292 | 0 | uint64_t add_flags = 0; |
293 | 0 | int ret; |
294 | | |
295 | | /* |
296 | | * Check if there is another fde we can attach to |
297 | | */ |
298 | 0 | primary = tevent_common_fd_mpx_add(add_fde); |
299 | 0 | if (primary == NULL) { |
300 | | /* the caller calls epoll_panic() */ |
301 | 0 | return -1; |
302 | 0 | } |
303 | | |
304 | | /* |
305 | | * First propagate the HAS_EVENT flag from |
306 | | * the primary to all others (mainly add_fde) |
307 | | */ |
308 | 0 | if (primary->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) { |
309 | 0 | add_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
310 | 0 | tevent_common_fd_mpx_additional_flags(primary, 0, add_flags); |
311 | 0 | } |
312 | | |
313 | | /* |
314 | | * Update the mpx internals and check if |
315 | | * there is an update needed. |
316 | | */ |
317 | 0 | primary = tevent_common_fd_mpx_update(primary); |
318 | 0 | if (primary == NULL) { |
319 | | /* |
320 | | * It seems the primary was already |
321 | | * watching (at least) the same flags |
322 | | * as add_fde, so we are done. |
323 | | */ |
324 | 0 | return 0; |
325 | 0 | } |
326 | | |
327 | | /* |
328 | | * Before me modify the low level epoll state, |
329 | | * we clear HAS_EVENT on all fdes. |
330 | | */ |
331 | 0 | clear_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
332 | 0 | tevent_common_fd_mpx_additional_flags(primary, clear_flags, 0); |
333 | |
|
334 | 0 | effective_flags = tevent_common_fd_mpx_flags(primary); |
335 | | |
336 | | /* |
337 | | * Modify the low level epoll state to reflect |
338 | | * the effective flags we want to monitor. |
339 | | */ |
340 | 0 | ZERO_STRUCT(event); |
341 | 0 | event.events = epoll_map_flags(effective_flags); |
342 | 0 | event.data.ptr = primary; |
343 | 0 | ret = epoll_ctl(epoll_ev->epoll_fd, |
344 | 0 | EPOLL_CTL_MOD, |
345 | 0 | primary->fd, |
346 | 0 | &event); |
347 | 0 | if (ret != 0 && errno == EBADF) { |
348 | 0 | struct tevent_common_fd_buf pbuf = {}; |
349 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_ERROR, |
350 | 0 | "EPOLL_CTL_MOD EBADF for " |
351 | 0 | "%s - disabling\n", |
352 | 0 | tevent_common_fd_str(&pbuf, "primary", primary)); |
353 | 0 | tevent_common_fd_mpx_disarm_all(primary); |
354 | 0 | return 0; |
355 | 0 | } else if (ret != 0) { |
356 | 0 | struct tevent_common_fd_buf pbuf = {}; |
357 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_FATAL, |
358 | 0 | "EPOLL_CTL_MOD for %s - failed - %s", |
359 | 0 | tevent_common_fd_str(&pbuf, "primary", primary), |
360 | 0 | strerror(errno)); |
361 | | /* the caller calls epoll_panic() */ |
362 | 0 | return ret; |
363 | 0 | } |
364 | | |
365 | | /* |
366 | | * Finally re-add HAS_EVENT to all fdes |
367 | | */ |
368 | 0 | add_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
369 | 0 | tevent_common_fd_mpx_additional_flags(primary, 0, add_flags); |
370 | |
|
371 | 0 | return 0; |
372 | 0 | } |
373 | | |
374 | | /* |
375 | | add the epoll event to the given fd_event |
376 | | */ |
377 | | static void epoll_add_event(struct epoll_event_context *epoll_ev, |
378 | | struct tevent_fd *_primary) |
379 | 0 | { |
380 | 0 | struct tevent_fd *primary = tevent_common_fd_mpx_primary(_primary); |
381 | 0 | uint16_t effective_flags = tevent_common_fd_mpx_flags(primary); |
382 | 0 | struct epoll_event event; |
383 | 0 | uint64_t clear_flags = 0; |
384 | 0 | uint64_t add_flags = 0; |
385 | 0 | int ret; |
386 | | |
387 | | /* |
388 | | * Before me modify the low level epoll state, |
389 | | * we clear HAS_EVENT on all fdes. |
390 | | */ |
391 | 0 | clear_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
392 | 0 | tevent_common_fd_mpx_additional_flags(primary, clear_flags, 0); |
393 | | |
394 | | /* |
395 | | * Modify the low level epoll state to reflect |
396 | | * the effective flags we want to monitor. |
397 | | * |
398 | | * Most likely we won't trigger the EEXIST |
399 | | * case, so it's much cheaper to try and |
400 | | * react on EEXIST if needed, than to always |
401 | | * scan the list of all existing events. |
402 | | */ |
403 | 0 | ZERO_STRUCT(event); |
404 | 0 | event.events = epoll_map_flags(effective_flags); |
405 | 0 | event.data.ptr = primary; |
406 | 0 | ret = epoll_ctl(epoll_ev->epoll_fd, |
407 | 0 | EPOLL_CTL_ADD, |
408 | 0 | primary->fd, |
409 | 0 | &event); |
410 | 0 | if (ret != 0 && errno == EBADF) { |
411 | 0 | struct tevent_common_fd_buf pbuf = {}; |
412 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_ERROR, |
413 | 0 | "EPOLL_CTL_ADD EBADF for " |
414 | 0 | "%s - disabling\n", |
415 | 0 | tevent_common_fd_str(&pbuf, "primary", primary)); |
416 | 0 | tevent_common_fd_mpx_disarm_all(primary); |
417 | 0 | return; |
418 | 0 | } else if (ret != 0 && errno == EEXIST) { |
419 | 0 | ret = epoll_add_multiplex_fd(epoll_ev, primary); |
420 | 0 | if (ret != 0) { |
421 | 0 | epoll_panic(epoll_ev, "epoll_add_multiplex_fd failed", |
422 | 0 | false); |
423 | 0 | return; |
424 | 0 | } |
425 | | /* |
426 | | * epoll_add_multiplex_fd() already |
427 | | * added EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT |
428 | | */ |
429 | 0 | return; |
430 | 0 | } else if (ret != 0) { |
431 | 0 | epoll_panic(epoll_ev, "EPOLL_CTL_ADD failed", false); |
432 | 0 | return; |
433 | 0 | } |
434 | | |
435 | | /* |
436 | | * Finally re-add HAS_EVENT to all fdes |
437 | | */ |
438 | 0 | add_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
439 | 0 | tevent_common_fd_mpx_additional_flags(primary, 0, add_flags); |
440 | 0 | } |
441 | | |
442 | | /* |
443 | | delete the epoll event for given fd_event |
444 | | */ |
445 | | static void epoll_del_event(struct epoll_event_context *epoll_ev, |
446 | | struct tevent_fd *_primary) |
447 | 0 | { |
448 | 0 | struct tevent_fd *primary = tevent_common_fd_mpx_primary(_primary); |
449 | 0 | struct epoll_event event; |
450 | 0 | uint64_t clear_flags = 0; |
451 | 0 | int ret; |
452 | | |
453 | | /* |
454 | | * Before me delete the low level epoll state, |
455 | | * we clear HAS_EVENT on all fdes. |
456 | | */ |
457 | 0 | clear_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
458 | 0 | tevent_common_fd_mpx_additional_flags(primary, clear_flags, 0); |
459 | | |
460 | | /* |
461 | | * Delete the low level epoll state to reflect |
462 | | * the effective flags we want to monitor. |
463 | | */ |
464 | 0 | ZERO_STRUCT(event); |
465 | 0 | ret = epoll_ctl(epoll_ev->epoll_fd, |
466 | 0 | EPOLL_CTL_DEL, |
467 | 0 | primary->fd, |
468 | 0 | &event); |
469 | 0 | if (ret != 0 && errno == ENOENT) { |
470 | 0 | struct tevent_common_fd_buf pbuf = {}; |
471 | | /* |
472 | | * This can happen after a epoll_check_reopen |
473 | | * within epoll_event_fd_destructor. |
474 | | */ |
475 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_TRACE, |
476 | 0 | "EPOLL_CTL_DEL ignoring ENOENT for %s\n", |
477 | 0 | tevent_common_fd_str(&pbuf, "primary", primary)); |
478 | 0 | return; |
479 | 0 | } else if (ret != 0 && errno == EBADF) { |
480 | 0 | struct tevent_common_fd_buf pbuf = {}; |
481 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_WARNING, |
482 | 0 | "EPOLL_CTL_DEL EBADF for %s - disabling\n", |
483 | 0 | tevent_common_fd_str(&pbuf, "primary", primary)); |
484 | 0 | tevent_common_fd_mpx_disarm_all(primary); |
485 | 0 | return; |
486 | 0 | } else if (ret != 0) { |
487 | 0 | struct tevent_common_fd_buf pbuf = {}; |
488 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_FATAL, |
489 | 0 | "EPOLL_CTL_DEL for %s - failed - %s", |
490 | 0 | tevent_common_fd_str(&pbuf, "primary", primary), |
491 | 0 | strerror(errno)); |
492 | 0 | epoll_panic(epoll_ev, "EPOLL_CTL_DEL failed", false); |
493 | 0 | return; |
494 | 0 | } |
495 | 0 | } |
496 | | |
497 | | /* |
498 | | change the epoll event to the given fd_event |
499 | | */ |
500 | | static void epoll_mod_event(struct epoll_event_context *epoll_ev, |
501 | | struct tevent_fd *_primary) |
502 | 0 | { |
503 | 0 | struct tevent_fd *primary = tevent_common_fd_mpx_primary(_primary); |
504 | 0 | uint16_t effective_flags = tevent_common_fd_mpx_flags(primary); |
505 | 0 | struct epoll_event event; |
506 | 0 | uint64_t clear_flags = 0; |
507 | 0 | uint64_t add_flags = 0; |
508 | 0 | int ret; |
509 | | |
510 | | /* |
511 | | * Before me modify the low level epoll state, |
512 | | * we clear HAS_EVENT on all fdes. |
513 | | */ |
514 | 0 | clear_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
515 | 0 | tevent_common_fd_mpx_additional_flags(primary, clear_flags, 0); |
516 | | |
517 | | /* |
518 | | * Modify the low level epoll state to reflect |
519 | | * the effective flags we want to monitor. |
520 | | */ |
521 | 0 | ZERO_STRUCT(event); |
522 | 0 | event.events = epoll_map_flags(effective_flags); |
523 | 0 | event.data.ptr = primary; |
524 | 0 | ret = epoll_ctl(epoll_ev->epoll_fd, |
525 | 0 | EPOLL_CTL_MOD, |
526 | 0 | primary->fd, |
527 | 0 | &event); |
528 | 0 | if (ret != 0 && errno == EBADF) { |
529 | 0 | struct tevent_common_fd_buf pbuf = {}; |
530 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_ERROR, |
531 | 0 | "EPOLL_CTL_MOD EBADF for %s - disabling\n", |
532 | 0 | tevent_common_fd_str(&pbuf, "primary", primary)); |
533 | 0 | tevent_common_fd_mpx_disarm_all(primary); |
534 | 0 | return; |
535 | 0 | } else if (ret != 0) { |
536 | 0 | struct tevent_common_fd_buf pbuf = {}; |
537 | 0 | TEVENT_DEBUG(epoll_ev->ev, TEVENT_DEBUG_FATAL, |
538 | 0 | "EPOLL_CTL_MOD for %s - failed - %s", |
539 | 0 | tevent_common_fd_str(&pbuf, "primary", primary), |
540 | 0 | strerror(errno)); |
541 | 0 | epoll_panic(epoll_ev, "EPOLL_CTL_MOD failed", false); |
542 | 0 | return; |
543 | 0 | } |
544 | | |
545 | | /* |
546 | | * Finally re-add HAS_EVENT to all fdes |
547 | | */ |
548 | 0 | add_flags |= EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT; |
549 | 0 | tevent_common_fd_mpx_additional_flags(primary, 0, add_flags); |
550 | 0 | } |
551 | | |
552 | | static void epoll_update_event(struct epoll_event_context *epoll_ev, struct tevent_fd *fde) |
553 | 0 | { |
554 | 0 | struct tevent_fd *primary = tevent_common_fd_mpx_primary(fde); |
555 | 0 | uint64_t _paf = primary->additional_flags; |
556 | 0 | bool got_error = (_paf & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR); |
557 | 0 | uint16_t effective_flags = tevent_common_fd_mpx_flags(primary); |
558 | 0 | bool want_read = (effective_flags & TEVENT_FD_READ); |
559 | 0 | bool want_write= (effective_flags & TEVENT_FD_WRITE); |
560 | 0 | bool want_error= (effective_flags & TEVENT_FD_ERROR); |
561 | | |
562 | | /* there's already an event */ |
563 | 0 | if (primary->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_HAS_EVENT) { |
564 | 0 | if (want_read || want_error || (want_write && !got_error)) { |
565 | 0 | epoll_mod_event(epoll_ev, primary); |
566 | 0 | return; |
567 | 0 | } |
568 | | /* |
569 | | * if we want to match the select behavior, we need to remove the epoll_event |
570 | | * when the caller isn't interested in events. |
571 | | * |
572 | | * this is because epoll reports EPOLLERR and EPOLLHUP, even without asking for them |
573 | | */ |
574 | 0 | epoll_del_event(epoll_ev, primary); |
575 | 0 | return; |
576 | 0 | } |
577 | | |
578 | | /* there's no epoll_event attached to the fde */ |
579 | 0 | if (want_read || want_error || (want_write && !got_error)) { |
580 | 0 | epoll_add_event(epoll_ev, primary); |
581 | 0 | return; |
582 | 0 | } |
583 | 0 | } |
584 | | |
585 | | /* |
586 | | event loop handling using epoll |
587 | | */ |
588 | | static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp) |
589 | 0 | { |
590 | 0 | int ret, i; |
591 | 0 | #define MAXEVENTS 1 |
592 | 0 | struct epoll_event events[MAXEVENTS]; |
593 | 0 | int timeout = tevent_common_timeout_msec(tvalp); |
594 | 0 | int wait_errno; |
595 | |
|
596 | 0 | if (epoll_ev->ev->signal_events && |
597 | 0 | tevent_common_check_signal(epoll_ev->ev)) { |
598 | 0 | return 0; |
599 | 0 | } |
600 | | |
601 | 0 | tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_BEFORE_WAIT); |
602 | 0 | ret = epoll_wait(epoll_ev->epoll_fd, events, MAXEVENTS, timeout); |
603 | 0 | wait_errno = errno; |
604 | 0 | tevent_trace_point_callback(epoll_ev->ev, TEVENT_TRACE_AFTER_WAIT); |
605 | |
|
606 | 0 | if (ret == -1 && wait_errno == EINTR && epoll_ev->ev->signal_events) { |
607 | 0 | if (tevent_common_check_signal(epoll_ev->ev)) { |
608 | 0 | return 0; |
609 | 0 | } |
610 | 0 | } |
611 | | |
612 | 0 | if (ret == -1 && wait_errno != EINTR) { |
613 | 0 | epoll_panic(epoll_ev, "epoll_wait() failed", true); |
614 | 0 | return -1; |
615 | 0 | } |
616 | | |
617 | 0 | if (ret == 0) { |
618 | | /* |
619 | | * tevent_context_set_wait_timeout(0) was used. |
620 | | */ |
621 | 0 | if (tevent_common_no_timeout(tvalp)) { |
622 | 0 | errno = EAGAIN; |
623 | 0 | return -1; |
624 | 0 | } |
625 | | |
626 | | /* we don't care about a possible delay here */ |
627 | 0 | tevent_common_loop_timer_delay(epoll_ev->ev); |
628 | 0 | return 0; |
629 | 0 | } |
630 | | |
631 | 0 | for (i=0;i<ret;i++) { |
632 | 0 | struct tevent_fd *fde = talloc_get_type(events[i].data.ptr, |
633 | 0 | struct tevent_fd); |
634 | 0 | struct tevent_fd *selected = NULL; |
635 | 0 | uint16_t effective_flags; |
636 | 0 | uint16_t flags = 0; |
637 | 0 | bool got_error = false; |
638 | |
|
639 | 0 | if (fde == NULL) { |
640 | 0 | epoll_panic(epoll_ev, "epoll_wait() gave bad data", true); |
641 | 0 | return -1; |
642 | 0 | } |
643 | 0 | effective_flags = tevent_common_fd_mpx_flags(fde); |
644 | 0 | if (events[i].events & (EPOLLHUP|EPOLLERR|EPOLLRDHUP)) { |
645 | 0 | uint64_t add_flags = 0; |
646 | |
|
647 | 0 | add_flags |= EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR; |
648 | 0 | tevent_common_fd_mpx_additional_flags(fde, |
649 | 0 | 0, |
650 | 0 | add_flags); |
651 | |
|
652 | 0 | if (effective_flags & TEVENT_FD_ERROR) { |
653 | 0 | flags |= TEVENT_FD_ERROR; |
654 | 0 | } |
655 | 0 | if (effective_flags & TEVENT_FD_READ) { |
656 | 0 | flags |= TEVENT_FD_READ; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | if (events[i].events & EPOLLIN) { |
660 | 0 | if (effective_flags & TEVENT_FD_READ) { |
661 | 0 | flags |= TEVENT_FD_READ; |
662 | 0 | } |
663 | 0 | } |
664 | 0 | if (events[i].events & EPOLLOUT) { |
665 | 0 | if (effective_flags & TEVENT_FD_WRITE) { |
666 | 0 | flags |= TEVENT_FD_WRITE; |
667 | 0 | } |
668 | 0 | } |
669 | |
|
670 | 0 | if (fde->additional_flags & EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR) |
671 | 0 | { |
672 | 0 | got_error = true; |
673 | 0 | } |
674 | |
|
675 | 0 | selected = tevent_common_fd_mpx_select(fde, flags, got_error); |
676 | 0 | if (selected == NULL) { |
677 | 0 | if (got_error) { |
678 | | /* |
679 | | * if we only wait for TEVENT_FD_WRITE, we |
680 | | * should not tell the event handler about it, |
681 | | * and remove the epoll_event, as we only |
682 | | * report errors when waiting for read events, |
683 | | * to match the select() behavior |
684 | | * |
685 | | * Do the same as the poll backend and |
686 | | * remove the writeable flag. |
687 | | */ |
688 | 0 | tevent_common_fd_mpx_clear_writeable(fde); |
689 | 0 | epoll_update_event(epoll_ev, fde); |
690 | 0 | } |
691 | 0 | continue; |
692 | 0 | } |
693 | | |
694 | | /* |
695 | | * make sure we only pass the flags |
696 | | * the handler is expecting. |
697 | | */ |
698 | 0 | flags &= selected->flags; |
699 | 0 | return tevent_common_invoke_fd_handler(selected, |
700 | 0 | flags, |
701 | 0 | NULL); |
702 | 0 | } |
703 | | |
704 | 0 | return 0; |
705 | 0 | } |
706 | | |
707 | | /* |
708 | | create a epoll_event_context structure. |
709 | | */ |
710 | | static int epoll_event_context_init(struct tevent_context *ev) |
711 | 0 | { |
712 | 0 | int ret; |
713 | 0 | struct epoll_event_context *epoll_ev; |
714 | | |
715 | | /* |
716 | | * We might be called during tevent_re_initialise() |
717 | | * which means we need to free our old additional_data. |
718 | | */ |
719 | 0 | TALLOC_FREE(ev->additional_data); |
720 | |
|
721 | 0 | epoll_ev = talloc_zero(ev, struct epoll_event_context); |
722 | 0 | if (!epoll_ev) return -1; |
723 | 0 | epoll_ev->ev = ev; |
724 | 0 | epoll_ev->epoll_fd = -1; |
725 | |
|
726 | 0 | ret = epoll_init_ctx(epoll_ev); |
727 | 0 | if (ret != 0) { |
728 | 0 | talloc_free(epoll_ev); |
729 | 0 | return ret; |
730 | 0 | } |
731 | | |
732 | 0 | ev->additional_data = epoll_ev; |
733 | 0 | return 0; |
734 | 0 | } |
735 | | |
736 | | /* |
737 | | destroy an fd_event |
738 | | */ |
739 | | static int epoll_event_fd_destructor(struct tevent_fd *fde) |
740 | 0 | { |
741 | 0 | struct tevent_fd *old_primary = NULL; |
742 | 0 | struct tevent_fd *new_primary = NULL; |
743 | 0 | struct tevent_fd *update_primary = NULL; |
744 | 0 | struct tevent_context *ev = fde->event_ctx; |
745 | 0 | struct epoll_event_context *epoll_ev = NULL; |
746 | 0 | bool panic_triggered = false; |
747 | |
|
748 | 0 | if (ev == NULL) { |
749 | 0 | tevent_common_fd_mpx_reinit(fde); |
750 | 0 | return tevent_common_fd_destructor(fde); |
751 | 0 | } |
752 | | |
753 | 0 | epoll_ev = talloc_get_type_abort(ev->additional_data, |
754 | 0 | struct epoll_event_context); |
755 | | |
756 | | /* |
757 | | * we must remove the event from the list |
758 | | * otherwise a panic fallback handler may |
759 | | * reuse invalid memory |
760 | | */ |
761 | 0 | DLIST_REMOVE(ev->fd_events, fde); |
762 | |
|
763 | 0 | epoll_ev->panic_state = &panic_triggered; |
764 | 0 | if (epoll_ev->pid != tevent_cached_getpid()) { |
765 | 0 | epoll_check_reopen(epoll_ev); |
766 | 0 | if (panic_triggered) { |
767 | 0 | tevent_common_fd_mpx_reinit(fde); |
768 | 0 | return tevent_common_fd_destructor(fde); |
769 | 0 | } |
770 | 0 | } |
771 | | |
772 | 0 | old_primary = tevent_common_fd_mpx_primary(fde); |
773 | |
|
774 | 0 | if (old_primary == fde) { |
775 | 0 | epoll_del_event(epoll_ev, fde); |
776 | 0 | if (panic_triggered) { |
777 | 0 | tevent_common_fd_mpx_reinit(fde); |
778 | 0 | return tevent_common_fd_destructor(fde); |
779 | 0 | } |
780 | 0 | } |
781 | | |
782 | 0 | new_primary = tevent_common_fd_mpx_remove(fde); |
783 | 0 | if (new_primary == NULL) { |
784 | 0 | epoll_ev->panic_state = NULL; |
785 | 0 | return tevent_common_fd_destructor(fde); |
786 | 0 | } |
787 | 0 | update_primary = tevent_common_fd_mpx_update(new_primary); |
788 | 0 | if (update_primary == NULL) { |
789 | 0 | epoll_ev->panic_state = NULL; |
790 | 0 | return tevent_common_fd_destructor(fde); |
791 | 0 | } |
792 | | |
793 | 0 | epoll_update_event(epoll_ev, update_primary); |
794 | 0 | if (panic_triggered) { |
795 | 0 | return tevent_common_fd_destructor(fde); |
796 | 0 | } |
797 | 0 | epoll_ev->panic_state = NULL; |
798 | |
|
799 | 0 | return tevent_common_fd_destructor(fde); |
800 | 0 | } |
801 | | |
802 | | /* |
803 | | add a fd based event |
804 | | return NULL on failure (memory allocation error) |
805 | | */ |
806 | | static struct tevent_fd *epoll_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx, |
807 | | int fd, uint16_t flags, |
808 | | tevent_fd_handler_t handler, |
809 | | void *private_data, |
810 | | const char *handler_name, |
811 | | const char *location) |
812 | 0 | { |
813 | 0 | struct epoll_event_context *epoll_ev = |
814 | 0 | talloc_get_type_abort(ev->additional_data, |
815 | 0 | struct epoll_event_context); |
816 | 0 | struct tevent_fd *fde; |
817 | 0 | bool panic_triggered = false; |
818 | 0 | pid_t old_pid = epoll_ev->pid; |
819 | |
|
820 | 0 | fde = tevent_common_add_fd(ev, mem_ctx, fd, flags, |
821 | 0 | handler, private_data, |
822 | 0 | handler_name, location); |
823 | 0 | if (!fde) return NULL; |
824 | | |
825 | 0 | talloc_set_destructor(fde, epoll_event_fd_destructor); |
826 | | |
827 | | /* |
828 | | * prepare for tevent_common_fd_mpx_flags() |
829 | | * in epoll_update_event() |
830 | | */ |
831 | 0 | tevent_common_fd_mpx_update_flags(fde); |
832 | |
|
833 | 0 | if (epoll_ev->pid != tevent_cached_getpid()) { |
834 | 0 | epoll_ev->panic_state = &panic_triggered; |
835 | 0 | epoll_check_reopen(epoll_ev); |
836 | 0 | if (panic_triggered) { |
837 | 0 | return fde; |
838 | 0 | } |
839 | 0 | epoll_ev->panic_state = NULL; |
840 | 0 | } |
841 | | |
842 | 0 | if (epoll_ev->pid == old_pid) { |
843 | 0 | epoll_update_event(epoll_ev, fde); |
844 | 0 | } |
845 | |
|
846 | 0 | return fde; |
847 | 0 | } |
848 | | |
849 | | /* |
850 | | set the fd event flags |
851 | | */ |
852 | | static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags) |
853 | 0 | { |
854 | 0 | struct tevent_context *ev; |
855 | 0 | struct epoll_event_context *epoll_ev; |
856 | 0 | bool panic_triggered = false; |
857 | 0 | pid_t old_pid; |
858 | |
|
859 | 0 | if (fde->flags == flags) return; |
860 | | |
861 | 0 | ev = fde->event_ctx; |
862 | 0 | epoll_ev = talloc_get_type_abort(ev->additional_data, |
863 | 0 | struct epoll_event_context); |
864 | 0 | old_pid = epoll_ev->pid; |
865 | |
|
866 | 0 | fde->flags = flags; |
867 | | /* |
868 | | * prepare for tevent_common_fd_mpx_flags() |
869 | | * in epoll_update_event() |
870 | | */ |
871 | 0 | tevent_common_fd_mpx_update_flags(fde); |
872 | |
|
873 | 0 | if (epoll_ev->pid != tevent_cached_getpid()) { |
874 | 0 | epoll_ev->panic_state = &panic_triggered; |
875 | 0 | epoll_check_reopen(epoll_ev); |
876 | 0 | if (panic_triggered) { |
877 | 0 | return; |
878 | 0 | } |
879 | 0 | epoll_ev->panic_state = NULL; |
880 | 0 | } |
881 | | |
882 | 0 | if (epoll_ev->pid == old_pid) { |
883 | 0 | epoll_update_event(epoll_ev, fde); |
884 | 0 | } |
885 | 0 | } |
886 | | |
887 | | /* |
888 | | do a single event loop using the events defined in ev |
889 | | */ |
890 | | static int epoll_event_loop_once(struct tevent_context *ev, const char *location) |
891 | 0 | { |
892 | 0 | struct epoll_event_context *epoll_ev = |
893 | 0 | talloc_get_type_abort(ev->additional_data, |
894 | 0 | struct epoll_event_context); |
895 | 0 | struct timeval tval; |
896 | 0 | bool panic_triggered = false; |
897 | |
|
898 | 0 | if (ev->signal_events && |
899 | 0 | tevent_common_check_signal(ev)) { |
900 | 0 | return 0; |
901 | 0 | } |
902 | | |
903 | 0 | if (ev->threaded_contexts != NULL) { |
904 | 0 | tevent_common_threaded_activate_immediate(ev); |
905 | 0 | } |
906 | |
|
907 | 0 | if (ev->immediate_events && |
908 | 0 | tevent_common_loop_immediate(ev)) { |
909 | 0 | return 0; |
910 | 0 | } |
911 | | |
912 | 0 | tval = tevent_common_loop_timer_delay(ev); |
913 | 0 | if (tevent_timeval_is_zero(&tval)) { |
914 | 0 | return 0; |
915 | 0 | } |
916 | | |
917 | 0 | if (epoll_ev->pid != tevent_cached_getpid()) { |
918 | 0 | epoll_ev->panic_state = &panic_triggered; |
919 | 0 | epoll_ev->panic_force_replay = true; |
920 | 0 | epoll_check_reopen(epoll_ev); |
921 | 0 | if (panic_triggered) { |
922 | 0 | errno = EINVAL; |
923 | 0 | return -1; |
924 | 0 | } |
925 | 0 | epoll_ev->panic_force_replay = false; |
926 | 0 | epoll_ev->panic_state = NULL; |
927 | 0 | } |
928 | | |
929 | 0 | return epoll_event_loop(epoll_ev, &tval); |
930 | 0 | } |
931 | | |
932 | | static const struct tevent_ops epoll_event_ops = { |
933 | | .context_init = epoll_event_context_init, |
934 | | .add_fd = epoll_event_add_fd, |
935 | | .set_fd_close_fn = tevent_common_fd_set_close_fn, |
936 | | .get_fd_flags = tevent_common_fd_get_flags, |
937 | | .set_fd_flags = epoll_event_set_fd_flags, |
938 | | .add_timer = tevent_common_add_timer_v2, |
939 | | .schedule_immediate = tevent_common_schedule_immediate, |
940 | | .add_signal = tevent_common_add_signal, |
941 | | .loop_once = epoll_event_loop_once, |
942 | | .loop_wait = tevent_common_loop_wait, |
943 | | }; |
944 | | |
945 | | _PRIVATE_ bool tevent_epoll_init(void) |
946 | 0 | { |
947 | 0 | return tevent_register_backend("epoll", &epoll_event_ops); |
948 | 0 | } |