ctdb-tcp: Factor out function ctdb_tcp_start_outgoing()
[Samba.git] / lib / tevent / tevent_fd.c
bloba0557fedbecd6f699330e201b6388190b3efa058
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 #define TEVENT_DEPRECATED 1
28 #include "tevent.h"
29 #include "tevent_internal.h"
30 #include "tevent_util.h"
32 int tevent_common_fd_destructor(struct tevent_fd *fde)
34 if (fde->destroyed) {
35 tevent_common_check_double_free(fde, "tevent_fd double free");
36 goto done;
38 fde->destroyed = true;
40 if (fde->event_ctx) {
41 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
44 if (fde->close_fn) {
45 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
46 fde->fd = -1;
47 fde->close_fn = NULL;
50 fde->event_ctx = NULL;
51 done:
52 if (fde->busy) {
53 return -1;
55 fde->wrapper = NULL;
57 return 0;
60 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
61 int fd, uint16_t flags,
62 tevent_fd_handler_t handler,
63 void *private_data,
64 const char *handler_name,
65 const char *location)
67 struct tevent_fd *fde;
69 /* tevent will crash later on select() if we save
70 * a negative file descriptor. Better to fail here
71 * so that consumers will be able to debug it
73 if (fd < 0) return NULL;
75 fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
76 if (!fde) return NULL;
78 *fde = (struct tevent_fd) {
79 .event_ctx = ev,
80 .fd = fd,
81 .flags = flags,
82 .handler = handler,
83 .private_data = private_data,
84 .handler_name = handler_name,
85 .location = location,
88 DLIST_ADD(ev->fd_events, fde);
90 talloc_set_destructor(fde, tevent_common_fd_destructor);
92 return fde;
94 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
96 return fde->flags;
99 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
101 if (fde->flags == flags) return;
102 fde->flags = flags;
105 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
106 tevent_fd_close_fn_t close_fn)
108 fde->close_fn = close_fn;
111 int tevent_common_invoke_fd_handler(struct tevent_fd *fde, uint16_t flags,
112 bool *removed)
114 struct tevent_context *handler_ev = fde->event_ctx;
116 if (removed != NULL) {
117 *removed = false;
120 if (fde->event_ctx == NULL) {
121 return 0;
124 fde->busy = true;
125 if (fde->wrapper != NULL) {
126 handler_ev = fde->wrapper->wrap_ev;
128 tevent_wrapper_push_use_internal(handler_ev, fde->wrapper);
129 fde->wrapper->ops->before_fd_handler(
130 fde->wrapper->wrap_ev,
131 fde->wrapper->private_state,
132 fde->wrapper->main_ev,
133 fde,
134 flags,
135 fde->handler_name,
136 fde->location);
138 fde->handler(handler_ev, fde, flags, fde->private_data);
139 if (fde->wrapper != NULL) {
140 fde->wrapper->ops->after_fd_handler(
141 fde->wrapper->wrap_ev,
142 fde->wrapper->private_state,
143 fde->wrapper->main_ev,
144 fde,
145 flags,
146 fde->handler_name,
147 fde->location);
148 tevent_wrapper_pop_use_internal(handler_ev, fde->wrapper);
150 fde->busy = false;
152 if (fde->destroyed) {
153 talloc_set_destructor(fde, NULL);
154 TALLOC_FREE(fde);
155 if (removed != NULL) {
156 *removed = true;
160 return 0;