talloc: use the system pytalloc-util for python3 as well
[sfrench/samba-autobuild/.git] / lib / tevent / tevent_req.c
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         size_t payload;
66
67         payload = sizeof(struct tevent_immediate) + data_size;
68         if (payload < sizeof(struct tevent_immediate)) {
69                 /* overflow */
70                 return NULL;
71         }
72
73         req = talloc_pooled_object(
74                 mem_ctx, struct tevent_req, 2,
75                 sizeof(struct tevent_immediate) + data_size);
76         if (req == NULL) {
77                 return NULL;
78         }
79
80         *req = (struct tevent_req) {
81                 .internal.private_type          = type,
82                 .internal.create_location       = location,
83                 .internal.state                 = TEVENT_REQ_IN_PROGRESS,
84                 .internal.trigger               = tevent_create_immediate(req)
85         };
86
87         data = talloc_zero_size(req, data_size);
88
89         /*
90          * No need to check for req->internal.trigger!=NULL or
91          * data!=NULL, this can't fail: talloc_pooled_object has
92          * already allocated sufficient memory.
93          */
94
95         talloc_set_name_const(data, type);
96
97         req->data = data;
98
99         talloc_set_destructor(req, tevent_req_destructor);
100
101         *ppdata = data;
102         return req;
103 }
104
105 static int tevent_req_destructor(struct tevent_req *req)
106 {
107         tevent_req_received(req);
108         return 0;
109 }
110
111 void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
112 {
113         req->internal.finish_location = location;
114         if (req->internal.defer_callback_ev) {
115                 (void)tevent_req_post(req, req->internal.defer_callback_ev);
116                 req->internal.defer_callback_ev = NULL;
117                 return;
118         }
119         if (req->async.fn != NULL) {
120                 req->async.fn(req);
121         }
122 }
123
124 static void tevent_req_cleanup(struct tevent_req *req)
125 {
126         if (req->private_cleanup.fn == NULL) {
127                 return;
128         }
129
130         if (req->private_cleanup.state >= req->internal.state) {
131                 /*
132                  * Don't call the cleanup_function multiple times for the same
133                  * state recursively
134                  */
135                 return;
136         }
137
138         req->private_cleanup.state = req->internal.state;
139         req->private_cleanup.fn(req, req->internal.state);
140 }
141
142 static void tevent_req_finish(struct tevent_req *req,
143                               enum tevent_req_state state,
144                               const char *location)
145 {
146         /*
147          * make sure we do not timeout after
148          * the request was already finished
149          */
150         TALLOC_FREE(req->internal.timer);
151
152         req->internal.state = state;
153         req->internal.finish_location = location;
154
155         tevent_req_cleanup(req);
156
157         _tevent_req_notify_callback(req, location);
158 }
159
160 void _tevent_req_done(struct tevent_req *req,
161                       const char *location)
162 {
163         tevent_req_finish(req, TEVENT_REQ_DONE, location);
164 }
165
166 bool _tevent_req_error(struct tevent_req *req,
167                        uint64_t error,
168                        const char *location)
169 {
170         if (error == 0) {
171                 return false;
172         }
173
174         req->internal.error = error;
175         tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
176         return true;
177 }
178
179 void _tevent_req_oom(struct tevent_req *req, const char *location)
180 {
181         tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
182 }
183
184 bool _tevent_req_nomem(const void *p,
185                        struct tevent_req *req,
186                        const char *location)
187 {
188         if (p != NULL) {
189                 return false;
190         }
191         _tevent_req_oom(req, location);
192         return true;
193 }
194
195 /**
196  * @internal
197  *
198  * @brief Immediate event callback.
199  *
200  * @param[in]  ev       The event context to use.
201  *
202  * @param[in]  im       The immediate event.
203  *
204  * @param[in]  priv     The async request to be finished.
205  */
206 static void tevent_req_trigger(struct tevent_context *ev,
207                                struct tevent_immediate *im,
208                                void *private_data)
209 {
210         struct tevent_req *req =
211                 talloc_get_type_abort(private_data,
212                 struct tevent_req);
213
214         tevent_req_finish(req, req->internal.state,
215                           req->internal.finish_location);
216 }
217
218 struct tevent_req *tevent_req_post(struct tevent_req *req,
219                                    struct tevent_context *ev)
220 {
221         tevent_schedule_immediate(req->internal.trigger,
222                                   ev, tevent_req_trigger, req);
223         return req;
224 }
225
226 void tevent_req_defer_callback(struct tevent_req *req,
227                                struct tevent_context *ev)
228 {
229         req->internal.defer_callback_ev = ev;
230 }
231
232 bool tevent_req_is_in_progress(struct tevent_req *req)
233 {
234         if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
235                 return true;
236         }
237
238         return false;
239 }
240
241 void tevent_req_received(struct tevent_req *req)
242 {
243         talloc_set_destructor(req, NULL);
244
245         req->private_print = NULL;
246         req->private_cancel = NULL;
247
248         TALLOC_FREE(req->internal.trigger);
249         TALLOC_FREE(req->internal.timer);
250
251         req->internal.state = TEVENT_REQ_RECEIVED;
252
253         tevent_req_cleanup(req);
254
255         TALLOC_FREE(req->data);
256 }
257
258 bool tevent_req_poll(struct tevent_req *req,
259                      struct tevent_context *ev)
260 {
261         while (tevent_req_is_in_progress(req)) {
262                 int ret;
263
264                 ret = tevent_loop_once(ev);
265                 if (ret != 0) {
266                         return false;
267                 }
268         }
269
270         return true;
271 }
272
273 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
274                         uint64_t *error)
275 {
276         if (req->internal.state == TEVENT_REQ_DONE) {
277                 return false;
278         }
279         if (req->internal.state == TEVENT_REQ_USER_ERROR) {
280                 *error = req->internal.error;
281         }
282         *state = req->internal.state;
283         return true;
284 }
285
286 static void tevent_req_timedout(struct tevent_context *ev,
287                                struct tevent_timer *te,
288                                struct timeval now,
289                                void *private_data)
290 {
291         struct tevent_req *req =
292                 talloc_get_type_abort(private_data,
293                 struct tevent_req);
294
295         TALLOC_FREE(req->internal.timer);
296
297         tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
298 }
299
300 bool tevent_req_set_endtime(struct tevent_req *req,
301                             struct tevent_context *ev,
302                             struct timeval endtime)
303 {
304         TALLOC_FREE(req->internal.timer);
305
306         req->internal.timer = tevent_add_timer(ev, req, endtime,
307                                                tevent_req_timedout,
308                                                req);
309         if (tevent_req_nomem(req->internal.timer, req)) {
310                 return false;
311         }
312
313         return true;
314 }
315
316 void tevent_req_reset_endtime(struct tevent_req *req)
317 {
318         TALLOC_FREE(req->internal.timer);
319 }
320
321 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
322 {
323         req->async.fn = fn;
324         req->async.private_data = pvt;
325 }
326
327 void *_tevent_req_callback_data(struct tevent_req *req)
328 {
329         return req->async.private_data;
330 }
331
332 void *_tevent_req_data(struct tevent_req *req)
333 {
334         return req->data;
335 }
336
337 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
338 {
339         req->private_print = fn;
340 }
341
342 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn)
343 {
344         req->private_cancel = fn;
345 }
346
347 bool _tevent_req_cancel(struct tevent_req *req, const char *location)
348 {
349         if (req->private_cancel == NULL) {
350                 return false;
351         }
352
353         return req->private_cancel(req);
354 }
355
356 void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn)
357 {
358         req->private_cleanup.state = req->internal.state;
359         req->private_cleanup.fn = fn;
360 }