Split up async_req into a generic and a NTSTATUS specific part
[samba.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, error=%d, "
47                                "priv=%s", req->state, (int)req->error,
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->error = 0;
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, uint32_t error)
102 {
103         req->error = error;
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 (req->error == 0) {
125                 async_req_done(req);
126         }
127         else {
128                 async_req_error(req, req->error);
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_error(struct async_req *req, struct tevent_context *ev,
146                       uint32_t error)
147 {
148         req->error = error;
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 bool async_req_is_error(struct async_req *req, uint32_t *error)
158 {
159         if (req->state < ASYNC_REQ_DONE) {
160                 return true;
161         }
162         if (req->state == ASYNC_REQ_ERROR) {
163                 *error = req->error;
164                 return true;
165         }
166         return false;
167 }
168
169 static void async_req_timedout(struct tevent_context *ev,
170                                struct tevent_timer *te,
171                                struct timeval now,
172                                void *priv)
173 {
174         struct async_req *req = talloc_get_type_abort(
175                 priv, struct async_req);
176         TALLOC_FREE(te);
177         async_req_nterror(req, NT_STATUS_IO_TIMEOUT);
178 }
179
180 bool async_req_set_timeout(struct async_req *req, struct tevent_context *ev,
181                            struct timeval to)
182 {
183         return (tevent_add_timer(ev, req,
184                                 timeval_current_ofs(to.tv_sec, to.tv_usec),
185                                 async_req_timedout, req)
186                 != NULL);
187 }
188
189 struct async_req *async_wait_send(TALLOC_CTX *mem_ctx,
190                                   struct tevent_context *ev,
191                                   struct timeval to)
192 {
193         struct async_req *result;
194
195         result = async_req_new(mem_ctx);
196         if (result == NULL) {
197                 return result;
198         }
199         if (!async_req_set_timeout(result, ev, to)) {
200                 TALLOC_FREE(result);
201                 return NULL;
202         }
203         return result;
204 }
205
206 bool async_wait_recv(struct async_req *req)
207 {
208         return true;
209 }
210
211 struct async_queue_entry {
212         struct async_queue_entry *prev, *next;
213         struct async_req_queue *queue;
214         struct async_req *req;
215         void (*trigger)(struct async_req *req);
216 };
217
218 struct async_req_queue {
219         struct async_queue_entry *queue;
220 };
221
222 struct async_req_queue *async_req_queue_init(TALLOC_CTX *mem_ctx)
223 {
224         return talloc_zero(mem_ctx, struct async_req_queue);
225 }
226
227 static int async_queue_entry_destructor(struct async_queue_entry *e)
228 {
229         struct async_req_queue *queue = e->queue;
230
231         DLIST_REMOVE(queue->queue, e);
232
233         if (queue->queue != NULL) {
234                 queue->queue->trigger(queue->queue->req);
235         }
236
237         return 0;
238 }
239
240 static void async_req_immediate_trigger(struct tevent_context *ev,
241                                         struct tevent_timer *te,
242                                         struct timeval now,
243                                         void *priv)
244 {
245         struct async_queue_entry *e = talloc_get_type_abort(
246                 priv, struct async_queue_entry);
247
248         TALLOC_FREE(te);
249         e->trigger(e->req);
250 }
251
252 bool async_req_enqueue(struct async_req_queue *queue, struct tevent_context *ev,
253                        struct async_req *req,
254                        void (*trigger)(struct async_req *req))
255 {
256         struct async_queue_entry *e;
257         bool busy;
258
259         busy = (queue->queue != NULL);
260
261         e = talloc(req, struct async_queue_entry);
262         if (e == NULL) {
263                 return false;
264         }
265
266         e->req = req;
267         e->trigger = trigger;
268         e->queue = queue;
269
270         DLIST_ADD_END(queue->queue, e, struct async_queue_entry *);
271         talloc_set_destructor(e, async_queue_entry_destructor);
272
273         if (!busy) {
274                 struct tevent_timer *te;
275
276                 te = tevent_add_timer(ev, e, timeval_zero(),
277                                      async_req_immediate_trigger,
278                                      e);
279                 if (te == NULL) {
280                         TALLOC_FREE(e);
281                         return false;
282                 }
283         }
284
285         return true;
286 }
287
288 bool _async_req_setup(TALLOC_CTX *mem_ctx, struct async_req **preq,
289                       void *pstate, size_t state_size, const char *typename)
290 {
291         struct async_req *req;
292         void **ppstate = (void **)pstate;
293         void *state;
294
295         req = async_req_new(mem_ctx);
296         if (req == NULL) {
297                 return false;
298         }
299         state = talloc_size(req, state_size);
300         if (state == NULL) {
301                 TALLOC_FREE(req);
302                 return false;
303         }
304         talloc_set_name_const(state, typename);
305         req->private_data = state;
306
307         *preq = req;
308         *ppstate = state;
309
310         return true;
311 }