async_req: Fix the S4 build
[metze/samba/wip.git] / lib / async_req / async_req.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async requests
4    Copyright (C) Volker Lendecke 2008
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/tevent/tevent.h"
22 #include "lib/talloc/talloc.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/async_req/async_req.h"
25
26 #ifndef TALLOC_FREE
27 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
28 #endif
29
30 /**
31  * @brief Print an async_req structure
32  * @param[in] mem_ctx   The memory context for the result
33  * @param[in] req       The request to be printed
34  * @retval              Text representation of req
35  *
36  * This is a default print function for async requests. Implementations should
37  * override this with more specific information.
38  *
39  * This function should not be used by async API users, this is non-static
40  * only to allow implementations to easily provide default information in
41  * their specific functions.
42  */
43
44 char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
45 {
46         return talloc_asprintf(mem_ctx, "async_req: state=%d, status=%s, "
47                                "priv=%s", req->state, nt_errstr(req->status),
48                                talloc_get_name(req->private_data));
49 }
50
51 /**
52  * @brief Create an async request
53  * @param[in] mem_ctx   The memory context for the result
54  * @param[in] ev        The event context this async request will be driven by
55  * @retval              A new async request
56  *
57  * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS
58  */
59
60 struct async_req *async_req_new(TALLOC_CTX *mem_ctx)
61 {
62         struct async_req *result;
63
64         result = talloc_zero(mem_ctx, struct async_req);
65         if (result == NULL) {
66                 return NULL;
67         }
68         result->state = ASYNC_REQ_IN_PROGRESS;
69         result->print = async_req_print;
70         return result;
71 }
72
73 /**
74  * @brief An async request has successfully finished
75  * @param[in] req       The finished request
76  *
77  * async_req_done is to be used by implementors of async requests. When a
78  * request is successfully finished, this function calls the user's completion
79  * function.
80  */
81
82 void async_req_done(struct async_req *req)
83 {
84         req->status = NT_STATUS_OK;
85         req->state = ASYNC_REQ_DONE;
86         if (req->async.fn != NULL) {
87                 req->async.fn(req);
88         }
89 }
90
91 /**
92  * @brief An async request has seen an error
93  * @param[in] req       The request with an error
94  * @param[in] status    The error code
95  *
96  * async_req_done is to be used by implementors of async requests. When a
97  * request can not successfully completed, the implementation should call this
98  * function with the appropriate status code.
99  */
100
101 void async_req_error(struct async_req *req, NTSTATUS status)
102 {
103         req->status = status;
104         req->state = ASYNC_REQ_ERROR;
105         if (req->async.fn != NULL) {
106                 req->async.fn(req);
107         }
108 }
109
110 /**
111  * @brief Timed event callback
112  * @param[in] ev        Event context
113  * @param[in] te        The timed event
114  * @param[in] now       zero time
115  * @param[in] priv      The async request to be finished
116  */
117
118 static void async_trigger(struct tevent_context *ev, struct tevent_timer *te,
119                           struct timeval now, void *priv)
120 {
121         struct async_req *req = talloc_get_type_abort(priv, struct async_req);
122
123         TALLOC_FREE(te);
124         if (NT_STATUS_IS_OK(req->status)) {
125                 async_req_done(req);
126         }
127         else {
128                 async_req_error(req, req->status);
129         }
130 }
131
132 /**
133  * @brief Finish a request before it started processing
134  * @param[in] req       The finished request
135  * @param[in] status    The success code
136  *
137  * An implementation of an async request might find that it can either finish
138  * the request without waiting for an external event, or it can't even start
139  * the engine. To present the illusion of a callback to the user of the API,
140  * the implementation can call this helper function which triggers an
141  * immediate timed event. This way the caller can use the same calling
142  * conventions, independent of whether the request was actually deferred.
143  */
144
145 bool async_post_status(struct async_req *req, struct tevent_context *ev,
146                        NTSTATUS status)
147 {
148         req->status = status;
149
150         if (tevent_add_timer(ev, req, timeval_zero(),
151                             async_trigger, req) == NULL) {
152                 return false;
153         }
154         return true;
155 }
156
157 /**
158  * @brief Helper function for nomem check
159  * @param[in] p         The pointer to be checked
160  * @param[in] req       The request being processed
161  *
162  * Convenience helper to easily check alloc failure within a callback
163  * implementing the next step of an async request.
164  *
165  * Call pattern would be
166  * \code
167  * p = talloc(mem_ctx, bla);
168  * if (async_req_nomem(p, req)) {
169  *      return;
170  * }
171  * \endcode
172  */
173
174 bool async_req_nomem(const void *p, struct async_req *req)
175 {
176         if (p != NULL) {
177                 return false;
178         }
179         async_req_error(req, NT_STATUS_NO_MEMORY);
180         return true;
181 }
182
183 bool async_req_is_error(struct async_req *req, NTSTATUS *status)
184 {
185         if (req->state < ASYNC_REQ_DONE) {
186                 *status = NT_STATUS_INTERNAL_ERROR;
187                 return true;
188         }
189         if (req->state == ASYNC_REQ_ERROR) {
190                 *status = req->status;
191                 return true;
192         }
193         return false;
194 }
195
196 NTSTATUS async_req_simple_recv(struct async_req *req)
197 {
198         NTSTATUS status;
199
200         if (async_req_is_error(req, &status)) {
201                 return status;
202         }
203         return NT_STATUS_OK;
204 }
205
206 static void async_req_timedout(struct tevent_context *ev,
207                                struct tevent_timer *te,
208                                struct timeval now,
209                                void *priv)
210 {
211         struct async_req *req = talloc_get_type_abort(
212                 priv, struct async_req);
213         TALLOC_FREE(te);
214         async_req_error(req, NT_STATUS_IO_TIMEOUT);
215 }
216
217 bool async_req_set_timeout(struct async_req *req, struct tevent_context *ev,
218                            struct timeval to)
219 {
220         return (tevent_add_timer(ev, req,
221                                 timeval_current_ofs(to.tv_sec, to.tv_usec),
222                                 async_req_timedout, req)
223                 != NULL);
224 }
225
226 struct async_req *async_wait_send(TALLOC_CTX *mem_ctx,
227                                   struct tevent_context *ev,
228                                   struct timeval to)
229 {
230         struct async_req *result;
231
232         result = async_req_new(mem_ctx);
233         if (result == NULL) {
234                 return result;
235         }
236         if (!async_req_set_timeout(result, ev, to)) {
237                 TALLOC_FREE(result);
238                 return NULL;
239         }
240         return result;
241 }
242
243 NTSTATUS async_wait_recv(struct async_req *req)
244 {
245         return NT_STATUS_OK;
246 }
247
248 struct async_queue_entry {
249         struct async_queue_entry *prev, *next;
250         struct async_req_queue *queue;
251         struct async_req *req;
252         void (*trigger)(struct async_req *req);
253 };
254
255 struct async_req_queue {
256         struct async_queue_entry *queue;
257 };
258
259 struct async_req_queue *async_req_queue_init(TALLOC_CTX *mem_ctx)
260 {
261         return talloc_zero(mem_ctx, struct async_req_queue);
262 }
263
264 static int async_queue_entry_destructor(struct async_queue_entry *e)
265 {
266         struct async_req_queue *queue = e->queue;
267
268         DLIST_REMOVE(queue->queue, e);
269
270         if (queue->queue != NULL) {
271                 queue->queue->trigger(queue->queue->req);
272         }
273
274         return 0;
275 }
276
277 static void async_req_immediate_trigger(struct tevent_context *ev,
278                                         struct tevent_timer *te,
279                                         struct timeval now,
280                                         void *priv)
281 {
282         struct async_queue_entry *e = talloc_get_type_abort(
283                 priv, struct async_queue_entry);
284
285         TALLOC_FREE(te);
286         e->trigger(e->req);
287 }
288
289 bool async_req_enqueue(struct async_req_queue *queue, struct tevent_context *ev,
290                        struct async_req *req,
291                        void (*trigger)(struct async_req *req))
292 {
293         struct async_queue_entry *e;
294         bool busy;
295
296         busy = (queue->queue != NULL);
297
298         e = talloc(req, struct async_queue_entry);
299         if (e == NULL) {
300                 return false;
301         }
302
303         e->req = req;
304         e->trigger = trigger;
305         e->queue = queue;
306
307         DLIST_ADD_END(queue->queue, e, struct async_queue_entry *);
308         talloc_set_destructor(e, async_queue_entry_destructor);
309
310         if (!busy) {
311                 struct tevent_timer *te;
312
313                 te = tevent_add_timer(ev, e, timeval_zero(),
314                                      async_req_immediate_trigger,
315                                      e);
316                 if (te == NULL) {
317                         TALLOC_FREE(e);
318                         return false;
319                 }
320         }
321
322         return true;
323 }
324
325 bool _async_req_setup(TALLOC_CTX *mem_ctx, struct async_req **preq,
326                       void *pstate, size_t state_size, const char *typename)
327 {
328         struct async_req *req;
329         void **ppstate = (void **)pstate;
330         void *state;
331
332         req = async_req_new(mem_ctx);
333         if (req == NULL) {
334                 return false;
335         }
336         state = talloc_size(req, state_size);
337         if (state == NULL) {
338                 TALLOC_FREE(req);
339                 return false;
340         }
341         talloc_set_name_const(state, typename);
342         req->private_data = state;
343
344         *preq = req;
345         *ppstate = state;
346
347         return true;
348 }