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