1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Infrastructure for async requests
|
---|
4 | Copyright (C) Volker Lendecke 2008
|
---|
5 | Copyright (C) Stefan Metzmacher 2009
|
---|
6 |
|
---|
7 | ** NOTE! The following LGPL license applies to the tevent
|
---|
8 | ** library. This does NOT imply that all of Samba is released
|
---|
9 | ** under the LGPL
|
---|
10 |
|
---|
11 | This library is free software; you can redistribute it and/or
|
---|
12 | modify it under the terms of the GNU Lesser General Public
|
---|
13 | License as published by the Free Software Foundation; either
|
---|
14 | version 3 of the License, or (at your option) any later version.
|
---|
15 |
|
---|
16 | This library is distributed in the hope that it will be useful,
|
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | Lesser General Public License for more details.
|
---|
20 |
|
---|
21 | You should have received a copy of the GNU Lesser General Public
|
---|
22 | License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "replace.h"
|
---|
26 | #include "tevent.h"
|
---|
27 | #include "tevent_internal.h"
|
---|
28 | #include "tevent_util.h"
|
---|
29 |
|
---|
30 | char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
|
---|
31 | {
|
---|
32 | return talloc_asprintf(mem_ctx,
|
---|
33 | "tevent_req[%p/%s]: state[%d] error[%lld (0x%llX)] "
|
---|
34 | " state[%s (%p)] timer[%p]",
|
---|
35 | req, req->internal.create_location,
|
---|
36 | req->internal.state,
|
---|
37 | (unsigned long long)req->internal.error,
|
---|
38 | (unsigned long long)req->internal.error,
|
---|
39 | talloc_get_name(req->data),
|
---|
40 | req->data,
|
---|
41 | req->internal.timer
|
---|
42 | );
|
---|
43 | }
|
---|
44 |
|
---|
45 | char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
|
---|
46 | {
|
---|
47 | if (!req->private_print) {
|
---|
48 | return tevent_req_default_print(req, mem_ctx);
|
---|
49 | }
|
---|
50 |
|
---|
51 | return req->private_print(req, mem_ctx);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static int tevent_req_destructor(struct tevent_req *req);
|
---|
55 |
|
---|
56 | struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
|
---|
57 | void *pdata,
|
---|
58 | size_t data_size,
|
---|
59 | const char *type,
|
---|
60 | const char *location)
|
---|
61 | {
|
---|
62 | struct tevent_req *req;
|
---|
63 | void **ppdata = (void **)pdata;
|
---|
64 | void *data;
|
---|
65 |
|
---|
66 | req = talloc_pooled_object(
|
---|
67 | mem_ctx, struct tevent_req, 2,
|
---|
68 | sizeof(struct tevent_immediate) + data_size);
|
---|
69 | if (req == NULL) {
|
---|
70 | return NULL;
|
---|
71 | }
|
---|
72 | ZERO_STRUCTP(req);
|
---|
73 | req->internal.private_type = type;
|
---|
74 | req->internal.create_location = location;
|
---|
75 | req->internal.state = TEVENT_REQ_IN_PROGRESS;
|
---|
76 | req->internal.trigger = tevent_create_immediate(req);
|
---|
77 | if (!req->internal.trigger) {
|
---|
78 | talloc_free(req);
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 |
|
---|
82 | data = talloc_zero_size(req, data_size);
|
---|
83 | if (data == NULL) {
|
---|
84 | talloc_free(req);
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | talloc_set_name_const(data, type);
|
---|
88 |
|
---|
89 | req->data = data;
|
---|
90 |
|
---|
91 | talloc_set_destructor(req, tevent_req_destructor);
|
---|
92 |
|
---|
93 | *ppdata = data;
|
---|
94 | return req;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static int tevent_req_destructor(struct tevent_req *req)
|
---|
98 | {
|
---|
99 | tevent_req_received(req);
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
|
---|
104 | {
|
---|
105 | req->internal.finish_location = location;
|
---|
106 | if (req->internal.defer_callback_ev) {
|
---|
107 | (void)tevent_req_post(req, req->internal.defer_callback_ev);
|
---|
108 | req->internal.defer_callback_ev = NULL;
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | if (req->async.fn != NULL) {
|
---|
112 | req->async.fn(req);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void tevent_req_cleanup(struct tevent_req *req)
|
---|
117 | {
|
---|
118 | if (req->private_cleanup.fn == NULL) {
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (req->private_cleanup.state >= req->internal.state) {
|
---|
123 | /*
|
---|
124 | * Don't call the cleanup_function multiple times for the same
|
---|
125 | * state recursively
|
---|
126 | */
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | req->private_cleanup.state = req->internal.state;
|
---|
131 | req->private_cleanup.fn(req, req->internal.state);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static void tevent_req_finish(struct tevent_req *req,
|
---|
135 | enum tevent_req_state state,
|
---|
136 | const char *location)
|
---|
137 | {
|
---|
138 | /*
|
---|
139 | * make sure we do not timeout after
|
---|
140 | * the request was already finished
|
---|
141 | */
|
---|
142 | TALLOC_FREE(req->internal.timer);
|
---|
143 |
|
---|
144 | req->internal.state = state;
|
---|
145 | req->internal.finish_location = location;
|
---|
146 |
|
---|
147 | tevent_req_cleanup(req);
|
---|
148 |
|
---|
149 | _tevent_req_notify_callback(req, location);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void _tevent_req_done(struct tevent_req *req,
|
---|
153 | const char *location)
|
---|
154 | {
|
---|
155 | tevent_req_finish(req, TEVENT_REQ_DONE, location);
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool _tevent_req_error(struct tevent_req *req,
|
---|
159 | uint64_t error,
|
---|
160 | const char *location)
|
---|
161 | {
|
---|
162 | if (error == 0) {
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | req->internal.error = error;
|
---|
167 | tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void _tevent_req_oom(struct tevent_req *req, const char *location)
|
---|
172 | {
|
---|
173 | tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool _tevent_req_nomem(const void *p,
|
---|
177 | struct tevent_req *req,
|
---|
178 | const char *location)
|
---|
179 | {
|
---|
180 | if (p != NULL) {
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 | _tevent_req_oom(req, location);
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * @internal
|
---|
189 | *
|
---|
190 | * @brief Immediate event callback.
|
---|
191 | *
|
---|
192 | * @param[in] ev The event context to use.
|
---|
193 | *
|
---|
194 | * @param[in] im The immediate event.
|
---|
195 | *
|
---|
196 | * @param[in] priv The async request to be finished.
|
---|
197 | */
|
---|
198 | static void tevent_req_trigger(struct tevent_context *ev,
|
---|
199 | struct tevent_immediate *im,
|
---|
200 | void *private_data)
|
---|
201 | {
|
---|
202 | struct tevent_req *req =
|
---|
203 | talloc_get_type_abort(private_data,
|
---|
204 | struct tevent_req);
|
---|
205 |
|
---|
206 | tevent_req_finish(req, req->internal.state,
|
---|
207 | req->internal.finish_location);
|
---|
208 | }
|
---|
209 |
|
---|
210 | struct tevent_req *tevent_req_post(struct tevent_req *req,
|
---|
211 | struct tevent_context *ev)
|
---|
212 | {
|
---|
213 | tevent_schedule_immediate(req->internal.trigger,
|
---|
214 | ev, tevent_req_trigger, req);
|
---|
215 | return req;
|
---|
216 | }
|
---|
217 |
|
---|
218 | void tevent_req_defer_callback(struct tevent_req *req,
|
---|
219 | struct tevent_context *ev)
|
---|
220 | {
|
---|
221 | req->internal.defer_callback_ev = ev;
|
---|
222 | }
|
---|
223 |
|
---|
224 | bool tevent_req_is_in_progress(struct tevent_req *req)
|
---|
225 | {
|
---|
226 | if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
|
---|
227 | return true;
|
---|
228 | }
|
---|
229 |
|
---|
230 | return false;
|
---|
231 | }
|
---|
232 |
|
---|
233 | void tevent_req_received(struct tevent_req *req)
|
---|
234 | {
|
---|
235 | talloc_set_destructor(req, NULL);
|
---|
236 |
|
---|
237 | req->private_print = NULL;
|
---|
238 | req->private_cancel = NULL;
|
---|
239 |
|
---|
240 | TALLOC_FREE(req->internal.trigger);
|
---|
241 | TALLOC_FREE(req->internal.timer);
|
---|
242 |
|
---|
243 | req->internal.state = TEVENT_REQ_RECEIVED;
|
---|
244 |
|
---|
245 | tevent_req_cleanup(req);
|
---|
246 |
|
---|
247 | TALLOC_FREE(req->data);
|
---|
248 | }
|
---|
249 |
|
---|
250 | bool tevent_req_poll(struct tevent_req *req,
|
---|
251 | struct tevent_context *ev)
|
---|
252 | {
|
---|
253 | while (tevent_req_is_in_progress(req)) {
|
---|
254 | int ret;
|
---|
255 |
|
---|
256 | ret = tevent_loop_once(ev);
|
---|
257 | if (ret != 0) {
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | return true;
|
---|
263 | }
|
---|
264 |
|
---|
265 | bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
|
---|
266 | uint64_t *error)
|
---|
267 | {
|
---|
268 | if (req->internal.state == TEVENT_REQ_DONE) {
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 | if (req->internal.state == TEVENT_REQ_USER_ERROR) {
|
---|
272 | *error = req->internal.error;
|
---|
273 | }
|
---|
274 | *state = req->internal.state;
|
---|
275 | return true;
|
---|
276 | }
|
---|
277 |
|
---|
278 | static void tevent_req_timedout(struct tevent_context *ev,
|
---|
279 | struct tevent_timer *te,
|
---|
280 | struct timeval now,
|
---|
281 | void *private_data)
|
---|
282 | {
|
---|
283 | struct tevent_req *req =
|
---|
284 | talloc_get_type_abort(private_data,
|
---|
285 | struct tevent_req);
|
---|
286 |
|
---|
287 | TALLOC_FREE(req->internal.timer);
|
---|
288 |
|
---|
289 | tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
|
---|
290 | }
|
---|
291 |
|
---|
292 | bool tevent_req_set_endtime(struct tevent_req *req,
|
---|
293 | struct tevent_context *ev,
|
---|
294 | struct timeval endtime)
|
---|
295 | {
|
---|
296 | TALLOC_FREE(req->internal.timer);
|
---|
297 |
|
---|
298 | req->internal.timer = tevent_add_timer(ev, req, endtime,
|
---|
299 | tevent_req_timedout,
|
---|
300 | req);
|
---|
301 | if (tevent_req_nomem(req->internal.timer, req)) {
|
---|
302 | return false;
|
---|
303 | }
|
---|
304 |
|
---|
305 | return true;
|
---|
306 | }
|
---|
307 |
|
---|
308 | void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
|
---|
309 | {
|
---|
310 | req->async.fn = fn;
|
---|
311 | req->async.private_data = pvt;
|
---|
312 | }
|
---|
313 |
|
---|
314 | void *_tevent_req_callback_data(struct tevent_req *req)
|
---|
315 | {
|
---|
316 | return req->async.private_data;
|
---|
317 | }
|
---|
318 |
|
---|
319 | void *_tevent_req_data(struct tevent_req *req)
|
---|
320 | {
|
---|
321 | return req->data;
|
---|
322 | }
|
---|
323 |
|
---|
324 | void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
|
---|
325 | {
|
---|
326 | req->private_print = fn;
|
---|
327 | }
|
---|
328 |
|
---|
329 | void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn)
|
---|
330 | {
|
---|
331 | req->private_cancel = fn;
|
---|
332 | }
|
---|
333 |
|
---|
334 | bool _tevent_req_cancel(struct tevent_req *req, const char *location)
|
---|
335 | {
|
---|
336 | if (req->private_cancel == NULL) {
|
---|
337 | return false;
|
---|
338 | }
|
---|
339 |
|
---|
340 | return req->private_cancel(req);
|
---|
341 | }
|
---|
342 |
|
---|
343 | void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn)
|
---|
344 | {
|
---|
345 | req->private_cleanup.state = req->internal.state;
|
---|
346 | req->private_cleanup.fn = fn;
|
---|
347 | }
|
---|