Merge branch 'checktalloc' of /home/jelmer/samba4
[amitay/samba.git] / lib / tevent / tevent.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher 2005-2009
8    Copyright (C) Volker Lendecke 2008
9
10      ** NOTE! The following LGPL license applies to the tevent
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #ifndef __TEVENT_H__
29 #define __TEVENT_H__
30
31 #include <stdint.h>
32 #include <talloc.h>
33 #include <sys/time.h>
34 #include <stdbool.h>
35
36 struct tevent_context;
37 struct tevent_ops;
38 struct tevent_fd;
39 struct tevent_timer;
40 struct tevent_signal;
41
42 /* event handler types */
43 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
44                                     struct tevent_fd *fde,
45                                     uint16_t flags,
46                                     void *private_data);
47 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
48                                      struct tevent_fd *fde,
49                                      int fd,
50                                      void *private_data);
51 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
52                                        struct tevent_timer *te,
53                                        struct timeval current_time,
54                                        void *private_data);
55 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
56                                         struct tevent_signal *se,
57                                         int signum,
58                                         int count,
59                                         void *siginfo,
60                                         void *private_data);
61
62 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
63 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
64 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
65 void tevent_set_default_backend(const char *backend);
66
67 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
68                                  TALLOC_CTX *mem_ctx,
69                                  int fd,
70                                  uint16_t flags,
71                                  tevent_fd_handler_t handler,
72                                  void *private_data,
73                                  const char *handler_name,
74                                  const char *location);
75 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
76         _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
77                        #handler, __location__)
78
79 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
80                                        TALLOC_CTX *mem_ctx,
81                                        struct timeval next_event,
82                                        tevent_timer_handler_t handler,
83                                        void *private_data,
84                                        const char *handler_name,
85                                        const char *location);
86 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
87         _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
88                           #handler, __location__)
89
90 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
91                                          TALLOC_CTX *mem_ctx,
92                                          int signum,
93                                          int sa_flags,
94                                          tevent_signal_handler_t handler,
95                                          void *private_data,
96                                          const char *handler_name,
97                                          const char *location);
98 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
99         _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
100                            #handler, __location__)
101
102 int _tevent_loop_once(struct tevent_context *ev, const char *location);
103 #define tevent_loop_once(ev) \
104         _tevent_loop_once(ev, __location__) \
105
106 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
107 #define tevent_loop_wait(ev) \
108         _tevent_loop_wait(ev, __location__) \
109
110 void tevent_fd_set_close_fn(struct tevent_fd *fde,
111                             tevent_fd_close_fn_t close_fn);
112 void tevent_fd_set_auto_close(struct tevent_fd *fde);
113 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
114 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
115
116 bool tevent_signal_support(struct tevent_context *ev);
117
118 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
119
120 /* bits for file descriptor event flags */
121 #define TEVENT_FD_READ 1
122 #define TEVENT_FD_WRITE 2
123
124 #define TEVENT_FD_WRITEABLE(fde) \
125         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
126 #define TEVENT_FD_READABLE(fde) \
127         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
128
129 #define TEVENT_FD_NOT_WRITEABLE(fde) \
130         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
131 #define TEVENT_FD_NOT_READABLE(fde) \
132         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
133
134 /* DEBUG */
135 enum tevent_debug_level {
136         TEVENT_DEBUG_FATAL,
137         TEVENT_DEBUG_ERROR,
138         TEVENT_DEBUG_WARNING,
139         TEVENT_DEBUG_TRACE
140 };
141
142 int tevent_set_debug(struct tevent_context *ev,
143                      void (*debug)(void *context,
144                                    enum tevent_debug_level level,
145                                    const char *fmt,
146                                    va_list ap) PRINTF_ATTRIBUTE(3,0),
147                      void *context);
148 int tevent_set_debug_stderr(struct tevent_context *ev);
149
150 /**
151  * An async request moves between the following 4 states:
152  */
153 enum tevent_req_state {
154         /**
155          * we are creating the request
156          */
157         TEVENT_REQ_INIT,
158         /**
159          * we are waiting the request to complete
160          */
161         TEVENT_REQ_IN_PROGRESS,
162         /**
163          * the request is finished
164          */
165         TEVENT_REQ_DONE,
166         /**
167          * A user error has occured
168          */
169         TEVENT_REQ_USER_ERROR,
170         /**
171          * Request timed out
172          */
173         TEVENT_REQ_TIMED_OUT,
174         /**
175          * No memory in between
176          */
177         TEVENT_REQ_NO_MEMORY,
178         /**
179          * the request is already received by the caller
180          */
181         TEVENT_REQ_RECEIVED
182 };
183
184 /**
185  * @brief An async request
186  *
187  * This represents an async request being processed by callbacks via an event
188  * context. A user can issue for example a write request to a socket, giving
189  * an implementation function the fd, the buffer and the number of bytes to
190  * transfer. The function issuing the request will immediately return without
191  * blocking most likely without having sent anything. The API user then fills
192  * in req->async.fn and req->async.private_data, functions that are called
193  * when the request is finished.
194  *
195  * It is up to the user of the async request to talloc_free it after it has
196  * finished. This can happen while the completion function is called.
197  */
198
199 struct tevent_req;
200
201 typedef void (*tevent_req_fn)(struct tevent_req *);
202
203 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
204 void *_tevent_req_callback_data(struct tevent_req *req);
205 void *_tevent_req_data(struct tevent_req *req);
206
207 #define tevent_req_callback_data(_req, _type) \
208         talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
209 #define tevent_req_callback_data_void(_req) \
210         _tevent_req_callback_data(_req)
211 #define tevent_req_data(_req, _type) \
212         talloc_get_type_abort(_tevent_req_data(_req), _type)
213
214 typedef char *(*tevent_req_print_fn)(struct tevent_req *, TALLOC_CTX *);
215
216 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
217
218 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
219
220 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
221
222 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
223                                       void *pstate,
224                                       size_t state_size,
225                                       const char *type,
226                                       const char *location);
227
228 #define tevent_req_create(_mem_ctx, _pstate, _type) \
229         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
230                            #_type, __location__)
231
232 bool tevent_req_set_endtime(struct tevent_req *req,
233                             struct tevent_context *ev,
234                             struct timeval endtime);
235
236 void tevent_req_done(struct tevent_req *req);
237
238 bool tevent_req_error(struct tevent_req *req,
239                       uint64_t error);
240
241 bool tevent_req_nomem(const void *p,
242                       struct tevent_req *req);
243
244 struct tevent_req *tevent_req_post(struct tevent_req *req,
245                                    struct tevent_context *ev);
246
247 bool tevent_req_is_in_progress(struct tevent_req *req);
248
249 bool tevent_req_poll(struct tevent_req *req,
250                      struct tevent_context *ev);
251
252 bool tevent_req_is_error(struct tevent_req *req,
253                          enum tevent_req_state *state,
254                          uint64_t *error);
255
256 void tevent_req_received(struct tevent_req *req);
257
258 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
259                                       struct tevent_context *ev,
260                                       struct timeval wakeup_time);
261 bool tevent_wakeup_recv(struct tevent_req *req);
262
263 int tevent_timeval_compare(const struct timeval *tv1,
264                            const struct timeval *tv2);
265
266 struct timeval tevent_timeval_zero(void);
267
268 struct timeval tevent_timeval_current(void);
269
270 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
271
272 struct timeval tevent_timeval_until(const struct timeval *tv1,
273                                     const struct timeval *tv2);
274
275 bool tevent_timeval_is_zero(const struct timeval *tv);
276
277 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
278                                   uint32_t usecs);
279
280 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
281
282 struct tevent_queue;
283
284 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
285                                           const char *name,
286                                           const char *location);
287
288 #define tevent_queue_create(_mem_ctx, _name) \
289         _tevent_queue_create((_mem_ctx), (_name), __location__)
290
291 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
292                                           void *private_data);
293 bool tevent_queue_add(struct tevent_queue *queue,
294                       struct tevent_context *ev,
295                       struct tevent_req *req,
296                       tevent_queue_trigger_fn_t trigger,
297                       void *private_data);
298 bool tevent_queue_start(struct tevent_queue *queue,
299                         struct tevent_context *ev);
300 void tevent_queue_stop(struct tevent_queue *queue);
301
302 size_t tevent_queue_length(struct tevent_queue *queue);
303
304 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
305                                    void *private_data,
306                                    uint32_t level,
307                                    bool begin,
308                                    void *stack_ptr,
309                                    const char *location);
310 #ifdef TEVENT_DEPRECATED
311 #ifndef _DEPRECATED_
312 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
313 #define _DEPRECATED_ __attribute__ ((deprecated))
314 #else
315 #define _DEPRECATED_
316 #endif
317 #endif
318 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
319 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
320                                   tevent_nesting_hook hook,
321                                   void *private_data) _DEPRECATED_;
322 int _tevent_loop_until(struct tevent_context *ev,
323                        bool (*finished)(void *private_data),
324                        void *private_data,
325                        const char *location) _DEPRECATED_;
326 #define tevent_loop_until(ev, finished, private_data) \
327         _tevent_loop_until(ev, finished, private_data, __location__)
328 #endif
329
330 #ifdef TEVENT_COMPAT_DEFINES
331
332 #define event_context   tevent_context
333 #define event_ops       tevent_ops
334 #define fd_event        tevent_fd
335 #define timed_event     tevent_timer
336 #define signal_event    tevent_signal
337
338 #define event_fd_handler_t      tevent_fd_handler_t
339 #define event_timed_handler_t   tevent_timer_handler_t
340 #define event_signal_handler_t  tevent_signal_handler_t
341
342 #define event_context_init(mem_ctx) \
343         tevent_context_init(mem_ctx)
344
345 #define event_context_init_byname(mem_ctx, name) \
346         tevent_context_init_byname(mem_ctx, name)
347
348 #define event_backend_list(mem_ctx) \
349         tevent_backend_list(mem_ctx)
350
351 #define event_set_default_backend(backend) \
352         tevent_set_default_backend(backend)
353
354 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
355         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
356
357 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
358         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
359
360 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
361         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
362
363 #define event_loop_once(ev) \
364         tevent_loop_once(ev)
365
366 #define event_loop_wait(ev) \
367         tevent_loop_wait(ev)
368
369 #define event_get_fd_flags(fde) \
370         tevent_fd_get_flags(fde)
371
372 #define event_set_fd_flags(fde, flags) \
373         tevent_fd_set_flags(fde, flags)
374
375 #define EVENT_FD_READ           TEVENT_FD_READ
376 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
377
378 #define EVENT_FD_WRITEABLE(fde) \
379         TEVENT_FD_WRITEABLE(fde)
380
381 #define EVENT_FD_READABLE(fde) \
382         TEVENT_FD_READABLE(fde)
383
384 #define EVENT_FD_NOT_WRITEABLE(fde) \
385         TEVENT_FD_NOT_WRITEABLE(fde)
386
387 #define EVENT_FD_NOT_READABLE(fde) \
388         TEVENT_FD_NOT_READABLE(fde)
389
390 #define ev_debug_level          tevent_debug_level
391
392 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
393 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
394 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
395 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
396
397 #define ev_set_debug(ev, debug, context) \
398         tevent_set_debug(ev, debug, context)
399
400 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
401
402 #endif /* TEVENT_COMPAT_DEFINES */
403
404 #endif /* __TEVENT_H__ */