s3/docs: Fix typo.
[Samba/gebeck_regimport.git] / lib / tevent / tevent_req.c
blob01700006eb9eca777de7d2fa35b485fd8b8100c7
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2009
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
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.
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.
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/>.
25 #include "replace.h"
26 #include "tevent.h"
27 #include "tevent_internal.h"
28 #include "tevent_util.h"
30 /**
31 * @brief The default print function for creating debug messages
32 * @param[in] req The request to be printed
33 * @param[in] mem_ctx The memory context for the result
34 * @retval Text representation of req
36 * The function should not be used by users of the asynx API,
37 * but custom print function can use it and append custom text
38 * to the string.
41 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
43 return talloc_asprintf(mem_ctx,
44 "tevent_req[%p/%s]: state[%d] error[%lld (0x%llX)] "
45 " state[%s (%p)] timer[%p]",
46 req, req->internal.create_location,
47 req->internal.state,
48 (unsigned long long)req->internal.error,
49 (unsigned long long)req->internal.error,
50 talloc_get_name(req->data),
51 req->data,
52 req->internal.timer
56 /**
57 * @brief Print an tevent_req structure in debug messages
58 * @param[in] mem_ctx The memory context for the result
59 * @param[in] req The request to be printed
60 * @retval Text representation of req
62 * This function should be used by callers of the async API
65 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
67 if (!req->private_print) {
68 return tevent_req_default_print(req, mem_ctx);
71 return req->private_print(req, mem_ctx);
74 /**
75 * @brief Create an async request
76 * @param[in] mem_ctx The memory context for the result
77 * @param[in] ev The event context this async request will be driven by
78 * @retval A new async request
80 * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS
83 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
84 void *pdata,
85 size_t data_size,
86 const char *type,
87 const char *location)
89 struct tevent_req *req;
90 void **ppdata = (void **)pdata;
91 void *data;
93 req = talloc_zero(mem_ctx, struct tevent_req);
94 if (req == NULL) {
95 return NULL;
97 req->internal.private_type = type;
98 req->internal.create_location = location;
99 req->internal.finish_location = NULL;
100 req->internal.state = TEVENT_REQ_IN_PROGRESS;
101 req->internal.trigger = tevent_create_immediate(req);
102 if (!req->internal.trigger) {
103 talloc_free(req);
104 return NULL;
107 data = talloc_size(req, data_size);
108 if (data == NULL) {
109 talloc_free(req);
110 return NULL;
112 talloc_set_name_const(data, type);
114 req->data = data;
116 *ppdata = data;
117 return req;
120 static void tevent_req_finish(struct tevent_req *req,
121 enum tevent_req_state state,
122 const char *location)
124 req->internal.state = state;
125 req->internal.finish_location = location;
126 if (req->async.fn != NULL) {
127 req->async.fn(req);
132 * @brief An async request has successfully finished
133 * @param[in] req The finished request
135 * tevent_req_done is to be used by implementors of async requests. When a
136 * request is successfully finished, this function calls the user's completion
137 * function.
140 void _tevent_req_done(struct tevent_req *req,
141 const char *location)
143 tevent_req_finish(req, TEVENT_REQ_DONE, location);
147 * @brief An async request has seen an error
148 * @param[in] req The request with an error
149 * @param[in] error The error code
151 * tevent_req_done is to be used by implementors of async requests. When a
152 * request can not successfully completed, the implementation should call this
153 * function with the appropriate status code.
155 * If error is 0 the function returns false and does nothing more.
157 * Call pattern would be
158 * \code
159 * int error = first_function();
160 * if (tevent_req_error(req, error)) {
161 * return;
164 * error = second_function();
165 * if (tevent_req_error(req, error)) {
166 * return;
169 * tevent_req_done(req);
170 * return;
171 * \endcode
174 bool _tevent_req_error(struct tevent_req *req,
175 uint64_t error,
176 const char *location)
178 if (error == 0) {
179 return false;
182 req->internal.error = error;
183 tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
184 return true;
188 * @brief Helper function for nomem check
189 * @param[in] p The pointer to be checked
190 * @param[in] req The request being processed
192 * Convenience helper to easily check alloc failure within a callback
193 * implementing the next step of an async request.
195 * Call pattern would be
196 * \code
197 * p = talloc(mem_ctx, bla);
198 * if (tevent_req_nomem(p, req)) {
199 * return;
201 * \endcode
204 bool _tevent_req_nomem(const void *p,
205 struct tevent_req *req,
206 const char *location)
208 if (p != NULL) {
209 return false;
211 tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
212 return true;
216 * @brief Immediate event callback
217 * @param[in] ev Event context
218 * @param[in] im The immediate event
219 * @param[in] priv The async request to be finished
221 static void tevent_req_trigger(struct tevent_context *ev,
222 struct tevent_immediate *im,
223 void *private_data)
225 struct tevent_req *req = talloc_get_type(private_data,
226 struct tevent_req);
228 tevent_req_finish(req, req->internal.state,
229 req->internal.finish_location);
233 * @brief Finish a request before the caller had the change to set the callback
234 * @param[in] req The finished request
235 * @param[in] ev The tevent_context for the timed event
236 * @retval req will be returned
238 * An implementation of an async request might find that it can either finish
239 * the request without waiting for an external event, or it can't even start
240 * the engine. To present the illusion of a callback to the user of the API,
241 * the implementation can call this helper function which triggers an
242 * immediate timed event. This way the caller can use the same calling
243 * conventions, independent of whether the request was actually deferred.
246 struct tevent_req *tevent_req_post(struct tevent_req *req,
247 struct tevent_context *ev)
249 tevent_schedule_immediate(req->internal.trigger,
250 ev, tevent_req_trigger, req);
251 return req;
254 bool tevent_req_is_in_progress(struct tevent_req *req)
256 if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
257 return true;
260 return false;
264 * @brief This function destroys the attached private data
265 * @param[in] req The finished request
267 * This function can be called as last action of a _recv()
268 * function, it destroys the data attached to the tevent_req.
270 void tevent_req_received(struct tevent_req *req)
272 TALLOC_FREE(req->data);
273 req->private_print = NULL;
275 TALLOC_FREE(req->internal.trigger);
276 TALLOC_FREE(req->internal.timer);
278 req->internal.state = TEVENT_REQ_RECEIVED;
281 bool tevent_req_poll(struct tevent_req *req,
282 struct tevent_context *ev)
284 while (tevent_req_is_in_progress(req)) {
285 int ret;
287 ret = tevent_loop_once(ev);
288 if (ret != 0) {
289 return false;
293 return true;
296 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
297 uint64_t *error)
299 if (req->internal.state == TEVENT_REQ_DONE) {
300 return false;
302 if (req->internal.state == TEVENT_REQ_USER_ERROR) {
303 *error = req->internal.error;
305 *state = req->internal.state;
306 return true;
309 static void tevent_req_timedout(struct tevent_context *ev,
310 struct tevent_timer *te,
311 struct timeval now,
312 void *private_data)
314 struct tevent_req *req = talloc_get_type(private_data,
315 struct tevent_req);
317 TALLOC_FREE(req->internal.timer);
319 tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
322 bool tevent_req_set_endtime(struct tevent_req *req,
323 struct tevent_context *ev,
324 struct timeval endtime)
326 TALLOC_FREE(req->internal.timer);
328 req->internal.timer = tevent_add_timer(ev, req, endtime,
329 tevent_req_timedout,
330 req);
331 if (tevent_req_nomem(req->internal.timer, req)) {
332 return false;
335 return true;
338 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
340 req->async.fn = fn;
341 req->async.private_data = pvt;
344 void *_tevent_req_callback_data(struct tevent_req *req)
346 return req->async.private_data;
349 void *_tevent_req_data(struct tevent_req *req)
351 return req->data;
354 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
356 req->private_print = fn;