tevent_poll: fix the usage of tevent_re_initialise()
[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 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
55                                     void *pdata,
56                                     size_t data_size,
57                                     const char *type,
58                                     const char *location)
59 {
60         struct tevent_req *req;
61         void **ppdata = (void **)pdata;
62         void *data;
63
64         req = talloc_zero(mem_ctx, struct tevent_req);
65         if (req == NULL) {
66                 return NULL;
67         }
68         req->internal.private_type      = type;
69         req->internal.create_location   = location;
70         req->internal.finish_location   = NULL;
71         req->internal.state             = TEVENT_REQ_IN_PROGRESS;
72         req->internal.trigger           = tevent_create_immediate(req);
73         if (!req->internal.trigger) {
74                 talloc_free(req);
75                 return NULL;
76         }
77         req->internal.defer_callback_ev = NULL;
78
79         data = talloc_zero_size(req, data_size);
80         if (data == NULL) {
81                 talloc_free(req);
82                 return NULL;
83         }
84         talloc_set_name_const(data, type);
85
86         req->data = data;
87
88         *ppdata = data;
89         return req;
90 }
91
92 void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
93 {
94         req->internal.finish_location = location;
95         if (req->internal.defer_callback_ev) {
96                 (void)tevent_req_post(req, req->internal.defer_callback_ev);
97                 req->internal.defer_callback_ev = NULL;
98                 return;
99         }
100         if (req->async.fn != NULL) {
101                 req->async.fn(req);
102         }
103 }
104
105 static void tevent_req_finish(struct tevent_req *req,
106                               enum tevent_req_state state,
107                               const char *location)
108 {
109         req->internal.state = state;
110         _tevent_req_notify_callback(req, location);
111 }
112
113 void _tevent_req_done(struct tevent_req *req,
114                       const char *location)
115 {
116         tevent_req_finish(req, TEVENT_REQ_DONE, location);
117 }
118
119 bool _tevent_req_error(struct tevent_req *req,
120                        uint64_t error,
121                        const char *location)
122 {
123         if (error == 0) {
124                 return false;
125         }
126
127         req->internal.error = error;
128         tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
129         return true;
130 }
131
132 void _tevent_req_oom(struct tevent_req *req, const char *location)
133 {
134         tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
135 }
136
137 bool _tevent_req_nomem(const void *p,
138                        struct tevent_req *req,
139                        const char *location)
140 {
141         if (p != NULL) {
142                 return false;
143         }
144         _tevent_req_oom(req, location);
145         return true;
146 }
147
148 /**
149  * @internal
150  *
151  * @brief Immediate event callback.
152  *
153  * @param[in]  ev       The event context to use.
154  *
155  * @param[in]  im       The immediate event.
156  *
157  * @param[in]  priv     The async request to be finished.
158  */
159 static void tevent_req_trigger(struct tevent_context *ev,
160                                struct tevent_immediate *im,
161                                void *private_data)
162 {
163         struct tevent_req *req = talloc_get_type(private_data,
164                                  struct tevent_req);
165
166         tevent_req_finish(req, req->internal.state,
167                           req->internal.finish_location);
168 }
169
170 struct tevent_req *tevent_req_post(struct tevent_req *req,
171                                    struct tevent_context *ev)
172 {
173         tevent_schedule_immediate(req->internal.trigger,
174                                   ev, tevent_req_trigger, req);
175         return req;
176 }
177
178 void tevent_req_defer_callback(struct tevent_req *req,
179                                struct tevent_context *ev)
180 {
181         req->internal.defer_callback_ev = ev;
182 }
183
184 bool tevent_req_is_in_progress(struct tevent_req *req)
185 {
186         if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
187                 return true;
188         }
189
190         return false;
191 }
192
193 void tevent_req_received(struct tevent_req *req)
194 {
195         TALLOC_FREE(req->data);
196         req->private_print = NULL;
197
198         TALLOC_FREE(req->internal.trigger);
199         TALLOC_FREE(req->internal.timer);
200
201         req->internal.state = TEVENT_REQ_RECEIVED;
202 }
203
204 bool tevent_req_poll(struct tevent_req *req,
205                      struct tevent_context *ev)
206 {
207         while (tevent_req_is_in_progress(req)) {
208                 int ret;
209
210                 ret = tevent_loop_once(ev);
211                 if (ret != 0) {
212                         return false;
213                 }
214         }
215
216         return true;
217 }
218
219 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
220                         uint64_t *error)
221 {
222         if (req->internal.state == TEVENT_REQ_DONE) {
223                 return false;
224         }
225         if (req->internal.state == TEVENT_REQ_USER_ERROR) {
226                 *error = req->internal.error;
227         }
228         *state = req->internal.state;
229         return true;
230 }
231
232 static void tevent_req_timedout(struct tevent_context *ev,
233                                struct tevent_timer *te,
234                                struct timeval now,
235                                void *private_data)
236 {
237         struct tevent_req *req = talloc_get_type(private_data,
238                                  struct tevent_req);
239
240         TALLOC_FREE(req->internal.timer);
241
242         tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
243 }
244
245 bool tevent_req_set_endtime(struct tevent_req *req,
246                             struct tevent_context *ev,
247                             struct timeval endtime)
248 {
249         TALLOC_FREE(req->internal.timer);
250
251         req->internal.timer = tevent_add_timer(ev, req, endtime,
252                                                tevent_req_timedout,
253                                                req);
254         if (tevent_req_nomem(req->internal.timer, req)) {
255                 return false;
256         }
257
258         return true;
259 }
260
261 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
262 {
263         req->async.fn = fn;
264         req->async.private_data = pvt;
265 }
266
267 void *_tevent_req_callback_data(struct tevent_req *req)
268 {
269         return req->async.private_data;
270 }
271
272 void *_tevent_req_data(struct tevent_req *req)
273 {
274         return req->data;
275 }
276
277 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
278 {
279         req->private_print = fn;
280 }
281
282 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn)
283 {
284         req->private_cancel = fn;
285 }
286
287 bool _tevent_req_cancel(struct tevent_req *req, const char *location)
288 {
289         if (req->private_cancel == NULL) {
290                 return false;
291         }
292
293         return req->private_cancel(req);
294 }