Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : common events code for fd events
5 :
6 : Copyright (C) Stefan Metzmacher 2009
7 :
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
11 :
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.
16 :
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.
21 :
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/>.
24 : */
25 :
26 : #include "replace.h"
27 : #define TEVENT_DEPRECATED 1
28 : #include "tevent.h"
29 : #include "tevent_internal.h"
30 : #include "tevent_util.h"
31 :
32 11906547 : int tevent_common_fd_destructor(struct tevent_fd *fde)
33 : {
34 11906547 : if (fde->destroyed) {
35 3036083 : tevent_common_check_double_free(fde, "tevent_fd double free");
36 3036083 : goto done;
37 : }
38 8870464 : fde->destroyed = true;
39 :
40 8870464 : if (fde->event_ctx) {
41 8528365 : tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_DETACH);
42 8528365 : DLIST_REMOVE(fde->event_ctx->fd_events, fde);
43 : }
44 :
45 8870464 : if (fde->close_fn) {
46 6870381 : fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
47 6870381 : fde->fd = -1;
48 6870381 : fde->close_fn = NULL;
49 : }
50 :
51 8870464 : fde->event_ctx = NULL;
52 11906547 : done:
53 11906547 : if (fde->busy) {
54 10998758 : return -1;
55 : }
56 907789 : fde->wrapper = NULL;
57 :
58 907789 : return 0;
59 : }
60 :
61 8875895 : struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
62 : int fd, uint16_t flags,
63 : tevent_fd_handler_t handler,
64 : void *private_data,
65 : const char *handler_name,
66 : const char *location)
67 : {
68 : struct tevent_fd *fde;
69 :
70 : /* tevent will crash later on select() if we save
71 : * a negative file descriptor. Better to fail here
72 : * so that consumers will be able to debug it
73 : */
74 8875895 : if (fd < 0) return NULL;
75 :
76 8875895 : fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
77 8875895 : if (!fde) return NULL;
78 :
79 8875895 : *fde = (struct tevent_fd) {
80 : .event_ctx = ev,
81 : .fd = fd,
82 : .flags = flags,
83 : .handler = handler,
84 : .private_data = private_data,
85 : .handler_name = handler_name,
86 : .location = location,
87 : };
88 :
89 8875895 : tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_ATTACH);
90 8875895 : DLIST_ADD(ev->fd_events, fde);
91 :
92 8875895 : talloc_set_destructor(fde, tevent_common_fd_destructor);
93 :
94 :
95 8875895 : return fde;
96 : }
97 27740862 : uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
98 : {
99 27740862 : return fde->flags;
100 : }
101 :
102 0 : void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
103 : {
104 0 : if (fde->flags == flags) return;
105 0 : fde->flags = flags;
106 : }
107 :
108 6881190 : void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
109 : tevent_fd_close_fn_t close_fn)
110 : {
111 6881190 : fde->close_fn = close_fn;
112 6881190 : }
113 :
114 46370510 : int tevent_common_invoke_fd_handler(struct tevent_fd *fde, uint16_t flags,
115 : bool *removed)
116 : {
117 46370510 : struct tevent_context *handler_ev = fde->event_ctx;
118 :
119 46370510 : if (removed != NULL) {
120 0 : *removed = false;
121 : }
122 :
123 46370510 : if (fde->event_ctx == NULL) {
124 0 : return 0;
125 : }
126 :
127 46370510 : fde->busy = true;
128 46370510 : if (fde->wrapper != NULL) {
129 0 : handler_ev = fde->wrapper->wrap_ev;
130 :
131 0 : tevent_wrapper_push_use_internal(handler_ev, fde->wrapper);
132 0 : fde->wrapper->ops->before_fd_handler(
133 0 : fde->wrapper->wrap_ev,
134 0 : fde->wrapper->private_state,
135 0 : fde->wrapper->main_ev,
136 : fde,
137 : flags,
138 : fde->handler_name,
139 : fde->location);
140 : }
141 46370510 : tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_BEFORE_HANDLER);
142 46370510 : fde->handler(handler_ev, fde, flags, fde->private_data);
143 46345828 : if (fde->wrapper != NULL) {
144 0 : fde->wrapper->ops->after_fd_handler(
145 0 : fde->wrapper->wrap_ev,
146 0 : fde->wrapper->private_state,
147 0 : fde->wrapper->main_ev,
148 : fde,
149 : flags,
150 : fde->handler_name,
151 : fde->location);
152 0 : tevent_wrapper_pop_use_internal(handler_ev, fde->wrapper);
153 : }
154 46345828 : fde->busy = false;
155 :
156 46345828 : if (fde->destroyed) {
157 7938050 : talloc_set_destructor(fde, NULL);
158 7938050 : TALLOC_FREE(fde);
159 7938050 : if (removed != NULL) {
160 0 : *removed = true;
161 : }
162 : }
163 :
164 46345828 : return 0;
165 : }
166 :
167 5 : void tevent_fd_set_tag(struct tevent_fd *fde, uint64_t tag)
168 : {
169 5 : if (fde == NULL) {
170 0 : return;
171 : }
172 :
173 5 : fde->tag = tag;
174 : }
175 :
176 15 : uint64_t tevent_fd_get_tag(const struct tevent_fd *fde)
177 : {
178 15 : if (fde == NULL) {
179 0 : return 0;
180 : }
181 :
182 15 : return fde->tag;
183 : }
|