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
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/>.
34 #include <sys/types.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
42 #ifdef TEVENT_DEPRECATED
44 #if __has_attribute(deprecated) || (__GNUC__ >= 3)
45 #define _DEPRECATED_ __attribute__ ((deprecated))
52 struct tevent_context
;
56 struct tevent_immediate
;
58 struct tevent_thread_proxy
;
59 struct tevent_threaded_context
;
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.
73 /* event handler types */
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
,
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
,
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
,
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
,
107 * Called after tevent detects the specified signal.
109 typedef void (*tevent_signal_handler_t
)(struct tevent_context
*ev
,
110 struct tevent_signal
*se
,
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
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
);
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, #TEVENT_FD_WRITE or #TEVENT_FD_ERROR.
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
203 struct tevent_fd
*tevent_add_fd(struct tevent_context
*ev
,
207 tevent_fd_handler_t handler
,
210 struct tevent_fd
*_tevent_add_fd(struct tevent_context
*ev
,
214 tevent_fd_handler_t handler
,
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__)
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
);
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
,
271 struct timeval next_event
,
272 tevent_timer_handler_t handler
,
275 struct tevent_timer
*_tevent_add_timer(struct tevent_context
*ev
,
277 struct timeval next_event
,
278 tevent_timer_handler_t handler
,
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__)
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
);
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
);
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__)
338 * Schedule an event for immediate execution. This event will occur
339 * immediately after returning from the current event (before any other
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
,
352 void _tevent_schedule_immediate(struct tevent_immediate
*im
,
353 struct tevent_context
*ctx
,
354 tevent_immediate_handler_t handler
,
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__);
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
);
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
,
410 tevent_signal_handler_t handler
,
413 struct tevent_signal
*_tevent_add_signal(struct tevent_context
*ev
,
417 tevent_signal_handler_t handler
,
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__)
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);
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
);
479 int _tevent_loop_once(struct tevent_context
*ev
, const char *location
);
480 #define tevent_loop_once(ev) \
481 _tevent_loop_once(ev, __location__)
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
495 int tevent_loop_wait(struct tevent_context
*ev
);
497 int _tevent_loop_wait(struct tevent_context
*ev
, const char *location
);
498 #define tevent_loop_wait(ev) \
499 _tevent_loop_wait(ev, __location__)
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,
539 * #TEVENT_FD_WRITE and #TEVENT_FD_ERROR
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,
548 * #TEVENT_FD_WRITE and #TEVENT_FD_ERROR
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 and errors
568 * Note: we map this from/to POLLIN, POLLHUP, POLLERR and
569 * where available POLLRDHUP
571 #define TEVENT_FD_READ 1
573 * Monitor a file descriptor for writeability
575 * Note: we map this from/to POLLOUT
577 #define TEVENT_FD_WRITE 2
579 * Monitor a file descriptor for errors
581 * Note: we map this from/to POLLHUP, POLLERR and
582 * where available POLLRDHUP
584 #define TEVENT_FD_ERROR 4
587 * Convenience function for declaring a tevent_fd writable
589 #define TEVENT_FD_WRITEABLE(fde) \
590 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
593 * Convenience function for declaring a tevent_fd readable
595 #define TEVENT_FD_READABLE(fde) \
596 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
599 * Convenience function for declaring a tevent_fd waiting for errors
601 #define TEVENT_FD_WANTERROR(fde) \
602 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_ERROR)
605 * Convenience function for declaring a tevent_fd non-writable
607 #define TEVENT_FD_NOT_WRITEABLE(fde) \
608 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
611 * Convenience function for declaring a tevent_fd non-readable
613 #define TEVENT_FD_NOT_READABLE(fde) \
614 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
617 * Convenience function for declaring a tevent_fd not waiting for errors
619 #define TEVENT_FD_NOT_WANTERROR(fde) \
620 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_ERROR)
623 * Debug level of tevent
625 enum tevent_debug_level
{
628 TEVENT_DEBUG_WARNING
,
633 * @brief The tevent debug callbac.
635 * @param[in] context The memory context to use.
637 * @param[in] level The debug level.
639 * @param[in] fmt The format string.
641 * @param[in] ap The arguments for the format string.
643 typedef void (*tevent_debug_fn
)(void *context
,
644 enum tevent_debug_level level
,
646 va_list ap
) PRINTF_ATTRIBUTE(3,0);
649 * Set destination for tevent debug messages
651 * As of version 0.15.0 the invocation of
652 * the debug function for individual messages
653 * is limited by the current max_debug_level,
654 * which means TEVENT_DEBUG_TRACE messages
655 * are not passed by default:
657 * - tevent_set_debug() with debug == NULL implies
658 * tevent_set_max_debug_level(ev, TEVENT_DEBUG_FATAL).
660 * - tevent_set_debug() with debug != NULL implies
661 * tevent_set_max_debug_level(ev, TEVENT_DEBUG_WARNING).
663 * @param[in] ev Event context to debug
664 * @param[in] debug Function to handle output printing
665 * @param[in] context The context to pass to the debug function.
667 * @return Always returns 0 as of version 0.9.8
669 * @note Default is to emit no debug messages
671 * @see tevent_set_max_debug_level()
673 int tevent_set_debug(struct tevent_context
*ev
,
674 tevent_debug_fn debug
,
678 * Set maximum debug level for tevent debug messages
680 * @param[in] ev Event context to debug
681 * @param[in] max_level Function to handle output printing
683 * @return The former max level is returned.
685 * @see tevent_set_debug()
687 * @note Available as of tevent 0.15.0
689 enum tevent_debug_level
690 tevent_set_max_debug_level(struct tevent_context
*ev
,
691 enum tevent_debug_level max_level
);
694 * Designate stderr for debug message output
696 * @param[in] ev Event context to debug
698 * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
699 * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
700 * function for tevent_set_debug()
702 int tevent_set_debug_stderr(struct tevent_context
*ev
);
704 enum tevent_trace_point
{
706 * Corresponds to a trace point just before waiting
708 TEVENT_TRACE_BEFORE_WAIT
,
710 * Corresponds to a trace point just after waiting
712 TEVENT_TRACE_AFTER_WAIT
,
713 #define TEVENT_HAS_LOOP_ONCE_TRACE_POINTS 1
715 * Corresponds to a trace point just before calling
716 * the loop_once() backend function.
718 TEVENT_TRACE_BEFORE_LOOP_ONCE
,
720 * Corresponds to a trace point right after the
721 * loop_once() backend function has returned.
723 TEVENT_TRACE_AFTER_LOOP_ONCE
,
726 typedef void (*tevent_trace_callback_t
)(enum tevent_trace_point
,
730 * Register a callback to be called at certain trace points
732 * @param[in] ev Event context
733 * @param[in] cb Trace callback
734 * @param[in] private_data Data to be passed to callback
736 * @note The callback will be called at trace points defined by
737 * tevent_trace_point. Call with NULL to reset.
739 void tevent_set_trace_callback(struct tevent_context
*ev
,
740 tevent_trace_callback_t cb
,
744 * Retrieve the current trace callback
746 * @param[in] ev Event context
747 * @param[out] cb Registered trace callback
748 * @param[out] private_data Registered data to be passed to callback
750 * @note This can be used to allow one component that wants to
751 * register a callback to respect the callback that another component
752 * has already registered.
754 void tevent_get_trace_callback(struct tevent_context
*ev
,
755 tevent_trace_callback_t
*cb
,
758 enum tevent_event_trace_point
{
760 * Corresponds to a trace point just before the event is added.
762 TEVENT_EVENT_TRACE_ATTACH
,
765 * Corresponds to a trace point just before the event is removed.
767 TEVENT_EVENT_TRACE_DETACH
,
770 * Corresponds to a trace point just before the event handler is called.
772 TEVENT_EVENT_TRACE_BEFORE_HANDLER
,
775 typedef void (*tevent_trace_fd_callback_t
)(struct tevent_fd
*fde
,
776 enum tevent_event_trace_point
,
779 typedef void (*tevent_trace_signal_callback_t
)(struct tevent_signal
*se
,
780 enum tevent_event_trace_point
,
783 typedef void (*tevent_trace_timer_callback_t
)(struct tevent_timer
*te
,
784 enum tevent_event_trace_point
,
787 typedef void (*tevent_trace_immediate_callback_t
)(struct tevent_immediate
*im
,
788 enum tevent_event_trace_point
,
792 * Register a callback to be called at certain trace points of fd event.
794 * @param[in] ev Event context
795 * @param[in] cb Trace callback
796 * @param[in] private_data Data to be passed to callback
798 * @note The callback will be called at trace points defined by
799 * tevent_event_trace_point. Call with NULL to reset.
801 void tevent_set_trace_fd_callback(struct tevent_context
*ev
,
802 tevent_trace_fd_callback_t cb
,
806 * Retrieve the current trace callback of file descriptor event.
808 * @param[in] ev Event context
809 * @param[out] cb Registered trace callback
810 * @param[out] p_private_data Registered data to be passed to callback
812 * @note This can be used to allow one component that wants to
813 * register a callback to respect the callback that another component
814 * has already registered.
816 void tevent_get_trace_fd_callback(struct tevent_context
*ev
,
817 tevent_trace_fd_callback_t
*cb
,
818 void *p_private_data
);
821 * Register a callback to be called at certain trace points of signal event.
823 * @param[in] ev Event context
824 * @param[in] cb Trace callback
825 * @param[in] private_data Data to be passed to callback
827 * @note The callback will be called at trace points defined by
828 * tevent_event_trace_point. Call with NULL to reset.
830 void tevent_set_trace_signal_callback(struct tevent_context
*ev
,
831 tevent_trace_signal_callback_t cb
,
835 * Retrieve the current trace callback of signal event.
837 * @param[in] ev Event context
838 * @param[out] cb Registered trace callback
839 * @param[out] p_private_data Registered data to be passed to callback
841 * @note This can be used to allow one component that wants to
842 * register a callback to respect the callback that another component
843 * has already registered.
845 void tevent_get_trace_signal_callback(struct tevent_context
*ev
,
846 tevent_trace_signal_callback_t
*cb
,
847 void *p_private_data
);
850 * Register a callback to be called at certain trace points of timer event.
852 * @param[in] ev Event context
853 * @param[in] cb Trace callback
854 * @param[in] private_data Data to be passed to callback
856 * @note The callback will be called at trace points defined by
857 * tevent_event_trace_point. Call with NULL to reset.
859 void tevent_set_trace_timer_callback(struct tevent_context
*ev
,
860 tevent_trace_timer_callback_t cb
,
864 * Retrieve the current trace callback of timer event.
866 * @param[in] ev Event context
867 * @param[out] cb Registered trace callback
868 * @param[out] p_private_data Registered data to be passed to callback
870 * @note This can be used to allow one component that wants to
871 * register a callback to respect the callback that another component
872 * has already registered.
874 void tevent_get_trace_timer_callback(struct tevent_context
*ev
,
875 tevent_trace_timer_callback_t
*cb
,
876 void *p_private_data
);
879 * Register a callback to be called at certain trace points of immediate event.
881 * @param[in] ev Event context
882 * @param[in] cb Trace callback
883 * @param[in] private_data Data to be passed to callback
885 * @note The callback will be called at trace points defined by
886 * tevent_event_trace_point. Call with NULL to reset.
888 void tevent_set_trace_immediate_callback(struct tevent_context
*ev
,
889 tevent_trace_immediate_callback_t cb
,
893 * Retrieve the current trace callback of immediate event.
895 * @param[in] ev Event context
896 * @param[out] cb Registered trace callback
897 * @param[out] p_private_data Registered data to be passed to callback
899 * @note This can be used to allow one component that wants to
900 * register a callback to respect the callback that another component
901 * has already registered.
903 void tevent_get_trace_immediate_callback(struct tevent_context
*ev
,
904 tevent_trace_immediate_callback_t
*cb
,
905 void *p_private_data
);
912 * @defgroup tevent_request The tevent request functions.
915 * A tevent_req represents an asynchronous computation.
917 * The tevent_req group of API calls is the recommended way of
918 * programming async computations within tevent. In particular the
919 * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
920 * are considered too low-level to be used in larger computations. To
921 * read and write from and to sockets, Samba provides two calls on top
922 * of tevent_add_fd: tstream_read_packet_send/recv and tstream_writev_send/recv.
923 * These requests are much easier to compose than the low-level event
924 * handlers called from tevent_add_fd.
926 * A lot of the simplicity tevent_req has brought to the notoriously
927 * hairy async programming came via a set of conventions that every
928 * async computation programmed should follow. One central piece of
929 * these conventions is the naming of routines and variables.
931 * Every async computation needs a name (sensibly called "computation"
932 * down from here). From this name quite a few naming conventions are
935 * Every computation that requires local state needs a
937 * struct computation_state {
941 * Even if no local variables are required, such a state struct should
942 * be created containing a dummy variable. Quite a few helper
943 * functions and macros (for example tevent_req_create()) assume such
946 * An async computation is started by a computation_send
947 * function. When it is finished, its result can be received by a
948 * computation_recv function. For an example how to set up an async
949 * computation, see the code example in the documentation for
950 * tevent_req_create() and tevent_req_post(). The prototypes for _send
951 * and _recv functions should follow some conventions:
954 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
955 * struct tevent_context *ev,
957 * int computation_recv(struct tevent_req *req, ... further output args);
960 * The "int" result of computation_recv() depends on the result the
961 * sync version of the function would have, "int" is just an example
964 * Another important piece of the conventions is that the program flow
965 * is interrupted as little as possible. Because a blocking
966 * sub-computation requires that the flow needs to continue in a
967 * separate function that is the logical sequel of some computation,
968 * it should lexically follow sending off the blocking
969 * sub-computation. Setting the callback function via
970 * tevent_req_set_callback() requires referencing a function lexically
971 * below the call to tevent_req_set_callback(), forward declarations
972 * are required. A lot of the async computations thus begin with a
973 * sequence of declarations such as
976 * static void computation_step1_done(struct tevent_req *subreq);
977 * static void computation_step2_done(struct tevent_req *subreq);
978 * static void computation_step3_done(struct tevent_req *subreq);
981 * It really helps readability a lot to do these forward declarations,
982 * because the lexically sequential program flow makes the async
983 * computations almost as clear to read as a normal, sync program
986 * It is up to the user of the async computation to talloc_free it
987 * after it has finished. If an async computation should be aborted,
988 * the tevent_req structure can be talloc_free'ed. After it has
989 * finished, it should talloc_free'ed by the API user.
991 * tevent_req variable naming conventions:
993 * The name of the variable pointing to the tevent_req structure
994 * returned by a _send() function SHOULD be named differently between
995 * implementation and caller.
997 * From the point of view of the implementation (of the _send() and
998 * _recv() functions) the variable returned by tevent_req_create() is
999 * always called @em req.
1001 * While the caller of the _send() function should use @em subreq to
1004 * @see tevent_req_create()
1005 * @see tevent_req_fn()
1011 * An async request moves from TEVENT_REQ_INIT to
1012 * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
1015 enum tevent_req_state
{
1017 * We are creating the request
1021 * We are waiting the request to complete
1023 TEVENT_REQ_IN_PROGRESS
,
1025 * The request is finished successfully
1029 * A user error has occurred. The user error has been
1030 * indicated by tevent_req_error(), it can be retrieved via
1031 * tevent_req_is_error().
1033 TEVENT_REQ_USER_ERROR
,
1035 * Request timed out after the timeout set by tevent_req_set_endtime.
1037 TEVENT_REQ_TIMED_OUT
,
1039 * An internal allocation has failed, or tevent_req_nomem has
1040 * been given a NULL pointer as the first argument.
1042 TEVENT_REQ_NO_MEMORY
,
1044 * The request has been received by the caller. No further
1051 * @brief An async request
1056 * @brief A tevent request callback function.
1058 * @param[in] subreq The tevent async request which executed this callback.
1060 typedef void (*tevent_req_fn
)(struct tevent_req
*subreq
);
1063 * @brief Set an async request callback.
1065 * See the documentation of tevent_req_post() for an example how this
1066 * is supposed to be used.
1068 * @param[in] req The async request to set the callback.
1070 * @param[in] fn The callback function to set.
1072 * @param[in] pvt A pointer to private data to pass to the async request
1075 void tevent_req_set_callback(struct tevent_req
*req
, tevent_req_fn fn
, void *pvt
);
1076 void _tevent_req_set_callback(struct tevent_req
*req
,
1078 const char *fn_name
,
1081 #define tevent_req_set_callback(req, fn, pvt) \
1082 _tevent_req_set_callback(req, fn, #fn, pvt)
1086 * @brief Get the private data cast to the given type for a callback from
1087 * a tevent request structure.
1090 * static void computation_done(struct tevent_req *subreq) {
1091 * struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
1092 * struct computation_state *state = tevent_req_data(req, struct computation_state);
1093 * .... more things, eventually maybe call tevent_req_done(req);
1097 * @param[in] req The structure to get the callback data from.
1099 * @param[in] type The type of the private callback data to get.
1101 * @return The type casted private data set NULL if not set.
1103 void *tevent_req_callback_data(struct tevent_req
*req
, #type);
1105 void *_tevent_req_callback_data(struct tevent_req
*req
);
1106 #define tevent_req_callback_data(_req, _type) \
1107 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
1112 * @brief Get the private data for a callback from a tevent request structure.
1114 * @param[in] req The structure to get the callback data from.
1116 * @return The private data or NULL if not set.
1118 void *tevent_req_callback_data_void(struct tevent_req
*req
);
1120 #define tevent_req_callback_data_void(_req) \
1121 _tevent_req_callback_data(_req)
1126 * @brief Get the private data from a tevent request structure.
1128 * When the tevent_req has been created by tevent_req_create, the
1129 * result of tevent_req_data() is the state variable created by
1130 * tevent_req_create() as a child of the req.
1132 * @param[in] req The structure to get the private data from.
1134 * @param[in] type The type of the private data
1136 * @return The private data or NULL if not set.
1138 void *tevent_req_data(struct tevent_req
*req
, #type);
1140 void *_tevent_req_data(struct tevent_req
*req
);
1141 #define tevent_req_data(_req, _type) \
1142 talloc_get_type_abort(_tevent_req_data(_req), _type)
1146 * @brief The print function which can be set for a tevent async request.
1148 * @param[in] req The tevent async request.
1150 * @param[in] ctx A talloc memory context which can be uses to allocate
1153 * @return An allocated string buffer to print.
1157 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
1159 * struct my_data *data = tevent_req_data(req, struct my_data);
1162 * result = tevent_req_default_print(mem_ctx, req);
1163 * if (result == NULL) {
1167 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
1168 * data->foo, data->bar);
1172 typedef char *(*tevent_req_print_fn
)(struct tevent_req
*req
, TALLOC_CTX
*ctx
);
1175 * @brief This function sets a print function for the given request.
1177 * This function can be used to setup a print function for the given request.
1178 * This will be triggered if the tevent_req_print() function was
1179 * called on the given request.
1181 * @param[in] req The request to use.
1183 * @param[in] fn A pointer to the print function
1185 * @note This function should only be used for debugging.
1187 void tevent_req_set_print_fn(struct tevent_req
*req
, tevent_req_print_fn fn
);
1190 * @brief The default print function for creating debug messages.
1192 * The function should not be used by users of the async API,
1193 * but custom print function can use it and append custom text
1196 * @param[in] req The request to be printed.
1198 * @param[in] mem_ctx The memory context for the result.
1200 * @return Text representation of request.
1203 char *tevent_req_default_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
);
1206 * @brief Print an tevent_req structure in debug messages.
1208 * This function should be used by callers of the async API.
1210 * @param[in] mem_ctx The memory context for the result.
1212 * @param[in] req The request to be printed.
1214 * @return Text representation of request.
1216 char *tevent_req_print(TALLOC_CTX
*mem_ctx
, struct tevent_req
*req
);
1219 * @brief A typedef for a cancel function for a tevent request.
1221 * @param[in] req The tevent request calling this function.
1223 * @return True if the request could be canceled, false if not.
1225 typedef bool (*tevent_req_cancel_fn
)(struct tevent_req
*req
);
1228 * @brief This function sets a cancel function for the given tevent request.
1230 * This function can be used to setup a cancel function for the given request.
1231 * This will be triggered if the tevent_req_cancel() function was
1232 * called on the given request.
1234 * @param[in] req The request to use.
1236 * @param[in] fn A pointer to the cancel function.
1238 void tevent_req_set_cancel_fn(struct tevent_req
*req
, tevent_req_cancel_fn fn
);
1239 void _tevent_req_set_cancel_fn(struct tevent_req
*req
,
1240 tevent_req_cancel_fn fn
,
1241 const char *fn_name
);
1242 #define tevent_req_set_cancel_fn(req, fn) \
1243 _tevent_req_set_cancel_fn(req, fn, #fn)
1247 * @brief Try to cancel the given tevent request.
1249 * This function can be used to cancel the given request.
1251 * It is only possible to cancel a request when the implementation
1252 * has registered a cancel function via the tevent_req_set_cancel_fn().
1254 * @param[in] req The request to use.
1256 * @return This function returns true if the request is
1257 * cancelable, otherwise false is returned.
1259 * @note Even if the function returns true, the caller need to wait
1260 * for the function to complete normally.
1261 * Only the _recv() function of the given request indicates
1262 * if the request was really canceled.
1264 bool tevent_req_cancel(struct tevent_req
*req
);
1266 bool _tevent_req_cancel(struct tevent_req
*req
, const char *location
);
1267 #define tevent_req_cancel(req) \
1268 _tevent_req_cancel(req, __location__)
1272 * @brief A typedef for a cleanup function for a tevent request.
1274 * @param[in] req The tevent request calling this function.
1276 * @param[in] req_state The current tevent_req_state.
1279 typedef void (*tevent_req_cleanup_fn
)(struct tevent_req
*req
,
1280 enum tevent_req_state req_state
);
1283 * @brief This function sets a cleanup function for the given tevent request.
1285 * This function can be used to setup a cleanup function for the given request.
1286 * This will be triggered when the tevent_req_done() or tevent_req_error()
1287 * function was called, before notifying the callers callback function,
1288 * and also before scheduling the deferred trigger.
1290 * This might be useful if more than one tevent_req belong together
1291 * and need to finish both requests at the same time.
1293 * The cleanup function is able to call tevent_req_done() or tevent_req_error()
1294 * recursively, the cleanup function is only triggered the first time.
1296 * The cleanup function is also called by tevent_req_received()
1297 * (possibly triggered from tevent_req_destructor()) before destroying
1298 * the private data of the tevent_req.
1300 * @param[in] req The request to use.
1302 * @param[in] fn A pointer to the cancel function.
1304 void tevent_req_set_cleanup_fn(struct tevent_req
*req
, tevent_req_cleanup_fn fn
);
1305 void _tevent_req_set_cleanup_fn(struct tevent_req
*req
,
1306 tevent_req_cleanup_fn fn
,
1307 const char *fn_name
);
1308 #define tevent_req_set_cleanup_fn(req, fn) \
1309 _tevent_req_set_cleanup_fn(req, fn, #fn)
1313 * @brief Create an async tevent request.
1315 * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
1318 * struct tevent_req *req;
1319 * struct computation_state *state;
1320 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1323 * Tevent_req_create() allocates and zeros the state variable as a talloc
1324 * child of its result. The state variable should be used as the talloc
1325 * parent for all temporary variables that are allocated during the async
1326 * computation. This way, when the user of the async computation frees
1327 * the request, the state as a talloc child will be free'd along with
1328 * all the temporary variables hanging off the state.
1330 * @param[in] mem_ctx The memory context for the result.
1331 * @param[in] pstate Pointer to the private request state.
1332 * @param[in] type The name of the request.
1334 * @return A new async request. NULL on error.
1336 struct tevent_req
*tevent_req_create(TALLOC_CTX
*mem_ctx
,
1337 void **pstate
, #type);
1339 struct tevent_req
*_tevent_req_create(TALLOC_CTX
*mem_ctx
,
1343 const char *location
);
1345 struct tevent_req
*__tevent_req_create(TALLOC_CTX
*mem_ctx
,
1350 const char *location
);
1352 #define tevent_req_create(_mem_ctx, _pstate, _type) \
1353 __tevent_req_create((_mem_ctx), \
1362 * @brief Set a timeout for an async request. On failure, "req" is already
1363 * set to state TEVENT_REQ_NO_MEMORY.
1365 * @param[in] req The request to set the timeout for.
1367 * @param[in] ev The event context to use for the timer.
1369 * @param[in] endtime The endtime of the request.
1371 * @return True if succeeded, false if not.
1373 bool tevent_req_set_endtime(struct tevent_req
*req
,
1374 struct tevent_context
*ev
,
1375 struct timeval endtime
);
1378 * @brief Reset the timer set by tevent_req_set_endtime.
1380 * @param[in] req The request to reset the timeout for
1382 void tevent_req_reset_endtime(struct tevent_req
*req
);
1386 * @brief Call the notify callback of the given tevent request manually.
1388 * @param[in] req The tevent request to call the notify function from.
1390 * @see tevent_req_set_callback()
1392 void tevent_req_notify_callback(struct tevent_req
*req
);
1394 void _tevent_req_notify_callback(struct tevent_req
*req
, const char *location
);
1395 #define tevent_req_notify_callback(req) \
1396 _tevent_req_notify_callback(req, __location__)
1401 * @brief An async request has successfully finished.
1403 * This function is to be used by implementors of async requests. When a
1404 * request is successfully finished, this function calls the user's completion
1407 * @param[in] req The finished request.
1409 void tevent_req_done(struct tevent_req
*req
);
1411 void _tevent_req_done(struct tevent_req
*req
,
1412 const char *location
);
1413 #define tevent_req_done(req) \
1414 _tevent_req_done(req, __location__)
1419 * @brief An async request has seen an error.
1421 * This function is to be used by implementors of async requests. When a
1422 * request can not successfully completed, the implementation should call this
1423 * function with the appropriate status code.
1425 * If error is 0 the function returns false and does nothing more.
1427 * @param[in] req The request with an error.
1429 * @param[in] error The error code.
1431 * @return On success true is returned, false if error is 0.
1434 * int error = first_function();
1435 * if (tevent_req_error(req, error)) {
1439 * error = second_function();
1440 * if (tevent_req_error(req, error)) {
1444 * tevent_req_done(req);
1448 bool tevent_req_error(struct tevent_req
*req
,
1451 bool _tevent_req_error(struct tevent_req
*req
,
1453 const char *location
);
1454 #define tevent_req_error(req, error) \
1455 _tevent_req_error(req, error, __location__)
1460 * @brief Helper function for nomem check.
1462 * Convenience helper to easily check alloc failure within a callback
1463 * implementing the next step of an async request.
1465 * @param[in] p The pointer to be checked.
1467 * @param[in] req The request being processed.
1470 * p = talloc(mem_ctx, bla);
1471 * if (tevent_req_nomem(p, req)) {
1476 bool tevent_req_nomem(const void *p
,
1477 struct tevent_req
*req
);
1479 bool _tevent_req_nomem(const void *p
,
1480 struct tevent_req
*req
,
1481 const char *location
);
1482 #define tevent_req_nomem(p, req) \
1483 _tevent_req_nomem(p, req, __location__)
1488 * @brief Indicate out of memory to a request
1490 * @param[in] req The request being processed.
1492 void tevent_req_oom(struct tevent_req
*req
);
1494 void _tevent_req_oom(struct tevent_req
*req
,
1495 const char *location
);
1496 #define tevent_req_oom(req) \
1497 _tevent_req_oom(req, __location__)
1501 * @brief Finish a request before the caller had a chance to set the callback.
1503 * An implementation of an async request might find that it can either finish
1504 * the request without waiting for an external event, or it can not even start
1505 * the engine. To present the illusion of a callback to the user of the API,
1506 * the implementation can call this helper function which triggers an
1507 * immediate event. This way the caller can use the same calling
1508 * conventions, independent of whether the request was actually deferred.
1511 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1512 * struct tevent_context *ev)
1514 * struct tevent_req *req, *subreq;
1515 * struct computation_state *state;
1516 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1517 * if (req == NULL) {
1520 * subreq = subcomputation_send(state, ev);
1521 * if (tevent_req_nomem(subreq, req)) {
1522 * return tevent_req_post(req, ev);
1524 * tevent_req_set_callback(subreq, computation_done, req);
1529 * @param[in] req The finished request.
1531 * @param[in] ev The tevent_context for the immediate event.
1533 * @return The given request will be returned.
1535 struct tevent_req
*tevent_req_post(struct tevent_req
*req
,
1536 struct tevent_context
*ev
);
1539 * @brief Finish multiple requests within one function
1541 * Normally tevent_req_notify_callback() and all wrappers
1542 * (e.g. tevent_req_done() and tevent_req_error())
1543 * need to be the last thing an event handler should call.
1544 * This is because the callback is likely to destroy the
1545 * context of the current function.
1547 * If a function wants to notify more than one caller,
1548 * it is dangerous if it just triggers multiple callbacks
1549 * in a row. With tevent_req_defer_callback() it is possible
1550 * to set an event context that will be used to defer the callback
1551 * via an immediate event (similar to tevent_req_post()).
1554 * struct complete_state {
1555 * struct tevent_context *ev;
1557 * struct tevent_req **reqs;
1560 * void complete(struct complete_state *state)
1562 * size_t i, c = talloc_array_length(state->reqs);
1564 * for (i=0; i < c; i++) {
1565 * tevent_req_defer_callback(state->reqs[i], state->ev);
1566 * tevent_req_done(state->reqs[i]);
1571 * @param[in] req The finished request.
1573 * @param[in] ev The tevent_context for the immediate event.
1575 * @return The given request will be returned.
1577 void tevent_req_defer_callback(struct tevent_req
*req
,
1578 struct tevent_context
*ev
);
1581 * @brief Check if the given request is still in progress.
1583 * It is typically used by sync wrapper functions.
1585 * @param[in] req The request to poll.
1587 * @return The boolean form of "is in progress".
1589 bool tevent_req_is_in_progress(struct tevent_req
*req
);
1592 * @brief Actively poll for the given request to finish.
1594 * This function is typically used by sync wrapper functions.
1596 * @param[in] req The request to poll.
1598 * @param[in] ev The tevent_context to be used.
1600 * @return On success true is returned. If a critical error has
1601 * happened in the tevent loop layer false is returned.
1602 * This is not the return value of the given request!
1604 * @note This should only be used if the given tevent context was created by the
1605 * caller, to avoid event loop nesting.
1608 * req = tstream_writev_queue_send(mem_ctx,
1613 * ok = tevent_req_poll(req, tctx->ev);
1614 * rc = tstream_writev_queue_recv(req, &sys_errno);
1618 bool tevent_req_poll(struct tevent_req
*req
,
1619 struct tevent_context
*ev
);
1622 * @brief Get the tevent request state and the actual error set by
1626 * int computation_recv(struct tevent_req *req, uint64_t *perr)
1628 * enum tevent_req_state state;
1630 * if (tevent_req_is_error(req, &state, &err)) {
1638 * @param[in] req The tevent request to get the error from.
1640 * @param[out] state A pointer to store the tevent request error state.
1642 * @param[out] error A pointer to store the error set by tevent_req_error().
1644 * @return True if the function could set error and state, false
1647 * @see tevent_req_error()
1649 bool tevent_req_is_error(struct tevent_req
*req
,
1650 enum tevent_req_state
*state
,
1654 * @brief Use as the last action of a _recv() function.
1656 * This function destroys the attached private data.
1658 * @param[in] req The finished request.
1660 void tevent_req_received(struct tevent_req
*req
);
1663 * @brief Mark a tevent_req for profiling
1665 * This will turn on profiling for this tevent_req an all subreqs that
1666 * are directly started as helper requests off this
1667 * tevent_req. subreqs are chained by walking up the talloc_parent
1668 * hierarchy at a subreq's tevent_req_create. This means to get the
1669 * profiling chain right the subreq that needs to be profiled as part
1670 * of this tevent_req's profile must be a talloc child of the requests
1673 * @param[in] req The request to do tracing for
1675 * @return False if the profile could not be activated
1677 bool tevent_req_set_profile(struct tevent_req
*req
);
1679 struct tevent_req_profile
;
1682 * @brief Get a request's profile for inspection
1684 * @param[in] req The request to get the profile from
1686 * @return The request's profile
1688 const struct tevent_req_profile
*tevent_req_get_profile(
1689 struct tevent_req
*req
);
1692 * @brief Move the profile out of a request
1694 * This function detaches the request's profile from the request, so
1695 * that the profile can outlive the request in a _recv function.
1697 * @param[in] req The request to move the profile out of
1698 * @param[in] mem_ctx The new talloc context for the profile
1700 * @return The moved profile
1703 struct tevent_req_profile
*tevent_req_move_profile(struct tevent_req
*req
,
1704 TALLOC_CTX
*mem_ctx
);
1707 * @brief Get a profile description
1709 * @param[in] profile The profile to be queried
1710 * @param[in] req_name The name of the request (state's name)
1712 * "req_name" after this call is still in talloc-posession of "profile"
1714 void tevent_req_profile_get_name(const struct tevent_req_profile
*profile
,
1715 const char **req_name
);
1718 * @brief Get a profile's start event data
1720 * @param[in] profile The profile to be queried
1721 * @param[in] start_location The location where this event started
1722 * @param[in] start_time The time this event started
1724 * "start_location" after this call is still in talloc-posession of "profile"
1726 void tevent_req_profile_get_start(const struct tevent_req_profile
*profile
,
1727 const char **start_location
,
1728 struct timeval
*start_time
);
1731 * @brief Get a profile's stop event data
1733 * @param[in] profile The profile to be queried
1734 * @param[in] stop_location The location where this event stopped
1735 * @param[in] stop_time The time this event stopped
1737 * "stop_location" after this call is still in talloc-posession of "profile"
1739 void tevent_req_profile_get_stop(const struct tevent_req_profile
*profile
,
1740 const char **stop_location
,
1741 struct timeval
*stop_time
);
1744 * @brief Get a profile's result data
1746 * @param[in] pid The process where this profile was taken
1747 * @param[in] state The status the profile's tevent_req finished with
1748 * @param[in] user_error The user error of the profile's tevent_req
1750 void tevent_req_profile_get_status(const struct tevent_req_profile
*profile
,
1752 enum tevent_req_state
*state
,
1753 uint64_t *user_error
);
1756 * @brief Retrieve the first subreq's profile from a profile
1758 * @param[in] profile The profile to query
1760 * @return The first tevent subreq's profile
1762 const struct tevent_req_profile
*tevent_req_profile_get_subprofiles(
1763 const struct tevent_req_profile
*profile
);
1766 * @brief Walk the chain of subreqs
1768 * @param[in] profile The subreq's profile to walk
1770 * @return The next subprofile in the list
1772 const struct tevent_req_profile
*tevent_req_profile_next(
1773 const struct tevent_req_profile
*profile
);
1776 * @brief Create a fresh tevent_req_profile
1778 * @param[in] mem_ctx The talloc context to hang the fresh struct off
1780 * @return The fresh struct
1782 struct tevent_req_profile
*tevent_req_profile_create(TALLOC_CTX
*mem_ctx
);
1785 * @brief Set a profile's name
1787 * @param[in] profile The profile to set the name for
1788 * @param[in] name The new name for the profile
1790 * @return True if the internal talloc_strdup succeeded
1792 bool tevent_req_profile_set_name(struct tevent_req_profile
*profile
,
1796 * @brief Set a profile's start event
1798 * @param[in] profile The profile to set the start data for
1799 * @param[in] start_location The new start location
1800 * @param[in] start_time The new start time
1802 * @return True if the internal talloc_strdup succeeded
1804 bool tevent_req_profile_set_start(struct tevent_req_profile
*profile
,
1805 const char *start_location
,
1806 struct timeval start_time
);
1809 * @brief Set a profile's stop event
1811 * @param[in] profile The profile to set the stop data for
1812 * @param[in] stop_location The new stop location
1813 * @param[in] stop_time The new stop time
1815 * @return True if the internal talloc_strdup succeeded
1817 bool tevent_req_profile_set_stop(struct tevent_req_profile
*profile
,
1818 const char *stop_location
,
1819 struct timeval stop_time
);
1822 * @brief Set a profile's exit status
1824 * @param[in] profile The profile to set the exit status for
1825 * @param[in] pid The process where this profile was taken
1826 * @param[in] state The status the profile's tevent_req finished with
1827 * @param[in] user_error The user error of the profile's tevent_req
1829 void tevent_req_profile_set_status(struct tevent_req_profile
*profile
,
1831 enum tevent_req_state state
,
1832 uint64_t user_error
);
1835 * @brief Add a subprofile to a profile
1837 * @param[in] parent_profile The profile to be modified
1838 * @param[in] sub_profile The subreqs profile profile to be added
1840 * "subreq" is talloc_move'ed into "parent_profile", so the talloc
1841 * ownership of "sub_profile" changes
1844 void tevent_req_profile_append_sub(struct tevent_req_profile
*parent_profile
,
1845 struct tevent_req_profile
**sub_profile
);
1848 * @brief Create a tevent subrequest at a given time.
1850 * The idea is that always the same syntax for tevent requests.
1852 * @param[in] mem_ctx The talloc memory context to use.
1854 * @param[in] ev The event handle to setup the request.
1856 * @param[in] wakeup_time The time to wakeup and execute the request.
1858 * @return The new subrequest, NULL on error.
1862 * static void my_callback_wakeup_done(tevent_req *subreq)
1864 * struct tevent_req *req = tevent_req_callback_data(subreq,
1865 * struct tevent_req);
1868 * ok = tevent_wakeup_recv(subreq);
1869 * TALLOC_FREE(subreq);
1871 * tevent_req_error(req, -1);
1879 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1880 * if (tevent_req_nomem(subreq, req)) {
1883 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1886 * @see tevent_wakeup_recv()
1888 struct tevent_req
*tevent_wakeup_send(TALLOC_CTX
*mem_ctx
,
1889 struct tevent_context
*ev
,
1890 struct timeval wakeup_time
);
1893 * @brief Check if the wakeup has been correctly executed.
1895 * This function needs to be called in the callback function set after calling
1896 * tevent_wakeup_send().
1898 * @param[in] req The tevent request to check.
1900 * @return True on success, false otherwise.
1902 * @see tevent_wakeup_recv()
1904 bool tevent_wakeup_recv(struct tevent_req
*req
);
1909 * @defgroup tevent_helpers The tevent helper functions
1918 * @brief Compare two timeval values.
1920 * @param[in] tv1 The first timeval value to compare.
1922 * @param[in] tv2 The second timeval value to compare.
1924 * @return 0 if they are equal.
1925 * 1 if the first time is greater than the second.
1926 * -1 if the first time is smaller than the second.
1928 int tevent_timeval_compare(const struct timeval
*tv1
,
1929 const struct timeval
*tv2
);
1932 * @brief Get a zero timeval value.
1934 * @return A zero timeval value.
1936 struct timeval
tevent_timeval_zero(void);
1939 * @brief Get a timeval value for the current time.
1941 * @return A timeval value with the current time.
1943 struct timeval
tevent_timeval_current(void);
1946 * @brief Get a timeval structure with the given values.
1948 * @param[in] secs The seconds to set.
1950 * @param[in] usecs The microseconds to set.
1952 * @return A timeval structure with the given values.
1954 struct timeval
tevent_timeval_set(uint32_t secs
, uint32_t usecs
);
1957 * @brief Get the difference between two timeval values.
1959 * @param[in] tv1 The first timeval.
1961 * @param[in] tv2 The second timeval.
1963 * @return A timeval structure with the difference between the
1964 * first and the second value.
1966 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
1967 const struct timeval
*tv2
);
1970 * @brief Check if a given timeval structure is zero.
1972 * @param[in] tv The timeval to check if it is zero.
1974 * @return True if it is zero, false otherwise.
1976 bool tevent_timeval_is_zero(const struct timeval
*tv
);
1979 * @brief Add the given amount of time to a timeval structure.
1981 * @param[in] tv The timeval structure to add the time.
1983 * @param[in] secs The seconds to add to the timeval.
1985 * @param[in] usecs The microseconds to add to the timeval.
1987 * @return The timeval structure with the new time.
1989 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
1993 * @brief Get a timeval in the future with a specified offset from now.
1995 * @param[in] secs The seconds of the offset from now.
1997 * @param[in] usecs The microseconds of the offset from now.
1999 * @return A timeval with the given offset in the future.
2001 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
);
2005 * @brief A cached version of getpid()
2007 * We use getpid() in a lot a performance critical situations
2008 * in order to check if caches are still valid in the current process.
2010 * Calling getpid() always add the cost of an additional syscall!
2012 * When tevent is build with pthread support, we already make use
2013 * of pthread_atfork(), so it's trivial to use it maintain a cache for getpid().
2015 * @return The pid of the current process.
2017 pid_t
tevent_cached_getpid(void);
2023 * @defgroup tevent_thread_call_depth The tevent call depth tracking functions
2027 * The call depth tracking consists of two parts.
2029 * Part 1 - storing the depth inside each tevent request.
2031 * Each instance of 'struct tevent_req' internally stores the value of the
2032 * current depth. If a new subrequest is created via tevent_req_create(), the
2033 * newly created subrequest gets the value from the parent incremented by 1.
2035 * Part 2 - updating external variable with the call depth of the currently
2036 * processed tevent request.
2038 * The intended use of call depth is for the trace indentation. This is done
2039 * by registering the address of an external size_t variable via
2040 * tevent_thread_call_depth_activate(). And the tracing code just reads it's
2043 * The updates happen during:
2045 * tevent_req_create()
2046 * - external variable is set to the value of the newly created request (i.e.
2047 * value of the parent incremented by 1)
2049 * tevent_req_notify_callback()
2050 * - external variable is set to the value of the parent tevent request, which
2051 * is just about to be processed
2053 * tevent_queue_immediate_trigger()
2054 * - external variable is set to the value of the request coming from the queue
2057 * While 'Part 1' maintains the call depth value inside each teven request
2058 * precisely, the value of the external variable depends on the call flow and
2059 * can be changed after return from a function call, so it no longer matches
2060 * the value of the request being processed in the current function.
2063 * struct tevent_req *foo_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev)
2065 * struct tevent_req *req, *subreq;
2066 * struct foo_state *state;
2068 * // External variable has value 'X', which is the value in parent code
2069 * // It is ok, since tracing starts often only after tevent_req_create()
2070 * req = tevent_req_create(mem_ctx, &state, struct foo_state);
2072 * // External variable has now value 'X + 1'
2073 * D_DEBUG("foo_send(): the external variable has the expected value\n");
2075 * subreq = bar_send(state, ev, ...);
2076 * tevent_req_set_callback(subreq, foo_done, req);
2078 * // External variable has value 'X + 1 + n', where n > 0 and n is the
2079 * // depth reached in bar_send().
2080 * // We want to reset it via tevent_thread_call_depth_reset_from_req(),
2081 * // since we want the following D_DEBUG() to have the right trace
2084 * tevent_thread_call_depth_reset_from_req(req);
2085 * // External variable has again value 'X + 1' taken from req.
2086 * D_DEBUG("foo_send(): the external variable has the expected value\n");
2090 * static void foo_done(struct tevent_req *subreq)
2092 * struct tevent_req *req =
2093 * tevent_req_callback_data(subreq,
2094 * struct tevent_req);
2095 * struct foo_state *state =
2096 * tevent_req_data(req,
2097 * struct foo_state);
2099 * // external variable has value 'X + 1'
2101 * D_DEBUG("foo_done(): the external variable has the expected value\n");
2102 * status = bar_recv(subreq, state, ...)
2103 * tevent_req_done(req);
2106 * NTSTATUS foo_recv(struct tevent_req *req)
2108 * struct foo_state *state = tevent_req_data( req, struct foo_state);
2110 * // external variable has value 'X' (not 'X + 1')
2111 * // which is ok, if we consider _recv() to be an access function
2112 * // called from the parent context
2114 * D_DEBUG("foo_recv(): external variable has the value from parent\n");
2115 * return NT_STATUS_OK;
2119 * Interface has 3 parts:
2121 * Part 1: activation/deactivation
2123 * void tevent_thread_call_depth_set_callback(f, private_data)
2124 * Register a callback that can track 'call depth' and 'request flow'
2125 * NULL as a function callback means deactivation.
2127 * Part 2: Mark the request (and its subrequests) to be tracked
2129 * tevent_thread_call_depth_start(struct tevent_req *req)
2131 * By default, all newly created requests have call depth set to 0.
2132 * tevent_thread_call_depth_start() should be called shortly after
2133 * tevent_req_create(). It sets the call depth to 1.
2134 * Subrequest will have call depth 2 and so on.
2136 * Part 3: reset the external variable using value from tevent request
2138 * tevent_thread_call_depth_reset_from_req(struct tevent_req *req)
2140 * If the call depth is used for trace indentation, it might be useful to
2141 * reset the external variable to the call depth of currently processed tevent
2142 * request, since the ext. variable can be changed after return from a function
2143 * call that has created subrequests.
2147 * The state is thread specific, i.e. each thread can activate it and register
2148 * its own external variable.
2153 enum tevent_thread_call_depth_cmd
{
2154 TEVENT_CALL_FLOW_REQ_RESET
,
2155 TEVENT_CALL_FLOW_REQ_CREATE
,
2156 TEVENT_CALL_FLOW_REQ_CANCEL
,
2157 TEVENT_CALL_FLOW_REQ_CLEANUP
,
2158 TEVENT_CALL_FLOW_REQ_NOTIFY_CB
,
2159 TEVENT_CALL_FLOW_REQ_QUEUE_ENTER
,
2160 TEVENT_CALL_FLOW_REQ_QUEUE_TRIGGER
,
2161 TEVENT_CALL_FLOW_REQ_QUEUE_LEAVE
,
2164 typedef void (*tevent_call_depth_callback_t
)(
2166 enum tevent_thread_call_depth_cmd cmd
,
2167 struct tevent_req
*req
,
2171 struct tevent_thread_call_depth_state
{
2172 tevent_call_depth_callback_t cb
;
2176 extern __thread
struct tevent_thread_call_depth_state
2177 tevent_thread_call_depth_state_g
;
2180 * Register callback function for request/subrequest call depth / flow tracking.
2182 * @param[in] f External call depth and flow handling function
2184 void tevent_thread_call_depth_set_callback(tevent_call_depth_callback_t f
,
2185 void *private_data
);
2187 #ifdef TEVENT_DEPRECATED
2189 void tevent_thread_call_depth_activate(size_t *ptr
) _DEPRECATED_
;
2190 void tevent_thread_call_depth_deactivate(void) _DEPRECATED_
;
2191 void tevent_thread_call_depth_start(struct tevent_req
*req
) _DEPRECATED_
;
2196 * Reset the external call depth to the call depth of the request.
2198 * @param[in] req Request from which the call depth is reset.
2201 void tevent_thread_call_depth_reset_from_req(struct tevent_req
*req
);
2203 void _tevent_thread_call_depth_reset_from_req(struct tevent_req
*req
,
2206 #define tevent_thread_call_depth_reset_from_req(req) \
2207 _tevent_thread_call_depth_reset_from_req(req, __func__)
2213 * @defgroup tevent_queue The tevent queue functions
2216 * A tevent_queue is used to queue up async requests that must be
2217 * serialized. For example writing buffers into a socket must be
2218 * serialized. Writing a large lump of data into a socket can require
2219 * multiple write(2) or send(2) system calls. If more than one async
2220 * request is outstanding to write large buffers into a socket, every
2221 * request must individually be completed before the next one begins,
2222 * even if multiple syscalls are required.
2224 * Take a look at @ref tevent_queue_tutorial for more details.
2228 struct tevent_queue
;
2229 struct tevent_queue_entry
;
2232 * @brief Associate a custom tag with the queue entry.
2234 * This tag can be then retrieved with tevent_queue_entry_get_tag()
2236 * @param[in] qe The queue entry.
2238 * @param[in] tag Custom tag.
2240 void tevent_queue_entry_set_tag(struct tevent_queue_entry
*qe
, uint64_t tag
);
2243 * @brief Get custom queue entry tag.
2245 uint64_t tevent_queue_entry_get_tag(const struct tevent_queue_entry
*qe
);
2247 typedef void (*tevent_trace_queue_callback_t
)(struct tevent_queue_entry
*qe
,
2248 enum tevent_event_trace_point
,
2249 void *private_data
);
2252 * Register a callback to be called at certain trace points of queue.
2254 * @param[in] ev Event context
2255 * @param[in] cb Trace callback
2256 * @param[in] private_data Data to be passed to callback
2258 * @note The callback will be called at trace points defined by
2259 * tevent_event_trace_point. Call with NULL to reset.
2261 void tevent_set_trace_queue_callback(struct tevent_context
*ev
,
2262 tevent_trace_queue_callback_t cb
,
2263 void *private_data
);
2266 * Retrieve the current trace callback of queue.
2268 * @param[in] ev Event context
2269 * @param[out] cb Registered trace callback
2270 * @param[out] p_private_data Registered data to be passed to callback
2272 * @note This can be used to allow one component that wants to
2273 * register a callback to respect the callback that another component
2274 * has already registered.
2276 void tevent_get_trace_queue_callback(struct tevent_context
*ev
,
2277 tevent_trace_queue_callback_t
*cb
,
2278 void *p_private_data
);
2282 * @brief Create and start a tevent queue.
2284 * @param[in] mem_ctx The talloc memory context to allocate the queue.
2286 * @param[in] name The name to use to identify the queue.
2288 * @return An allocated tevent queue on success, NULL on error.
2290 * @see tevent_queue_start()
2291 * @see tevent_queue_stop()
2293 struct tevent_queue
*tevent_queue_create(TALLOC_CTX
*mem_ctx
,
2296 struct tevent_queue
*_tevent_queue_create(TALLOC_CTX
*mem_ctx
,
2298 const char *location
);
2300 #define tevent_queue_create(_mem_ctx, _name) \
2301 _tevent_queue_create((_mem_ctx), (_name), __location__)
2305 * @brief A callback trigger function run by the queue.
2307 * @param[in] req The tevent request the trigger function is executed on.
2309 * @param[in] private_data The private data pointer specified by
2310 * tevent_queue_add().
2312 * @see tevent_queue_add()
2313 * @see tevent_queue_add_entry()
2314 * @see tevent_queue_add_optimize_empty()
2316 typedef void (*tevent_queue_trigger_fn_t
)(struct tevent_req
*req
,
2317 void *private_data
);
2320 * @brief Add a tevent request to the queue.
2322 * @param[in] queue The queue to add the request.
2324 * @param[in] ev The event handle to use for the request.
2326 * @param[in] req The tevent request to add to the queue.
2328 * @param[in] trigger The function triggered by the queue when the request
2329 * is called. Since tevent 0.9.14 it's possible to
2330 * pass NULL, in order to just add a "blocker" to the
2333 * @param[in] private_data The private data passed to the trigger function.
2335 * @return True if the request has been successfully added, false
2338 bool tevent_queue_add(struct tevent_queue
*queue
,
2339 struct tevent_context
*ev
,
2340 struct tevent_req
*req
,
2341 tevent_queue_trigger_fn_t trigger
,
2342 void *private_data
);
2344 bool _tevent_queue_add(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(queue, ev, req, trigger, private_data) \
2352 _tevent_queue_add(queue, ev, req, trigger, #trigger, private_data)
2355 * @brief Add a tevent request to the queue.
2357 * The request can be removed from the queue by calling talloc_free()
2358 * (or a similar function) on the returned queue entry. This
2359 * is the only difference to tevent_queue_add().
2361 * @param[in] queue The queue to add the request.
2363 * @param[in] ev The event handle to use for the request.
2365 * @param[in] req The tevent request to add to the queue.
2367 * @param[in] trigger The function triggered by the queue when the request
2368 * is called. Since tevent 0.9.14 it's possible to
2369 * pass NULL, in order to just add a "blocker" to the
2372 * @param[in] private_data The private data passed to the trigger function.
2374 * @return a pointer to the tevent_queue_entry if the request
2375 * has been successfully added, NULL otherwise.
2377 * @see tevent_queue_add()
2378 * @see tevent_queue_add_optimize_empty()
2380 struct tevent_queue_entry
*tevent_queue_add_entry(
2381 struct tevent_queue
*queue
,
2382 struct tevent_context
*ev
,
2383 struct tevent_req
*req
,
2384 tevent_queue_trigger_fn_t trigger
,
2385 void *private_data
);
2387 struct tevent_queue_entry
*_tevent_queue_add_entry(
2388 struct tevent_queue
*queue
,
2389 struct tevent_context
*ev
,
2390 struct tevent_req
*req
,
2391 tevent_queue_trigger_fn_t trigger
,
2392 const char* trigger_name
,
2393 void *private_data
);
2395 #define tevent_queue_add_entry(queue, ev, req, trigger, private_data) \
2396 _tevent_queue_add_entry(queue, ev, req, trigger, #trigger, private_data);
2399 * @brief Add a tevent request to the queue using a possible optimization.
2401 * This tries to optimize for the empty queue case and may calls
2402 * the trigger function directly. This is the only difference compared
2403 * to tevent_queue_add_entry().
2405 * The caller needs to be prepared that the trigger function has
2406 * already called tevent_req_notify_callback(), tevent_req_error(),
2407 * tevent_req_done() or a similar function.
2409 * The trigger function has no chance to see the returned
2410 * queue_entry in the optimized case.
2412 * The request can be removed from the queue by calling talloc_free()
2413 * (or a similar function) on the returned queue entry.
2415 * @param[in] queue The queue to add the request.
2417 * @param[in] ev The event handle to use for the request.
2419 * @param[in] req The tevent request to add to the queue.
2421 * @param[in] trigger The function triggered by the queue when the request
2422 * is called. Since tevent 0.9.14 it's possible to
2423 * pass NULL, in order to just add a "blocker" to the
2426 * @param[in] private_data The private data passed to the trigger function.
2428 * @return a pointer to the tevent_queue_entry if the request
2429 * has been successfully added, NULL otherwise.
2431 * @see tevent_queue_add()
2432 * @see tevent_queue_add_entry()
2434 struct tevent_queue_entry
*tevent_queue_add_optimize_empty(
2435 struct tevent_queue
*queue
,
2436 struct tevent_context
*ev
,
2437 struct tevent_req
*req
,
2438 tevent_queue_trigger_fn_t trigger
,
2439 void *private_data
);
2441 struct tevent_queue_entry
*_tevent_queue_add_optimize_empty(
2442 struct tevent_queue
*queue
,
2443 struct tevent_context
*ev
,
2444 struct tevent_req
*req
,
2445 tevent_queue_trigger_fn_t trigger
,
2446 const char* trigger_name
,
2447 void *private_data
);
2449 #define tevent_queue_add_optimize_empty(queue, ev, req, trigger, private_data) \
2450 _tevent_queue_add_optimize_empty(queue, ev, req, trigger, #trigger, private_data)
2453 * @brief Untrigger an already triggered queue entry.
2455 * If a trigger function detects that it needs to remain
2456 * in the queue, it needs to call tevent_queue_stop()
2457 * followed by tevent_queue_entry_untrigger().
2459 * @note In order to call tevent_queue_entry_untrigger()
2460 * the queue must be already stopped and the given queue_entry
2461 * must be the first one in the queue! Otherwise it calls abort().
2463 * @note You can't use this together with tevent_queue_add_optimize_empty()
2464 * because the trigger function doesn't have access to the queue entry
2465 * in the case of an empty queue.
2467 * @param[in] queue_entry The queue entry to rearm.
2469 * @see tevent_queue_add_entry()
2470 * @see tevent_queue_stop()
2472 void tevent_queue_entry_untrigger(struct tevent_queue_entry
*entry
);
2475 * @brief Start a tevent queue.
2477 * The queue is started by default.
2479 * @param[in] queue The queue to start.
2481 void tevent_queue_start(struct tevent_queue
*queue
);
2484 * @brief Stop a tevent queue.
2486 * The queue is started by default.
2488 * @param[in] queue The queue to stop.
2490 void tevent_queue_stop(struct tevent_queue
*queue
);
2493 * @brief Get the length of the queue.
2495 * @param[in] queue The queue to get the length from.
2497 * @return The number of elements.
2499 size_t tevent_queue_length(struct tevent_queue
*queue
);
2502 * @brief Is the tevent queue running.
2504 * The queue is started by default.
2506 * @param[in] queue The queue.
2508 * @return Whether the queue is running or not..
2510 bool tevent_queue_running(struct tevent_queue
*queue
);
2513 * @brief Create a tevent subrequest that waits in a tevent_queue
2515 * The idea is that always the same syntax for tevent requests.
2517 * @param[in] mem_ctx The talloc memory context to use.
2519 * @param[in] ev The event handle to setup the request.
2521 * @param[in] queue The queue to wait in.
2523 * @return The new subrequest, NULL on error.
2525 * @see tevent_queue_wait_recv()
2527 struct tevent_req
*tevent_queue_wait_send(TALLOC_CTX
*mem_ctx
,
2528 struct tevent_context
*ev
,
2529 struct tevent_queue
*queue
);
2532 * @brief Check if we no longer need to wait in the queue.
2534 * This function needs to be called in the callback function set after calling
2535 * tevent_queue_wait_send().
2537 * @param[in] req The tevent request to check.
2539 * @return True on success, false otherwise.
2541 * @see tevent_queue_wait_send()
2543 bool tevent_queue_wait_recv(struct tevent_req
*req
);
2545 typedef int (*tevent_nesting_hook
)(struct tevent_context
*ev
,
2550 const char *location
);
2553 * @brief Create a tevent_thread_proxy for message passing between threads.
2555 * The tevent_context must have been allocated on the NULL
2556 * talloc context, and talloc_disable_null_tracking() must
2559 * @param[in] dest_ev_ctx The tevent_context to receive events.
2561 * @return An allocated tevent_thread_proxy, NULL on error.
2562 * If tevent was compiled without PTHREAD support
2563 * NULL is always returned and errno set to ENOSYS.
2565 * @see tevent_thread_proxy_schedule()
2567 struct tevent_thread_proxy
*tevent_thread_proxy_create(
2568 struct tevent_context
*dest_ev_ctx
);
2571 * @brief Schedule an immediate event on an event context from another thread.
2573 * Causes dest_ev_ctx, being run by another thread, to receive an
2574 * immediate event calling the handler with the *pp_private parameter.
2576 * *pp_im must be a pointer to an immediate event talloced on a context owned
2577 * by the calling thread, or the NULL context. Ownership will
2578 * be transferred to the tevent_thread_proxy and *pp_im will be returned as NULL.
2580 * *pp_private_data must be a talloced area of memory with no destructors.
2581 * Ownership of this memory will be transferred to the tevent library and
2582 * *pp_private_data will be set to NULL on successful completion of
2583 * the call. Set pp_private to NULL if no parameter transfer
2584 * needed (a pure callback). This is an asynchronous request, caller
2585 * does not wait for callback to be completed before returning.
2587 * @param[in] tp The tevent_thread_proxy to use.
2589 * @param[in] pp_im Pointer to immediate event pointer.
2591 * @param[in] handler The function that will be called.
2593 * @param[in] pp_private_data The talloced memory to transfer.
2595 * @see tevent_thread_proxy_create()
2597 void tevent_thread_proxy_schedule(struct tevent_thread_proxy
*tp
,
2598 struct tevent_immediate
**pp_im
,
2599 tevent_immediate_handler_t handler
,
2600 void *pp_private_data
);
2603 * @brief Create a context for threaded activation of immediates
2605 * A tevent_treaded_context provides a link into an event
2606 * context. Using tevent_threaded_schedule_immediate, it is possible
2607 * to activate an immediate event from within a thread.
2609 * It is the duty of the caller of tevent_threaded_context_create() to
2610 * keep the event context around longer than any
2611 * tevent_threaded_context. tevent will abort if ev is talloc_free'ed
2612 * with an active tevent_threaded_context.
2614 * If tevent is build without pthread support, this always returns
2615 * NULL with errno=ENOSYS.
2617 * @param[in] mem_ctx The talloc memory context to use.
2618 * @param[in] ev The event context to link this to.
2619 * @return The threaded context, or NULL with errno set.
2621 * @see tevent_threaded_schedule_immediate()
2623 * @note Available as of tevent 0.9.30
2625 struct tevent_threaded_context
*tevent_threaded_context_create(
2626 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
);
2630 * @brief Activate an immediate from a thread
2632 * Activate an immediate from within a thread.
2634 * This routine does not watch out for talloc hierarchies. This means
2635 * that it is highly recommended to create the tevent_immediate in the
2636 * thread owning tctx, allocate a threaded job description for the
2637 * thread, hand over both pointers to a helper thread and not touch it
2638 * in the main thread at all anymore.
2640 * tevent_threaded_schedule_immediate is intended as a job completion
2641 * indicator for simple threaded helpers.
2643 * Please be aware that tevent_threaded_schedule_immediate is very
2644 * picky about its arguments: An immediate may not already be
2645 * activated and the handler must exist. With
2646 * tevent_threaded_schedule_immediate memory ownership is transferred
2647 * to the main thread holding the tevent context behind tctx, the
2648 * helper thread can't access it anymore.
2650 * @param[in] tctx The threaded context to go through
2651 * @param[in] im The immediate event to activate
2652 * @param[in] handler The immediate handler to call in the main thread
2653 * @param[in] private_data Pointer for the immediate handler
2655 * @see tevent_threaded_context_create()
2657 * @note Available as of tevent 0.9.30
2659 void tevent_threaded_schedule_immediate(struct tevent_threaded_context
*tctx
,
2660 struct tevent_immediate
*im
,
2661 tevent_immediate_handler_t handler
,
2662 void *private_data
);
2664 void _tevent_threaded_schedule_immediate(struct tevent_threaded_context
*tctx
,
2665 struct tevent_immediate
*im
,
2666 tevent_immediate_handler_t handler
,
2668 const char *handler_name
,
2669 const char *location
);
2670 #define tevent_threaded_schedule_immediate(tctx, im, handler, private_data) \
2671 _tevent_threaded_schedule_immediate(tctx, im, handler, private_data, \
2672 #handler, __location__);
2675 #ifdef TEVENT_DEPRECATED
2676 void tevent_loop_allow_nesting(struct tevent_context
*ev
) _DEPRECATED_
;
2677 void tevent_loop_set_nesting_hook(struct tevent_context
*ev
,
2678 tevent_nesting_hook hook
,
2679 void *private_data
) _DEPRECATED_
;
2680 int _tevent_loop_until(struct tevent_context
*ev
,
2681 bool (*finished
)(void *private_data
),
2683 const char *location
) _DEPRECATED_
;
2684 #define tevent_loop_until(ev, finished, private_data) \
2685 _tevent_loop_until(ev, finished, private_data, __location__)
2688 int tevent_re_initialise(struct tevent_context
*ev
);
2693 * @defgroup tevent_ops The tevent operation functions
2696 * The following structure and registration functions are exclusively
2697 * needed for people writing and plugging a different event engine.
2698 * There is nothing useful for normal tevent user in here.
2704 int (*context_init
)(struct tevent_context
*ev
);
2706 /* fd_event functions */
2707 struct tevent_fd
*(*add_fd
)(struct tevent_context
*ev
,
2708 TALLOC_CTX
*mem_ctx
,
2709 int fd
, uint16_t flags
,
2710 tevent_fd_handler_t handler
,
2712 const char *handler_name
,
2713 const char *location
);
2714 void (*set_fd_close_fn
)(struct tevent_fd
*fde
,
2715 tevent_fd_close_fn_t close_fn
);
2716 uint16_t (*get_fd_flags
)(struct tevent_fd
*fde
);
2717 void (*set_fd_flags
)(struct tevent_fd
*fde
, uint16_t flags
);
2719 /* timed_event functions */
2720 struct tevent_timer
*(*add_timer
)(struct tevent_context
*ev
,
2721 TALLOC_CTX
*mem_ctx
,
2722 struct timeval next_event
,
2723 tevent_timer_handler_t handler
,
2725 const char *handler_name
,
2726 const char *location
);
2728 /* immediate event functions */
2729 void (*schedule_immediate
)(struct tevent_immediate
*im
,
2730 struct tevent_context
*ev
,
2731 tevent_immediate_handler_t handler
,
2733 const char *handler_name
,
2734 const char *location
);
2736 /* signal functions */
2737 struct tevent_signal
*(*add_signal
)(struct tevent_context
*ev
,
2738 TALLOC_CTX
*mem_ctx
,
2739 int signum
, int sa_flags
,
2740 tevent_signal_handler_t handler
,
2742 const char *handler_name
,
2743 const char *location
);
2745 /* loop functions */
2746 int (*loop_once
)(struct tevent_context
*ev
, const char *location
);
2747 int (*loop_wait
)(struct tevent_context
*ev
, const char *location
);
2750 bool tevent_register_backend(const char *name
, const struct tevent_ops
*ops
);
2751 const struct tevent_ops
*tevent_find_ops_byname(const char *name
);
2755 #ifdef TEVENT_DEPRECATED
2757 * @defgroup tevent_wrapper_ops The tevent wrapper operation functions
2760 * The following structure and registration functions are exclusively
2761 * needed for people writing wrapper functions for event handlers
2762 * e.g. wrappers can be used for debugging/profiling or impersonation.
2764 * There is nothing useful for normal tevent user in here.
2766 * @note That the close_fn() on tevent_fd is *NOT* wrapped!
2768 * @see tevent_context_wrapper_create
2769 * @see tevent_fd_set_auto_close
2773 struct tevent_wrapper_ops
{
2776 bool (*before_use
)(struct tevent_context
*wrap_ev
,
2777 void *private_state
,
2778 struct tevent_context
*main_ev
,
2779 const char *location
);
2780 void (*after_use
)(struct tevent_context
*wrap_ev
,
2781 void *private_state
,
2782 struct tevent_context
*main_ev
,
2783 const char *location
);
2785 void (*before_fd_handler
)(struct tevent_context
*wrap_ev
,
2786 void *private_state
,
2787 struct tevent_context
*main_ev
,
2788 struct tevent_fd
*fde
,
2790 const char *handler_name
,
2791 const char *location
);
2792 void (*after_fd_handler
)(struct tevent_context
*wrap_ev
,
2793 void *private_state
,
2794 struct tevent_context
*main_ev
,
2795 struct tevent_fd
*fde
,
2797 const char *handler_name
,
2798 const char *location
);
2800 void (*before_timer_handler
)(struct tevent_context
*wrap_ev
,
2801 void *private_state
,
2802 struct tevent_context
*main_ev
,
2803 struct tevent_timer
*te
,
2804 struct timeval requested_time
,
2805 struct timeval trigger_time
,
2806 const char *handler_name
,
2807 const char *location
);
2808 void (*after_timer_handler
)(struct tevent_context
*wrap_ev
,
2809 void *private_state
,
2810 struct tevent_context
*main_ev
,
2811 struct tevent_timer
*te
,
2812 struct timeval requested_time
,
2813 struct timeval trigger_time
,
2814 const char *handler_name
,
2815 const char *location
);
2817 void (*before_immediate_handler
)(struct tevent_context
*wrap_ev
,
2818 void *private_state
,
2819 struct tevent_context
*main_ev
,
2820 struct tevent_immediate
*im
,
2821 const char *handler_name
,
2822 const char *location
);
2823 void (*after_immediate_handler
)(struct tevent_context
*wrap_ev
,
2824 void *private_state
,
2825 struct tevent_context
*main_ev
,
2826 struct tevent_immediate
*im
,
2827 const char *handler_name
,
2828 const char *location
);
2830 void (*before_signal_handler
)(struct tevent_context
*wrap_ev
,
2831 void *private_state
,
2832 struct tevent_context
*main_ev
,
2833 struct tevent_signal
*se
,
2837 const char *handler_name
,
2838 const char *location
);
2839 void (*after_signal_handler
)(struct tevent_context
*wrap_ev
,
2840 void *private_state
,
2841 struct tevent_context
*main_ev
,
2842 struct tevent_signal
*se
,
2846 const char *handler_name
,
2847 const char *location
);
2852 * @brief Create a wrapper tevent_context.
2854 * @param[in] main_ev The main event context to work on.
2856 * @param[in] mem_ctx The talloc memory context to use.
2858 * @param[in] ops The tevent_wrapper_ops function table.
2860 * @param[out] private_state The private state use by the wrapper functions.
2862 * @param[in] private_type The talloc type of the private_state.
2864 * @return The wrapper event context, NULL on error.
2866 * @note Available as of tevent 0.9.37
2867 * @note Deprecated as of tevent 0.9.38
2869 struct tevent_context
*tevent_context_wrapper_create(struct tevent_context
*main_ev
,
2870 TALLOC_CTX
*mem_ctx
,
2871 const struct tevent_wrapper_ops
*ops
,
2872 void **private_state
,
2873 const char *private_type
);
2875 struct tevent_context
*_tevent_context_wrapper_create(struct tevent_context
*main_ev
,
2876 TALLOC_CTX
*mem_ctx
,
2877 const struct tevent_wrapper_ops
*ops
,
2881 const char *location
) _DEPRECATED_
;
2882 #define tevent_context_wrapper_create(main_ev, mem_ctx, ops, state, type) \
2883 _tevent_context_wrapper_create(main_ev, mem_ctx, ops, \
2884 state, sizeof(type), #type, __location__)
2888 * @brief Check if the event context is a wrapper event context.
2890 * @param[in] ev The event context to work on.
2892 * @return Is a wrapper (true), otherwise (false).
2894 * @see tevent_context_wrapper_create()
2896 * @note Available as of tevent 0.9.37
2897 * @note Deprecated as of tevent 0.9.38
2899 bool tevent_context_is_wrapper(struct tevent_context
*ev
) _DEPRECATED_
;
2903 * @brief Prepare the environment of a (wrapper) event context.
2905 * A caller might call this before passing a wrapper event context
2906 * to a tevent_req based *_send() function.
2908 * The wrapper event context might do something like impersonation.
2910 * tevent_context_push_use() must always be used in combination
2911 * with tevent_context_pop_use().
2913 * There is a global stack of currently active/busy wrapper event contexts.
2914 * Each wrapper can only appear once on that global stack!
2915 * The stack size is limited to 32 elements, which should be enough
2916 * for all useful scenarios.
2918 * In addition to an explicit tevent_context_push_use() also
2919 * the invocation of an immediate, timer or fd handler implicitly
2920 * pushes the wrapper on the stack.
2922 * Therefore there are some strict constraints for the usage of
2923 * tevent_context_push_use():
2924 * - It must not be called from within an event handler
2925 * that already acts on the wrapper.
2926 * - tevent_context_pop_use() must be called before
2927 * leaving the code block that called tevent_context_push_use().
2928 * - The caller is responsible ensure the correct stack ordering
2929 * - Any violation of these constraints results in calling
2930 * the abort handler of the given tevent context.
2932 * Calling tevent_context_push_use() on a raw event context
2933 * still consumes an element on the stack, but it's otherwise
2936 * If tevent_context_push_use() returns false, it means
2937 * that the wrapper's before_use() hook returned this failure,
2938 * in that case you must not call tevent_context_pop_use() as
2939 * the wrapper is not pushed onto the stack.
2941 * @param[in] ev The event context to work on.
2943 * @return Success (true) or failure (false).
2945 * @note This is only needed if wrapper event contexts are in use.
2947 * @see tevent_context_pop_use
2949 * @note Available as of tevent 0.9.37
2950 * @note Deprecated as of tevent 0.9.38
2952 bool tevent_context_push_use(struct tevent_context
*ev
);
2954 bool _tevent_context_push_use(struct tevent_context
*ev
,
2955 const char *location
) _DEPRECATED_
;
2956 #define tevent_context_push_use(ev) \
2957 _tevent_context_push_use(ev, __location__)
2962 * @brief Release the environment of a (wrapper) event context.
2964 * The wrapper event context might undo something like impersonation.
2966 * This must be called after a successful tevent_context_push_use().
2967 * Any ordering violation results in calling
2968 * the abort handler of the given tevent context.
2970 * This basically calls the wrapper's after_use() hook.
2972 * @param[in] ev The event context to work on.
2974 * @note This is only needed if wrapper event contexts are in use.
2976 * @see tevent_context_push_use
2978 * @note Available as of tevent 0.9.37
2979 * @note Deprecated as of tevent 0.9.38
2981 void tevent_context_pop_use(struct tevent_context
*ev
);
2983 void _tevent_context_pop_use(struct tevent_context
*ev
,
2984 const char *location
) _DEPRECATED_
;
2985 #define tevent_context_pop_use(ev) \
2986 _tevent_context_pop_use(ev, __location__)
2990 * @brief Check is the two context pointers belong to the same low level loop
2992 * With the introduction of wrapper contexts it's not trivial
2993 * to check if two context pointers belong to the same low level
2994 * event loop. Some code may need to know this in order
2995 * to make some caching decisions.
2997 * @param[in] ev1 The first event context.
2998 * @param[in] ev2 The second event context.
3000 * @return true if both contexts belong to the same (still existing) context
3001 * loop, false otherwise.
3003 * @see tevent_context_wrapper_create
3005 * @note Available as of tevent 0.9.37
3006 * @note Deprecated as of tevent 0.9.38
3008 bool tevent_context_same_loop(struct tevent_context
*ev1
,
3009 struct tevent_context
*ev2
) _DEPRECATED_
;
3012 #endif /* TEVENT_DEPRECATED */
3015 * @defgroup tevent_compat The tevent compatibility functions
3018 * The following definitions are useful only for compatibility with the
3019 * implementation originally developed within the samba4 code and will be
3020 * soon removed. Please NEVER use in new code.
3027 #ifdef TEVENT_COMPAT_DEFINES
3029 #define event_context tevent_context
3030 #define event_ops tevent_ops
3031 #define fd_event tevent_fd
3032 #define timed_event tevent_timer
3033 #define signal_event tevent_signal
3035 #define event_fd_handler_t tevent_fd_handler_t
3036 #define event_timed_handler_t tevent_timer_handler_t
3037 #define event_signal_handler_t tevent_signal_handler_t
3039 #define event_context_init(mem_ctx) \
3040 tevent_context_init(mem_ctx)
3042 #define event_context_init_byname(mem_ctx, name) \
3043 tevent_context_init_byname(mem_ctx, name)
3045 #define event_backend_list(mem_ctx) \
3046 tevent_backend_list(mem_ctx)
3048 #define event_set_default_backend(backend) \
3049 tevent_set_default_backend(backend)
3051 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
3052 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
3054 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
3055 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
3057 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
3058 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
3060 #define event_loop_once(ev) \
3061 tevent_loop_once(ev)
3063 #define event_loop_wait(ev) \
3064 tevent_loop_wait(ev)
3066 #define event_get_fd_flags(fde) \
3067 tevent_fd_get_flags(fde)
3069 #define event_set_fd_flags(fde, flags) \
3070 tevent_fd_set_flags(fde, flags)
3072 #define EVENT_FD_READ TEVENT_FD_READ
3073 #define EVENT_FD_WRITE TEVENT_FD_WRITE
3075 #define EVENT_FD_WRITEABLE(fde) \
3076 TEVENT_FD_WRITEABLE(fde)
3078 #define EVENT_FD_READABLE(fde) \
3079 TEVENT_FD_READABLE(fde)
3081 #define EVENT_FD_NOT_WRITEABLE(fde) \
3082 TEVENT_FD_NOT_WRITEABLE(fde)
3084 #define EVENT_FD_NOT_READABLE(fde) \
3085 TEVENT_FD_NOT_READABLE(fde)
3087 #define ev_debug_level tevent_debug_level
3089 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
3090 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
3091 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
3092 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
3094 #define ev_set_debug(ev, debug, context) \
3095 tevent_set_debug(ev, debug, context)
3097 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
3099 #endif /* TEVENT_COMPAT_DEFINES */
3103 #endif /* __TEVENT_H__ */