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