tevent: Flow: store cleanup function name in tevent_req
[Samba.git] / lib / tevent / tevent.h
blob93c0983489221e68112cf43668d5cef4fcf8a07a
1 /*
2 Unix SMB/CIFS implementation.
4 generalised event loop handling
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2005-2009
8 Copyright (C) Volker Lendecke 2008
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
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.
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.
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/>.
28 #ifndef __TEVENT_H__
29 #define __TEVENT_H__
31 #include <stdint.h>
32 #include <talloc.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <stdbool.h>
37 /* for old gcc releases that don't have the feature test macro __has_attribute */
38 #ifndef __has_attribute
39 #define __has_attribute(x) 0
40 #endif
42 #ifdef TEVENT_DEPRECATED
43 #ifndef _DEPRECATED_
44 #if __has_attribute(deprecated) || (__GNUC__ >= 3)
45 #define _DEPRECATED_ __attribute__ ((deprecated))
46 #else
47 #define _DEPRECATED_
48 #endif
49 #endif
50 #endif
52 struct tevent_context;
53 struct tevent_ops;
54 struct tevent_fd;
55 struct tevent_timer;
56 struct tevent_immediate;
57 struct tevent_signal;
58 struct tevent_thread_proxy;
59 struct tevent_threaded_context;
61 /**
62 * @defgroup tevent The tevent API
64 * The tevent low-level API
66 * This API provides the public interface to manage events in the tevent
67 * mainloop. Functions are provided for managing low-level events such
68 * as timer events, fd events and signal handling.
70 * @{
73 /* event handler types */
74 /**
75 * Called when a file descriptor monitored by tevent has
76 * data to be read or written on it.
78 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
79 struct tevent_fd *fde,
80 uint16_t flags,
81 void *private_data);
83 /**
84 * Called when tevent is ceasing the monitoring of a file descriptor.
86 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
87 struct tevent_fd *fde,
88 int fd,
89 void *private_data);
91 /**
92 * Called when a tevent timer has fired.
94 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
95 struct tevent_timer *te,
96 struct timeval current_time,
97 void *private_data);
99 /**
100 * Called when a tevent immediate event is invoked.
102 typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx,
103 struct tevent_immediate *im,
104 void *private_data);
107 * Called after tevent detects the specified signal.
109 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
110 struct tevent_signal *se,
111 int signum,
112 int count,
113 void *siginfo,
114 void *private_data);
117 * @brief Create a event_context structure.
119 * This must be the first events call, and all subsequent calls pass this
120 * event_context as the first element. Event handlers also receive this as
121 * their first argument.
123 * @param[in] mem_ctx The memory context to use.
125 * @return An allocated tevent context, NULL on error.
127 * @see tevent_context_init()
129 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
132 * @brief Create a event_context structure and select a specific backend.
134 * This must be the first events call, and all subsequent calls pass this
135 * event_context as the first element. Event handlers also receive this as
136 * their first argument.
138 * @param[in] mem_ctx The memory context to use.
140 * @param[in] name The name of the backend to use.
142 * @return An allocated tevent context, NULL on error.
144 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
147 * @brief Create a custom event context
149 * @param[in] mem_ctx The memory context to use.
150 * @param[in] ops The function pointer table of the backend.
151 * @param[in] additional_data The additional/private data to this instance
153 * @return An allocated tevent context, NULL on error.
156 struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
157 const struct tevent_ops *ops,
158 void *additional_data);
161 * @brief List available backends.
163 * @param[in] mem_ctx The memory context to use.
165 * @return A string vector with a terminating NULL element, NULL
166 * on error.
168 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
171 * @brief Set the default tevent backend.
173 * @param[in] backend The name of the backend to set.
175 void tevent_set_default_backend(const char *backend);
177 #ifdef DOXYGEN
179 * @brief Add a file descriptor based event.
181 * @param[in] ev The event context to work on.
183 * @param[in] mem_ctx The talloc memory context to use.
185 * @param[in] fd The file descriptor to base the event on.
187 * @param[in] flags #TEVENT_FD_READ or #TEVENT_FD_WRITE
189 * @param[in] handler The callback handler for the event.
191 * @param[in] private_data The private data passed to the callback handler.
193 * @return The file descriptor based event, NULL on error.
195 * @note To cancel the monitoring of a file descriptor, call talloc_free()
196 * on the object returned by this function.
198 * @note The caller should avoid closing the file descriptor before
199 * calling talloc_free()! Otherwise the behaviour is undefined which
200 * might result in crashes. See https://bugzilla.samba.org/show_bug.cgi?id=11141
201 * for an example.
203 struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
204 TALLOC_CTX *mem_ctx,
205 int fd,
206 uint16_t flags,
207 tevent_fd_handler_t handler,
208 void *private_data);
209 #else
210 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
211 TALLOC_CTX *mem_ctx,
212 int fd,
213 uint16_t flags,
214 tevent_fd_handler_t handler,
215 void *private_data,
216 const char *handler_name,
217 const char *location);
218 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
219 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
220 #handler, __location__)
221 #endif
224 * @brief Associate a custom tag with the event.
226 * This tag can be then retrieved with tevent_fd_get_tag()
228 * @param[in] fde The file descriptor event.
230 * @param[in] tag Custom tag.
232 void tevent_fd_set_tag(struct tevent_fd *fde, uint64_t tag);
235 * @brief Get custom event tag.
237 uint64_t tevent_fd_get_tag(const struct tevent_fd *fde);
239 #ifdef DOXYGEN
241 * @brief Add a timed event
243 * @param[in] ev The event context to work on.
245 * @param[in] mem_ctx The talloc memory context to use.
247 * @param[in] next_event Timeval specifying the absolute time to fire this
248 * event. This is not an offset.
250 * @param[in] handler The callback handler for the event.
252 * @param[in] private_data The private data passed to the callback handler.
254 * @return The newly-created timer event, or NULL on error.
256 * @note To cancel a timer event before it fires, call talloc_free() on the
257 * event returned from this function. This event is automatically
258 * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
260 * @note Unlike some mainloops, tevent timers are one-time events. To set up
261 * a recurring event, it is necessary to call tevent_add_timer() again during
262 * the handler processing.
264 * @note Due to the internal mainloop processing, a timer set to run
265 * immediately will do so after any other pending timers fire, but before
266 * any further file descriptor or signal handling events fire. Callers should
267 * not rely on this behavior!
269 struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
270 TALLOC_CTX *mem_ctx,
271 struct timeval next_event,
272 tevent_timer_handler_t handler,
273 void *private_data);
274 #else
275 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
276 TALLOC_CTX *mem_ctx,
277 struct timeval next_event,
278 tevent_timer_handler_t handler,
279 void *private_data,
280 const char *handler_name,
281 const char *location);
282 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
283 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
284 #handler, __location__)
285 #endif
288 * @brief Set the time a tevent_timer fires
290 * @param[in] te The timer event to reset
292 * @param[in] next_event Timeval specifying the absolute time to fire this
293 * event. This is not an offset.
295 void tevent_update_timer(struct tevent_timer *te, struct timeval next_event);
298 * @brief Associate a custom tag with the event.
300 * This tag can be then retrieved with tevent_timer_get_tag()
302 * @param[in] te The timer event.
304 * @param[in] tag Custom tag.
306 void tevent_timer_set_tag(struct tevent_timer *te, uint64_t tag);
309 * @brief Get custom event tag.
311 uint64_t tevent_timer_get_tag(const struct tevent_timer *te);
313 #ifdef DOXYGEN
315 * Initialize an immediate event object
317 * This object can be used to trigger an event to occur immediately after
318 * returning from the current event (before any other event occurs)
320 * @param[in] mem_ctx The talloc memory context to use as the parent
322 * @return An empty tevent_immediate object. Use tevent_schedule_immediate
323 * to populate and use it.
325 * @note Available as of tevent 0.9.8
327 struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
328 #else
329 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
330 const char *location);
331 #define tevent_create_immediate(mem_ctx) \
332 _tevent_create_immediate(mem_ctx, __location__)
333 #endif
335 #ifdef DOXYGEN
338 * Schedule an event for immediate execution. This event will occur
339 * immediately after returning from the current event (before any other
340 * event occurs)
342 * @param[in] im The tevent_immediate object to populate and use
343 * @param[in] ctx The tevent_context to run this event
344 * @param[in] handler The event handler to run when this event fires
345 * @param[in] private_data Data to pass to the event handler
347 void tevent_schedule_immediate(struct tevent_immediate *im,
348 struct tevent_context *ctx,
349 tevent_immediate_handler_t handler,
350 void *private_data);
351 #else
352 void _tevent_schedule_immediate(struct tevent_immediate *im,
353 struct tevent_context *ctx,
354 tevent_immediate_handler_t handler,
355 void *private_data,
356 const char *handler_name,
357 const char *location);
358 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
359 _tevent_schedule_immediate(im, ctx, handler, private_data, \
360 #handler, __location__);
361 #endif
364 * @brief Associate a custom tag with the event.
366 * This tag can be then retrieved with tevent_immediate_get_tag()
368 * @param[in] im The immediate event.
370 * @param[in] tag Custom tag.
372 void tevent_immediate_set_tag(struct tevent_immediate *im, uint64_t tag);
375 * @brief Get custom event tag.
377 uint64_t tevent_immediate_get_tag(const struct tevent_immediate *fde);
379 #ifdef DOXYGEN
381 * @brief Add a tevent signal handler
383 * tevent_add_signal() creates a new event for handling a signal the next
384 * time through the mainloop. It implements a very simple traditional signal
385 * handler whose only purpose is to add the handler event into the mainloop.
387 * @param[in] ev The event context to work on.
389 * @param[in] mem_ctx The talloc memory context to use.
391 * @param[in] signum The signal to trap
393 * @param[in] handler The callback handler for the signal.
395 * @param[in] sa_flags sigaction flags for this signal handler.
397 * @param[in] private_data The private data passed to the callback handler.
399 * @return The newly-created signal handler event, or NULL on error.
401 * @note To cancel a signal handler, call talloc_free() on the event returned
402 * from this function.
404 * @see tevent_num_signals, tevent_sa_info_queue_count
406 struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
407 TALLOC_CTX *mem_ctx,
408 int signum,
409 int sa_flags,
410 tevent_signal_handler_t handler,
411 void *private_data);
412 #else
413 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
414 TALLOC_CTX *mem_ctx,
415 int signum,
416 int sa_flags,
417 tevent_signal_handler_t handler,
418 void *private_data,
419 const char *handler_name,
420 const char *location);
421 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
422 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
423 #handler, __location__)
424 #endif
427 * @brief Associate a custom tag with the event.
429 * This tag can be then retrieved with tevent_signal_get_tag()
431 * @param[in] fde The signal event.
433 * @param[in] tag Custom tag.
435 void tevent_signal_set_tag(struct tevent_signal *se, uint64_t tag);
438 * @brief Get custom event tag.
440 uint64_t tevent_signal_get_tag(const struct tevent_signal *se);
443 * @brief the number of supported signals
445 * This returns value of the configure time TEVENT_NUM_SIGNALS constant.
447 * The 'signum' argument of tevent_add_signal() must be less than
448 * TEVENT_NUM_SIGNALS.
450 * @see tevent_add_signal
452 size_t tevent_num_signals(void);
455 * @brief the number of pending realtime signals
457 * This returns value of TEVENT_SA_INFO_QUEUE_COUNT.
459 * The tevent internals remember the last TEVENT_SA_INFO_QUEUE_COUNT
460 * siginfo_t structures for SA_SIGINFO signals. If the system generates
461 * more some signals get lost.
463 * @see tevent_add_signal
465 size_t tevent_sa_info_queue_count(void);
467 #ifdef DOXYGEN
469 * @brief Pass a single time through the mainloop
471 * This will process any appropriate signal, immediate, fd and timer events
473 * @param[in] ev The event context to process
475 * @return Zero on success, nonzero if an internal error occurred
477 int tevent_loop_once(struct tevent_context *ev);
478 #else
479 int _tevent_loop_once(struct tevent_context *ev, const char *location);
480 #define tevent_loop_once(ev) \
481 _tevent_loop_once(ev, __location__)
482 #endif
484 #ifdef DOXYGEN
486 * @brief Run the mainloop
488 * The mainloop will run until there are no events remaining to be processed
490 * @param[in] ev The event context to process
492 * @return Zero if all events have been processed. Nonzero if an internal
493 * error occurred.
495 int tevent_loop_wait(struct tevent_context *ev);
496 #else
497 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
498 #define tevent_loop_wait(ev) \
499 _tevent_loop_wait(ev, __location__)
500 #endif
504 * Assign a function to run when a tevent_fd is freed
506 * This function is a destructor for the tevent_fd. It does not automatically
507 * close the file descriptor. If this is the desired behavior, then it must be
508 * performed by the close_fn.
510 * @param[in] fde File descriptor event on which to set the destructor
511 * @param[in] close_fn Destructor to execute when fde is freed
513 * @note That the close_fn() on tevent_fd is *NOT* wrapped on contexts
514 * created by tevent_context_wrapper_create()!
516 * @see tevent_fd_set_close_fn
517 * @see tevent_context_wrapper_create
519 void tevent_fd_set_close_fn(struct tevent_fd *fde,
520 tevent_fd_close_fn_t close_fn);
523 * Automatically close the file descriptor when the tevent_fd is freed
525 * This function calls close(fd) internally.
527 * @param[in] fde File descriptor event to auto-close
529 * @see tevent_fd_set_close_fn
531 void tevent_fd_set_auto_close(struct tevent_fd *fde);
534 * Return the flags set on this file descriptor event
536 * @param[in] fde File descriptor event to query
538 * @return The flags set on the event. See #TEVENT_FD_READ and
539 * #TEVENT_FD_WRITE
541 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
544 * Set flags on a file descriptor event
546 * @param[in] fde File descriptor event to set
547 * @param[in] flags Flags to set on the event. See #TEVENT_FD_READ and
548 * #TEVENT_FD_WRITE
550 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
553 * Query whether tevent supports signal handling
555 * @param[in] ev An initialized tevent context
557 * @return True if this platform and tevent context support signal handling
559 bool tevent_signal_support(struct tevent_context *ev);
561 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
563 /* bits for file descriptor event flags */
566 * Monitor a file descriptor for data to be read
568 #define TEVENT_FD_READ 1
570 * Monitor a file descriptor for writeability
572 #define TEVENT_FD_WRITE 2
575 * Convenience function for declaring a tevent_fd writable
577 #define TEVENT_FD_WRITEABLE(fde) \
578 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
581 * Convenience function for declaring a tevent_fd readable
583 #define TEVENT_FD_READABLE(fde) \
584 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
587 * Convenience function for declaring a tevent_fd non-writable
589 #define TEVENT_FD_NOT_WRITEABLE(fde) \
590 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
593 * Convenience function for declaring a tevent_fd non-readable
595 #define TEVENT_FD_NOT_READABLE(fde) \
596 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
599 * Debug level of tevent
601 enum tevent_debug_level {
602 TEVENT_DEBUG_FATAL,
603 TEVENT_DEBUG_ERROR,
604 TEVENT_DEBUG_WARNING,
605 TEVENT_DEBUG_TRACE
609 * @brief The tevent debug callbac.
611 * @param[in] context The memory context to use.
613 * @param[in] level The debug level.
615 * @param[in] fmt The format string.
617 * @param[in] ap The arguments for the format string.
619 typedef void (*tevent_debug_fn)(void *context,
620 enum tevent_debug_level level,
621 const char *fmt,
622 va_list ap) PRINTF_ATTRIBUTE(3,0);
625 * Set destination for tevent debug messages
627 * @param[in] ev Event context to debug
628 * @param[in] debug Function to handle output printing
629 * @param[in] context The context to pass to the debug function.
631 * @return Always returns 0 as of version 0.9.8
633 * @note Default is to emit no debug messages
635 int tevent_set_debug(struct tevent_context *ev,
636 tevent_debug_fn debug,
637 void *context);
640 * Designate stderr for debug message output
642 * @param[in] ev Event context to debug
644 * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
645 * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
646 * function for tevent_set_debug()
648 int tevent_set_debug_stderr(struct tevent_context *ev);
650 enum tevent_trace_point {
652 * Corresponds to a trace point just before waiting
654 TEVENT_TRACE_BEFORE_WAIT,
656 * Corresponds to a trace point just after waiting
658 TEVENT_TRACE_AFTER_WAIT,
659 #define TEVENT_HAS_LOOP_ONCE_TRACE_POINTS 1
661 * Corresponds to a trace point just before calling
662 * the loop_once() backend function.
664 TEVENT_TRACE_BEFORE_LOOP_ONCE,
666 * Corresponds to a trace point right after the
667 * loop_once() backend function has returned.
669 TEVENT_TRACE_AFTER_LOOP_ONCE,
672 typedef void (*tevent_trace_callback_t)(enum tevent_trace_point,
673 void *private_data);
676 * Register a callback to be called at certain trace points
678 * @param[in] ev Event context
679 * @param[in] cb Trace callback
680 * @param[in] private_data Data to be passed to callback
682 * @note The callback will be called at trace points defined by
683 * tevent_trace_point. Call with NULL to reset.
685 void tevent_set_trace_callback(struct tevent_context *ev,
686 tevent_trace_callback_t cb,
687 void *private_data);
690 * Retrieve the current trace callback
692 * @param[in] ev Event context
693 * @param[out] cb Registered trace callback
694 * @param[out] private_data Registered data to be passed to callback
696 * @note This can be used to allow one component that wants to
697 * register a callback to respect the callback that another component
698 * has already registered.
700 void tevent_get_trace_callback(struct tevent_context *ev,
701 tevent_trace_callback_t *cb,
702 void *private_data);
704 enum tevent_event_trace_point {
706 * Corresponds to a trace point just before the event is added.
708 TEVENT_EVENT_TRACE_ATTACH,
711 * Corresponds to a trace point just before the event is removed.
713 TEVENT_EVENT_TRACE_DETACH,
716 * Corresponds to a trace point just before the event handler is called.
718 TEVENT_EVENT_TRACE_BEFORE_HANDLER,
721 typedef void (*tevent_trace_fd_callback_t)(struct tevent_fd *fde,
722 enum tevent_event_trace_point,
723 void *private_data);
725 typedef void (*tevent_trace_signal_callback_t)(struct tevent_signal *se,
726 enum tevent_event_trace_point,
727 void *private_data);
729 typedef void (*tevent_trace_timer_callback_t)(struct tevent_timer *te,
730 enum tevent_event_trace_point,
731 void *private_data);
733 typedef void (*tevent_trace_immediate_callback_t)(struct tevent_immediate *im,
734 enum tevent_event_trace_point,
735 void *private_data);
738 * Register a callback to be called at certain trace points of fd event.
740 * @param[in] ev Event context
741 * @param[in] cb Trace callback
742 * @param[in] private_data Data to be passed to callback
744 * @note The callback will be called at trace points defined by
745 * tevent_event_trace_point. Call with NULL to reset.
747 void tevent_set_trace_fd_callback(struct tevent_context *ev,
748 tevent_trace_fd_callback_t cb,
749 void *private_data);
752 * Retrieve the current trace callback of file descriptor event.
754 * @param[in] ev Event context
755 * @param[out] cb Registered trace callback
756 * @param[out] p_private_data Registered data to be passed to callback
758 * @note This can be used to allow one component that wants to
759 * register a callback to respect the callback that another component
760 * has already registered.
762 void tevent_get_trace_fd_callback(struct tevent_context *ev,
763 tevent_trace_fd_callback_t *cb,
764 void *p_private_data);
767 * Register a callback to be called at certain trace points of signal event.
769 * @param[in] ev Event context
770 * @param[in] cb Trace callback
771 * @param[in] private_data Data to be passed to callback
773 * @note The callback will be called at trace points defined by
774 * tevent_event_trace_point. Call with NULL to reset.
776 void tevent_set_trace_signal_callback(struct tevent_context *ev,
777 tevent_trace_signal_callback_t cb,
778 void *private_data);
781 * Retrieve the current trace callback of signal event.
783 * @param[in] ev Event context
784 * @param[out] cb Registered trace callback
785 * @param[out] p_private_data Registered data to be passed to callback
787 * @note This can be used to allow one component that wants to
788 * register a callback to respect the callback that another component
789 * has already registered.
791 void tevent_get_trace_signal_callback(struct tevent_context *ev,
792 tevent_trace_signal_callback_t *cb,
793 void *p_private_data);
796 * Register a callback to be called at certain trace points of timer event.
798 * @param[in] ev Event context
799 * @param[in] cb Trace callback
800 * @param[in] private_data Data to be passed to callback
802 * @note The callback will be called at trace points defined by
803 * tevent_event_trace_point. Call with NULL to reset.
805 void tevent_set_trace_timer_callback(struct tevent_context *ev,
806 tevent_trace_timer_callback_t cb,
807 void *private_data);
810 * Retrieve the current trace callback of timer event.
812 * @param[in] ev Event context
813 * @param[out] cb Registered trace callback
814 * @param[out] p_private_data Registered data to be passed to callback
816 * @note This can be used to allow one component that wants to
817 * register a callback to respect the callback that another component
818 * has already registered.
820 void tevent_get_trace_timer_callback(struct tevent_context *ev,
821 tevent_trace_timer_callback_t *cb,
822 void *p_private_data);
825 * Register a callback to be called at certain trace points of immediate event.
827 * @param[in] ev Event context
828 * @param[in] cb Trace callback
829 * @param[in] private_data Data to be passed to callback
831 * @note The callback will be called at trace points defined by
832 * tevent_event_trace_point. Call with NULL to reset.
834 void tevent_set_trace_immediate_callback(struct tevent_context *ev,
835 tevent_trace_immediate_callback_t cb,
836 void *private_data);
839 * Retrieve the current trace callback of immediate event.
841 * @param[in] ev Event context
842 * @param[out] cb Registered trace callback
843 * @param[out] p_private_data Registered data to be passed to callback
845 * @note This can be used to allow one component that wants to
846 * register a callback to respect the callback that another component
847 * has already registered.
849 void tevent_get_trace_immediate_callback(struct tevent_context *ev,
850 tevent_trace_immediate_callback_t *cb,
851 void *p_private_data);
854 * @}
858 * @defgroup tevent_request The tevent request functions.
859 * @ingroup tevent
861 * A tevent_req represents an asynchronous computation.
863 * The tevent_req group of API calls is the recommended way of
864 * programming async computations within tevent. In particular the
865 * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
866 * are considered too low-level to be used in larger computations. To
867 * read and write from and to sockets, Samba provides two calls on top
868 * of tevent_add_fd: tstream_read_packet_send/recv and tstream_writev_send/recv.
869 * These requests are much easier to compose than the low-level event
870 * handlers called from tevent_add_fd.
872 * A lot of the simplicity tevent_req has brought to the notoriously
873 * hairy async programming came via a set of conventions that every
874 * async computation programmed should follow. One central piece of
875 * these conventions is the naming of routines and variables.
877 * Every async computation needs a name (sensibly called "computation"
878 * down from here). From this name quite a few naming conventions are
879 * derived.
881 * Every computation that requires local state needs a
882 * @code
883 * struct computation_state {
884 * int local_var;
885 * };
886 * @endcode
887 * Even if no local variables are required, such a state struct should
888 * be created containing a dummy variable. Quite a few helper
889 * functions and macros (for example tevent_req_create()) assume such
890 * a state struct.
892 * An async computation is started by a computation_send
893 * function. When it is finished, its result can be received by a
894 * computation_recv function. For an example how to set up an async
895 * computation, see the code example in the documentation for
896 * tevent_req_create() and tevent_req_post(). The prototypes for _send
897 * and _recv functions should follow some conventions:
899 * @code
900 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
901 * struct tevent_context *ev,
902 * ... further args);
903 * int computation_recv(struct tevent_req *req, ... further output args);
904 * @endcode
906 * The "int" result of computation_recv() depends on the result the
907 * sync version of the function would have, "int" is just an example
908 * here.
910 * Another important piece of the conventions is that the program flow
911 * is interrupted as little as possible. Because a blocking
912 * sub-computation requires that the flow needs to continue in a
913 * separate function that is the logical sequel of some computation,
914 * it should lexically follow sending off the blocking
915 * sub-computation. Setting the callback function via
916 * tevent_req_set_callback() requires referencing a function lexically
917 * below the call to tevent_req_set_callback(), forward declarations
918 * are required. A lot of the async computations thus begin with a
919 * sequence of declarations such as
921 * @code
922 * static void computation_step1_done(struct tevent_req *subreq);
923 * static void computation_step2_done(struct tevent_req *subreq);
924 * static void computation_step3_done(struct tevent_req *subreq);
925 * @endcode
927 * It really helps readability a lot to do these forward declarations,
928 * because the lexically sequential program flow makes the async
929 * computations almost as clear to read as a normal, sync program
930 * flow.
932 * It is up to the user of the async computation to talloc_free it
933 * after it has finished. If an async computation should be aborted,
934 * the tevent_req structure can be talloc_free'ed. After it has
935 * finished, it should talloc_free'ed by the API user.
937 * tevent_req variable naming conventions:
939 * The name of the variable pointing to the tevent_req structure
940 * returned by a _send() function SHOULD be named differently between
941 * implementation and caller.
943 * From the point of view of the implementation (of the _send() and
944 * _recv() functions) the variable returned by tevent_req_create() is
945 * always called @em req.
947 * While the caller of the _send() function should use @em subreq to
948 * hold the result.
950 * @see tevent_req_create()
951 * @see tevent_req_fn()
953 * @{
957 * An async request moves from TEVENT_REQ_INIT to
958 * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
959 * has finished.
961 enum tevent_req_state {
963 * We are creating the request
965 TEVENT_REQ_INIT,
967 * We are waiting the request to complete
969 TEVENT_REQ_IN_PROGRESS,
971 * The request is finished successfully
973 TEVENT_REQ_DONE,
975 * A user error has occurred. The user error has been
976 * indicated by tevent_req_error(), it can be retrieved via
977 * tevent_req_is_error().
979 TEVENT_REQ_USER_ERROR,
981 * Request timed out after the timeout set by tevent_req_set_endtime.
983 TEVENT_REQ_TIMED_OUT,
985 * An internal allocation has failed, or tevent_req_nomem has
986 * been given a NULL pointer as the first argument.
988 TEVENT_REQ_NO_MEMORY,
990 * The request has been received by the caller. No further
991 * action is valid.
993 TEVENT_REQ_RECEIVED
997 * @brief An async request
999 struct tevent_req;
1002 * @brief A tevent request callback function.
1004 * @param[in] subreq The tevent async request which executed this callback.
1006 typedef void (*tevent_req_fn)(struct tevent_req *subreq);
1009 * @brief Set an async request callback.
1011 * See the documentation of tevent_req_post() for an example how this
1012 * is supposed to be used.
1014 * @param[in] req The async request to set the callback.
1016 * @param[in] fn The callback function to set.
1018 * @param[in] pvt A pointer to private data to pass to the async request
1019 * callback.
1021 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
1022 void _tevent_req_set_callback(struct tevent_req *req,
1023 tevent_req_fn fn,
1024 const char *fn_name,
1025 void *pvt);
1027 #define tevent_req_set_callback(req, fn, pvt) \
1028 _tevent_req_set_callback(req, fn, #fn, pvt)
1030 #ifdef DOXYGEN
1032 * @brief Get the private data cast to the given type for a callback from
1033 * a tevent request structure.
1035 * @code
1036 * static void computation_done(struct tevent_req *subreq) {
1037 * struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
1038 * struct computation_state *state = tevent_req_data(req, struct computation_state);
1039 * .... more things, eventually maybe call tevent_req_done(req);
1041 * @endcode
1043 * @param[in] req The structure to get the callback data from.
1045 * @param[in] type The type of the private callback data to get.
1047 * @return The type casted private data set NULL if not set.
1049 void *tevent_req_callback_data(struct tevent_req *req, #type);
1050 #else
1051 void *_tevent_req_callback_data(struct tevent_req *req);
1052 #define tevent_req_callback_data(_req, _type) \
1053 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
1054 #endif
1056 #ifdef DOXYGEN
1058 * @brief Get the private data for a callback from a tevent request structure.
1060 * @param[in] req The structure to get the callback data from.
1062 * @return The private data or NULL if not set.
1064 void *tevent_req_callback_data_void(struct tevent_req *req);
1065 #else
1066 #define tevent_req_callback_data_void(_req) \
1067 _tevent_req_callback_data(_req)
1068 #endif
1070 #ifdef DOXYGEN
1072 * @brief Get the private data from a tevent request structure.
1074 * When the tevent_req has been created by tevent_req_create, the
1075 * result of tevent_req_data() is the state variable created by
1076 * tevent_req_create() as a child of the req.
1078 * @param[in] req The structure to get the private data from.
1080 * @param[in] type The type of the private data
1082 * @return The private data or NULL if not set.
1084 void *tevent_req_data(struct tevent_req *req, #type);
1085 #else
1086 void *_tevent_req_data(struct tevent_req *req);
1087 #define tevent_req_data(_req, _type) \
1088 talloc_get_type_abort(_tevent_req_data(_req), _type)
1089 #endif
1092 * @brief The print function which can be set for a tevent async request.
1094 * @param[in] req The tevent async request.
1096 * @param[in] ctx A talloc memory context which can be uses to allocate
1097 * memory.
1099 * @return An allocated string buffer to print.
1101 * Example:
1102 * @code
1103 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
1105 * struct my_data *data = tevent_req_data(req, struct my_data);
1106 * char *result;
1108 * result = tevent_req_default_print(mem_ctx, req);
1109 * if (result == NULL) {
1110 * return NULL;
1113 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
1114 * data->foo, data->bar);
1116 * @endcode
1118 typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
1121 * @brief This function sets a print function for the given request.
1123 * This function can be used to setup a print function for the given request.
1124 * This will be triggered if the tevent_req_print() function was
1125 * called on the given request.
1127 * @param[in] req The request to use.
1129 * @param[in] fn A pointer to the print function
1131 * @note This function should only be used for debugging.
1133 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
1136 * @brief The default print function for creating debug messages.
1138 * The function should not be used by users of the async API,
1139 * but custom print function can use it and append custom text
1140 * to the string.
1142 * @param[in] req The request to be printed.
1144 * @param[in] mem_ctx The memory context for the result.
1146 * @return Text representation of request.
1149 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
1152 * @brief Print an tevent_req structure in debug messages.
1154 * This function should be used by callers of the async API.
1156 * @param[in] mem_ctx The memory context for the result.
1158 * @param[in] req The request to be printed.
1160 * @return Text representation of request.
1162 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
1165 * @brief A typedef for a cancel function for a tevent request.
1167 * @param[in] req The tevent request calling this function.
1169 * @return True if the request could be canceled, false if not.
1171 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
1174 * @brief This function sets a cancel function for the given tevent request.
1176 * This function can be used to setup a cancel function for the given request.
1177 * This will be triggered if the tevent_req_cancel() function was
1178 * called on the given request.
1180 * @param[in] req The request to use.
1182 * @param[in] fn A pointer to the cancel function.
1184 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
1185 void _tevent_req_set_cancel_fn(struct tevent_req *req,
1186 tevent_req_cancel_fn fn,
1187 const char *fn_name);
1188 #define tevent_req_set_cancel_fn(req, fn) \
1189 _tevent_req_set_cancel_fn(req, fn, #fn)
1191 #ifdef DOXYGEN
1193 * @brief Try to cancel the given tevent request.
1195 * This function can be used to cancel the given request.
1197 * It is only possible to cancel a request when the implementation
1198 * has registered a cancel function via the tevent_req_set_cancel_fn().
1200 * @param[in] req The request to use.
1202 * @return This function returns true if the request is
1203 * cancelable, otherwise false is returned.
1205 * @note Even if the function returns true, the caller need to wait
1206 * for the function to complete normally.
1207 * Only the _recv() function of the given request indicates
1208 * if the request was really canceled.
1210 bool tevent_req_cancel(struct tevent_req *req);
1211 #else
1212 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
1213 #define tevent_req_cancel(req) \
1214 _tevent_req_cancel(req, __location__)
1215 #endif
1218 * @brief A typedef for a cleanup function for a tevent request.
1220 * @param[in] req The tevent request calling this function.
1222 * @param[in] req_state The current tevent_req_state.
1225 typedef void (*tevent_req_cleanup_fn)(struct tevent_req *req,
1226 enum tevent_req_state req_state);
1229 * @brief This function sets a cleanup function for the given tevent request.
1231 * This function can be used to setup a cleanup function for the given request.
1232 * This will be triggered when the tevent_req_done() or tevent_req_error()
1233 * function was called, before notifying the callers callback function,
1234 * and also before scheduling the deferred trigger.
1236 * This might be useful if more than one tevent_req belong together
1237 * and need to finish both requests at the same time.
1239 * The cleanup function is able to call tevent_req_done() or tevent_req_error()
1240 * recursively, the cleanup function is only triggered the first time.
1242 * The cleanup function is also called by tevent_req_received()
1243 * (possibly triggered from tevent_req_destructor()) before destroying
1244 * the private data of the tevent_req.
1246 * @param[in] req The request to use.
1248 * @param[in] fn A pointer to the cancel function.
1250 void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn);
1251 void _tevent_req_set_cleanup_fn(struct tevent_req *req,
1252 tevent_req_cleanup_fn fn,
1253 const char *fn_name);
1254 #define tevent_req_set_cleanup_fn(req, fn) \
1255 _tevent_req_set_cleanup_fn(req, fn, #fn)
1257 #ifdef DOXYGEN
1259 * @brief Create an async tevent request.
1261 * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
1263 * @code
1264 * struct tevent_req *req;
1265 * struct computation_state *state;
1266 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1267 * @endcode
1269 * Tevent_req_create() allocates and zeros the state variable as a talloc
1270 * child of its result. The state variable should be used as the talloc
1271 * parent for all temporary variables that are allocated during the async
1272 * computation. This way, when the user of the async computation frees
1273 * the request, the state as a talloc child will be free'd along with
1274 * all the temporary variables hanging off the state.
1276 * @param[in] mem_ctx The memory context for the result.
1277 * @param[in] pstate Pointer to the private request state.
1278 * @param[in] type The name of the request.
1280 * @return A new async request. NULL on error.
1282 struct tevent_req *tevent_req_create(TALLOC_CTX *mem_ctx,
1283 void **pstate, #type);
1284 #else
1285 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
1286 void *pstate,
1287 size_t state_size,
1288 const char *type,
1289 const char *location);
1291 struct tevent_req *__tevent_req_create(TALLOC_CTX *mem_ctx,
1292 void *pstate,
1293 size_t state_size,
1294 const char *type,
1295 const char *func,
1296 const char *location);
1298 #define tevent_req_create(_mem_ctx, _pstate, _type) \
1299 __tevent_req_create((_mem_ctx), \
1300 (_pstate), \
1301 sizeof(_type), \
1302 #_type, \
1303 __func__, \
1304 __location__)
1305 #endif
1308 * @brief Set a timeout for an async request. On failure, "req" is already
1309 * set to state TEVENT_REQ_NO_MEMORY.
1311 * @param[in] req The request to set the timeout for.
1313 * @param[in] ev The event context to use for the timer.
1315 * @param[in] endtime The endtime of the request.
1317 * @return True if succeeded, false if not.
1319 bool tevent_req_set_endtime(struct tevent_req *req,
1320 struct tevent_context *ev,
1321 struct timeval endtime);
1324 * @brief Reset the timer set by tevent_req_set_endtime.
1326 * @param[in] req The request to reset the timeout for
1328 void tevent_req_reset_endtime(struct tevent_req *req);
1330 #ifdef DOXYGEN
1332 * @brief Call the notify callback of the given tevent request manually.
1334 * @param[in] req The tevent request to call the notify function from.
1336 * @see tevent_req_set_callback()
1338 void tevent_req_notify_callback(struct tevent_req *req);
1339 #else
1340 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
1341 #define tevent_req_notify_callback(req) \
1342 _tevent_req_notify_callback(req, __location__)
1343 #endif
1345 #ifdef DOXYGEN
1347 * @brief An async request has successfully finished.
1349 * This function is to be used by implementors of async requests. When a
1350 * request is successfully finished, this function calls the user's completion
1351 * function.
1353 * @param[in] req The finished request.
1355 void tevent_req_done(struct tevent_req *req);
1356 #else
1357 void _tevent_req_done(struct tevent_req *req,
1358 const char *location);
1359 #define tevent_req_done(req) \
1360 _tevent_req_done(req, __location__)
1361 #endif
1363 #ifdef DOXYGEN
1365 * @brief An async request has seen an error.
1367 * This function is to be used by implementors of async requests. When a
1368 * request can not successfully completed, the implementation should call this
1369 * function with the appropriate status code.
1371 * If error is 0 the function returns false and does nothing more.
1373 * @param[in] req The request with an error.
1375 * @param[in] error The error code.
1377 * @return On success true is returned, false if error is 0.
1379 * @code
1380 * int error = first_function();
1381 * if (tevent_req_error(req, error)) {
1382 * return;
1385 * error = second_function();
1386 * if (tevent_req_error(req, error)) {
1387 * return;
1390 * tevent_req_done(req);
1391 * return;
1392 * @endcode
1394 bool tevent_req_error(struct tevent_req *req,
1395 uint64_t error);
1396 #else
1397 bool _tevent_req_error(struct tevent_req *req,
1398 uint64_t error,
1399 const char *location);
1400 #define tevent_req_error(req, error) \
1401 _tevent_req_error(req, error, __location__)
1402 #endif
1404 #ifdef DOXYGEN
1406 * @brief Helper function for nomem check.
1408 * Convenience helper to easily check alloc failure within a callback
1409 * implementing the next step of an async request.
1411 * @param[in] p The pointer to be checked.
1413 * @param[in] req The request being processed.
1415 * @code
1416 * p = talloc(mem_ctx, bla);
1417 * if (tevent_req_nomem(p, req)) {
1418 * return;
1420 * @endcode
1422 bool tevent_req_nomem(const void *p,
1423 struct tevent_req *req);
1424 #else
1425 bool _tevent_req_nomem(const void *p,
1426 struct tevent_req *req,
1427 const char *location);
1428 #define tevent_req_nomem(p, req) \
1429 _tevent_req_nomem(p, req, __location__)
1430 #endif
1432 #ifdef DOXYGEN
1434 * @brief Indicate out of memory to a request
1436 * @param[in] req The request being processed.
1438 void tevent_req_oom(struct tevent_req *req);
1439 #else
1440 void _tevent_req_oom(struct tevent_req *req,
1441 const char *location);
1442 #define tevent_req_oom(req) \
1443 _tevent_req_oom(req, __location__)
1444 #endif
1447 * @brief Finish a request before the caller had a chance to set the callback.
1449 * An implementation of an async request might find that it can either finish
1450 * the request without waiting for an external event, or it can not even start
1451 * the engine. To present the illusion of a callback to the user of the API,
1452 * the implementation can call this helper function which triggers an
1453 * immediate event. This way the caller can use the same calling
1454 * conventions, independent of whether the request was actually deferred.
1456 * @code
1457 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1458 * struct tevent_context *ev)
1460 * struct tevent_req *req, *subreq;
1461 * struct computation_state *state;
1462 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1463 * if (req == NULL) {
1464 * return NULL;
1466 * subreq = subcomputation_send(state, ev);
1467 * if (tevent_req_nomem(subreq, req)) {
1468 * return tevent_req_post(req, ev);
1470 * tevent_req_set_callback(subreq, computation_done, req);
1471 * return req;
1473 * @endcode
1475 * @param[in] req The finished request.
1477 * @param[in] ev The tevent_context for the immediate event.
1479 * @return The given request will be returned.
1481 struct tevent_req *tevent_req_post(struct tevent_req *req,
1482 struct tevent_context *ev);
1485 * @brief Finish multiple requests within one function
1487 * Normally tevent_req_notify_callback() and all wrappers
1488 * (e.g. tevent_req_done() and tevent_req_error())
1489 * need to be the last thing an event handler should call.
1490 * This is because the callback is likely to destroy the
1491 * context of the current function.
1493 * If a function wants to notify more than one caller,
1494 * it is dangerous if it just triggers multiple callbacks
1495 * in a row. With tevent_req_defer_callback() it is possible
1496 * to set an event context that will be used to defer the callback
1497 * via an immediate event (similar to tevent_req_post()).
1499 * @code
1500 * struct complete_state {
1501 * struct tevent_context *ev;
1503 * struct tevent_req **reqs;
1504 * };
1506 * void complete(struct complete_state *state)
1508 * size_t i, c = talloc_array_length(state->reqs);
1510 * for (i=0; i < c; i++) {
1511 * tevent_req_defer_callback(state->reqs[i], state->ev);
1512 * tevent_req_done(state->reqs[i]);
1515 * @endcode
1517 * @param[in] req The finished request.
1519 * @param[in] ev The tevent_context for the immediate event.
1521 * @return The given request will be returned.
1523 void tevent_req_defer_callback(struct tevent_req *req,
1524 struct tevent_context *ev);
1527 * @brief Check if the given request is still in progress.
1529 * It is typically used by sync wrapper functions.
1531 * @param[in] req The request to poll.
1533 * @return The boolean form of "is in progress".
1535 bool tevent_req_is_in_progress(struct tevent_req *req);
1538 * @brief Actively poll for the given request to finish.
1540 * This function is typically used by sync wrapper functions.
1542 * @param[in] req The request to poll.
1544 * @param[in] ev The tevent_context to be used.
1546 * @return On success true is returned. If a critical error has
1547 * happened in the tevent loop layer false is returned.
1548 * This is not the return value of the given request!
1550 * @note This should only be used if the given tevent context was created by the
1551 * caller, to avoid event loop nesting.
1553 * @code
1554 * req = tstream_writev_queue_send(mem_ctx,
1555 * ev_ctx,
1556 * tstream,
1557 * send_queue,
1558 * iov, 2);
1559 * ok = tevent_req_poll(req, tctx->ev);
1560 * rc = tstream_writev_queue_recv(req, &sys_errno);
1561 * TALLOC_FREE(req);
1562 * @endcode
1564 bool tevent_req_poll(struct tevent_req *req,
1565 struct tevent_context *ev);
1568 * @brief Get the tevent request state and the actual error set by
1569 * tevent_req_error.
1571 * @code
1572 * int computation_recv(struct tevent_req *req, uint64_t *perr)
1574 * enum tevent_req_state state;
1575 * uint64_t err;
1576 * if (tevent_req_is_error(req, &state, &err)) {
1577 * *perr = err;
1578 * return -1;
1580 * return 0;
1582 * @endcode
1584 * @param[in] req The tevent request to get the error from.
1586 * @param[out] state A pointer to store the tevent request error state.
1588 * @param[out] error A pointer to store the error set by tevent_req_error().
1590 * @return True if the function could set error and state, false
1591 * otherwise.
1593 * @see tevent_req_error()
1595 bool tevent_req_is_error(struct tevent_req *req,
1596 enum tevent_req_state *state,
1597 uint64_t *error);
1600 * @brief Use as the last action of a _recv() function.
1602 * This function destroys the attached private data.
1604 * @param[in] req The finished request.
1606 void tevent_req_received(struct tevent_req *req);
1609 * @brief Mark a tevent_req for profiling
1611 * This will turn on profiling for this tevent_req an all subreqs that
1612 * are directly started as helper requests off this
1613 * tevent_req. subreqs are chained by walking up the talloc_parent
1614 * hierarchy at a subreq's tevent_req_create. This means to get the
1615 * profiling chain right the subreq that needs to be profiled as part
1616 * of this tevent_req's profile must be a talloc child of the requests
1617 * state variable.
1619 * @param[in] req The request to do tracing for
1621 * @return False if the profile could not be activated
1623 bool tevent_req_set_profile(struct tevent_req *req);
1625 struct tevent_req_profile;
1628 * @brief Get the a request's profile for inspection
1630 * @param[in] req The request to get the profile from
1632 * @return The request's profile
1634 const struct tevent_req_profile *tevent_req_get_profile(
1635 struct tevent_req *req);
1638 * @brief Move the profile out of a request
1640 * This function detaches the request's profile from the request, so
1641 * that the profile can outlive the request in a _recv function.
1643 * @param[in] req The request to move the profile out of
1644 * @param[in] mem_ctx The new talloc context for the profile
1646 * @return The moved profile
1649 struct tevent_req_profile *tevent_req_move_profile(struct tevent_req *req,
1650 TALLOC_CTX *mem_ctx);
1653 * @brief Get a profile description
1655 * @param[in] profile The profile to be queried
1656 * @param[in] req_name The name of the request (state's name)
1658 * "req_name" after this call is still in talloc-posession of "profile"
1660 void tevent_req_profile_get_name(const struct tevent_req_profile *profile,
1661 const char **req_name);
1664 * @brief Get a profile's start event data
1666 * @param[in] profile The profile to be queried
1667 * @param[in] start_location The location where this event started
1668 * @param[in] start_time The time this event started
1670 * "start_location" after this call is still in talloc-posession of "profile"
1672 void tevent_req_profile_get_start(const struct tevent_req_profile *profile,
1673 const char **start_location,
1674 struct timeval *start_time);
1677 * @brief Get a profile's stop event data
1679 * @param[in] profile The profile to be queried
1680 * @param[in] stop_location The location where this event stopped
1681 * @param[in] stop_time The time this event stopped
1683 * "stop_location" after this call is still in talloc-posession of "profile"
1685 void tevent_req_profile_get_stop(const struct tevent_req_profile *profile,
1686 const char **stop_location,
1687 struct timeval *stop_time);
1690 * @brief Get a profile's result data
1692 * @param[in] pid The process where this profile was taken
1693 * @param[in] state The status the profile's tevent_req finished with
1694 * @param[in] user_error The user error of the profile's tevent_req
1696 void tevent_req_profile_get_status(const struct tevent_req_profile *profile,
1697 pid_t *pid,
1698 enum tevent_req_state *state,
1699 uint64_t *user_error);
1702 * @brief Retrieve the first subreq's profile from a profile
1704 * @param[in] profile The profile to query
1706 * @return The first tevent subreq's profile
1708 const struct tevent_req_profile *tevent_req_profile_get_subprofiles(
1709 const struct tevent_req_profile *profile);
1712 * @brief Walk the chain of subreqs
1714 * @param[in] profile The subreq's profile to walk
1716 * @return The next subprofile in the list
1718 const struct tevent_req_profile *tevent_req_profile_next(
1719 const struct tevent_req_profile *profile);
1722 * @brief Create a fresh tevent_req_profile
1724 * @param[in] mem_ctx The talloc context to hang the fresh struct off
1726 * @return The fresh struct
1728 struct tevent_req_profile *tevent_req_profile_create(TALLOC_CTX *mem_ctx);
1731 * @brief Set a profile's name
1733 * @param[in] profile The profile to set the name for
1734 * @param[in] name The new name for the profile
1736 * @return True if the internal talloc_strdup succeeded
1738 bool tevent_req_profile_set_name(struct tevent_req_profile *profile,
1739 const char *name);
1742 * @brief Set a profile's start event
1744 * @param[in] profile The profile to set the start data for
1745 * @param[in] start_location The new start location
1746 * @param[in] start_time The new start time
1748 * @return True if the internal talloc_strdup succeeded
1750 bool tevent_req_profile_set_start(struct tevent_req_profile *profile,
1751 const char *start_location,
1752 struct timeval start_time);
1755 * @brief Set a profile's stop event
1757 * @param[in] profile The profile to set the stop data for
1758 * @param[in] stop_location The new stop location
1759 * @param[in] stop_time The new stop time
1761 * @return True if the internal talloc_strdup succeeded
1763 bool tevent_req_profile_set_stop(struct tevent_req_profile *profile,
1764 const char *stop_location,
1765 struct timeval stop_time);
1768 * @brief Set a profile's exit status
1770 * @param[in] profile The profile to set the exit status for
1771 * @param[in] pid The process where this profile was taken
1772 * @param[in] state The status the profile's tevent_req finished with
1773 * @param[in] user_error The user error of the profile's tevent_req
1775 void tevent_req_profile_set_status(struct tevent_req_profile *profile,
1776 pid_t pid,
1777 enum tevent_req_state state,
1778 uint64_t user_error);
1781 * @brief Add a subprofile to a profile
1783 * @param[in] parent_profile The profile to be modified
1784 * @param[in] sub_profile The subreqs profile profile to be added
1786 * "subreq" is talloc_move'ed into "parent_profile", so the talloc
1787 * ownership of "sub_profile" changes
1790 void tevent_req_profile_append_sub(struct tevent_req_profile *parent_profile,
1791 struct tevent_req_profile **sub_profile);
1794 * @brief Create a tevent subrequest at a given time.
1796 * The idea is that always the same syntax for tevent requests.
1798 * @param[in] mem_ctx The talloc memory context to use.
1800 * @param[in] ev The event handle to setup the request.
1802 * @param[in] wakeup_time The time to wakeup and execute the request.
1804 * @return The new subrequest, NULL on error.
1806 * Example:
1807 * @code
1808 * static void my_callback_wakeup_done(tevent_req *subreq)
1810 * struct tevent_req *req = tevent_req_callback_data(subreq,
1811 * struct tevent_req);
1812 * bool ok;
1814 * ok = tevent_wakeup_recv(subreq);
1815 * TALLOC_FREE(subreq);
1816 * if (!ok) {
1817 * tevent_req_error(req, -1);
1818 * return;
1820 * ...
1822 * @endcode
1824 * @code
1825 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1826 * if (tevent_req_nomem(subreq, req)) {
1827 * return false;
1829 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1830 * @endcode
1832 * @see tevent_wakeup_recv()
1834 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
1835 struct tevent_context *ev,
1836 struct timeval wakeup_time);
1839 * @brief Check if the wakeup has been correctly executed.
1841 * This function needs to be called in the callback function set after calling
1842 * tevent_wakeup_send().
1844 * @param[in] req The tevent request to check.
1846 * @return True on success, false otherwise.
1848 * @see tevent_wakeup_recv()
1850 bool tevent_wakeup_recv(struct tevent_req *req);
1852 /* @} */
1855 * @defgroup tevent_helpers The tevent helper functions
1856 * @ingroup tevent
1858 * @todo description
1860 * @{
1864 * @brief Compare two timeval values.
1866 * @param[in] tv1 The first timeval value to compare.
1868 * @param[in] tv2 The second timeval value to compare.
1870 * @return 0 if they are equal.
1871 * 1 if the first time is greater than the second.
1872 * -1 if the first time is smaller than the second.
1874 int tevent_timeval_compare(const struct timeval *tv1,
1875 const struct timeval *tv2);
1878 * @brief Get a zero timeval value.
1880 * @return A zero timeval value.
1882 struct timeval tevent_timeval_zero(void);
1885 * @brief Get a timeval value for the current time.
1887 * @return A timeval value with the current time.
1889 struct timeval tevent_timeval_current(void);
1892 * @brief Get a timeval structure with the given values.
1894 * @param[in] secs The seconds to set.
1896 * @param[in] usecs The microseconds to set.
1898 * @return A timeval structure with the given values.
1900 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
1903 * @brief Get the difference between two timeval values.
1905 * @param[in] tv1 The first timeval.
1907 * @param[in] tv2 The second timeval.
1909 * @return A timeval structure with the difference between the
1910 * first and the second value.
1912 struct timeval tevent_timeval_until(const struct timeval *tv1,
1913 const struct timeval *tv2);
1916 * @brief Check if a given timeval structure is zero.
1918 * @param[in] tv The timeval to check if it is zero.
1920 * @return True if it is zero, false otherwise.
1922 bool tevent_timeval_is_zero(const struct timeval *tv);
1925 * @brief Add the given amount of time to a timeval structure.
1927 * @param[in] tv The timeval structure to add the time.
1929 * @param[in] secs The seconds to add to the timeval.
1931 * @param[in] usecs The microseconds to add to the timeval.
1933 * @return The timeval structure with the new time.
1935 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
1936 uint32_t usecs);
1939 * @brief Get a timeval in the future with a specified offset from now.
1941 * @param[in] secs The seconds of the offset from now.
1943 * @param[in] usecs The microseconds of the offset from now.
1945 * @return A timeval with the given offset in the future.
1947 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
1951 * @brief A cached version of getpid()
1953 * We use getpid() in a lot a performance critical situations
1954 * in order to check if caches are still valid in the current process.
1956 * Calling getpid() always add the cost of an additional syscall!
1958 * When tevent is build with pthread support, we already make use
1959 * of pthread_atfork(), so it's trivial to use it maintain a cache for getpid().
1961 * @return The pid of the current process.
1963 pid_t tevent_cached_getpid(void);
1965 /* @} */
1969 * @defgroup tevent_thread_call_depth The tevent call depth tracking functions
1970 * @ingroup tevent
1973 * The call depth tracking consists of two parts.
1975 * Part 1 - storing the depth inside each tevent request.
1977 * Each instance of 'struct tevent_req' internally stores the value of the
1978 * current depth. If a new subrequest is created via tevent_req_create(), the
1979 * newly created subrequest gets the value from the parent incremented by 1.
1981 * Part 2 - updating external variable with the call depth of the currently
1982 * processed tevent request.
1984 * The intended use of call depth is for the trace indentation. This is done
1985 * by registering the address of an external size_t variable via
1986 * tevent_thread_call_depth_activate(). And the tracing code just reads it's
1987 * value.
1989 * The updates happen during:
1991 * tevent_req_create()
1992 * - external variable is set to the value of the newly created request (i.e.
1993 * value of the parent incremented by 1)
1995 * tevent_req_notify_callback()
1996 * - external variable is set to the value of the parent tevent request, which
1997 * is just about to be processed
1999 * tevent_queue_immediate_trigger()
2000 * - external variable is set to the value of the request coming from the queue
2003 * While 'Part 1' maintains the call depth value inside each teven request
2004 * precisely, the value of the external variable depends on the call flow and
2005 * can be changed after return from a function call, so it no longer matches
2006 * the value of the request being processed in the current function.
2008 * @code
2009 * struct tevent_req *foo_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
2011 * struct tevent_req *req, *subreq;
2012 * struct foo_state *state;
2014 * // External variable has value 'X', which is the value in parent code
2015 * // It is ok, since tracing starts often only after tevent_req_create()
2016 * req = tevent_req_create(mem_ctx, &state, struct foo_state);
2018 * // External variable has now value 'X + 1'
2019 * D_DEBUG("foo_send(): the external variable has the expected value\n");
2021 * subreq = bar_send(state, ev, ...);
2022 * tevent_req_set_callback(subreq, foo_done, req);
2024 * // External variable has value 'X + 1 + n', where n > 0 and n is the
2025 * // depth reached in bar_send().
2026 * // We want to reset it via tevent_thread_call_depth_reset_from_req(),
2027 * // since we want the following D_DEBUG() to have the right trace
2028 * //indentation.
2030 * tevent_thread_call_depth_reset_from_req(req);
2031 * // External variable has again value 'X + 1' taken from req.
2032 * D_DEBUG("foo_send(): the external variable has the expected value\n");
2033 * return req;
2036 * static void foo_done(struct tevent_req *subreq)
2038 * struct tevent_req *req =
2039 * tevent_req_callback_data(subreq,
2040 * struct tevent_req);
2041 * struct foo_state *state =
2042 * tevent_req_data(req,
2043 * struct foo_state);
2045 * // external variable has value 'X + 1'
2047 * D_DEBUG("foo_done(): the external variable has the expected value\n");
2048 * status = bar_recv(subreq, state, ...)
2049 * tevent_req_done(req);
2052 * NTSTATUS foo_recv(struct tevent_req *req)
2054 * struct foo_state *state = tevent_req_data( req, struct foo_state);
2056 * // external variable has value 'X' (not 'X + 1')
2057 * // which is ok, if we consider _recv() to be an access function
2058 * // called from the parent context
2060 * D_DEBUG("foo_recv(): external variable has the value from parent\n");
2061 * return NT_STATUS_OK;
2063 * @endcode
2065 * Interface has 3 parts:
2067 * Part 1: activation/deactivation
2069 * Part 2: Mark the request (and its subrequests) to be tracked
2071 * tevent_thread_call_depth_start(struct tevent_req *req)
2073 * By default, all newly created requests have call depth set to 0.
2074 * tevent_thread_call_depth_start() should be called shortly after
2075 * tevent_req_create(). It sets the call depth to 1.
2076 * Subrequest will have call depth 2 and so on.
2078 * Part 3: reset the external variable using value from tevent request
2080 * tevent_thread_call_depth_reset_from_req(struct tevent_req *req)
2082 * If the call depth is used for trace indentation, it might be useful to
2083 * reset the external variable to the call depth of currently processed tevent
2084 * request, since the ext. variable can be changed after return from a function
2085 * call that has created subrequests.
2087 * THREADING
2089 * The state is thread specific, i.e. each thread can activate it and register
2090 * its own external variable.
2092 * @{
2095 #ifdef TEVENT_DEPRECATED
2097 void tevent_thread_call_depth_activate(size_t *ptr) _DEPRECATED_;
2098 void tevent_thread_call_depth_deactivate(void) _DEPRECATED_;
2099 void tevent_thread_call_depth_start(struct tevent_req *req) _DEPRECATED_;
2101 #endif
2104 * Set the external variable to the call depth of the request req.
2106 * @param[in] req Request from which the call depth is assigned to ext.
2107 * variable.
2109 void tevent_thread_call_depth_reset_from_req(struct tevent_req *req);
2111 /* @} */
2115 * @defgroup tevent_queue The tevent queue functions
2116 * @ingroup tevent
2118 * A tevent_queue is used to queue up async requests that must be
2119 * serialized. For example writing buffers into a socket must be
2120 * serialized. Writing a large lump of data into a socket can require
2121 * multiple write(2) or send(2) system calls. If more than one async
2122 * request is outstanding to write large buffers into a socket, every
2123 * request must individually be completed before the next one begins,
2124 * even if multiple syscalls are required.
2126 * Take a look at @ref tevent_queue_tutorial for more details.
2127 * @{
2130 struct tevent_queue;
2131 struct tevent_queue_entry;
2134 * @brief Associate a custom tag with the queue entry.
2136 * This tag can be then retrieved with tevent_queue_entry_get_tag()
2138 * @param[in] qe The queue entry.
2140 * @param[in] tag Custom tag.
2142 void tevent_queue_entry_set_tag(struct tevent_queue_entry *qe, uint64_t tag);
2145 * @brief Get custom queue entry tag.
2147 uint64_t tevent_queue_entry_get_tag(const struct tevent_queue_entry *qe);
2149 typedef void (*tevent_trace_queue_callback_t)(struct tevent_queue_entry *qe,
2150 enum tevent_event_trace_point,
2151 void *private_data);
2154 * Register a callback to be called at certain trace points of queue.
2156 * @param[in] ev Event context
2157 * @param[in] cb Trace callback
2158 * @param[in] private_data Data to be passed to callback
2160 * @note The callback will be called at trace points defined by
2161 * tevent_event_trace_point. Call with NULL to reset.
2163 void tevent_set_trace_queue_callback(struct tevent_context *ev,
2164 tevent_trace_queue_callback_t cb,
2165 void *private_data);
2168 * Retrieve the current trace callback of queue.
2170 * @param[in] ev Event context
2171 * @param[out] cb Registered trace callback
2172 * @param[out] p_private_data Registered data to be passed to callback
2174 * @note This can be used to allow one component that wants to
2175 * register a callback to respect the callback that another component
2176 * has already registered.
2178 void tevent_get_trace_queue_callback(struct tevent_context *ev,
2179 tevent_trace_queue_callback_t *cb,
2180 void *p_private_data);
2182 #ifdef DOXYGEN
2184 * @brief Create and start a tevent queue.
2186 * @param[in] mem_ctx The talloc memory context to allocate the queue.
2188 * @param[in] name The name to use to identify the queue.
2190 * @return An allocated tevent queue on success, NULL on error.
2192 * @see tevent_queue_start()
2193 * @see tevent_queue_stop()
2195 struct tevent_queue *tevent_queue_create(TALLOC_CTX *mem_ctx,
2196 const char *name);
2197 #else
2198 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
2199 const char *name,
2200 const char *location);
2202 #define tevent_queue_create(_mem_ctx, _name) \
2203 _tevent_queue_create((_mem_ctx), (_name), __location__)
2204 #endif
2207 * @brief A callback trigger function run by the queue.
2209 * @param[in] req The tevent request the trigger function is executed on.
2211 * @param[in] private_data The private data pointer specified by
2212 * tevent_queue_add().
2214 * @see tevent_queue_add()
2215 * @see tevent_queue_add_entry()
2216 * @see tevent_queue_add_optimize_empty()
2218 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
2219 void *private_data);
2222 * @brief Add a tevent request to the queue.
2224 * @param[in] queue The queue to add the request.
2226 * @param[in] ev The event handle to use for the request.
2228 * @param[in] req The tevent request to add to the queue.
2230 * @param[in] trigger The function triggered by the queue when the request
2231 * is called. Since tevent 0.9.14 it's possible to
2232 * pass NULL, in order to just add a "blocker" to the
2233 * queue.
2235 * @param[in] private_data The private data passed to the trigger function.
2237 * @return True if the request has been successfully added, false
2238 * otherwise.
2240 bool tevent_queue_add(struct tevent_queue *queue,
2241 struct tevent_context *ev,
2242 struct tevent_req *req,
2243 tevent_queue_trigger_fn_t trigger,
2244 void *private_data);
2246 bool _tevent_queue_add(struct tevent_queue *queue,
2247 struct tevent_context *ev,
2248 struct tevent_req *req,
2249 tevent_queue_trigger_fn_t trigger,
2250 const char* trigger_name,
2251 void *private_data);
2253 #define tevent_queue_add(queue, ev, req, trigger, private_data) \
2254 _tevent_queue_add(queue, ev, req, trigger, #trigger, private_data)
2257 * @brief Add a tevent request to the queue.
2259 * The request can be removed from the queue by calling talloc_free()
2260 * (or a similar function) on the returned queue entry. This
2261 * is the only difference to tevent_queue_add().
2263 * @param[in] queue The queue to add the request.
2265 * @param[in] ev The event handle to use for the request.
2267 * @param[in] req The tevent request to add to the queue.
2269 * @param[in] trigger The function triggered by the queue when the request
2270 * is called. Since tevent 0.9.14 it's possible to
2271 * pass NULL, in order to just add a "blocker" to the
2272 * queue.
2274 * @param[in] private_data The private data passed to the trigger function.
2276 * @return a pointer to the tevent_queue_entry if the request
2277 * has been successfully added, NULL otherwise.
2279 * @see tevent_queue_add()
2280 * @see tevent_queue_add_optimize_empty()
2282 struct tevent_queue_entry *tevent_queue_add_entry(
2283 struct tevent_queue *queue,
2284 struct tevent_context *ev,
2285 struct tevent_req *req,
2286 tevent_queue_trigger_fn_t trigger,
2287 void *private_data);
2289 struct tevent_queue_entry *_tevent_queue_add_entry(
2290 struct tevent_queue *queue,
2291 struct tevent_context *ev,
2292 struct tevent_req *req,
2293 tevent_queue_trigger_fn_t trigger,
2294 const char* trigger_name,
2295 void *private_data);
2297 #define tevent_queue_add_entry(queue, ev, req, trigger, private_data) \
2298 _tevent_queue_add_entry(queue, ev, req, trigger, #trigger, private_data);
2301 * @brief Add a tevent request to the queue using a possible optimization.
2303 * This tries to optimize for the empty queue case and may calls
2304 * the trigger function directly. This is the only difference compared
2305 * to tevent_queue_add_entry().
2307 * The caller needs to be prepared that the trigger function has
2308 * already called tevent_req_notify_callback(), tevent_req_error(),
2309 * tevent_req_done() or a similar function.
2311 * The trigger function has no chance to see the returned
2312 * queue_entry in the optimized case.
2314 * The request can be removed from the queue by calling talloc_free()
2315 * (or a similar function) on the returned queue entry.
2317 * @param[in] queue The queue to add the request.
2319 * @param[in] ev The event handle to use for the request.
2321 * @param[in] req The tevent request to add to the queue.
2323 * @param[in] trigger The function triggered by the queue when the request
2324 * is called. Since tevent 0.9.14 it's possible to
2325 * pass NULL, in order to just add a "blocker" to the
2326 * queue.
2328 * @param[in] private_data The private data passed to the trigger function.
2330 * @return a pointer to the tevent_queue_entry if the request
2331 * has been successfully added, NULL otherwise.
2333 * @see tevent_queue_add()
2334 * @see tevent_queue_add_entry()
2336 struct tevent_queue_entry *tevent_queue_add_optimize_empty(
2337 struct tevent_queue *queue,
2338 struct tevent_context *ev,
2339 struct tevent_req *req,
2340 tevent_queue_trigger_fn_t trigger,
2341 void *private_data);
2343 struct tevent_queue_entry *_tevent_queue_add_optimize_empty(
2344 struct tevent_queue *queue,
2345 struct tevent_context *ev,
2346 struct tevent_req *req,
2347 tevent_queue_trigger_fn_t trigger,
2348 const char* trigger_name,
2349 void *private_data);
2351 #define tevent_queue_add_optimize_empty(queue, ev, req, trigger, private_data) \
2352 _tevent_queue_add_optimize_empty(queue, ev, req, trigger, #trigger, private_data)
2355 * @brief Untrigger an already triggered queue entry.
2357 * If a trigger function detects that it needs to remain
2358 * in the queue, it needs to call tevent_queue_stop()
2359 * followed by tevent_queue_entry_untrigger().
2361 * @note In order to call tevent_queue_entry_untrigger()
2362 * the queue must be already stopped and the given queue_entry
2363 * must be the first one in the queue! Otherwise it calls abort().
2365 * @note You can't use this together with tevent_queue_add_optimize_empty()
2366 * because the trigger function don't have access to the quene entry
2367 * in the case of an empty queue.
2369 * @param[in] queue_entry The queue entry to rearm.
2371 * @see tevent_queue_add_entry()
2372 * @see tevent_queue_stop()
2374 void tevent_queue_entry_untrigger(struct tevent_queue_entry *entry);
2377 * @brief Start a tevent queue.
2379 * The queue is started by default.
2381 * @param[in] queue The queue to start.
2383 void tevent_queue_start(struct tevent_queue *queue);
2386 * @brief Stop a tevent queue.
2388 * The queue is started by default.
2390 * @param[in] queue The queue to stop.
2392 void tevent_queue_stop(struct tevent_queue *queue);
2395 * @brief Get the length of the queue.
2397 * @param[in] queue The queue to get the length from.
2399 * @return The number of elements.
2401 size_t tevent_queue_length(struct tevent_queue *queue);
2404 * @brief Is the tevent queue running.
2406 * The queue is started by default.
2408 * @param[in] queue The queue.
2410 * @return Whether the queue is running or not..
2412 bool tevent_queue_running(struct tevent_queue *queue);
2415 * @brief Create a tevent subrequest that waits in a tevent_queue
2417 * The idea is that always the same syntax for tevent requests.
2419 * @param[in] mem_ctx The talloc memory context to use.
2421 * @param[in] ev The event handle to setup the request.
2423 * @param[in] queue The queue to wait in.
2425 * @return The new subrequest, NULL on error.
2427 * @see tevent_queue_wait_recv()
2429 struct tevent_req *tevent_queue_wait_send(TALLOC_CTX *mem_ctx,
2430 struct tevent_context *ev,
2431 struct tevent_queue *queue);
2434 * @brief Check if we no longer need to wait in the queue.
2436 * This function needs to be called in the callback function set after calling
2437 * tevent_queue_wait_send().
2439 * @param[in] req The tevent request to check.
2441 * @return True on success, false otherwise.
2443 * @see tevent_queue_wait_send()
2445 bool tevent_queue_wait_recv(struct tevent_req *req);
2447 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
2448 void *private_data,
2449 uint32_t level,
2450 bool begin,
2451 void *stack_ptr,
2452 const char *location);
2455 * @brief Create a tevent_thread_proxy for message passing between threads.
2457 * The tevent_context must have been allocated on the NULL
2458 * talloc context, and talloc_disable_null_tracking() must
2459 * have been called.
2461 * @param[in] dest_ev_ctx The tevent_context to receive events.
2463 * @return An allocated tevent_thread_proxy, NULL on error.
2464 * If tevent was compiled without PTHREAD support
2465 * NULL is always returned and errno set to ENOSYS.
2467 * @see tevent_thread_proxy_schedule()
2469 struct tevent_thread_proxy *tevent_thread_proxy_create(
2470 struct tevent_context *dest_ev_ctx);
2473 * @brief Schedule an immediate event on an event context from another thread.
2475 * Causes dest_ev_ctx, being run by another thread, to receive an
2476 * immediate event calling the handler with the *pp_private parameter.
2478 * *pp_im must be a pointer to an immediate event talloced on a context owned
2479 * by the calling thread, or the NULL context. Ownership will
2480 * be transferred to the tevent_thread_proxy and *pp_im will be returned as NULL.
2482 * *pp_private_data must be a talloced area of memory with no destructors.
2483 * Ownership of this memory will be transferred to the tevent library and
2484 * *pp_private_data will be set to NULL on successful completion of
2485 * the call. Set pp_private to NULL if no parameter transfer
2486 * needed (a pure callback). This is an asynchronous request, caller
2487 * does not wait for callback to be completed before returning.
2489 * @param[in] tp The tevent_thread_proxy to use.
2491 * @param[in] pp_im Pointer to immediate event pointer.
2493 * @param[in] handler The function that will be called.
2495 * @param[in] pp_private_data The talloced memory to transfer.
2497 * @see tevent_thread_proxy_create()
2499 void tevent_thread_proxy_schedule(struct tevent_thread_proxy *tp,
2500 struct tevent_immediate **pp_im,
2501 tevent_immediate_handler_t handler,
2502 void *pp_private_data);
2505 * @brief Create a context for threaded activation of immediates
2507 * A tevent_treaded_context provides a link into an event
2508 * context. Using tevent_threaded_schedule_immediate, it is possible
2509 * to activate an immediate event from within a thread.
2511 * It is the duty of the caller of tevent_threaded_context_create() to
2512 * keep the event context around longer than any
2513 * tevent_threaded_context. tevent will abort if ev is talloc_free'ed
2514 * with an active tevent_threaded_context.
2516 * If tevent is build without pthread support, this always returns
2517 * NULL with errno=ENOSYS.
2519 * @param[in] mem_ctx The talloc memory context to use.
2520 * @param[in] ev The event context to link this to.
2521 * @return The threaded context, or NULL with errno set.
2523 * @see tevent_threaded_schedule_immediate()
2525 * @note Available as of tevent 0.9.30
2527 struct tevent_threaded_context *tevent_threaded_context_create(
2528 TALLOC_CTX *mem_ctx, struct tevent_context *ev);
2530 #ifdef DOXYGEN
2532 * @brief Activate an immediate from a thread
2534 * Activate an immediate from within a thread.
2536 * This routine does not watch out for talloc hierarchies. This means
2537 * that it is highly recommended to create the tevent_immediate in the
2538 * thread owning tctx, allocate a threaded job description for the
2539 * thread, hand over both pointers to a helper thread and not touch it
2540 * in the main thread at all anymore.
2542 * tevent_threaded_schedule_immediate is intended as a job completion
2543 * indicator for simple threaded helpers.
2545 * Please be aware that tevent_threaded_schedule_immediate is very
2546 * picky about its arguments: An immediate may not already be
2547 * activated and the handler must exist. With
2548 * tevent_threaded_schedule_immediate memory ownership is transferred
2549 * to the main thread holding the tevent context behind tctx, the
2550 * helper thread can't access it anymore.
2552 * @param[in] tctx The threaded context to go through
2553 * @param[in] im The immediate event to activate
2554 * @param[in] handler The immediate handler to call in the main thread
2555 * @param[in] private_data Pointer for the immediate handler
2557 * @see tevent_threaded_context_create()
2559 * @note Available as of tevent 0.9.30
2561 void tevent_threaded_schedule_immediate(struct tevent_threaded_context *tctx,
2562 struct tevent_immediate *im,
2563 tevent_immediate_handler_t handler,
2564 void *private_data);
2565 #else
2566 void _tevent_threaded_schedule_immediate(struct tevent_threaded_context *tctx,
2567 struct tevent_immediate *im,
2568 tevent_immediate_handler_t handler,
2569 void *private_data,
2570 const char *handler_name,
2571 const char *location);
2572 #define tevent_threaded_schedule_immediate(tctx, im, handler, private_data) \
2573 _tevent_threaded_schedule_immediate(tctx, im, handler, private_data, \
2574 #handler, __location__);
2575 #endif
2577 #ifdef TEVENT_DEPRECATED
2578 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
2579 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
2580 tevent_nesting_hook hook,
2581 void *private_data) _DEPRECATED_;
2582 int _tevent_loop_until(struct tevent_context *ev,
2583 bool (*finished)(void *private_data),
2584 void *private_data,
2585 const char *location) _DEPRECATED_;
2586 #define tevent_loop_until(ev, finished, private_data) \
2587 _tevent_loop_until(ev, finished, private_data, __location__)
2588 #endif
2590 int tevent_re_initialise(struct tevent_context *ev);
2592 /* @} */
2595 * @defgroup tevent_ops The tevent operation functions
2596 * @ingroup tevent
2598 * The following structure and registration functions are exclusively
2599 * needed for people writing and pluggin a different event engine.
2600 * There is nothing useful for normal tevent user in here.
2601 * @{
2604 struct tevent_ops {
2605 /* context init */
2606 int (*context_init)(struct tevent_context *ev);
2608 /* fd_event functions */
2609 struct tevent_fd *(*add_fd)(struct tevent_context *ev,
2610 TALLOC_CTX *mem_ctx,
2611 int fd, uint16_t flags,
2612 tevent_fd_handler_t handler,
2613 void *private_data,
2614 const char *handler_name,
2615 const char *location);
2616 void (*set_fd_close_fn)(struct tevent_fd *fde,
2617 tevent_fd_close_fn_t close_fn);
2618 uint16_t (*get_fd_flags)(struct tevent_fd *fde);
2619 void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
2621 /* timed_event functions */
2622 struct tevent_timer *(*add_timer)(struct tevent_context *ev,
2623 TALLOC_CTX *mem_ctx,
2624 struct timeval next_event,
2625 tevent_timer_handler_t handler,
2626 void *private_data,
2627 const char *handler_name,
2628 const char *location);
2630 /* immediate event functions */
2631 void (*schedule_immediate)(struct tevent_immediate *im,
2632 struct tevent_context *ev,
2633 tevent_immediate_handler_t handler,
2634 void *private_data,
2635 const char *handler_name,
2636 const char *location);
2638 /* signal functions */
2639 struct tevent_signal *(*add_signal)(struct tevent_context *ev,
2640 TALLOC_CTX *mem_ctx,
2641 int signum, int sa_flags,
2642 tevent_signal_handler_t handler,
2643 void *private_data,
2644 const char *handler_name,
2645 const char *location);
2647 /* loop functions */
2648 int (*loop_once)(struct tevent_context *ev, const char *location);
2649 int (*loop_wait)(struct tevent_context *ev, const char *location);
2652 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
2653 const struct tevent_ops *tevent_find_ops_byname(const char *name);
2655 /* @} */
2657 #ifdef TEVENT_DEPRECATED
2659 * @defgroup tevent_wrapper_ops The tevent wrapper operation functions
2660 * @ingroup tevent
2662 * The following structure and registration functions are exclusively
2663 * needed for people writing wrapper functions for event handlers
2664 * e.g. wrappers can be used for debugging/profiling or impersonation.
2666 * There is nothing useful for normal tevent user in here.
2668 * @note That the close_fn() on tevent_fd is *NOT* wrapped!
2670 * @see tevent_context_wrapper_create
2671 * @see tevent_fd_set_auto_close
2672 * @{
2675 struct tevent_wrapper_ops {
2676 const char *name;
2678 bool (*before_use)(struct tevent_context *wrap_ev,
2679 void *private_state,
2680 struct tevent_context *main_ev,
2681 const char *location);
2682 void (*after_use)(struct tevent_context *wrap_ev,
2683 void *private_state,
2684 struct tevent_context *main_ev,
2685 const char *location);
2687 void (*before_fd_handler)(struct tevent_context *wrap_ev,
2688 void *private_state,
2689 struct tevent_context *main_ev,
2690 struct tevent_fd *fde,
2691 uint16_t flags,
2692 const char *handler_name,
2693 const char *location);
2694 void (*after_fd_handler)(struct tevent_context *wrap_ev,
2695 void *private_state,
2696 struct tevent_context *main_ev,
2697 struct tevent_fd *fde,
2698 uint16_t flags,
2699 const char *handler_name,
2700 const char *location);
2702 void (*before_timer_handler)(struct tevent_context *wrap_ev,
2703 void *private_state,
2704 struct tevent_context *main_ev,
2705 struct tevent_timer *te,
2706 struct timeval requested_time,
2707 struct timeval trigger_time,
2708 const char *handler_name,
2709 const char *location);
2710 void (*after_timer_handler)(struct tevent_context *wrap_ev,
2711 void *private_state,
2712 struct tevent_context *main_ev,
2713 struct tevent_timer *te,
2714 struct timeval requested_time,
2715 struct timeval trigger_time,
2716 const char *handler_name,
2717 const char *location);
2719 void (*before_immediate_handler)(struct tevent_context *wrap_ev,
2720 void *private_state,
2721 struct tevent_context *main_ev,
2722 struct tevent_immediate *im,
2723 const char *handler_name,
2724 const char *location);
2725 void (*after_immediate_handler)(struct tevent_context *wrap_ev,
2726 void *private_state,
2727 struct tevent_context *main_ev,
2728 struct tevent_immediate *im,
2729 const char *handler_name,
2730 const char *location);
2732 void (*before_signal_handler)(struct tevent_context *wrap_ev,
2733 void *private_state,
2734 struct tevent_context *main_ev,
2735 struct tevent_signal *se,
2736 int signum,
2737 int count,
2738 void *siginfo,
2739 const char *handler_name,
2740 const char *location);
2741 void (*after_signal_handler)(struct tevent_context *wrap_ev,
2742 void *private_state,
2743 struct tevent_context *main_ev,
2744 struct tevent_signal *se,
2745 int signum,
2746 int count,
2747 void *siginfo,
2748 const char *handler_name,
2749 const char *location);
2752 #ifdef DOXYGEN
2754 * @brief Create a wrapper tevent_context.
2756 * @param[in] main_ev The main event context to work on.
2758 * @param[in] mem_ctx The talloc memory context to use.
2760 * @param[in] ops The tevent_wrapper_ops function table.
2762 * @param[out] private_state The private state use by the wrapper functions.
2764 * @param[in] private_type The talloc type of the private_state.
2766 * @return The wrapper event context, NULL on error.
2768 * @note Available as of tevent 0.9.37
2769 * @note Deprecated as of tevent 0.9.38
2771 struct tevent_context *tevent_context_wrapper_create(struct tevent_context *main_ev,
2772 TALLOC_CTX *mem_ctx,
2773 const struct tevent_wrapper_ops *ops,
2774 void **private_state,
2775 const char *private_type);
2776 #else
2777 struct tevent_context *_tevent_context_wrapper_create(struct tevent_context *main_ev,
2778 TALLOC_CTX *mem_ctx,
2779 const struct tevent_wrapper_ops *ops,
2780 void *pstate,
2781 size_t psize,
2782 const char *type,
2783 const char *location) _DEPRECATED_;
2784 #define tevent_context_wrapper_create(main_ev, mem_ctx, ops, state, type) \
2785 _tevent_context_wrapper_create(main_ev, mem_ctx, ops, \
2786 state, sizeof(type), #type, __location__)
2787 #endif
2790 * @brief Check if the event context is a wrapper event context.
2792 * @param[in] ev The event context to work on.
2794 * @return Is a wrapper (true), otherwise (false).
2796 * @see tevent_context_wrapper_create()
2798 * @note Available as of tevent 0.9.37
2799 * @note Deprecated as of tevent 0.9.38
2801 bool tevent_context_is_wrapper(struct tevent_context *ev) _DEPRECATED_;
2803 #ifdef DOXYGEN
2805 * @brief Prepare the environment of a (wrapper) event context.
2807 * A caller might call this before passing a wrapper event context
2808 * to a tevent_req based *_send() function.
2810 * The wrapper event context might do something like impersonation.
2812 * tevent_context_push_use() must always be used in combination
2813 * with tevent_context_pop_use().
2815 * There is a global stack of currently active/busy wrapper event contexts.
2816 * Each wrapper can only appear once on that global stack!
2817 * The stack size is limited to 32 elements, which should be enough
2818 * for all useful scenarios.
2820 * In addition to an explicit tevent_context_push_use() also
2821 * the invocation of an immediate, timer or fd handler implicitly
2822 * pushes the wrapper on the stack.
2824 * Therefore there are some strict constraints for the usage of
2825 * tevent_context_push_use():
2826 * - It must not be called from within an event handler
2827 * that already acts on the wrapper.
2828 * - tevent_context_pop_use() must be called before
2829 * leaving the code block that called tevent_context_push_use().
2830 * - The caller is responsible ensure the correct stack ordering
2831 * - Any violation of these constraints results in calling
2832 * the abort handler of the given tevent context.
2834 * Calling tevent_context_push_use() on a raw event context
2835 * still consumes an element on the stack, but it's otherwise
2836 * a no-op.
2838 * If tevent_context_push_use() returns false, it means
2839 * that the wrapper's before_use() hook returned this failure,
2840 * in that case you must not call tevent_context_pop_use() as
2841 * the wrapper is not pushed onto the stack.
2843 * @param[in] ev The event context to work on.
2845 * @return Success (true) or failure (false).
2847 * @note This is only needed if wrapper event contexts are in use.
2849 * @see tevent_context_pop_use
2851 * @note Available as of tevent 0.9.37
2852 * @note Deprecated as of tevent 0.9.38
2854 bool tevent_context_push_use(struct tevent_context *ev);
2855 #else
2856 bool _tevent_context_push_use(struct tevent_context *ev,
2857 const char *location) _DEPRECATED_;
2858 #define tevent_context_push_use(ev) \
2859 _tevent_context_push_use(ev, __location__)
2860 #endif
2862 #ifdef DOXYGEN
2864 * @brief Release the environment of a (wrapper) event context.
2866 * The wrapper event context might undo something like impersonation.
2868 * This must be called after a successful tevent_context_push_use().
2869 * Any ordering violation results in calling
2870 * the abort handler of the given tevent context.
2872 * This basically calls the wrapper's after_use() hook.
2874 * @param[in] ev The event context to work on.
2876 * @note This is only needed if wrapper event contexts are in use.
2878 * @see tevent_context_push_use
2880 * @note Available as of tevent 0.9.37
2881 * @note Deprecated as of tevent 0.9.38
2883 void tevent_context_pop_use(struct tevent_context *ev);
2884 #else
2885 void _tevent_context_pop_use(struct tevent_context *ev,
2886 const char *location) _DEPRECATED_;
2887 #define tevent_context_pop_use(ev) \
2888 _tevent_context_pop_use(ev, __location__)
2889 #endif
2892 * @brief Check is the two context pointers belong to the same low level loop
2894 * With the introduction of wrapper contexts it's not trivial
2895 * to check if two context pointers belong to the same low level
2896 * event loop. Some code may need to know this in order
2897 * to make some caching decisions.
2899 * @param[in] ev1 The first event context.
2900 * @param[in] ev2 The second event context.
2902 * @return true if both contexts belong to the same (still existing) context
2903 * loop, false otherwise.
2905 * @see tevent_context_wrapper_create
2907 * @note Available as of tevent 0.9.37
2908 * @note Deprecated as of tevent 0.9.38
2910 bool tevent_context_same_loop(struct tevent_context *ev1,
2911 struct tevent_context *ev2) _DEPRECATED_;
2913 /* @} */
2914 #endif /* TEVENT_DEPRECATED */
2917 * @defgroup tevent_compat The tevent compatibility functions
2918 * @ingroup tevent
2920 * The following definitions are useful only for compatibility with the
2921 * implementation originally developed within the samba4 code and will be
2922 * soon removed. Please NEVER use in new code.
2924 * @todo Ignore it?
2926 * @{
2929 #ifdef TEVENT_COMPAT_DEFINES
2931 #define event_context tevent_context
2932 #define event_ops tevent_ops
2933 #define fd_event tevent_fd
2934 #define timed_event tevent_timer
2935 #define signal_event tevent_signal
2937 #define event_fd_handler_t tevent_fd_handler_t
2938 #define event_timed_handler_t tevent_timer_handler_t
2939 #define event_signal_handler_t tevent_signal_handler_t
2941 #define event_context_init(mem_ctx) \
2942 tevent_context_init(mem_ctx)
2944 #define event_context_init_byname(mem_ctx, name) \
2945 tevent_context_init_byname(mem_ctx, name)
2947 #define event_backend_list(mem_ctx) \
2948 tevent_backend_list(mem_ctx)
2950 #define event_set_default_backend(backend) \
2951 tevent_set_default_backend(backend)
2953 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
2954 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
2956 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
2957 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
2959 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
2960 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
2962 #define event_loop_once(ev) \
2963 tevent_loop_once(ev)
2965 #define event_loop_wait(ev) \
2966 tevent_loop_wait(ev)
2968 #define event_get_fd_flags(fde) \
2969 tevent_fd_get_flags(fde)
2971 #define event_set_fd_flags(fde, flags) \
2972 tevent_fd_set_flags(fde, flags)
2974 #define EVENT_FD_READ TEVENT_FD_READ
2975 #define EVENT_FD_WRITE TEVENT_FD_WRITE
2977 #define EVENT_FD_WRITEABLE(fde) \
2978 TEVENT_FD_WRITEABLE(fde)
2980 #define EVENT_FD_READABLE(fde) \
2981 TEVENT_FD_READABLE(fde)
2983 #define EVENT_FD_NOT_WRITEABLE(fde) \
2984 TEVENT_FD_NOT_WRITEABLE(fde)
2986 #define EVENT_FD_NOT_READABLE(fde) \
2987 TEVENT_FD_NOT_READABLE(fde)
2989 #define ev_debug_level tevent_debug_level
2991 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
2992 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
2993 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
2994 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
2996 #define ev_set_debug(ev, debug, context) \
2997 tevent_set_debug(ev, debug, context)
2999 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
3001 #endif /* TEVENT_COMPAT_DEFINES */
3003 /* @} */
3005 #endif /* __TEVENT_H__ */