tevent: Document the tevent_queue functions.
[ira/wip.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 name it.
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 for the tevent context.
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 List available backends.
130  *
131  * @param[in]  mem_ctx  The memory context to use.
132  *
133  * @return              A string vector with a terminating NULL element, NULL
134  *                      on error.
135  */
136 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
137
138 /**
139  * @brief Set the default tevent backent.
140  *
141  * @param[in]  backend  The name of the backend to set.
142  */
143 void tevent_set_default_backend(const char *backend);
144
145 #ifdef DOXYGEN
146 /**
147  * @brief Add a file descriptor based event.
148  *
149  * @param[in]  ev       The event context to work on.
150  *
151  * @param[in]  mem_ctx  The talloc memory context to use.
152  *
153  * @param[in]  fd       The file descriptor to base the event on.
154  *
155  * @param[in]  flags    #TEVENT_FD_READ or #TEVENT_FD_WRITE
156  *
157  * @param[in]  handler  The callback handler for the event.
158  *
159  * @param[in]  private_data  The private data passed to the callback handler.
160  *
161  * @return              The file descriptor based event, NULL on error.
162  *
163  * @note To cancel the monitoring of a file descriptor, call talloc_free()
164  * on the object returned by this function.
165  */
166 struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
167                                 TALLOC_CTX *mem_ctx,
168                                 int fd,
169                                 uint16_t flags,
170                                 tevent_fd_handler_t handler,
171                                 void *private_data);
172 #else
173 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
174                                  TALLOC_CTX *mem_ctx,
175                                  int fd,
176                                  uint16_t flags,
177                                  tevent_fd_handler_t handler,
178                                  void *private_data,
179                                  const char *handler_name,
180                                  const char *location);
181 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
182         _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
183                        #handler, __location__)
184 #endif
185
186 #ifdef DOXYGEN
187 /**
188  * @brief Add a timed event
189  *
190  * @param[in]  ev       The event context to work on.
191  *
192  * @param[in]  mem_ctx  The talloc memory context to use.
193  *
194  * @param[in]  next_event  Timeval specifying the absolute time to fire this
195  * event. This is not an offset.
196  *
197  * @param[in]  handler  The callback handler for the event.
198  *
199  * @param[in]  private_data  The private data passed to the callback handler.
200  *
201  * @return The newly-created timer event, or NULL on error.
202  *
203  * @note To cancel a timer event before it fires, call talloc_free() on the
204  * event returned from this function. This event is automatically
205  * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
206  *
207  * @note Unlike some mainloops, tevent timers are one-time events. To set up
208  * a recurring event, it is necessary to call tevent_add_timer() again during
209  * the handler processing.
210  *
211  * @note Due to the internal mainloop processing, a timer set to run
212  * immediately will do so after any other pending timers fire, but before
213  * any further file descriptor or signal handling events fire. Callers should
214  * not rely on this behavior!
215  */
216 struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
217                                       TALLOC_CTX *mem_ctx,
218                                       struct timeval next_event,
219                                       tevent_timer_handler_t handler,
220                                       void *private_data);
221 #else
222 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
223                                        TALLOC_CTX *mem_ctx,
224                                        struct timeval next_event,
225                                        tevent_timer_handler_t handler,
226                                        void *private_data,
227                                        const char *handler_name,
228                                        const char *location);
229 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
230         _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
231                           #handler, __location__)
232 #endif
233
234 #ifdef DOXYGEN
235 /**
236  * Initialize an immediate event object
237  *
238  * This object can be used to trigger an event to occur immediately after
239  * returning from the current event (before any other event occurs)
240  *
241  * @param[in] mem_ctx  The talloc memory context to use as the parent
242  *
243  * @return An empty tevent_immediate object. Use tevent_schedule_immediate
244  * to populate and use it.
245  *
246  * @note Available as of tevent 0.9.8
247  */
248 struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
249 #else
250 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
251                                                   const char *location);
252 #define tevent_create_immediate(mem_ctx) \
253         _tevent_create_immediate(mem_ctx, __location__)
254 #endif
255
256 #ifdef DOXYGEN
257
258 /**
259  * Schedule an event for immediate execution. This event will occur
260  * immediately after returning from the current event (before any other
261  * event occurs)
262  *
263  * @param[in] im       The tevent_immediate object to populate and use
264  * @param[in] ctx      The tevent_context to run this event
265  * @param[in] handler  The event handler to run when this event fires
266  * @param[in] private_data  Data to pass to the event handler
267  */
268 void tevent_schedule_immediate(struct tevent_immediate *im,
269                 struct tevent_context *ctx,
270                 tevent_immediate_handler_t handler,
271                 void *private_data);
272 #else
273 void _tevent_schedule_immediate(struct tevent_immediate *im,
274                                 struct tevent_context *ctx,
275                                 tevent_immediate_handler_t handler,
276                                 void *private_data,
277                                 const char *handler_name,
278                                 const char *location);
279 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
280         _tevent_schedule_immediate(im, ctx, handler, private_data, \
281                                    #handler, __location__);
282 #endif
283
284 #ifdef DOXYGEN
285 /**
286  * @brief Add a tevent signal handler
287  *
288  * tevent_add_signal() creates a new event for handling a signal the next
289  * time through the mainloop. It implements a very simple traditional signal
290  * handler whose only purpose is to add the handler event into the mainloop.
291  *
292  * @param[in]  ev       The event context to work on.
293  *
294  * @param[in]  mem_ctx  The talloc memory context to use.
295  *
296  * @param[in]  signum   The signal to trap
297  *
298  * @param[in]  handler  The callback handler for the signal.
299  *
300  * @param[in]  sa_flags sigaction flags for this signal handler.
301  *
302  * @param[in]  private_data  The private data passed to the callback handler.
303  *
304  * @return The newly-created signal handler event, or NULL on error.
305  *
306  * @note To cancel a signal handler, call talloc_free() on the event returned
307  * from this function.
308  */
309 struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
310                      TALLOC_CTX *mem_ctx,
311                      int signum,
312                      int sa_flags,
313                      tevent_signal_handler_t handler,
314                      void *private_data);
315 #else
316 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
317                                          TALLOC_CTX *mem_ctx,
318                                          int signum,
319                                          int sa_flags,
320                                          tevent_signal_handler_t handler,
321                                          void *private_data,
322                                          const char *handler_name,
323                                          const char *location);
324 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
325         _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
326                            #handler, __location__)
327 #endif
328
329 #ifdef DOXYGEN
330 /**
331  * @brief Pass a single time through the mainloop
332  *
333  * This will process any appropriate signal, immediate, fd and timer events
334  *
335  * @param[in]  ev The event context to process
336  *
337  * @return Zero on success, nonzero if an internal error occurred
338  */
339 int tevent_loop_once(struct tevent_context *ev);
340 #else
341 int _tevent_loop_once(struct tevent_context *ev, const char *location);
342 #define tevent_loop_once(ev) \
343         _tevent_loop_once(ev, __location__)
344 #endif
345
346 #ifdef DOXYGEN
347 /**
348  * @brief Run the mainloop
349  *
350  * The mainloop will run until there are no events remaining to be processed
351  *
352  * @param[in]  ev The event context to process
353  *
354  * @return Zero if all events have been processed. Nonzero if an internal
355  * error occurred.
356  */
357 int tevent_loop_wait(struct tevent_context *ev);
358 #else
359 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
360 #define tevent_loop_wait(ev) \
361         _tevent_loop_wait(ev, __location__)
362 #endif
363
364
365 /**
366  * Assign a function to run when a tevent_fd is freed
367  *
368  * This function is a destructor for the tevent_fd. It does not automatically
369  * close the file descriptor. If this is the desired behavior, then it must be
370  * performed by the close_fn.
371  *
372  * @param[in] fde       File descriptor event on which to set the destructor
373  * @param[in] close_fn  Destructor to execute when fde is freed
374  */
375 void tevent_fd_set_close_fn(struct tevent_fd *fde,
376                             tevent_fd_close_fn_t close_fn);
377
378 /**
379  * Automatically close the file descriptor when the tevent_fd is freed
380  *
381  * This function calls close(fd) internally.
382  *
383  * @param[in] fde  File descriptor event to auto-close
384  */
385 void tevent_fd_set_auto_close(struct tevent_fd *fde);
386
387 /**
388  * Return the flags set on this file descriptor event
389  *
390  * @param[in] fde  File descriptor event to query
391  *
392  * @return The flags set on the event. See #TEVENT_FD_READ and
393  * #TEVENT_FD_WRITE
394  */
395 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
396
397 /**
398  * Set flags on a file descriptor event
399  *
400  * @param[in] fde    File descriptor event to set
401  * @param[in] flags  Flags to set on the event. See #TEVENT_FD_READ and
402  * #TEVENT_FD_WRITE
403  */
404 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
405
406 /**
407  * Query whether tevent supports signal handling
408  *
409  * @param[in] ev  An initialized tevent context
410  *
411  * @return True if this platform and tevent context support signal handling
412  */
413 bool tevent_signal_support(struct tevent_context *ev);
414
415 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
416
417 /* bits for file descriptor event flags */
418
419 /**
420  * Monitor a file descriptor for write availability
421  */
422 #define TEVENT_FD_READ 1
423 /**
424  * Monitor a file descriptor for data to be read
425  */
426 #define TEVENT_FD_WRITE 2
427
428 /**
429  * Convenience function for declaring a tevent_fd writable
430  */
431 #define TEVENT_FD_WRITEABLE(fde) \
432         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
433
434 /**
435  * Convenience function for declaring a tevent_fd readable
436  */
437 #define TEVENT_FD_READABLE(fde) \
438         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
439
440 /**
441  * Convenience function for declaring a tevent_fd non-writable
442  */
443 #define TEVENT_FD_NOT_WRITEABLE(fde) \
444         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
445
446 /**
447  * Convenience function for declaring a tevent_fd non-readable
448  */
449 #define TEVENT_FD_NOT_READABLE(fde) \
450         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
451
452 /**
453  * Debug level of tevent
454  */
455 enum tevent_debug_level {
456         TEVENT_DEBUG_FATAL,
457         TEVENT_DEBUG_ERROR,
458         TEVENT_DEBUG_WARNING,
459         TEVENT_DEBUG_TRACE
460 };
461
462 /**
463  * @brief The tevent debug callbac.
464  *
465  * @param[in]  context  The memory context to use.
466  *
467  * @param[in]  level    The debug level.
468  *
469  * @param[in]  fmt      The format string.
470  *
471  * @param[in]  ap       The arguments for the format string.
472  */
473 typedef void (*tevent_debug_fn)(void *context,
474                                 enum tevent_debug_level level,
475                                 const char *fmt,
476                                 va_list ap) PRINTF_ATTRIBUTE(3,0);
477
478 /**
479  * Set destination for tevent debug messages
480  *
481  * @param[in] ev        Event context to debug
482  * @param[in] debug     Function to handle output printing
483  * @param[in] context   The context to pass to the debug function.
484  *
485  * @return Always returns 0 as of version 0.9.8
486  *
487  * @note Default is to emit no debug messages
488  */
489 int tevent_set_debug(struct tevent_context *ev,
490                      tevent_debug_fn debug,
491                      void *context);
492
493 /**
494  * Designate stderr for debug message output
495  *
496  * @param[in] ev     Event context to debug
497  *
498  * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
499  * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
500  * function for tevent_set_debug()
501  */
502 int tevent_set_debug_stderr(struct tevent_context *ev);
503
504 /**
505  * @}
506  */
507
508 /**
509  * @defgroup tevent_request The tevent request functions.
510  * @ingroup tevent
511  *
512  * This represents an async request being processed by callbacks via an event
513  * context. A user can issue for example a write request to a socket, giving
514  * an implementation function the fd, the buffer and the number of bytes to
515  * transfer. The function issuing the request will immediately return without
516  * blocking most likely without having sent anything. The API user then fills
517  * in req->async.fn and req->async.private_data, functions that are called
518  * when the request is finished.
519  *
520  * It is up to the user of the async request to talloc_free it after it has
521  * finished. This can happen while the completion function is called.
522  *
523  * @{
524  */
525
526 /**
527  * An async request moves between the following 4 states:
528  */
529 enum tevent_req_state {
530         /**
531          * we are creating the request
532          */
533         TEVENT_REQ_INIT,
534         /**
535          * we are waiting the request to complete
536          */
537         TEVENT_REQ_IN_PROGRESS,
538         /**
539          * the request is finished
540          */
541         TEVENT_REQ_DONE,
542         /**
543          * A user error has occurred
544          */
545         TEVENT_REQ_USER_ERROR,
546         /**
547          * Request timed out
548          */
549         TEVENT_REQ_TIMED_OUT,
550         /**
551          * No memory in between
552          */
553         TEVENT_REQ_NO_MEMORY,
554         /**
555          * the request is already received by the caller
556          */
557         TEVENT_REQ_RECEIVED
558 };
559
560 /**
561  * @brief An async request
562  */
563 struct tevent_req;
564
565 /**
566  * @brief A tevent request callback function.
567  *
568  * @param[in]  req      The tevent async request which executed this callback.
569  */
570 typedef void (*tevent_req_fn)(struct tevent_req *req);
571
572 /**
573  * @brief Set an async request callback.
574  *
575  * @param[in]  req      The async request to set the callback.
576  *
577  * @param[in]  fn       The callback function to set.
578  *
579  * @param[in]  pvt      A pointer to private data to pass to the async request
580  *                      callback.
581  */
582 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
583
584 #ifdef DOXYGEN
585 /**
586  * @brief Get the private data casted to the given type for a callback from
587  *        a tevent request structure.
588  *
589  * @param[in]  req      The structure to get the callback data from.
590  *
591  * @param[in]  type     The type of the private callback data to get.
592  *
593  * @return              The type casted private data set NULL if not set.
594  */
595 void *tevent_req_callback_data(struct tevent_req *req, #type);
596 #else
597 void *_tevent_req_callback_data(struct tevent_req *req);
598 #define tevent_req_callback_data(_req, _type) \
599         talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
600 #endif
601
602 #ifdef DOXYGEN
603 /**
604  * @brief Get the private data for a callback from a tevent request structure.
605  *
606  * @param[in]  req      The structure to get the callback data from.
607  *
608  * @param[in]  req      The structure to get the data from.
609  *
610  * @return              The private data or NULL if not set.
611  */
612 void *tevent_req_callback_data_void(struct tevent_req *req);
613 #else
614 #define tevent_req_callback_data_void(_req) \
615         _tevent_req_callback_data(_req)
616 #endif
617
618 #ifdef DOXYGEN
619 /**
620  * @brief Get the private data from a tevent request structure.
621  *
622  * @param[in]  req      The structure to get the private data from.
623  *
624  * @return              The private data or NULL if not set.
625  */
626 void *tevent_req_data(struct tevent_req *req);
627 #else
628 void *_tevent_req_data(struct tevent_req *req);
629 #define tevent_req_data(_req, _type) \
630         talloc_get_type_abort(_tevent_req_data(_req), _type)
631 #endif
632
633 /**
634  * @brief The print function which can be set for a tevent async request.
635  *
636  * @param[in]  req      The tevent async request.
637  *
638  * @param[in]  ctx      A talloc memory context which can be uses to allocate
639  *                      memory.
640  *
641  * @return              An allocated string buffer to print.
642  *
643  * Example:
644  * @code
645  *   static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
646  *   {
647  *     struct my_data *data = tevent_req_data(req, struct my_data);
648  *     char *result;
649  *
650  *     result = tevent_req_default_print(mem_ctx, req);
651  *     if (result == NULL) {
652  *       return NULL;
653  *     }
654  *
655  *     return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
656  *       data->foo, data->bar);
657  *   }
658  * @endcode
659  */
660 typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
661
662 /**
663  * @brief This function sets a print function for the given request.
664  *
665  * This function can be used to setup a print function for the given request.
666  * This will be triggered if the tevent_req_print() function was
667  * called on the given request.
668  *
669  * @param[in]  req      The request to use.
670  *
671  * @param[in]  fn       A pointer to the print function
672  *
673  * @note This function should only be used for debugging.
674  */
675 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
676
677 /**
678  * @brief The default print function for creating debug messages.
679  *
680  * The function should not be used by users of the async API,
681  * but custom print function can use it and append custom text
682  * to the string.
683  *
684  * @param[in]  req      The request to be printed.
685  *
686  * @param[in]  mem_ctx  The memory context for the result.
687  *
688  * @return              Text representation of request.
689  *
690  */
691 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
692
693 /**
694  * @brief Print an tevent_req structure in debug messages.
695  *
696  * This function should be used by callers of the async API.
697  *
698  * @param[in]  mem_ctx  The memory context for the result.
699  *
700  * @param[in] req       The request to be printed.
701  *
702  * @return              Text representation of request.
703  */
704 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
705
706 /**
707  * @brief A typedef for a cancel function for a tevent request.
708  *
709  * @param[in]  req      The tevent request calling this function.
710  *
711  * @return              True if the request could be canceled, false if not.
712  */
713 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
714
715 /**
716  * @brief This function sets a cancel function for the given tevent request.
717  *
718  * This function can be used to setup a cancel function for the given request.
719  * This will be triggered if the tevent_req_cancel() function was
720  * called on the given request.
721  *
722  * @param[in]  req      The request to use.
723  *
724  * @param[in]  fn       A pointer to the cancel function.
725  */
726 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
727
728 #ifdef DOXYGEN
729 /**
730  * @brief Try to cancel the given tevent request.
731  *
732  * This function can be used to cancel the given request.
733  *
734  * It is only possible to cancel a request when the implementation
735  * has registered a cancel function via the tevent_req_set_cancel_fn().
736  *
737  * @param[in]  req      The request to use.
738  *
739  * @return              This function returns true is the request is cancelable,
740  *                      othererwise false is returned.
741  *
742  * @note Even if the function returns true, the caller need to wait
743  *       for the function to complete normally.
744  *       Only the _recv() function of the given request indicates
745  *       if the request was really canceled.
746  */
747 bool tevent_req_cancel(struct tevent_req *req);
748 #else
749 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
750 #define tevent_req_cancel(req) \
751         _tevent_req_cancel(req, __location__)
752 #endif
753
754 #ifdef DOXYGEN
755 /**
756  * @brief Create an async tevent request.
757  *
758  * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS.
759  *
760  * @param[in] mem_ctx   The memory context for the result.
761  *
762  * @param[in] pstate    The private state of the request.
763  *
764  * @param[in] state_size  The size of the private state of the request.
765  *
766  * @param[in] type      The name of the request.
767  *
768  * @return              A new async request. NULL on error.
769  */
770 struct tevent_req *tevent_req_create(TALLOC_CTX *mem_ctx,
771                                       void *pstate,
772                                       size_t state_size,
773                                       const char *type);
774 #else
775 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
776                                       void *pstate,
777                                       size_t state_size,
778                                       const char *type,
779                                       const char *location);
780
781 #define tevent_req_create(_mem_ctx, _pstate, _type) \
782         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
783                            #_type, __location__)
784 #endif
785
786 /**
787  * @brief Set a timeout for an async request.
788  *
789  * @param[in]  req      The request to set the timeout for.
790  *
791  * @param[in]  ev       The event context to use for the timer.
792  *
793  * @param[in]  endtime  The endtime of the request.
794  *
795  * @return              True if succeeded, false if not.
796  */
797 bool tevent_req_set_endtime(struct tevent_req *req,
798                             struct tevent_context *ev,
799                             struct timeval endtime);
800
801 #ifdef DOXYGEN
802 /**
803  * @brief Call the notify callback of the given tevent request manually.
804  *
805  * @param[in]  req      The tevent request to call the notify function from.
806  *
807  * @see tevent_req_set_callback()
808  */
809 void tevent_req_notify_callback(struct tevent_req *req);
810 #else
811 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
812 #define tevent_req_notify_callback(req)         \
813         _tevent_req_notify_callback(req, __location__)
814 #endif
815
816 #ifdef DOXYGEN
817 /**
818  * @brief An async request has successfully finished.
819  *
820  * This function is to be used by implementors of async requests. When a
821  * request is successfully finished, this function calls the user's completion
822  * function.
823  *
824  * @param[in]  req       The finished request.
825  */
826 void tevent_req_done(struct tevent_req *req);
827 #else
828 void _tevent_req_done(struct tevent_req *req,
829                       const char *location);
830 #define tevent_req_done(req) \
831         _tevent_req_done(req, __location__)
832 #endif
833
834 #ifdef DOXYGEN
835 /**
836  * @brief An async request has seen an error.
837  *
838  * This function is to be used by implementors of async requests. When a
839  * request can not successfully completed, the implementation should call this
840  * function with the appropriate status code.
841  *
842  * If error is 0 the function returns false and does nothing more.
843  *
844  * @param[in]  req      The request with an error.
845  *
846  * @param[in]  error    The error code.
847  *
848  * @return              On success true is returned, false if error is 0.
849  *
850  * @code
851  * int error = first_function();
852  * if (tevent_req_error(req, error)) {
853  *      return;
854  * }
855  *
856  * error = second_function();
857  * if (tevent_req_error(req, error)) {
858  *      return;
859  * }
860  *
861  * tevent_req_done(req);
862  * return;
863  * @endcode
864  */
865 bool tevent_req_error(struct tevent_req *req,
866                       uint64_t error);
867 #else
868 bool _tevent_req_error(struct tevent_req *req,
869                        uint64_t error,
870                        const char *location);
871 #define tevent_req_error(req, error) \
872         _tevent_req_error(req, error, __location__)
873 #endif
874
875 #ifdef DOXYGEN
876 /**
877  * @brief Helper function for nomem check.
878  *
879  * Convenience helper to easily check alloc failure within a callback
880  * implementing the next step of an async request.
881  *
882  * @param[in]  p        The pointer to be checked.
883  *
884  * @param[in]  req      The request being processed.
885  *
886  * @code
887  * p = talloc(mem_ctx, bla);
888  * if (tevent_req_nomem(p, req)) {
889  *      return;
890  * }
891  * @endcode
892  */
893 bool tevent_req_nomem(const void *p,
894                       struct tevent_req *req);
895 #else
896 bool _tevent_req_nomem(const void *p,
897                        struct tevent_req *req,
898                        const char *location);
899 #define tevent_req_nomem(p, req) \
900         _tevent_req_nomem(p, req, __location__)
901 #endif
902
903 /**
904  * @brief Finish a request before the caller had the change to set the callback.
905  *
906  * An implementation of an async request might find that it can either finish
907  * the request without waiting for an external event, or it can't even start
908  * the engine. To present the illusion of a callback to the user of the API,
909  * the implementation can call this helper function which triggers an
910  * immediate timed event. This way the caller can use the same calling
911  * conventions, independent of whether the request was actually deferred.
912  *
913  * @param[in]  req      The finished request.
914  *
915  * @param[in]  ev       The tevent_context for the timed event.
916  *
917  * @return              The given request will be returned.
918  */
919 struct tevent_req *tevent_req_post(struct tevent_req *req,
920                                    struct tevent_context *ev);
921
922 /**
923  * @brief Check if the given request is still in progress.
924  *
925  * It is typically used by sync wrapper functions.
926  *
927  * This function destroys the attached private data.
928  *
929  * @param[in]  req      The request to poll.
930  *
931  * @return              The boolean form of "is in progress".
932  */
933 bool tevent_req_is_in_progress(struct tevent_req *req);
934
935 /**
936  * @brief Actively poll for the given request to finish.
937  *
938  * This function is typically used by sync wrapper functions.
939  *
940  * @param[in]  req      The request to poll.
941  *
942  * @param[in]  ev       The tevent_context to be used.
943  *
944  * @return              On success true is returned. If a critical error has
945  *                      happened in the tevent loop layer false is returned.
946  *                      This is not the return value of the given request!
947  *
948  * @note This should only be used if the given tevent context was created by the
949  * caller, to avoid event loop nesting.
950  *
951  * @code
952  * req = tstream_writev_queue_send(mem_ctx,
953  *                                 ev_ctx,
954  *                                 tstream,
955  *                                 send_queue,
956  *                                 iov, 2);
957  * ok = tevent_req_poll(req, tctx->ev);
958  * rc = tstream_writev_queue_recv(req, &sys_errno);
959  * TALLOC_FREE(req);
960  * @endcode
961  */
962 bool tevent_req_poll(struct tevent_req *req,
963                      struct tevent_context *ev);
964
965 /**
966  * @brief Get the tevent request and the actual error code you've set.
967  *
968  * @param[in]  req      The tevent request to get the error from.
969  *
970  * @param[out] state    A pointer to store the tevent request error state.
971  *
972  * @param[out] error    A pointer to store the error set by tevent_req_error().
973  *
974  * @return              True if the function could set error and state, false
975  *                      otherwise.
976  *
977  * @see tevent_req_error()
978  */
979 bool tevent_req_is_error(struct tevent_req *req,
980                          enum tevent_req_state *state,
981                          uint64_t *error);
982
983 /**
984  * @brief Use as the last action of a _recv() function.
985  *
986  * This function destroys the attached private data.
987  *
988  * @param[in]  req      The finished request.
989  */
990 void tevent_req_received(struct tevent_req *req);
991
992 /**
993  * @brief Create a tevent subrequest at a given time.
994  *
995  * The idea is that always the same syntax for tevent requests.
996  *
997  * @param[in]  mem_ctx  The talloc memory context to use.
998  *
999  * @param[in]  ev       The event handle to setup the request.
1000  *
1001  * @param[in]  wakeup_time The time to wakeup and execute the request.
1002  *
1003  * @return              The new subrequest, NULL on error.
1004  *
1005  * Example:
1006  * @code
1007  *   static my_callback_wakeup_done(tevent_req *req)
1008  *   {
1009  *     struct tevent_req *req = tevent_req_callback_data(subreq,
1010  *                              struct tevent_req);
1011  *     bool ok;
1012  *
1013  *     ok = tevent_wakeup_recv(subreq);
1014  *     TALLOC_FREE(subreq);
1015  *     if (!ok) {
1016  *         tevent_req_error(req, -1);
1017  *         return;
1018  *     }
1019  *     ...
1020  *   }
1021  * @endcode
1022  *
1023  * @code
1024  *   subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1025  *   if (tevent_req_nomem(subreq, req)) {
1026  *     return false;
1027  *   }
1028  *   tevent_set_callback(subreq, my_callback_wakeup_done, req);
1029  * @endcode
1030  *
1031  * @see tevent_wakeup_recv()
1032  */
1033 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
1034                                       struct tevent_context *ev,
1035                                       struct timeval wakeup_time);
1036
1037 /**
1038  * @brief Check if the wakeup has been correctly executed.
1039  *
1040  * This function needs to be called in the callback function set after calling
1041  * tevent_wakeup_send().
1042  *
1043  * @param[in]  req      The tevent request to check.
1044  *
1045  * @return              True on success, false otherwise.
1046  *
1047  * @see tevent_wakeup_recv()
1048  */
1049 bool tevent_wakeup_recv(struct tevent_req *req);
1050
1051 /* @} */
1052
1053 /**
1054  * @defgroup tevent_helpers The tevent helper functiions
1055  * @ingroup tevent
1056  *
1057  * @todo description
1058  *
1059  * @{
1060  */
1061
1062 /**
1063  * @brief Compare two timeval values.
1064  *
1065  * @param[in]  tv1      The first timeval value to compare.
1066  *
1067  * @param[in]  tv2      The second timeval value to compare.
1068  *
1069  * @return              0 if they are equal.
1070  *                      1 if the first time is greater than the second.
1071  *                      -1 if the first time is smaller than the second.
1072  */
1073 int tevent_timeval_compare(const struct timeval *tv1,
1074                            const struct timeval *tv2);
1075
1076 /**
1077  * @brief Get a zero timval value.
1078  *
1079  * @return              A zero timval value.
1080  */
1081 struct timeval tevent_timeval_zero(void);
1082
1083 /**
1084  * @brief Get a timeval value for the current time.
1085  *
1086  * @return              A timval value with the current time.
1087  */
1088 struct timeval tevent_timeval_current(void);
1089
1090 /**
1091  * @brief Get a timeval structure with the given values.
1092  *
1093  * @param[in]  secs     The seconds to set.
1094  *
1095  * @param[in]  usecs    The milliseconds to set.
1096  *
1097  * @return              A timeval structure with the given values.
1098  */
1099 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
1100
1101 /**
1102  * @brief Get the difference between two timeval values.
1103  *
1104  * @param[in]  tv1      The first timeval.
1105  *
1106  * @param[in]  tv2      The second timeval.
1107  *
1108  * @return              A timeval structure with the difference between the
1109  *                      first and the second value.
1110  */
1111 struct timeval tevent_timeval_until(const struct timeval *tv1,
1112                                     const struct timeval *tv2);
1113
1114 /**
1115  * @brief Check if a given timeval structure is zero.
1116  *
1117  * @param[in]  tv       The timeval to check if it is zero.
1118  *
1119  * @return              True if it is zero, false otherwise.
1120  */
1121 bool tevent_timeval_is_zero(const struct timeval *tv);
1122
1123 /**
1124  * @brief Add the given amount of time to a timeval structure.
1125  *
1126  * @param[in]  tv        The timeval structure to add the time.
1127  *
1128  * @param[in]  secs      The seconds to add to the timeval.
1129  *
1130  * @param[in]  usecs     The milliseconds to add to the timeval.
1131  *
1132  * @return               The timeval structure with the new time.
1133  */
1134 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
1135                                   uint32_t usecs);
1136
1137 /**
1138  * @brief Get a timeval in the future with a specified offset from now.
1139  *
1140  * @param[in]  secs     The seconds of the offset from now.
1141  *
1142  * @param[in]  usecs    The milliseconds of the offset from now.
1143  *
1144  * @return              A timval with the given offset in the future.
1145  */
1146 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
1147
1148 /* @} */
1149
1150
1151 /**
1152  * @defgroup tevent_queue The tevent queue functions
1153  * @ingroup tevent
1154  *
1155  * @{
1156  */
1157
1158 struct tevent_queue;
1159
1160 #ifdef DOXYGEN
1161 /**
1162  * @brief Create and start a tevent queue.
1163  *
1164  * @param[in]  mem_ctx  The talloc memory context to allocate the queue.
1165  *
1166  * @param[in]  name     The name to use to identify the queue.
1167  *
1168  * @return              An allocated tevent queue on success, NULL on error.
1169  *
1170  * @see tevent_start()
1171  * @see tevent_stop()
1172  */
1173 struct tevent_queue *tevent_queue_create(TALLOC_CTX *mem_ctx,
1174                                          const char *name);
1175 #else
1176 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
1177                                           const char *name,
1178                                           const char *location);
1179
1180 #define tevent_queue_create(_mem_ctx, _name) \
1181         _tevent_queue_create((_mem_ctx), (_name), __location__)
1182 #endif
1183
1184 /**
1185  * @brief A callback trigger function run by the queue.
1186  *
1187  * @param[in]  req      The tevent request the trigger function is executed on.
1188  *
1189  * @param[in]  private_data The private data pointer specified by
1190  *                          tevent_queue_add().
1191  *
1192  * @see tevent_queue_add()
1193  */
1194 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
1195                                           void *private_data);
1196
1197 /**
1198  * @brief Add a tevent request to the queue.
1199  *
1200  * @param[in]  queue    The queue to add the request.
1201  *
1202  * @param[in]  ev       The event handle to use for the request.
1203  *
1204  * @param[in]  req      The tevent request to add to the queue.
1205  *
1206  * @param[in]  trigger  The function triggered by the queue when the request
1207  *                      is called.
1208  *
1209  * @param[in]  private_data The private data passed to the trigger function.
1210  *
1211  * @return              True if the request has been successfully added, false
1212  *                      otherwise.
1213  */
1214 bool tevent_queue_add(struct tevent_queue *queue,
1215                       struct tevent_context *ev,
1216                       struct tevent_req *req,
1217                       tevent_queue_trigger_fn_t trigger,
1218                       void *private_data);
1219
1220 /**
1221  * @brief Start a tevent queue.
1222  *
1223  * The queue is started by default.
1224  *
1225  * @param[in]  queue    The queue to start.
1226  */
1227 void tevent_queue_start(struct tevent_queue *queue);
1228
1229 /**
1230  * @brief Stop a tevent queue.
1231  *
1232  * The queue is started by default.
1233  *
1234  * @param[in]  queue    The queue to stop.
1235  */
1236 void tevent_queue_stop(struct tevent_queue *queue);
1237
1238 /**
1239  * @brief Get the length of the queue.
1240  *
1241  * @param[in]  queue    The queue to get the length from.
1242  *
1243  * @return              The number of elements.
1244  */
1245 size_t tevent_queue_length(struct tevent_queue *queue);
1246
1247 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
1248                                    void *private_data,
1249                                    uint32_t level,
1250                                    bool begin,
1251                                    void *stack_ptr,
1252                                    const char *location);
1253 #ifdef TEVENT_DEPRECATED
1254 #ifndef _DEPRECATED_
1255 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1256 #define _DEPRECATED_ __attribute__ ((deprecated))
1257 #else
1258 #define _DEPRECATED_
1259 #endif
1260 #endif
1261 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
1262 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
1263                                   tevent_nesting_hook hook,
1264                                   void *private_data) _DEPRECATED_;
1265 int _tevent_loop_until(struct tevent_context *ev,
1266                        bool (*finished)(void *private_data),
1267                        void *private_data,
1268                        const char *location) _DEPRECATED_;
1269 #define tevent_loop_until(ev, finished, private_data) \
1270         _tevent_loop_until(ev, finished, private_data, __location__)
1271 #endif
1272
1273 int tevent_re_initialise(struct tevent_context *ev);
1274
1275 /* @} */
1276
1277 /**
1278  * @defgroup tevent_ops The tevent operation functions
1279  * @ingroup tevent
1280  *
1281  * The following structure and registration functions are exclusively
1282  * needed for people writing and pluggin a different event engine.
1283  * There is nothing useful for normal tevent user in here.
1284  * @{
1285  */
1286
1287 struct tevent_ops {
1288         /* context init */
1289         int (*context_init)(struct tevent_context *ev);
1290
1291         /* fd_event functions */
1292         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
1293                                     TALLOC_CTX *mem_ctx,
1294                                     int fd, uint16_t flags,
1295                                     tevent_fd_handler_t handler,
1296                                     void *private_data,
1297                                     const char *handler_name,
1298                                     const char *location);
1299         void (*set_fd_close_fn)(struct tevent_fd *fde,
1300                                 tevent_fd_close_fn_t close_fn);
1301         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
1302         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
1303
1304         /* timed_event functions */
1305         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
1306                                           TALLOC_CTX *mem_ctx,
1307                                           struct timeval next_event,
1308                                           tevent_timer_handler_t handler,
1309                                           void *private_data,
1310                                           const char *handler_name,
1311                                           const char *location);
1312
1313         /* immediate event functions */
1314         void (*schedule_immediate)(struct tevent_immediate *im,
1315                                    struct tevent_context *ev,
1316                                    tevent_immediate_handler_t handler,
1317                                    void *private_data,
1318                                    const char *handler_name,
1319                                    const char *location);
1320
1321         /* signal functions */
1322         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
1323                                             TALLOC_CTX *mem_ctx,
1324                                             int signum, int sa_flags,
1325                                             tevent_signal_handler_t handler,
1326                                             void *private_data,
1327                                             const char *handler_name,
1328                                             const char *location);
1329
1330         /* loop functions */
1331         int (*loop_once)(struct tevent_context *ev, const char *location);
1332         int (*loop_wait)(struct tevent_context *ev, const char *location);
1333 };
1334
1335 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
1336
1337 /* @} */
1338
1339 /**
1340  * @defgroup tevent_compat The tevent compatibility functions
1341  * @ingroup tevent
1342  *
1343  * The following definitions are usueful only for compatibility with the
1344  * implementation originally developed within the samba4 code and will be
1345  * soon removed. Please NEVER use in new code.
1346  *
1347  * @todo Ignore it?
1348  *
1349  * @{
1350  */
1351
1352 #ifdef TEVENT_COMPAT_DEFINES
1353
1354 #define event_context   tevent_context
1355 #define event_ops       tevent_ops
1356 #define fd_event        tevent_fd
1357 #define timed_event     tevent_timer
1358 #define signal_event    tevent_signal
1359
1360 #define event_fd_handler_t      tevent_fd_handler_t
1361 #define event_timed_handler_t   tevent_timer_handler_t
1362 #define event_signal_handler_t  tevent_signal_handler_t
1363
1364 #define event_context_init(mem_ctx) \
1365         tevent_context_init(mem_ctx)
1366
1367 #define event_context_init_byname(mem_ctx, name) \
1368         tevent_context_init_byname(mem_ctx, name)
1369
1370 #define event_backend_list(mem_ctx) \
1371         tevent_backend_list(mem_ctx)
1372
1373 #define event_set_default_backend(backend) \
1374         tevent_set_default_backend(backend)
1375
1376 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1377         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1378
1379 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1380         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1381
1382 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1383         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1384
1385 #define event_loop_once(ev) \
1386         tevent_loop_once(ev)
1387
1388 #define event_loop_wait(ev) \
1389         tevent_loop_wait(ev)
1390
1391 #define event_get_fd_flags(fde) \
1392         tevent_fd_get_flags(fde)
1393
1394 #define event_set_fd_flags(fde, flags) \
1395         tevent_fd_set_flags(fde, flags)
1396
1397 #define EVENT_FD_READ           TEVENT_FD_READ
1398 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
1399
1400 #define EVENT_FD_WRITEABLE(fde) \
1401         TEVENT_FD_WRITEABLE(fde)
1402
1403 #define EVENT_FD_READABLE(fde) \
1404         TEVENT_FD_READABLE(fde)
1405
1406 #define EVENT_FD_NOT_WRITEABLE(fde) \
1407         TEVENT_FD_NOT_WRITEABLE(fde)
1408
1409 #define EVENT_FD_NOT_READABLE(fde) \
1410         TEVENT_FD_NOT_READABLE(fde)
1411
1412 #define ev_debug_level          tevent_debug_level
1413
1414 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
1415 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
1416 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
1417 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
1418
1419 #define ev_set_debug(ev, debug, context) \
1420         tevent_set_debug(ev, debug, context)
1421
1422 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1423
1424 #endif /* TEVENT_COMPAT_DEFINES */
1425
1426 /* @} */
1427
1428 #endif /* __TEVENT_H__ */