Merge branch 'master' of ctdb into 'master' of samba
[nivanova/samba-autobuild/.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_immediate;
41 struct tevent_signal;
42
43 /**
44  * @defgroup tevent The tevent API
45  *
46  * The tevent low-level API
47  *
48  * This API provides the public interface to manage events in the tevent
49  * mainloop. Functions are provided for managing low-level events such
50  * as timer events, fd events and signal handling.
51  *
52  * @{
53  */
54
55 /* event handler types */
56 /**
57  * Called when a file descriptor monitored by tevent has
58  * data to be read or written on it.
59  */
60 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
61                                     struct tevent_fd *fde,
62                                     uint16_t flags,
63                                     void *private_data);
64
65 /**
66  * Called when tevent is ceasing the monitoring of a file descriptor.
67  */
68 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
69                                      struct tevent_fd *fde,
70                                      int fd,
71                                      void *private_data);
72
73 /**
74  * Called when a tevent timer has fired.
75  */
76 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
77                                        struct tevent_timer *te,
78                                        struct timeval current_time,
79                                        void *private_data);
80
81 /**
82  * Called when a tevent immediate event is invoked.
83  */
84 typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx,
85                                            struct tevent_immediate *im,
86                                            void *private_data);
87
88 /**
89  * Called after tevent detects the specified signal.
90  */
91 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
92                                         struct tevent_signal *se,
93                                         int signum,
94                                         int count,
95                                         void *siginfo,
96                                         void *private_data);
97
98 /**
99  * @brief Create a event_context structure.
100  *
101  * This must be the first events call, and all subsequent calls pass this
102  * event_context as the first element. Event handlers also receive this as
103  * their first argument.
104  *
105  * @param[in]  mem_ctx  The memory context to use.
106  *
107  * @return              An allocated tevent context, NULL on error.
108  *
109  * @see tevent_context_init()
110  */
111 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
112
113 /**
114  * @brief Create a event_context structure and select a specific backend.
115  *
116  * This must be the first events call, and all subsequent calls pass this
117  * event_context as the first element. Event handlers also receive this as
118  * their first argument.
119  *
120  * @param[in]  mem_ctx  The memory context to use.
121  *
122  * @param[in]  name     The name of the backend to use.
123  *
124  * @return              An allocated tevent context, NULL on error.
125  */
126 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
127
128 /**
129  * @brief Create a custom event context
130  *
131  * @param[in]  mem_ctx  The memory context to use.
132  * @param[in]  ops      The function pointer table of the backend.
133  * @param[in]  additional_data  The additional/private data to this instance
134  *
135  * @return              An allocated tevent context, NULL on error.
136  *
137  */
138 struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
139                                                const struct tevent_ops *ops,
140                                                void *additional_data);
141
142 /**
143  * @brief List available backends.
144  *
145  * @param[in]  mem_ctx  The memory context to use.
146  *
147  * @return              A string vector with a terminating NULL element, NULL
148  *                      on error.
149  */
150 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
151
152 /**
153  * @brief Set the default tevent backend.
154  *
155  * @param[in]  backend  The name of the backend to set.
156  */
157 void tevent_set_default_backend(const char *backend);
158
159 #ifdef DOXYGEN
160 /**
161  * @brief Add a file descriptor based event.
162  *
163  * @param[in]  ev       The event context to work on.
164  *
165  * @param[in]  mem_ctx  The talloc memory context to use.
166  *
167  * @param[in]  fd       The file descriptor to base the event on.
168  *
169  * @param[in]  flags    #TEVENT_FD_READ or #TEVENT_FD_WRITE
170  *
171  * @param[in]  handler  The callback handler for the event.
172  *
173  * @param[in]  private_data  The private data passed to the callback handler.
174  *
175  * @return              The file descriptor based event, NULL on error.
176  *
177  * @note To cancel the monitoring of a file descriptor, call talloc_free()
178  * on the object returned by this function.
179  */
180 struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
181                                 TALLOC_CTX *mem_ctx,
182                                 int fd,
183                                 uint16_t flags,
184                                 tevent_fd_handler_t handler,
185                                 void *private_data);
186 #else
187 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
188                                  TALLOC_CTX *mem_ctx,
189                                  int fd,
190                                  uint16_t flags,
191                                  tevent_fd_handler_t handler,
192                                  void *private_data,
193                                  const char *handler_name,
194                                  const char *location);
195 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
196         _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
197                        #handler, __location__)
198 #endif
199
200 #ifdef DOXYGEN
201 /**
202  * @brief Add a timed event
203  *
204  * @param[in]  ev       The event context to work on.
205  *
206  * @param[in]  mem_ctx  The talloc memory context to use.
207  *
208  * @param[in]  next_event  Timeval specifying the absolute time to fire this
209  * event. This is not an offset.
210  *
211  * @param[in]  handler  The callback handler for the event.
212  *
213  * @param[in]  private_data  The private data passed to the callback handler.
214  *
215  * @return The newly-created timer event, or NULL on error.
216  *
217  * @note To cancel a timer event before it fires, call talloc_free() on the
218  * event returned from this function. This event is automatically
219  * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
220  *
221  * @note Unlike some mainloops, tevent timers are one-time events. To set up
222  * a recurring event, it is necessary to call tevent_add_timer() again during
223  * the handler processing.
224  *
225  * @note Due to the internal mainloop processing, a timer set to run
226  * immediately will do so after any other pending timers fire, but before
227  * any further file descriptor or signal handling events fire. Callers should
228  * not rely on this behavior!
229  */
230 struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
231                                       TALLOC_CTX *mem_ctx,
232                                       struct timeval next_event,
233                                       tevent_timer_handler_t handler,
234                                       void *private_data);
235 #else
236 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
237                                        TALLOC_CTX *mem_ctx,
238                                        struct timeval next_event,
239                                        tevent_timer_handler_t handler,
240                                        void *private_data,
241                                        const char *handler_name,
242                                        const char *location);
243 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
244         _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
245                           #handler, __location__)
246 #endif
247
248 #ifdef DOXYGEN
249 /**
250  * Initialize an immediate event object
251  *
252  * This object can be used to trigger an event to occur immediately after
253  * returning from the current event (before any other event occurs)
254  *
255  * @param[in] mem_ctx  The talloc memory context to use as the parent
256  *
257  * @return An empty tevent_immediate object. Use tevent_schedule_immediate
258  * to populate and use it.
259  *
260  * @note Available as of tevent 0.9.8
261  */
262 struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
263 #else
264 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
265                                                   const char *location);
266 #define tevent_create_immediate(mem_ctx) \
267         _tevent_create_immediate(mem_ctx, __location__)
268 #endif
269
270 #ifdef DOXYGEN
271
272 /**
273  * Schedule an event for immediate execution. This event will occur
274  * immediately after returning from the current event (before any other
275  * event occurs)
276  *
277  * @param[in] im       The tevent_immediate object to populate and use
278  * @param[in] ctx      The tevent_context to run this event
279  * @param[in] handler  The event handler to run when this event fires
280  * @param[in] private_data  Data to pass to the event handler
281  */
282 void tevent_schedule_immediate(struct tevent_immediate *im,
283                 struct tevent_context *ctx,
284                 tevent_immediate_handler_t handler,
285                 void *private_data);
286 #else
287 void _tevent_schedule_immediate(struct tevent_immediate *im,
288                                 struct tevent_context *ctx,
289                                 tevent_immediate_handler_t handler,
290                                 void *private_data,
291                                 const char *handler_name,
292                                 const char *location);
293 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
294         _tevent_schedule_immediate(im, ctx, handler, private_data, \
295                                    #handler, __location__);
296 #endif
297
298 #ifdef DOXYGEN
299 /**
300  * @brief Add a tevent signal handler
301  *
302  * tevent_add_signal() creates a new event for handling a signal the next
303  * time through the mainloop. It implements a very simple traditional signal
304  * handler whose only purpose is to add the handler event into the mainloop.
305  *
306  * @param[in]  ev       The event context to work on.
307  *
308  * @param[in]  mem_ctx  The talloc memory context to use.
309  *
310  * @param[in]  signum   The signal to trap
311  *
312  * @param[in]  handler  The callback handler for the signal.
313  *
314  * @param[in]  sa_flags sigaction flags for this signal handler.
315  *
316  * @param[in]  private_data  The private data passed to the callback handler.
317  *
318  * @return The newly-created signal handler event, or NULL on error.
319  *
320  * @note To cancel a signal handler, call talloc_free() on the event returned
321  * from this function.
322  */
323 struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
324                      TALLOC_CTX *mem_ctx,
325                      int signum,
326                      int sa_flags,
327                      tevent_signal_handler_t handler,
328                      void *private_data);
329 #else
330 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
331                                          TALLOC_CTX *mem_ctx,
332                                          int signum,
333                                          int sa_flags,
334                                          tevent_signal_handler_t handler,
335                                          void *private_data,
336                                          const char *handler_name,
337                                          const char *location);
338 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
339         _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
340                            #handler, __location__)
341 #endif
342
343 #ifdef DOXYGEN
344 /**
345  * @brief Pass a single time through the mainloop
346  *
347  * This will process any appropriate signal, immediate, fd and timer events
348  *
349  * @param[in]  ev The event context to process
350  *
351  * @return Zero on success, nonzero if an internal error occurred
352  */
353 int tevent_loop_once(struct tevent_context *ev);
354 #else
355 int _tevent_loop_once(struct tevent_context *ev, const char *location);
356 #define tevent_loop_once(ev) \
357         _tevent_loop_once(ev, __location__)
358 #endif
359
360 #ifdef DOXYGEN
361 /**
362  * @brief Run the mainloop
363  *
364  * The mainloop will run until there are no events remaining to be processed
365  *
366  * @param[in]  ev The event context to process
367  *
368  * @return Zero if all events have been processed. Nonzero if an internal
369  * error occurred.
370  */
371 int tevent_loop_wait(struct tevent_context *ev);
372 #else
373 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
374 #define tevent_loop_wait(ev) \
375         _tevent_loop_wait(ev, __location__)
376 #endif
377
378
379 /**
380  * Assign a function to run when a tevent_fd is freed
381  *
382  * This function is a destructor for the tevent_fd. It does not automatically
383  * close the file descriptor. If this is the desired behavior, then it must be
384  * performed by the close_fn.
385  *
386  * @param[in] fde       File descriptor event on which to set the destructor
387  * @param[in] close_fn  Destructor to execute when fde is freed
388  */
389 void tevent_fd_set_close_fn(struct tevent_fd *fde,
390                             tevent_fd_close_fn_t close_fn);
391
392 /**
393  * Automatically close the file descriptor when the tevent_fd is freed
394  *
395  * This function calls close(fd) internally.
396  *
397  * @param[in] fde  File descriptor event to auto-close
398  */
399 void tevent_fd_set_auto_close(struct tevent_fd *fde);
400
401 /**
402  * Return the flags set on this file descriptor event
403  *
404  * @param[in] fde  File descriptor event to query
405  *
406  * @return The flags set on the event. See #TEVENT_FD_READ and
407  * #TEVENT_FD_WRITE
408  */
409 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
410
411 /**
412  * Set flags on a file descriptor event
413  *
414  * @param[in] fde    File descriptor event to set
415  * @param[in] flags  Flags to set on the event. See #TEVENT_FD_READ and
416  * #TEVENT_FD_WRITE
417  */
418 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
419
420 /**
421  * Query whether tevent supports signal handling
422  *
423  * @param[in] ev  An initialized tevent context
424  *
425  * @return True if this platform and tevent context support signal handling
426  */
427 bool tevent_signal_support(struct tevent_context *ev);
428
429 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
430
431 /* bits for file descriptor event flags */
432
433 /**
434  * Monitor a file descriptor for write availability
435  */
436 #define TEVENT_FD_READ 1
437 /**
438  * Monitor a file descriptor for data to be read
439  */
440 #define TEVENT_FD_WRITE 2
441
442 /**
443  * Convenience function for declaring a tevent_fd writable
444  */
445 #define TEVENT_FD_WRITEABLE(fde) \
446         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
447
448 /**
449  * Convenience function for declaring a tevent_fd readable
450  */
451 #define TEVENT_FD_READABLE(fde) \
452         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
453
454 /**
455  * Convenience function for declaring a tevent_fd non-writable
456  */
457 #define TEVENT_FD_NOT_WRITEABLE(fde) \
458         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
459
460 /**
461  * Convenience function for declaring a tevent_fd non-readable
462  */
463 #define TEVENT_FD_NOT_READABLE(fde) \
464         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
465
466 /**
467  * Debug level of tevent
468  */
469 enum tevent_debug_level {
470         TEVENT_DEBUG_FATAL,
471         TEVENT_DEBUG_ERROR,
472         TEVENT_DEBUG_WARNING,
473         TEVENT_DEBUG_TRACE
474 };
475
476 /**
477  * @brief The tevent debug callbac.
478  *
479  * @param[in]  context  The memory context to use.
480  *
481  * @param[in]  level    The debug level.
482  *
483  * @param[in]  fmt      The format string.
484  *
485  * @param[in]  ap       The arguments for the format string.
486  */
487 typedef void (*tevent_debug_fn)(void *context,
488                                 enum tevent_debug_level level,
489                                 const char *fmt,
490                                 va_list ap) PRINTF_ATTRIBUTE(3,0);
491
492 /**
493  * Set destination for tevent debug messages
494  *
495  * @param[in] ev        Event context to debug
496  * @param[in] debug     Function to handle output printing
497  * @param[in] context   The context to pass to the debug function.
498  *
499  * @return Always returns 0 as of version 0.9.8
500  *
501  * @note Default is to emit no debug messages
502  */
503 int tevent_set_debug(struct tevent_context *ev,
504                      tevent_debug_fn debug,
505                      void *context);
506
507 /**
508  * Designate stderr for debug message output
509  *
510  * @param[in] ev     Event context to debug
511  *
512  * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
513  * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
514  * function for tevent_set_debug()
515  */
516 int tevent_set_debug_stderr(struct tevent_context *ev);
517
518 enum tevent_trace_point {
519         /**
520          * Corresponds to a trace point just before waiting
521          */
522         TEVENT_TRACE_BEFORE_WAIT,
523         /**
524          * Corresponds to a trace point just after waiting
525          */
526         TEVENT_TRACE_AFTER_WAIT,
527 #define TEVENT_HAS_LOOP_ONCE_TRACE_POINTS 1
528         /**
529          * Corresponds to a trace point just before calling
530          * the loop_once() backend function.
531          */
532         TEVENT_TRACE_BEFORE_LOOP_ONCE,
533         /**
534          * Corresponds to a trace point right after the
535          * loop_once() backend function has returned.
536          */
537         TEVENT_TRACE_AFTER_LOOP_ONCE,
538 };
539
540 typedef void (*tevent_trace_callback_t)(enum tevent_trace_point,
541                                         void *private_data);
542
543 /**
544  * Register a callback to be called at certain trace points
545  *
546  * @param[in] ev             Event context
547  * @param[in] cb             Trace callback
548  * @param[in] private_data   Data to be passed to callback
549  *
550  * @note The callback will be called at trace points defined by
551  * tevent_trace_point.  Call with NULL to reset.
552  */
553 void tevent_set_trace_callback(struct tevent_context *ev,
554                                tevent_trace_callback_t cb,
555                                void *private_data);
556
557 /**
558  * Retrieve the current trace callback
559  *
560  * @param[in] ev             Event context
561  * @param[out] cb            Registered trace callback
562  * @param[out] private_data  Registered data to be passed to callback
563  *
564  * @note This can be used to allow one component that wants to
565  * register a callback to respect the callback that another component
566  * has already registered.
567  */
568 void tevent_get_trace_callback(struct tevent_context *ev,
569                                tevent_trace_callback_t *cb,
570                                void *private_data);
571
572 /**
573  * @}
574  */
575
576 /**
577  * @defgroup tevent_request The tevent request functions.
578  * @ingroup tevent
579  *
580  * A tevent_req represents an asynchronous computation.
581  *
582  * The tevent_req group of API calls is the recommended way of
583  * programming async computations within tevent. In particular the
584  * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
585  * are considered too low-level to be used in larger computations. To
586  * read and write from and to sockets, Samba provides two calls on top
587  * of tevent_add_fd: read_packet_send/recv and writev_send/recv. These
588  * requests are much easier to compose than the low-level event
589  * handlers called from tevent_add_fd.
590  *
591  * A lot of the simplicity tevent_req has brought to the notoriously
592  * hairy async programming came via a set of conventions that every
593  * async computation programmed should follow. One central piece of
594  * these conventions is the naming of routines and variables.
595  *
596  * Every async computation needs a name (sensibly called "computation"
597  * down from here). From this name quite a few naming conventions are
598  * derived.
599  *
600  * Every computation that requires local state needs a
601  * @code
602  * struct computation_state {
603  *     int local_var;
604  * };
605  * @endcode
606  * Even if no local variables are required, such a state struct should
607  * be created containing a dummy variable. Quite a few helper
608  * functions and macros (for example tevent_req_create()) assume such
609  * a state struct.
610  *
611  * An async computation is started by a computation_send
612  * function. When it is finished, its result can be received by a
613  * computation_recv function. For an example how to set up an async
614  * computation, see the code example in the documentation for
615  * tevent_req_create() and tevent_req_post(). The prototypes for _send
616  * and _recv functions should follow some conventions:
617  *
618  * @code
619  * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
620  *                                     struct tevent_req *ev,
621  *                                     ... further args);
622  * int computation_recv(struct tevent_req *req, ... further output args);
623  * @endcode
624  *
625  * The "int" result of computation_recv() depends on the result the
626  * sync version of the function would have, "int" is just an example
627  * here.
628  *
629  * Another important piece of the conventions is that the program flow
630  * is interrupted as little as possible. Because a blocking
631  * sub-computation requires that the flow needs to continue in a
632  * separate function that is the logical sequel of some computation,
633  * it should lexically follow sending off the blocking
634  * sub-computation. Setting the callback function via
635  * tevent_req_set_callback() requires referencing a function lexically
636  * below the call to tevent_req_set_callback(), forward declarations
637  * are required. A lot of the async computations thus begin with a
638  * sequence of declarations such as
639  *
640  * @code
641  * static void computation_step1_done(struct tevent_req *subreq);
642  * static void computation_step2_done(struct tevent_req *subreq);
643  * static void computation_step3_done(struct tevent_req *subreq);
644  * @endcode
645  *
646  * It really helps readability a lot to do these forward declarations,
647  * because the lexically sequential program flow makes the async
648  * computations almost as clear to read as a normal, sync program
649  * flow.
650  *
651  * It is up to the user of the async computation to talloc_free it
652  * after it has finished. If an async computation should be aborted,
653  * the tevent_req structure can be talloc_free'ed. After it has
654  * finished, it should talloc_free'ed by the API user.
655  *
656  * @{
657  */
658
659 /**
660  * An async request moves from TEVENT_REQ_INIT to
661  * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
662  * has finished.
663  */
664 enum tevent_req_state {
665         /**
666          * We are creating the request
667          */
668         TEVENT_REQ_INIT,
669         /**
670          * We are waiting the request to complete
671          */
672         TEVENT_REQ_IN_PROGRESS,
673         /**
674          * The request is finished successfully
675          */
676         TEVENT_REQ_DONE,
677         /**
678          * A user error has occurred. The user error has been
679          * indicated by tevent_req_error(), it can be retrieved via
680          * tevent_req_is_error().
681          */
682         TEVENT_REQ_USER_ERROR,
683         /**
684          * Request timed out after the timeout set by tevent_req_set_endtime.
685          */
686         TEVENT_REQ_TIMED_OUT,
687         /**
688          * An internal allocation has failed, or tevent_req_nomem has
689          * been given a NULL pointer as the first argument.
690          */
691         TEVENT_REQ_NO_MEMORY,
692         /**
693          * The request has been received by the caller. No further
694          * action is valid.
695          */
696         TEVENT_REQ_RECEIVED
697 };
698
699 /**
700  * @brief An async request
701  */
702 struct tevent_req;
703
704 /**
705  * @brief A tevent request callback function.
706  *
707  * @param[in]  req      The tevent async request which executed this callback.
708  */
709 typedef void (*tevent_req_fn)(struct tevent_req *req);
710
711 /**
712  * @brief Set an async request callback.
713  *
714  * See the documentation of tevent_req_post() for an example how this
715  * is supposed to be used.
716  *
717  * @param[in]  req      The async request to set the callback.
718  *
719  * @param[in]  fn       The callback function to set.
720  *
721  * @param[in]  pvt      A pointer to private data to pass to the async request
722  *                      callback.
723  */
724 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
725
726 #ifdef DOXYGEN
727 /**
728  * @brief Get the private data cast to the given type for a callback from
729  *        a tevent request structure.
730  *
731  * @code
732  * static void computation_done(struct tevent_req *subreq) {
733  *     struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
734  *     struct computation_state *state = tevent_req_data(req, struct computation_state);
735  *     .... more things, eventually maybe call tevent_req_done(req);
736  * }
737  * @endcode
738  *
739  * @param[in]  req      The structure to get the callback data from.
740  *
741  * @param[in]  type     The type of the private callback data to get.
742  *
743  * @return              The type casted private data set NULL if not set.
744  */
745 void *tevent_req_callback_data(struct tevent_req *req, #type);
746 #else
747 void *_tevent_req_callback_data(struct tevent_req *req);
748 #define tevent_req_callback_data(_req, _type) \
749         talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
750 #endif
751
752 #ifdef DOXYGEN
753 /**
754  * @brief Get the private data for a callback from a tevent request structure.
755  *
756  * @param[in]  req      The structure to get the callback data from.
757  *
758  * @param[in]  req      The structure to get the data from.
759  *
760  * @return              The private data or NULL if not set.
761  */
762 void *tevent_req_callback_data_void(struct tevent_req *req);
763 #else
764 #define tevent_req_callback_data_void(_req) \
765         _tevent_req_callback_data(_req)
766 #endif
767
768 #ifdef DOXYGEN
769 /**
770  * @brief Get the private data from a tevent request structure.
771  *
772  * When the tevent_req has been created by tevent_req_create, the
773  * result of tevent_req_data() is the state variable created by
774  * tevent_req_create() as a child of the req.
775  *
776  * @param[in]  req      The structure to get the private data from.
777  *
778  * @param[in]  type     The type of the private data
779  *
780  * @return              The private data or NULL if not set.
781  */
782 void *tevent_req_data(struct tevent_req *req, #type);
783 #else
784 void *_tevent_req_data(struct tevent_req *req);
785 #define tevent_req_data(_req, _type) \
786         talloc_get_type_abort(_tevent_req_data(_req), _type)
787 #endif
788
789 /**
790  * @brief The print function which can be set for a tevent async request.
791  *
792  * @param[in]  req      The tevent async request.
793  *
794  * @param[in]  ctx      A talloc memory context which can be uses to allocate
795  *                      memory.
796  *
797  * @return              An allocated string buffer to print.
798  *
799  * Example:
800  * @code
801  *   static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
802  *   {
803  *     struct my_data *data = tevent_req_data(req, struct my_data);
804  *     char *result;
805  *
806  *     result = tevent_req_default_print(mem_ctx, req);
807  *     if (result == NULL) {
808  *       return NULL;
809  *     }
810  *
811  *     return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
812  *       data->foo, data->bar);
813  *   }
814  * @endcode
815  */
816 typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
817
818 /**
819  * @brief This function sets a print function for the given request.
820  *
821  * This function can be used to setup a print function for the given request.
822  * This will be triggered if the tevent_req_print() function was
823  * called on the given request.
824  *
825  * @param[in]  req      The request to use.
826  *
827  * @param[in]  fn       A pointer to the print function
828  *
829  * @note This function should only be used for debugging.
830  */
831 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
832
833 /**
834  * @brief The default print function for creating debug messages.
835  *
836  * The function should not be used by users of the async API,
837  * but custom print function can use it and append custom text
838  * to the string.
839  *
840  * @param[in]  req      The request to be printed.
841  *
842  * @param[in]  mem_ctx  The memory context for the result.
843  *
844  * @return              Text representation of request.
845  *
846  */
847 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
848
849 /**
850  * @brief Print an tevent_req structure in debug messages.
851  *
852  * This function should be used by callers of the async API.
853  *
854  * @param[in]  mem_ctx  The memory context for the result.
855  *
856  * @param[in] req       The request to be printed.
857  *
858  * @return              Text representation of request.
859  */
860 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
861
862 /**
863  * @brief A typedef for a cancel function for a tevent request.
864  *
865  * @param[in]  req      The tevent request calling this function.
866  *
867  * @return              True if the request could be canceled, false if not.
868  */
869 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
870
871 /**
872  * @brief This function sets a cancel function for the given tevent request.
873  *
874  * This function can be used to setup a cancel function for the given request.
875  * This will be triggered if the tevent_req_cancel() function was
876  * called on the given request.
877  *
878  * @param[in]  req      The request to use.
879  *
880  * @param[in]  fn       A pointer to the cancel function.
881  */
882 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
883
884 #ifdef DOXYGEN
885 /**
886  * @brief Try to cancel the given tevent request.
887  *
888  * This function can be used to cancel the given request.
889  *
890  * It is only possible to cancel a request when the implementation
891  * has registered a cancel function via the tevent_req_set_cancel_fn().
892  *
893  * @param[in]  req      The request to use.
894  *
895  * @return              This function returns true is the request is cancelable,
896  *                      othererwise false is returned.
897  *
898  * @note Even if the function returns true, the caller need to wait
899  *       for the function to complete normally.
900  *       Only the _recv() function of the given request indicates
901  *       if the request was really canceled.
902  */
903 bool tevent_req_cancel(struct tevent_req *req);
904 #else
905 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
906 #define tevent_req_cancel(req) \
907         _tevent_req_cancel(req, __location__)
908 #endif
909
910 #ifdef DOXYGEN
911 /**
912  * @brief Create an async tevent request.
913  *
914  * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
915  *
916  * @code
917  * struct tevent_req *req;
918  * struct computation_state *state;
919  * req = tevent_req_create(mem_ctx, &state, struct computation_state);
920  * @endcode
921  *
922  * Tevent_req_create() allocates and zeros the state variable as a talloc
923  * child of its result. The state variable should be used as the talloc
924  * parent for all temporary variables that are allocated during the async
925  * computation. This way, when the user of the async computation frees
926  * the request, the state as a talloc child will be free'd along with
927  * all the temporary variables hanging off the state.
928  *
929  * @param[in] mem_ctx   The memory context for the result.
930  * @param[in] pstate    Pointer to the private request state.
931  * @param[in] type      The name of the request.
932  *
933  * @return              A new async request. NULL on error.
934  */
935 struct tevent_req *tevent_req_create(TALLOC_CTX *mem_ctx,
936                                      void **pstate, #type);
937 #else
938 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
939                                       void *pstate,
940                                       size_t state_size,
941                                       const char *type,
942                                       const char *location);
943
944 #define tevent_req_create(_mem_ctx, _pstate, _type) \
945         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
946                            #_type, __location__)
947 #endif
948
949 /**
950  * @brief Set a timeout for an async request.
951  *
952  * @param[in]  req      The request to set the timeout for.
953  *
954  * @param[in]  ev       The event context to use for the timer.
955  *
956  * @param[in]  endtime  The endtime of the request.
957  *
958  * @return              True if succeeded, false if not.
959  */
960 bool tevent_req_set_endtime(struct tevent_req *req,
961                             struct tevent_context *ev,
962                             struct timeval endtime);
963
964 #ifdef DOXYGEN
965 /**
966  * @brief Call the notify callback of the given tevent request manually.
967  *
968  * @param[in]  req      The tevent request to call the notify function from.
969  *
970  * @see tevent_req_set_callback()
971  */
972 void tevent_req_notify_callback(struct tevent_req *req);
973 #else
974 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
975 #define tevent_req_notify_callback(req)         \
976         _tevent_req_notify_callback(req, __location__)
977 #endif
978
979 #ifdef DOXYGEN
980 /**
981  * @brief An async request has successfully finished.
982  *
983  * This function is to be used by implementors of async requests. When a
984  * request is successfully finished, this function calls the user's completion
985  * function.
986  *
987  * @param[in]  req       The finished request.
988  */
989 void tevent_req_done(struct tevent_req *req);
990 #else
991 void _tevent_req_done(struct tevent_req *req,
992                       const char *location);
993 #define tevent_req_done(req) \
994         _tevent_req_done(req, __location__)
995 #endif
996
997 #ifdef DOXYGEN
998 /**
999  * @brief An async request has seen an error.
1000  *
1001  * This function is to be used by implementors of async requests. When a
1002  * request can not successfully completed, the implementation should call this
1003  * function with the appropriate status code.
1004  *
1005  * If error is 0 the function returns false and does nothing more.
1006  *
1007  * @param[in]  req      The request with an error.
1008  *
1009  * @param[in]  error    The error code.
1010  *
1011  * @return              On success true is returned, false if error is 0.
1012  *
1013  * @code
1014  * int error = first_function();
1015  * if (tevent_req_error(req, error)) {
1016  *      return;
1017  * }
1018  *
1019  * error = second_function();
1020  * if (tevent_req_error(req, error)) {
1021  *      return;
1022  * }
1023  *
1024  * tevent_req_done(req);
1025  * return;
1026  * @endcode
1027  */
1028 bool tevent_req_error(struct tevent_req *req,
1029                       uint64_t error);
1030 #else
1031 bool _tevent_req_error(struct tevent_req *req,
1032                        uint64_t error,
1033                        const char *location);
1034 #define tevent_req_error(req, error) \
1035         _tevent_req_error(req, error, __location__)
1036 #endif
1037
1038 #ifdef DOXYGEN
1039 /**
1040  * @brief Helper function for nomem check.
1041  *
1042  * Convenience helper to easily check alloc failure within a callback
1043  * implementing the next step of an async request.
1044  *
1045  * @param[in]  p        The pointer to be checked.
1046  *
1047  * @param[in]  req      The request being processed.
1048  *
1049  * @code
1050  * p = talloc(mem_ctx, bla);
1051  * if (tevent_req_nomem(p, req)) {
1052  *      return;
1053  * }
1054  * @endcode
1055  */
1056 bool tevent_req_nomem(const void *p,
1057                       struct tevent_req *req);
1058 #else
1059 bool _tevent_req_nomem(const void *p,
1060                        struct tevent_req *req,
1061                        const char *location);
1062 #define tevent_req_nomem(p, req) \
1063         _tevent_req_nomem(p, req, __location__)
1064 #endif
1065
1066 #ifdef DOXYGEN
1067 /**
1068  * @brief Indicate out of memory to a request
1069  *
1070  * @param[in]  req      The request being processed.
1071  */
1072 void tevent_req_oom(struct tevent_req *req);
1073 #else
1074 void _tevent_req_oom(struct tevent_req *req,
1075                      const char *location);
1076 #define tevent_req_oom(req) \
1077         _tevent_req_oom(req, __location__)
1078 #endif
1079
1080 /**
1081  * @brief Finish a request before the caller had the change to set the callback.
1082  *
1083  * An implementation of an async request might find that it can either finish
1084  * the request without waiting for an external event, or it can not even start
1085  * the engine. To present the illusion of a callback to the user of the API,
1086  * the implementation can call this helper function which triggers an
1087  * immediate event. This way the caller can use the same calling
1088  * conventions, independent of whether the request was actually deferred.
1089  *
1090  * @code
1091  * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1092  *                                     struct tevent_context *ev)
1093  * {
1094  *     struct tevent_req *req, *subreq;
1095  *     struct computation_state *state;
1096  *     req = tevent_req_create(mem_ctx, &state, struct computation_state);
1097  *     if (req == NULL) {
1098  *         return NULL;
1099  *     }
1100  *     subreq = subcomputation_send(state, ev);
1101  *     if (tevent_req_nomem(subreq, req)) {
1102  *         return tevent_req_post(req, ev);
1103  *     }
1104  *     tevent_req_set_callback(subreq, computation_done, req);
1105  *     return req;
1106  * }
1107  * @endcode
1108  *
1109  * @param[in]  req      The finished request.
1110  *
1111  * @param[in]  ev       The tevent_context for the immediate event.
1112  *
1113  * @return              The given request will be returned.
1114  */
1115 struct tevent_req *tevent_req_post(struct tevent_req *req,
1116                                    struct tevent_context *ev);
1117
1118 /**
1119  * @brief Finish multiple requests within one function
1120  *
1121  * Normally tevent_req_notify_callback() and all wrappers
1122  * (e.g. tevent_req_done() and tevent_req_error())
1123  * need to be the last thing an event handler should call.
1124  * This is because the callback is likely to destroy the
1125  * context of the current function.
1126  *
1127  * If a function wants to notify more than one caller,
1128  * it is dangerous if it just triggers multiple callbacks
1129  * in a row. With tevent_req_defer_callback() it is possible
1130  * to set an event context that will be used to defer the callback
1131  * via an immediate event (similar to tevent_req_post()).
1132  *
1133  * @code
1134  * struct complete_state {
1135  *       struct tevent_context *ev;
1136  *
1137  *       struct tevent_req **reqs;
1138  * };
1139  *
1140  * void complete(struct complete_state *state)
1141  * {
1142  *       size_t i, c = talloc_array_length(state->reqs);
1143  *
1144  *       for (i=0; i < c; i++) {
1145  *            tevent_req_defer_callback(state->reqs[i], state->ev);
1146  *            tevent_req_done(state->reqs[i]);
1147  *       }
1148  * }
1149  * @endcode
1150  *
1151  * @param[in]  req      The finished request.
1152  *
1153  * @param[in]  ev       The tevent_context for the immediate event.
1154  *
1155  * @return              The given request will be returned.
1156  */
1157 void tevent_req_defer_callback(struct tevent_req *req,
1158                                struct tevent_context *ev);
1159
1160 /**
1161  * @brief Check if the given request is still in progress.
1162  *
1163  * It is typically used by sync wrapper functions.
1164  *
1165  * @param[in]  req      The request to poll.
1166  *
1167  * @return              The boolean form of "is in progress".
1168  */
1169 bool tevent_req_is_in_progress(struct tevent_req *req);
1170
1171 /**
1172  * @brief Actively poll for the given request to finish.
1173  *
1174  * This function is typically used by sync wrapper functions.
1175  *
1176  * @param[in]  req      The request to poll.
1177  *
1178  * @param[in]  ev       The tevent_context to be used.
1179  *
1180  * @return              On success true is returned. If a critical error has
1181  *                      happened in the tevent loop layer false is returned.
1182  *                      This is not the return value of the given request!
1183  *
1184  * @note This should only be used if the given tevent context was created by the
1185  * caller, to avoid event loop nesting.
1186  *
1187  * @code
1188  * req = tstream_writev_queue_send(mem_ctx,
1189  *                                 ev_ctx,
1190  *                                 tstream,
1191  *                                 send_queue,
1192  *                                 iov, 2);
1193  * ok = tevent_req_poll(req, tctx->ev);
1194  * rc = tstream_writev_queue_recv(req, &sys_errno);
1195  * TALLOC_FREE(req);
1196  * @endcode
1197  */
1198 bool tevent_req_poll(struct tevent_req *req,
1199                      struct tevent_context *ev);
1200
1201 /**
1202  * @brief Get the tevent request state and the actual error set by
1203  * tevent_req_error.
1204  *
1205  * @code
1206  * int computation_recv(struct tevent_req *req, uint64_t *perr)
1207  * {
1208  *     enum tevent_req_state state;
1209  *     uint64_t err;
1210  *     if (tevent_req_is_error(req, &state, &err)) {
1211  *         *perr = err;
1212  *         return -1;
1213  *     }
1214  *     return 0;
1215  * }
1216  * @endcode
1217  *
1218  * @param[in]  req      The tevent request to get the error from.
1219  *
1220  * @param[out] state    A pointer to store the tevent request error state.
1221  *
1222  * @param[out] error    A pointer to store the error set by tevent_req_error().
1223  *
1224  * @return              True if the function could set error and state, false
1225  *                      otherwise.
1226  *
1227  * @see tevent_req_error()
1228  */
1229 bool tevent_req_is_error(struct tevent_req *req,
1230                          enum tevent_req_state *state,
1231                          uint64_t *error);
1232
1233 /**
1234  * @brief Use as the last action of a _recv() function.
1235  *
1236  * This function destroys the attached private data.
1237  *
1238  * @param[in]  req      The finished request.
1239  */
1240 void tevent_req_received(struct tevent_req *req);
1241
1242 /**
1243  * @brief Create a tevent subrequest at a given time.
1244  *
1245  * The idea is that always the same syntax for tevent requests.
1246  *
1247  * @param[in]  mem_ctx  The talloc memory context to use.
1248  *
1249  * @param[in]  ev       The event handle to setup the request.
1250  *
1251  * @param[in]  wakeup_time The time to wakeup and execute the request.
1252  *
1253  * @return              The new subrequest, NULL on error.
1254  *
1255  * Example:
1256  * @code
1257  *   static void my_callback_wakeup_done(tevent_req *subreq)
1258  *   {
1259  *     struct tevent_req *req = tevent_req_callback_data(subreq,
1260  *                              struct tevent_req);
1261  *     bool ok;
1262  *
1263  *     ok = tevent_wakeup_recv(subreq);
1264  *     TALLOC_FREE(subreq);
1265  *     if (!ok) {
1266  *         tevent_req_error(req, -1);
1267  *         return;
1268  *     }
1269  *     ...
1270  *   }
1271  * @endcode
1272  *
1273  * @code
1274  *   subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1275  *   if (tevent_req_nomem(subreq, req)) {
1276  *     return false;
1277  *   }
1278  *   tevent_set_callback(subreq, my_callback_wakeup_done, req);
1279  * @endcode
1280  *
1281  * @see tevent_wakeup_recv()
1282  */
1283 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
1284                                       struct tevent_context *ev,
1285                                       struct timeval wakeup_time);
1286
1287 /**
1288  * @brief Check if the wakeup has been correctly executed.
1289  *
1290  * This function needs to be called in the callback function set after calling
1291  * tevent_wakeup_send().
1292  *
1293  * @param[in]  req      The tevent request to check.
1294  *
1295  * @return              True on success, false otherwise.
1296  *
1297  * @see tevent_wakeup_recv()
1298  */
1299 bool tevent_wakeup_recv(struct tevent_req *req);
1300
1301 /* @} */
1302
1303 /**
1304  * @defgroup tevent_helpers The tevent helper functiions
1305  * @ingroup tevent
1306  *
1307  * @todo description
1308  *
1309  * @{
1310  */
1311
1312 /**
1313  * @brief Compare two timeval values.
1314  *
1315  * @param[in]  tv1      The first timeval value to compare.
1316  *
1317  * @param[in]  tv2      The second timeval value to compare.
1318  *
1319  * @return              0 if they are equal.
1320  *                      1 if the first time is greater than the second.
1321  *                      -1 if the first time is smaller than the second.
1322  */
1323 int tevent_timeval_compare(const struct timeval *tv1,
1324                            const struct timeval *tv2);
1325
1326 /**
1327  * @brief Get a zero timval value.
1328  *
1329  * @return              A zero timval value.
1330  */
1331 struct timeval tevent_timeval_zero(void);
1332
1333 /**
1334  * @brief Get a timeval value for the current time.
1335  *
1336  * @return              A timval value with the current time.
1337  */
1338 struct timeval tevent_timeval_current(void);
1339
1340 /**
1341  * @brief Get a timeval structure with the given values.
1342  *
1343  * @param[in]  secs     The seconds to set.
1344  *
1345  * @param[in]  usecs    The microseconds to set.
1346  *
1347  * @return              A timeval structure with the given values.
1348  */
1349 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
1350
1351 /**
1352  * @brief Get the difference between two timeval values.
1353  *
1354  * @param[in]  tv1      The first timeval.
1355  *
1356  * @param[in]  tv2      The second timeval.
1357  *
1358  * @return              A timeval structure with the difference between the
1359  *                      first and the second value.
1360  */
1361 struct timeval tevent_timeval_until(const struct timeval *tv1,
1362                                     const struct timeval *tv2);
1363
1364 /**
1365  * @brief Check if a given timeval structure is zero.
1366  *
1367  * @param[in]  tv       The timeval to check if it is zero.
1368  *
1369  * @return              True if it is zero, false otherwise.
1370  */
1371 bool tevent_timeval_is_zero(const struct timeval *tv);
1372
1373 /**
1374  * @brief Add the given amount of time to a timeval structure.
1375  *
1376  * @param[in]  tv        The timeval structure to add the time.
1377  *
1378  * @param[in]  secs      The seconds to add to the timeval.
1379  *
1380  * @param[in]  usecs     The microseconds to add to the timeval.
1381  *
1382  * @return               The timeval structure with the new time.
1383  */
1384 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
1385                                   uint32_t usecs);
1386
1387 /**
1388  * @brief Get a timeval in the future with a specified offset from now.
1389  *
1390  * @param[in]  secs     The seconds of the offset from now.
1391  *
1392  * @param[in]  usecs    The microseconds of the offset from now.
1393  *
1394  * @return              A timval with the given offset in the future.
1395  */
1396 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
1397
1398 /* @} */
1399
1400
1401 /**
1402  * @defgroup tevent_queue The tevent queue functions
1403  * @ingroup tevent
1404  *
1405  * A tevent_queue is used to queue up async requests that must be
1406  * serialized. For example writing buffers into a socket must be
1407  * serialized. Writing a large lump of data into a socket can require
1408  * multiple write(2) or send(2) system calls. If more than one async
1409  * request is outstanding to write large buffers into a socket, every
1410  * request must individually be completed before the next one begins,
1411  * even if multiple syscalls are required.
1412  *
1413  * Take a look at @ref tevent_queue_tutorial for more details.
1414  * @{
1415  */
1416
1417 struct tevent_queue;
1418 struct tevent_queue_entry;
1419
1420 #ifdef DOXYGEN
1421 /**
1422  * @brief Create and start a tevent queue.
1423  *
1424  * @param[in]  mem_ctx  The talloc memory context to allocate the queue.
1425  *
1426  * @param[in]  name     The name to use to identify the queue.
1427  *
1428  * @return              An allocated tevent queue on success, NULL on error.
1429  *
1430  * @see tevent_queue_start()
1431  * @see tevent_queue_stop()
1432  */
1433 struct tevent_queue *tevent_queue_create(TALLOC_CTX *mem_ctx,
1434                                          const char *name);
1435 #else
1436 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
1437                                           const char *name,
1438                                           const char *location);
1439
1440 #define tevent_queue_create(_mem_ctx, _name) \
1441         _tevent_queue_create((_mem_ctx), (_name), __location__)
1442 #endif
1443
1444 /**
1445  * @brief A callback trigger function run by the queue.
1446  *
1447  * @param[in]  req      The tevent request the trigger function is executed on.
1448  *
1449  * @param[in]  private_data The private data pointer specified by
1450  *                          tevent_queue_add().
1451  *
1452  * @see tevent_queue_add()
1453  * @see tevent_queue_add_entry()
1454  * @see tevent_queue_add_optimize_empty()
1455  */
1456 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
1457                                           void *private_data);
1458
1459 /**
1460  * @brief Add a tevent request to the queue.
1461  *
1462  * @param[in]  queue    The queue to add the request.
1463  *
1464  * @param[in]  ev       The event handle to use for the request.
1465  *
1466  * @param[in]  req      The tevent request to add to the queue.
1467  *
1468  * @param[in]  trigger  The function triggered by the queue when the request
1469  *                      is called. Since tevent 0.9.14 it's possible to
1470  *                      pass NULL, in order to just add a "blocker" to the
1471  *                      queue.
1472  *
1473  * @param[in]  private_data The private data passed to the trigger function.
1474  *
1475  * @return              True if the request has been successfully added, false
1476  *                      otherwise.
1477  */
1478 bool tevent_queue_add(struct tevent_queue *queue,
1479                       struct tevent_context *ev,
1480                       struct tevent_req *req,
1481                       tevent_queue_trigger_fn_t trigger,
1482                       void *private_data);
1483
1484 /**
1485  * @brief Add a tevent request to the queue.
1486  *
1487  * The request can be removed from the queue by calling talloc_free()
1488  * (or a similar function) on the returned queue entry. This
1489  * is the only difference to tevent_queue_add().
1490  *
1491  * @param[in]  queue    The queue to add the request.
1492  *
1493  * @param[in]  ev       The event handle to use for the request.
1494  *
1495  * @param[in]  req      The tevent request to add to the queue.
1496  *
1497  * @param[in]  trigger  The function triggered by the queue when the request
1498  *                      is called. Since tevent 0.9.14 it's possible to
1499  *                      pass NULL, in order to just add a "blocker" to the
1500  *                      queue.
1501  *
1502  * @param[in]  private_data The private data passed to the trigger function.
1503  *
1504  * @return              a pointer to the tevent_queue_entry if the request
1505  *                      has been successfully added, NULL otherwise.
1506  *
1507  * @see tevent_queue_add()
1508  * @see tevent_queue_add_optimize_empty()
1509  */
1510 struct tevent_queue_entry *tevent_queue_add_entry(
1511                                         struct tevent_queue *queue,
1512                                         struct tevent_context *ev,
1513                                         struct tevent_req *req,
1514                                         tevent_queue_trigger_fn_t trigger,
1515                                         void *private_data);
1516
1517 /**
1518  * @brief Add a tevent request to the queue using a possible optimization.
1519  *
1520  * This tries to optimize for the empty queue case and may calls
1521  * the trigger function directly. This is the only difference compared
1522  * to tevent_queue_add_entry().
1523  *
1524  * The caller needs to be prepared that the trigger function has
1525  * already called tevent_req_notify_callback(), tevent_req_error(),
1526  * tevent_req_done() or a similar function.
1527  *
1528  * The request can be removed from the queue by calling talloc_free()
1529  * (or a similar function) on the returned queue entry.
1530  *
1531  * @param[in]  queue    The queue to add the request.
1532  *
1533  * @param[in]  ev       The event handle to use for the request.
1534  *
1535  * @param[in]  req      The tevent request to add to the queue.
1536  *
1537  * @param[in]  trigger  The function triggered by the queue when the request
1538  *                      is called. Since tevent 0.9.14 it's possible to
1539  *                      pass NULL, in order to just add a "blocker" to the
1540  *                      queue.
1541  *
1542  * @param[in]  private_data The private data passed to the trigger function.
1543  *
1544  * @return              a pointer to the tevent_queue_entry if the request
1545  *                      has been successfully added, NULL otherwise.
1546  *
1547  * @see tevent_queue_add()
1548  * @see tevent_queue_add_entry()
1549  */
1550 struct tevent_queue_entry *tevent_queue_add_optimize_empty(
1551                                         struct tevent_queue *queue,
1552                                         struct tevent_context *ev,
1553                                         struct tevent_req *req,
1554                                         tevent_queue_trigger_fn_t trigger,
1555                                         void *private_data);
1556
1557 /**
1558  * @brief Start a tevent queue.
1559  *
1560  * The queue is started by default.
1561  *
1562  * @param[in]  queue    The queue to start.
1563  */
1564 void tevent_queue_start(struct tevent_queue *queue);
1565
1566 /**
1567  * @brief Stop a tevent queue.
1568  *
1569  * The queue is started by default.
1570  *
1571  * @param[in]  queue    The queue to stop.
1572  */
1573 void tevent_queue_stop(struct tevent_queue *queue);
1574
1575 /**
1576  * @brief Get the length of the queue.
1577  *
1578  * @param[in]  queue    The queue to get the length from.
1579  *
1580  * @return              The number of elements.
1581  */
1582 size_t tevent_queue_length(struct tevent_queue *queue);
1583
1584 /**
1585  * @brief Is the tevent queue running.
1586  *
1587  * The queue is started by default.
1588  *
1589  * @param[in]  queue    The queue.
1590  *
1591  * @return              Wether the queue is running or not..
1592  */
1593 bool tevent_queue_running(struct tevent_queue *queue);
1594
1595 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
1596                                    void *private_data,
1597                                    uint32_t level,
1598                                    bool begin,
1599                                    void *stack_ptr,
1600                                    const char *location);
1601 #ifdef TEVENT_DEPRECATED
1602 #ifndef _DEPRECATED_
1603 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1604 #define _DEPRECATED_ __attribute__ ((deprecated))
1605 #else
1606 #define _DEPRECATED_
1607 #endif
1608 #endif
1609 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
1610 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
1611                                   tevent_nesting_hook hook,
1612                                   void *private_data) _DEPRECATED_;
1613 int _tevent_loop_until(struct tevent_context *ev,
1614                        bool (*finished)(void *private_data),
1615                        void *private_data,
1616                        const char *location) _DEPRECATED_;
1617 #define tevent_loop_until(ev, finished, private_data) \
1618         _tevent_loop_until(ev, finished, private_data, __location__)
1619 #endif
1620
1621 int tevent_re_initialise(struct tevent_context *ev);
1622
1623 /* @} */
1624
1625 /**
1626  * @defgroup tevent_ops The tevent operation functions
1627  * @ingroup tevent
1628  *
1629  * The following structure and registration functions are exclusively
1630  * needed for people writing and pluggin a different event engine.
1631  * There is nothing useful for normal tevent user in here.
1632  * @{
1633  */
1634
1635 struct tevent_ops {
1636         /* context init */
1637         int (*context_init)(struct tevent_context *ev);
1638
1639         /* fd_event functions */
1640         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
1641                                     TALLOC_CTX *mem_ctx,
1642                                     int fd, uint16_t flags,
1643                                     tevent_fd_handler_t handler,
1644                                     void *private_data,
1645                                     const char *handler_name,
1646                                     const char *location);
1647         void (*set_fd_close_fn)(struct tevent_fd *fde,
1648                                 tevent_fd_close_fn_t close_fn);
1649         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
1650         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
1651
1652         /* timed_event functions */
1653         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
1654                                           TALLOC_CTX *mem_ctx,
1655                                           struct timeval next_event,
1656                                           tevent_timer_handler_t handler,
1657                                           void *private_data,
1658                                           const char *handler_name,
1659                                           const char *location);
1660
1661         /* immediate event functions */
1662         void (*schedule_immediate)(struct tevent_immediate *im,
1663                                    struct tevent_context *ev,
1664                                    tevent_immediate_handler_t handler,
1665                                    void *private_data,
1666                                    const char *handler_name,
1667                                    const char *location);
1668
1669         /* signal functions */
1670         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
1671                                             TALLOC_CTX *mem_ctx,
1672                                             int signum, int sa_flags,
1673                                             tevent_signal_handler_t handler,
1674                                             void *private_data,
1675                                             const char *handler_name,
1676                                             const char *location);
1677
1678         /* loop functions */
1679         int (*loop_once)(struct tevent_context *ev, const char *location);
1680         int (*loop_wait)(struct tevent_context *ev, const char *location);
1681 };
1682
1683 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
1684
1685 /* @} */
1686
1687 /**
1688  * @defgroup tevent_compat The tevent compatibility functions
1689  * @ingroup tevent
1690  *
1691  * The following definitions are usueful only for compatibility with the
1692  * implementation originally developed within the samba4 code and will be
1693  * soon removed. Please NEVER use in new code.
1694  *
1695  * @todo Ignore it?
1696  *
1697  * @{
1698  */
1699
1700 #ifdef TEVENT_COMPAT_DEFINES
1701
1702 #define event_context   tevent_context
1703 #define event_ops       tevent_ops
1704 #define fd_event        tevent_fd
1705 #define timed_event     tevent_timer
1706 #define signal_event    tevent_signal
1707
1708 #define event_fd_handler_t      tevent_fd_handler_t
1709 #define event_timed_handler_t   tevent_timer_handler_t
1710 #define event_signal_handler_t  tevent_signal_handler_t
1711
1712 #define event_context_init(mem_ctx) \
1713         tevent_context_init(mem_ctx)
1714
1715 #define event_context_init_byname(mem_ctx, name) \
1716         tevent_context_init_byname(mem_ctx, name)
1717
1718 #define event_backend_list(mem_ctx) \
1719         tevent_backend_list(mem_ctx)
1720
1721 #define event_set_default_backend(backend) \
1722         tevent_set_default_backend(backend)
1723
1724 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1725         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1726
1727 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1728         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1729
1730 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1731         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1732
1733 #define event_loop_once(ev) \
1734         tevent_loop_once(ev)
1735
1736 #define event_loop_wait(ev) \
1737         tevent_loop_wait(ev)
1738
1739 #define event_get_fd_flags(fde) \
1740         tevent_fd_get_flags(fde)
1741
1742 #define event_set_fd_flags(fde, flags) \
1743         tevent_fd_set_flags(fde, flags)
1744
1745 #define EVENT_FD_READ           TEVENT_FD_READ
1746 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
1747
1748 #define EVENT_FD_WRITEABLE(fde) \
1749         TEVENT_FD_WRITEABLE(fde)
1750
1751 #define EVENT_FD_READABLE(fde) \
1752         TEVENT_FD_READABLE(fde)
1753
1754 #define EVENT_FD_NOT_WRITEABLE(fde) \
1755         TEVENT_FD_NOT_WRITEABLE(fde)
1756
1757 #define EVENT_FD_NOT_READABLE(fde) \
1758         TEVENT_FD_NOT_READABLE(fde)
1759
1760 #define ev_debug_level          tevent_debug_level
1761
1762 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
1763 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
1764 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
1765 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
1766
1767 #define ev_set_debug(ev, debug, context) \
1768         tevent_set_debug(ev, debug, context)
1769
1770 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1771
1772 #endif /* TEVENT_COMPAT_DEFINES */
1773
1774 /* @} */
1775
1776 #endif /* __TEVENT_H__ */