dsdb:repl_meta_data: allow CONTROL_DBCHECK_FIX_LINK_DN_NAME to by pass rename
[Samba.git] / lib / tevent / tevent_fd.c
blobb92c45f1dddec1f6b7ec5cc9c0387c9e0e71c0f9
1 /*
2 Unix SMB/CIFS implementation.
4 common events code for fd events
6 Copyright (C) Stefan Metzmacher 2009
8 ** NOTE! The following LGPL license applies to the tevent
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "replace.h"
27 #include "tevent.h"
28 #include "tevent_internal.h"
29 #include "tevent_util.h"
31 int tevent_common_fd_destructor(struct tevent_fd *fde)
33 if (fde->destroyed) {
34 tevent_common_check_double_free(fde, "tevent_fd double free");
35 goto done;
37 fde->destroyed = true;
39 if (fde->event_ctx) {
40 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
43 if (fde->close_fn) {
44 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
45 fde->fd = -1;
46 fde->close_fn = NULL;
49 fde->event_ctx = NULL;
50 done:
51 if (fde->busy) {
52 return -1;
54 fde->wrapper = NULL;
56 return 0;
59 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
60 int fd, uint16_t flags,
61 tevent_fd_handler_t handler,
62 void *private_data,
63 const char *handler_name,
64 const char *location)
66 struct tevent_fd *fde;
68 /* tevent will crash later on select() if we save
69 * a negative file descriptor. Better to fail here
70 * so that consumers will be able to debug it
72 if (fd < 0) return NULL;
74 fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
75 if (!fde) return NULL;
77 *fde = (struct tevent_fd) {
78 .event_ctx = ev,
79 .fd = fd,
80 .flags = flags,
81 .handler = handler,
82 .private_data = private_data,
83 .handler_name = handler_name,
84 .location = location,
87 DLIST_ADD(ev->fd_events, fde);
89 talloc_set_destructor(fde, tevent_common_fd_destructor);
91 return fde;
93 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
95 return fde->flags;
98 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
100 if (fde->flags == flags) return;
101 fde->flags = flags;
104 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
105 tevent_fd_close_fn_t close_fn)
107 fde->close_fn = close_fn;
110 int tevent_common_invoke_fd_handler(struct tevent_fd *fde, uint16_t flags,
111 bool *removed)
113 struct tevent_context *handler_ev = fde->event_ctx;
115 if (removed != NULL) {
116 *removed = false;
119 if (fde->event_ctx == NULL) {
120 return 0;
123 fde->busy = true;
124 if (fde->wrapper != NULL) {
125 handler_ev = fde->wrapper->wrap_ev;
127 tevent_wrapper_push_use_internal(handler_ev, fde->wrapper);
128 fde->wrapper->ops->before_fd_handler(
129 fde->wrapper->wrap_ev,
130 fde->wrapper->private_state,
131 fde->wrapper->main_ev,
132 fde,
133 flags,
134 fde->handler_name,
135 fde->location);
137 fde->handler(handler_ev, fde, flags, fde->private_data);
138 if (fde->wrapper != NULL) {
139 fde->wrapper->ops->after_fd_handler(
140 fde->wrapper->wrap_ev,
141 fde->wrapper->private_state,
142 fde->wrapper->main_ev,
143 fde,
144 flags,
145 fde->handler_name,
146 fde->location);
147 tevent_wrapper_pop_use_internal(handler_ev, fde->wrapper);
149 fde->busy = false;
151 if (fde->destroyed) {
152 talloc_set_destructor(fde, NULL);
153 TALLOC_FREE(fde);
154 if (removed != NULL) {
155 *removed = true;
159 return 0;