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/>.
36 struct tevent_context
;
40 struct tevent_immediate
;
44 * @defgroup tevent The tevent API
46 * The tevent low-level API
48 * This API provides the public interface to manage events in the tevent
49 * mainloop. Functions are provided for managing low-level events such
50 * as timer events, fd events and signal handling.
55 /* event handler types */
57 * Called when a file descriptor monitored by tevent has
58 * data to be read or written on it.
60 typedef void (*tevent_fd_handler_t
)(struct tevent_context
*ev
,
61 struct tevent_fd
*fde
,
66 * Called when tevent is ceasing the monitoring of a file descriptor.
68 typedef void (*tevent_fd_close_fn_t
)(struct tevent_context
*ev
,
69 struct tevent_fd
*fde
,
74 * Called when a tevent timer has fired.
76 typedef void (*tevent_timer_handler_t
)(struct tevent_context
*ev
,
77 struct tevent_timer
*te
,
78 struct timeval current_time
,
82 * Called when a tevent immediate event is invoked.
84 typedef void (*tevent_immediate_handler_t
)(struct tevent_context
*ctx
,
85 struct tevent_immediate
*im
,
89 * Called after tevent detects the specified signal.
91 typedef void (*tevent_signal_handler_t
)(struct tevent_context
*ev
,
92 struct tevent_signal
*se
,
99 * @brief Create a event_context structure.
101 * This must be the first events call, and all subsequent calls pass this
102 * event_context as the first element. Event handlers also receive this as
103 * their first argument.
105 * @param[in] mem_ctx The memory context to use.
107 * @return An allocated tevent context, NULL on error.
109 * @see tevent_context_init()
111 struct tevent_context
*tevent_context_init(TALLOC_CTX
*mem_ctx
);
114 * @brief Create a event_context structure and select a specific backend.
116 * This must be the first events call, and all subsequent calls pass this
117 * event_context as the first element. Event handlers also receive this as
118 * their first argument.
120 * @param[in] mem_ctx The memory context to use.
122 * @param[in] name The name of the backend to use.
124 * @return An allocated tevent context, NULL on error.
126 struct tevent_context
*tevent_context_init_byname(TALLOC_CTX
*mem_ctx
, const char *name
);
129 * @brief Create a custom event context
131 * @param[in] mem_ctx The memory context to use.
132 * @param[in] ops The function pointer table of the backend.
133 * @param[in] additional_data The additional/private data to this instance
135 * @return An allocated tevent context, NULL on error.
138 struct tevent_context
*tevent_context_init_ops(TALLOC_CTX
*mem_ctx
,
139 const struct tevent_ops
*ops
,
140 void *additional_data
);
143 * @brief List available backends.
145 * @param[in] mem_ctx The memory context to use.
147 * @return A string vector with a terminating NULL element, NULL
150 const char **tevent_backend_list(TALLOC_CTX
*mem_ctx
);
153 * @brief Set the default tevent backend.
155 * @param[in] backend The name of the backend to set.
157 void tevent_set_default_backend(const char *backend
);
161 * @brief Add a file descriptor based event.
163 * @param[in] ev The event context to work on.
165 * @param[in] mem_ctx The talloc memory context to use.
167 * @param[in] fd The file descriptor to base the event on.
169 * @param[in] flags #TEVENT_FD_READ or #TEVENT_FD_WRITE
171 * @param[in] handler The callback handler for the event.
173 * @param[in] private_data The private data passed to the callback handler.
175 * @return The file descriptor based event, NULL on error.
177 * @note To cancel the monitoring of a file descriptor, call talloc_free()
178 * on the object returned by this function.
180 struct tevent_fd
*tevent_add_fd(struct tevent_context
*ev
,
184 tevent_fd_handler_t handler
,
187 struct tevent_fd
*_tevent_add_fd(struct tevent_context
*ev
,
191 tevent_fd_handler_t handler
,
193 const char *handler_name
,
194 const char *location
);
195 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
196 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
197 #handler, __location__)
202 * @brief Add a timed event
204 * @param[in] ev The event context to work on.
206 * @param[in] mem_ctx The talloc memory context to use.
208 * @param[in] next_event Timeval specifying the absolute time to fire this
209 * event. This is not an offset.
211 * @param[in] handler The callback handler for the event.
213 * @param[in] private_data The private data passed to the callback handler.
215 * @return The newly-created timer event, or NULL on error.
217 * @note To cancel a timer event before it fires, call talloc_free() on the
218 * event returned from this function. This event is automatically
219 * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
221 * @note Unlike some mainloops, tevent timers are one-time events. To set up
222 * a recurring event, it is necessary to call tevent_add_timer() again during
223 * the handler processing.
225 * @note Due to the internal mainloop processing, a timer set to run
226 * immediately will do so after any other pending timers fire, but before
227 * any further file descriptor or signal handling events fire. Callers should
228 * not rely on this behavior!
230 struct tevent_timer
*tevent_add_timer(struct tevent_context
*ev
,
232 struct timeval next_event
,
233 tevent_timer_handler_t handler
,
236 struct tevent_timer
*_tevent_add_timer(struct tevent_context
*ev
,
238 struct timeval next_event
,
239 tevent_timer_handler_t handler
,
241 const char *handler_name
,
242 const char *location
);
243 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
244 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
245 #handler, __location__)
250 * Initialize an immediate event object
252 * This object can be used to trigger an event to occur immediately after
253 * returning from the current event (before any other event occurs)
255 * @param[in] mem_ctx The talloc memory context to use as the parent
257 * @return An empty tevent_immediate object. Use tevent_schedule_immediate
258 * to populate and use it.
260 * @note Available as of tevent 0.9.8
262 struct tevent_immediate
*tevent_create_immediate(TALLOC_CTX
*mem_ctx
);
264 struct tevent_immediate
*_tevent_create_immediate(TALLOC_CTX
*mem_ctx
,
265 const char *location
);
266 #define tevent_create_immediate(mem_ctx) \
267 _tevent_create_immediate(mem_ctx, __location__)
273 * Schedule an event for immediate execution. This event will occur
274 * immediately after returning from the current event (before any other
277 * @param[in] im The tevent_immediate object to populate and use
278 * @param[in] ctx The tevent_context to run this event
279 * @param[in] handler The event handler to run when this event fires
280 * @param[in] private_data Data to pass to the event handler
282 void tevent_schedule_immediate(struct tevent_immediate
*im
,
283 struct tevent_context
*ctx
,
284 tevent_immediate_handler_t handler
,
287 void _tevent_schedule_immediate(struct tevent_immediate
*im
,
288 struct tevent_context
*ctx
,
289 tevent_immediate_handler_t handler
,
291 const char *handler_name
,
292 const char *location
);
293 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
294 _tevent_schedule_immediate(im, ctx, handler, private_data, \
295 #handler, __location__);
300 * @brief Add a tevent signal handler
302 * tevent_add_signal() creates a new event for handling a signal the next
303 * time through the mainloop. It implements a very simple traditional signal
304 * handler whose only purpose is to add the handler event into the mainloop.
306 * @param[in] ev The event context to work on.
308 * @param[in] mem_ctx The talloc memory context to use.
310 * @param[in] signum The signal to trap
312 * @param[in] handler The callback handler for the signal.
314 * @param[in] sa_flags sigaction flags for this signal handler.
316 * @param[in] private_data The private data passed to the callback handler.
318 * @return The newly-created signal handler event, or NULL on error.
320 * @note To cancel a signal handler, call talloc_free() on the event returned
321 * from this function.
323 struct tevent_signal
*tevent_add_signal(struct tevent_context
*ev
,
327 tevent_signal_handler_t handler
,
330 struct tevent_signal
*_tevent_add_signal(struct tevent_context
*ev
,
334 tevent_signal_handler_t handler
,
336 const char *handler_name
,
337 const char *location
);
338 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
339 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
340 #handler, __location__)
345 * @brief Pass a single time through the mainloop
347 * This will process any appropriate signal, immediate, fd and timer events
349 * @param[in] ev The event context to process
351 * @return Zero on success, nonzero if an internal error occurred
353 int tevent_loop_once(struct tevent_context
*ev
);
355 int _tevent_loop_once(struct tevent_context
*ev
, const char *location
);
356 #define tevent_loop_once(ev) \
357 _tevent_loop_once(ev, __location__)
362 * @brief Run the mainloop
364 * The mainloop will run until there are no events remaining to be processed
366 * @param[in] ev The event context to process
368 * @return Zero if all events have been processed. Nonzero if an internal
371 int tevent_loop_wait(struct tevent_context
*ev
);
373 int _tevent_loop_wait(struct tevent_context
*ev
, const char *location
);
374 #define tevent_loop_wait(ev) \
375 _tevent_loop_wait(ev, __location__)
380 * Assign a function to run when a tevent_fd is freed
382 * This function is a destructor for the tevent_fd. It does not automatically
383 * close the file descriptor. If this is the desired behavior, then it must be
384 * performed by the close_fn.
386 * @param[in] fde File descriptor event on which to set the destructor
387 * @param[in] close_fn Destructor to execute when fde is freed
389 void tevent_fd_set_close_fn(struct tevent_fd
*fde
,
390 tevent_fd_close_fn_t close_fn
);
393 * Automatically close the file descriptor when the tevent_fd is freed
395 * This function calls close(fd) internally.
397 * @param[in] fde File descriptor event to auto-close
399 void tevent_fd_set_auto_close(struct tevent_fd
*fde
);
402 * Return the flags set on this file descriptor event
404 * @param[in] fde File descriptor event to query
406 * @return The flags set on the event. See #TEVENT_FD_READ and
409 uint16_t tevent_fd_get_flags(struct tevent_fd
*fde
);
412 * Set flags on a file descriptor event
414 * @param[in] fde File descriptor event to set
415 * @param[in] flags Flags to set on the event. See #TEVENT_FD_READ and
418 void tevent_fd_set_flags(struct tevent_fd
*fde
, uint16_t flags
);
421 * Query whether tevent supports signal handling
423 * @param[in] ev An initialized tevent context
425 * @return True if this platform and tevent context support signal handling
427 bool tevent_signal_support(struct tevent_context
*ev
);
429 void tevent_set_abort_fn(void (*abort_fn
)(const char *reason
));
431 /* bits for file descriptor event flags */
434 * Monitor a file descriptor for write availability
436 #define TEVENT_FD_READ 1
438 * Monitor a file descriptor for data to be read
440 #define TEVENT_FD_WRITE 2
443 * Convenience function for declaring a tevent_fd writable
445 #define TEVENT_FD_WRITEABLE(fde) \
446 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
449 * Convenience function for declaring a tevent_fd readable
451 #define TEVENT_FD_READABLE(fde) \
452 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
455 * Convenience function for declaring a tevent_fd non-writable
457 #define TEVENT_FD_NOT_WRITEABLE(fde) \
458 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
461 * Convenience function for declaring a tevent_fd non-readable
463 #define TEVENT_FD_NOT_READABLE(fde) \
464 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
467 * Debug level of tevent
469 enum tevent_debug_level
{
472 TEVENT_DEBUG_WARNING
,
477 * @brief The tevent debug callbac.
479 * @param[in] context The memory context to use.
481 * @param[in] level The debug level.
483 * @param[in] fmt The format string.
485 * @param[in] ap The arguments for the format string.
487 typedef void (*tevent_debug_fn
)(void *context
,
488 enum tevent_debug_level level
,
490 va_list ap
) PRINTF_ATTRIBUTE(3,0);
493 * Set destination for tevent debug messages
495 * @param[in] ev Event context to debug
496 * @param[in] debug Function to handle output printing
497 * @param[in] context The context to pass to the debug function.
499 * @return Always returns 0 as of version 0.9.8
501 * @note Default is to emit no debug messages
503 int tevent_set_debug(struct tevent_context
*ev
,
504 tevent_debug_fn debug
,
508 * Designate stderr for debug message output
510 * @param[in] ev Event context to debug
512 * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
513 * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
514 * function for tevent_set_debug()
516 int tevent_set_debug_stderr(struct tevent_context
*ev
);
518 enum tevent_trace_point
{
520 * Corresponds to a trace point just before waiting
522 TEVENT_TRACE_BEFORE_WAIT
,
524 * Corresponds to a trace point just after waiting
526 TEVENT_TRACE_AFTER_WAIT
,
529 typedef void (*tevent_trace_callback_t
)(enum tevent_trace_point
,
533 * Register a callback to be called at certain trace points
535 * @param[in] ev Event context
536 * @param[in] cb Trace callback
537 * @param[in] private_data Data to be passed to callback
539 * @note The callback will be called at trace points defined by
540 * tevent_trace_point. Call with NULL to reset.
542 void tevent_set_trace_callback(struct tevent_context
*ev
,
543 tevent_trace_callback_t cb
,
547 * Retrieve the current trace callback
549 * @param[in] ev Event context
550 * @param[out] cb Registered trace callback
551 * @param[out] private_data Registered data to be passed to callback
553 * @note This can be used to allow one component that wants to
554 * register a callback to respect the callback that another component
555 * has already registered.
557 void tevent_get_trace_callback(struct tevent_context
*ev
,
558 tevent_trace_callback_t
*cb
,
566 * @defgroup tevent_request The tevent request functions.
569 * A tevent_req represents an asynchronous computation.
571 * The tevent_req group of API calls is the recommended way of
572 * programming async computations within tevent. In particular the
573 * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
574 * are considered too low-level to be used in larger computations. To
575 * read and write from and to sockets, Samba provides two calls on top
576 * of tevent_add_fd: read_packet_send/recv and writev_send/recv. These
577 * requests are much easier to compose than the low-level event
578 * handlers called from tevent_add_fd.
580 * A lot of the simplicity tevent_req has brought to the notoriously
581 * hairy async programming came via a set of conventions that every
582 * async computation programmed should follow. One central piece of
583 * these conventions is the naming of routines and variables.
585 * Every async computation needs a name (sensibly called "computation"
586 * down from here). From this name quite a few naming conventions are
589 * Every computation that requires local state needs a
591 * struct computation_state {
595 * Even if no local variables are required, such a state struct should
596 * be created containing a dummy variable. Quite a few helper
597 * functions and macros (for example tevent_req_create()) assume such
600 * An async computation is started by a computation_send
601 * function. When it is finished, its result can be received by a
602 * computation_recv function. For an example how to set up an async
603 * computation, see the code example in the documentation for
604 * tevent_req_create() and tevent_req_post(). The prototypes for _send
605 * and _recv functions should follow some conventions:
608 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
609 * struct tevent_req *ev,
611 * int computation_recv(struct tevent_req *req, ... further output args);
614 * The "int" result of computation_recv() depends on the result the
615 * sync version of the function would have, "int" is just an example
618 * Another important piece of the conventions is that the program flow
619 * is interrupted as little as possible. Because a blocking
620 * sub-computation requires that the flow needs to continue in a
621 * separate function that is the logical sequel of some computation,
622 * it should lexically follow sending off the blocking
623 * sub-computation. Setting the callback function via
624 * tevent_req_set_callback() requires referencing a function lexically
625 * below the call to tevent_req_set_callback(), forward declarations
626 * are required. A lot of the async computations thus begin with a
627 * sequence of declarations such as
630 * static void computation_step1_done(struct tevent_req *subreq);
631 * static void computation_step2_done(struct tevent_req *subreq);
632 * static void computation_step3_done(struct tevent_req *subreq);
635 * It really helps readability a lot to do these forward declarations,
636 * because the lexically sequential program flow makes the async
637 * computations almost as clear to read as a normal, sync program
640 * It is up to the user of the async computation to talloc_free it
641 * after it has finished. If an async computation should be aborted,
642 * the tevent_req structure can be talloc_free'ed. After it has
643 * finished, it should talloc_free'ed by the API user.
649 * An async request moves from TEVENT_REQ_INIT to
650 * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
653 enum tevent_req_state
{
655 * We are creating the request
659 * We are waiting the request to complete
661 TEVENT_REQ_IN_PROGRESS
,
663 * The request is finished successfully
667 * A user error has occurred. The user error has been
668 * indicated by tevent_req_error(), it can be retrieved via
669 * tevent_req_is_error().
671 TEVENT_REQ_USER_ERROR
,
673 * Request timed out after the timeout set by tevent_req_set_endtime.
675 TEVENT_REQ_TIMED_OUT
,
677 * An internal allocation has failed, or tevent_req_nomem has
678 * been given a NULL pointer as the first argument.
680 TEVENT_REQ_NO_MEMORY
,
682 * The request has been received by the caller. No further
689 * @brief An async request
694 * @brief A tevent request callback function.
696 * @param[in] req The tevent async request which executed this callback.
698 typedef void (*tevent_req_fn
)(struct tevent_req
*req
);
701 * @brief Set an async request callback.
703 * See the documentation of tevent_req_post() for an example how this
704 * is supposed to be used.
706 * @param[in] req The async request to set the callback.
708 * @param[in] fn The callback function to set.
710 * @param[in] pvt A pointer to private data to pass to the async request
713 void tevent_req_set_callback(struct tevent_req
*req
, tevent_req_fn fn
, void *pvt
);
717 * @brief Get the private data cast to the given type for a callback from
718 * a tevent request structure.
721 * static void computation_done(struct tevent_req *subreq) {
722 * struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
723 * struct computation_state *state = tevent_req_data(req, struct computation_state);
724 * .... more things, eventually maybe call tevent_req_done(req);
728 * @param[in] req The structure to get the callback data from.
730 * @param[in] type The type of the private callback data to get.
732 * @return The type casted private data set NULL if not set.
734 void *tevent_req_callback_data(struct tevent_req
*req
, #type);
736 void *_tevent_req_callback_data(struct tevent_req
*req
);
737 #define tevent_req_callback_data(_req, _type) \
738 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
743 * @brief Get the private data for a callback from a tevent request structure.
745 * @param[in] req The structure to get the callback data from.
747 * @param[in] req The structure to get the data from.
749 * @return The private data or NULL if not set.
751 void *tevent_req_callback_data_void(struct tevent_req
*req
);
753 #define tevent_req_callback_data_void(_req) \
754 _tevent_req_callback_data(_req)
759 * @brief Get the private data from a tevent request structure.
761 * When the tevent_req has been created by tevent_req_create, the
762 * result of tevent_req_data() is the state variable created by
763 * tevent_req_create() as a child of the req.
765 * @param[in] req The structure to get the private data from.
767 * @param[in] type The type of the private data
769 * @return The private data or NULL if not set.
771 void *tevent_req_data(struct tevent_req
*req
, #type);
773 void *_tevent_req_data(struct tevent_req
*req
);
774 #define tevent_req_data(_req, _type) \
775 talloc_get_type_abort(_tevent_req_data(_req), _type)
779 * @brief The print function which can be set for a tevent async request.
781 * @param[in] req The tevent async request.
783 * @param[in] ctx A talloc memory context which can be uses to allocate
786 * @return An allocated string buffer to print.
790 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
792 * struct my_data *data = tevent_req_data(req, struct my_data);
795 * result = tevent_req_default_print(mem_ctx, req);
796 * if (result == NULL) {
800 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
801 * data->foo, data->bar);
805 typedef char *(*tevent_req_print_fn
)(struct tevent_req
*req
, TALLOC_CTX
*ctx
);
808 * @brief This function sets a print function for the given request.
810 * This function can be used to setup a print function for the given request.
811 * This will be triggered if the tevent_req_print() function was
812 * called on the given request.
814 * @param[in] req The request to use.
816 * @param[in] fn A pointer to the print function
818 * @note This function should only be used for debugging.
820 void tevent_req_set_print_fn(struct tevent_req
*req
, tevent_req_print_fn fn
);
823 * @brief The default print function for creating debug messages.
825 * The function should not be used by users of the async API,
826 * but custom print function can use it and append custom text
829 * @param[in] req The request to be printed.
831 * @param[in] mem_ctx The memory context for the result.
833 * @return Text representation of request.
836 char *tevent_req_default_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
);
839 * @brief Print an tevent_req structure in debug messages.
841 * This function should be used by callers of the async API.
843 * @param[in] mem_ctx The memory context for the result.
845 * @param[in] req The request to be printed.
847 * @return Text representation of request.
849 char *tevent_req_print(TALLOC_CTX
*mem_ctx
, struct tevent_req
*req
);
852 * @brief A typedef for a cancel function for a tevent request.
854 * @param[in] req The tevent request calling this function.
856 * @return True if the request could be canceled, false if not.
858 typedef bool (*tevent_req_cancel_fn
)(struct tevent_req
*req
);
861 * @brief This function sets a cancel function for the given tevent request.
863 * This function can be used to setup a cancel function for the given request.
864 * This will be triggered if the tevent_req_cancel() function was
865 * called on the given request.
867 * @param[in] req The request to use.
869 * @param[in] fn A pointer to the cancel function.
871 void tevent_req_set_cancel_fn(struct tevent_req
*req
, tevent_req_cancel_fn fn
);
875 * @brief Try to cancel the given tevent request.
877 * This function can be used to cancel the given request.
879 * It is only possible to cancel a request when the implementation
880 * has registered a cancel function via the tevent_req_set_cancel_fn().
882 * @param[in] req The request to use.
884 * @return This function returns true is the request is cancelable,
885 * othererwise false is returned.
887 * @note Even if the function returns true, the caller need to wait
888 * for the function to complete normally.
889 * Only the _recv() function of the given request indicates
890 * if the request was really canceled.
892 bool tevent_req_cancel(struct tevent_req
*req
);
894 bool _tevent_req_cancel(struct tevent_req
*req
, const char *location
);
895 #define tevent_req_cancel(req) \
896 _tevent_req_cancel(req, __location__)
901 * @brief Create an async tevent request.
903 * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
906 * struct tevent_req *req;
907 * struct computation_state *state;
908 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
911 * Tevent_req_create() creates the state variable as a talloc child of
912 * its result. The state variable should be used as the talloc parent
913 * for all temporary variables that are allocated during the async
914 * computation. This way, when the user of the async computation frees
915 * the request, the state as a talloc child will be free'd along with
916 * all the temporary variables hanging off the state.
918 * @param[in] mem_ctx The memory context for the result.
919 * @param[in] pstate Pointer to the private request state.
920 * @param[in] type The name of the request.
922 * @return A new async request. NULL on error.
924 struct tevent_req
*tevent_req_create(TALLOC_CTX
*mem_ctx
,
925 void **pstate
, #type);
927 struct tevent_req
*_tevent_req_create(TALLOC_CTX
*mem_ctx
,
931 const char *location
);
933 #define tevent_req_create(_mem_ctx, _pstate, _type) \
934 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
935 #_type, __location__)
939 * @brief Set a timeout for an async request.
941 * @param[in] req The request to set the timeout for.
943 * @param[in] ev The event context to use for the timer.
945 * @param[in] endtime The endtime of the request.
947 * @return True if succeeded, false if not.
949 bool tevent_req_set_endtime(struct tevent_req
*req
,
950 struct tevent_context
*ev
,
951 struct timeval endtime
);
955 * @brief Call the notify callback of the given tevent request manually.
957 * @param[in] req The tevent request to call the notify function from.
959 * @see tevent_req_set_callback()
961 void tevent_req_notify_callback(struct tevent_req
*req
);
963 void _tevent_req_notify_callback(struct tevent_req
*req
, const char *location
);
964 #define tevent_req_notify_callback(req) \
965 _tevent_req_notify_callback(req, __location__)
970 * @brief An async request has successfully finished.
972 * This function is to be used by implementors of async requests. When a
973 * request is successfully finished, this function calls the user's completion
976 * @param[in] req The finished request.
978 void tevent_req_done(struct tevent_req
*req
);
980 void _tevent_req_done(struct tevent_req
*req
,
981 const char *location
);
982 #define tevent_req_done(req) \
983 _tevent_req_done(req, __location__)
988 * @brief An async request has seen an error.
990 * This function is to be used by implementors of async requests. When a
991 * request can not successfully completed, the implementation should call this
992 * function with the appropriate status code.
994 * If error is 0 the function returns false and does nothing more.
996 * @param[in] req The request with an error.
998 * @param[in] error The error code.
1000 * @return On success true is returned, false if error is 0.
1003 * int error = first_function();
1004 * if (tevent_req_error(req, error)) {
1008 * error = second_function();
1009 * if (tevent_req_error(req, error)) {
1013 * tevent_req_done(req);
1017 bool tevent_req_error(struct tevent_req
*req
,
1020 bool _tevent_req_error(struct tevent_req
*req
,
1022 const char *location
);
1023 #define tevent_req_error(req, error) \
1024 _tevent_req_error(req, error, __location__)
1029 * @brief Helper function for nomem check.
1031 * Convenience helper to easily check alloc failure within a callback
1032 * implementing the next step of an async request.
1034 * @param[in] p The pointer to be checked.
1036 * @param[in] req The request being processed.
1039 * p = talloc(mem_ctx, bla);
1040 * if (tevent_req_nomem(p, req)) {
1045 bool tevent_req_nomem(const void *p
,
1046 struct tevent_req
*req
);
1048 bool _tevent_req_nomem(const void *p
,
1049 struct tevent_req
*req
,
1050 const char *location
);
1051 #define tevent_req_nomem(p, req) \
1052 _tevent_req_nomem(p, req, __location__)
1057 * @brief Indicate out of memory to a request
1059 * @param[in] req The request being processed.
1061 void tevent_req_oom(struct tevent_req
*req
);
1063 void _tevent_req_oom(struct tevent_req
*req
,
1064 const char *location
);
1065 #define tevent_req_oom(req) \
1066 _tevent_req_oom(req, __location__)
1070 * @brief Finish a request before the caller had the change to set the callback.
1072 * An implementation of an async request might find that it can either finish
1073 * the request without waiting for an external event, or it can not even start
1074 * the engine. To present the illusion of a callback to the user of the API,
1075 * the implementation can call this helper function which triggers an
1076 * immediate event. This way the caller can use the same calling
1077 * conventions, independent of whether the request was actually deferred.
1080 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1081 * struct tevent_context *ev)
1083 * struct tevent_req *req, *subreq;
1084 * struct computation_state *state;
1085 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1086 * if (req == NULL) {
1089 * subreq = subcomputation_send(state, ev);
1090 * if (tevent_req_nomem(subreq, req)) {
1091 * return tevent_req_post(req, ev);
1093 * tevent_req_set_callback(subreq, computation_done, req);
1098 * @param[in] req The finished request.
1100 * @param[in] ev The tevent_context for the immediate event.
1102 * @return The given request will be returned.
1104 struct tevent_req
*tevent_req_post(struct tevent_req
*req
,
1105 struct tevent_context
*ev
);
1108 * @brief Finish multiple requests within one function
1110 * Normally tevent_req_notify_callback() and all wrappers
1111 * (e.g. tevent_req_done() and tevent_req_error())
1112 * need to be the last thing an event handler should call.
1113 * This is because the callback is likely to destroy the
1114 * context of the current function.
1116 * If a function wants to notify more than one caller,
1117 * it is dangerous if it just triggers multiple callbacks
1118 * in a row. With tevent_req_defer_callback() it is possible
1119 * to set an event context that will be used to defer the callback
1120 * via an immediate event (similar to tevent_req_post()).
1123 * struct complete_state {
1124 * struct tevent_context *ev;
1126 * struct tevent_req **reqs;
1129 * void complete(struct complete_state *state)
1131 * size_t i, c = talloc_array_length(state->reqs);
1133 * for (i=0; i < c; i++) {
1134 * tevent_req_defer_callback(state->reqs[i], state->ev);
1135 * tevent_req_done(state->reqs[i]);
1140 * @param[in] req The finished request.
1142 * @param[in] ev The tevent_context for the immediate event.
1144 * @return The given request will be returned.
1146 void tevent_req_defer_callback(struct tevent_req
*req
,
1147 struct tevent_context
*ev
);
1150 * @brief Check if the given request is still in progress.
1152 * It is typically used by sync wrapper functions.
1154 * @param[in] req The request to poll.
1156 * @return The boolean form of "is in progress".
1158 bool tevent_req_is_in_progress(struct tevent_req
*req
);
1161 * @brief Actively poll for the given request to finish.
1163 * This function is typically used by sync wrapper functions.
1165 * @param[in] req The request to poll.
1167 * @param[in] ev The tevent_context to be used.
1169 * @return On success true is returned. If a critical error has
1170 * happened in the tevent loop layer false is returned.
1171 * This is not the return value of the given request!
1173 * @note This should only be used if the given tevent context was created by the
1174 * caller, to avoid event loop nesting.
1177 * req = tstream_writev_queue_send(mem_ctx,
1182 * ok = tevent_req_poll(req, tctx->ev);
1183 * rc = tstream_writev_queue_recv(req, &sys_errno);
1187 bool tevent_req_poll(struct tevent_req
*req
,
1188 struct tevent_context
*ev
);
1191 * @brief Get the tevent request state and the actual error set by
1195 * int computation_recv(struct tevent_req *req, uint64_t *perr)
1197 * enum tevent_req_state state;
1199 * if (tevent_req_is_error(req, &state, &err)) {
1207 * @param[in] req The tevent request to get the error from.
1209 * @param[out] state A pointer to store the tevent request error state.
1211 * @param[out] error A pointer to store the error set by tevent_req_error().
1213 * @return True if the function could set error and state, false
1216 * @see tevent_req_error()
1218 bool tevent_req_is_error(struct tevent_req
*req
,
1219 enum tevent_req_state
*state
,
1223 * @brief Use as the last action of a _recv() function.
1225 * This function destroys the attached private data.
1227 * @param[in] req The finished request.
1229 void tevent_req_received(struct tevent_req
*req
);
1232 * @brief Create a tevent subrequest at a given time.
1234 * The idea is that always the same syntax for tevent requests.
1236 * @param[in] mem_ctx The talloc memory context to use.
1238 * @param[in] ev The event handle to setup the request.
1240 * @param[in] wakeup_time The time to wakeup and execute the request.
1242 * @return The new subrequest, NULL on error.
1246 * static void my_callback_wakeup_done(tevent_req *subreq)
1248 * struct tevent_req *req = tevent_req_callback_data(subreq,
1249 * struct tevent_req);
1252 * ok = tevent_wakeup_recv(subreq);
1253 * TALLOC_FREE(subreq);
1255 * tevent_req_error(req, -1);
1263 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1264 * if (tevent_req_nomem(subreq, req)) {
1267 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1270 * @see tevent_wakeup_recv()
1272 struct tevent_req
*tevent_wakeup_send(TALLOC_CTX
*mem_ctx
,
1273 struct tevent_context
*ev
,
1274 struct timeval wakeup_time
);
1277 * @brief Check if the wakeup has been correctly executed.
1279 * This function needs to be called in the callback function set after calling
1280 * tevent_wakeup_send().
1282 * @param[in] req The tevent request to check.
1284 * @return True on success, false otherwise.
1286 * @see tevent_wakeup_recv()
1288 bool tevent_wakeup_recv(struct tevent_req
*req
);
1293 * @defgroup tevent_helpers The tevent helper functiions
1302 * @brief Compare two timeval values.
1304 * @param[in] tv1 The first timeval value to compare.
1306 * @param[in] tv2 The second timeval value to compare.
1308 * @return 0 if they are equal.
1309 * 1 if the first time is greater than the second.
1310 * -1 if the first time is smaller than the second.
1312 int tevent_timeval_compare(const struct timeval
*tv1
,
1313 const struct timeval
*tv2
);
1316 * @brief Get a zero timval value.
1318 * @return A zero timval value.
1320 struct timeval
tevent_timeval_zero(void);
1323 * @brief Get a timeval value for the current time.
1325 * @return A timval value with the current time.
1327 struct timeval
tevent_timeval_current(void);
1330 * @brief Get a timeval structure with the given values.
1332 * @param[in] secs The seconds to set.
1334 * @param[in] usecs The microseconds to set.
1336 * @return A timeval structure with the given values.
1338 struct timeval
tevent_timeval_set(uint32_t secs
, uint32_t usecs
);
1341 * @brief Get the difference between two timeval values.
1343 * @param[in] tv1 The first timeval.
1345 * @param[in] tv2 The second timeval.
1347 * @return A timeval structure with the difference between the
1348 * first and the second value.
1350 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
1351 const struct timeval
*tv2
);
1354 * @brief Check if a given timeval structure is zero.
1356 * @param[in] tv The timeval to check if it is zero.
1358 * @return True if it is zero, false otherwise.
1360 bool tevent_timeval_is_zero(const struct timeval
*tv
);
1363 * @brief Add the given amount of time to a timeval structure.
1365 * @param[in] tv The timeval structure to add the time.
1367 * @param[in] secs The seconds to add to the timeval.
1369 * @param[in] usecs The microseconds to add to the timeval.
1371 * @return The timeval structure with the new time.
1373 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
1377 * @brief Get a timeval in the future with a specified offset from now.
1379 * @param[in] secs The seconds of the offset from now.
1381 * @param[in] usecs The microseconds of the offset from now.
1383 * @return A timval with the given offset in the future.
1385 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
);
1391 * @defgroup tevent_queue The tevent queue functions
1394 * A tevent_queue is used to queue up async requests that must be
1395 * serialized. For example writing buffers into a socket must be
1396 * serialized. Writing a large lump of data into a socket can require
1397 * multiple write(2) or send(2) system calls. If more than one async
1398 * request is outstanding to write large buffers into a socket, every
1399 * request must individually be completed before the next one begins,
1400 * even if multiple syscalls are required.
1402 * Take a look at @ref tevent_queue_tutorial for more details.
1406 struct tevent_queue
;
1407 struct tevent_queue_entry
;
1411 * @brief Create and start a tevent queue.
1413 * @param[in] mem_ctx The talloc memory context to allocate the queue.
1415 * @param[in] name The name to use to identify the queue.
1417 * @return An allocated tevent queue on success, NULL on error.
1419 * @see tevent_queue_start()
1420 * @see tevent_queue_stop()
1422 struct tevent_queue
*tevent_queue_create(TALLOC_CTX
*mem_ctx
,
1425 struct tevent_queue
*_tevent_queue_create(TALLOC_CTX
*mem_ctx
,
1427 const char *location
);
1429 #define tevent_queue_create(_mem_ctx, _name) \
1430 _tevent_queue_create((_mem_ctx), (_name), __location__)
1434 * @brief A callback trigger function run by the queue.
1436 * @param[in] req The tevent request the trigger function is executed on.
1438 * @param[in] private_data The private data pointer specified by
1439 * tevent_queue_add().
1441 * @see tevent_queue_add()
1442 * @see tevent_queue_add_entry()
1443 * @see tevent_queue_add_optimize_empty()
1445 typedef void (*tevent_queue_trigger_fn_t
)(struct tevent_req
*req
,
1446 void *private_data
);
1449 * @brief Add a tevent request to the queue.
1451 * @param[in] queue The queue to add the request.
1453 * @param[in] ev The event handle to use for the request.
1455 * @param[in] req The tevent request to add to the queue.
1457 * @param[in] trigger The function triggered by the queue when the request
1458 * is called. Since tevent 0.9.14 it's possible to
1459 * pass NULL, in order to just add a "blocker" to the
1462 * @param[in] private_data The private data passed to the trigger function.
1464 * @return True if the request has been successfully added, false
1467 bool tevent_queue_add(struct tevent_queue
*queue
,
1468 struct tevent_context
*ev
,
1469 struct tevent_req
*req
,
1470 tevent_queue_trigger_fn_t trigger
,
1471 void *private_data
);
1474 * @brief Add a tevent request to the queue.
1476 * The request can be removed from the queue by calling talloc_free()
1477 * (or a similar function) on the returned queue entry. This
1478 * is the only difference to tevent_queue_add().
1480 * @param[in] queue The queue to add the request.
1482 * @param[in] ev The event handle to use for the request.
1484 * @param[in] req The tevent request to add to the queue.
1486 * @param[in] trigger The function triggered by the queue when the request
1487 * is called. Since tevent 0.9.14 it's possible to
1488 * pass NULL, in order to just add a "blocker" to the
1491 * @param[in] private_data The private data passed to the trigger function.
1493 * @return a pointer to the tevent_queue_entry if the request
1494 * has been successfully added, NULL otherwise.
1496 * @see tevent_queue_add()
1497 * @see tevent_queue_add_optimize_empty()
1499 struct tevent_queue_entry
*tevent_queue_add_entry(
1500 struct tevent_queue
*queue
,
1501 struct tevent_context
*ev
,
1502 struct tevent_req
*req
,
1503 tevent_queue_trigger_fn_t trigger
,
1504 void *private_data
);
1507 * @brief Add a tevent request to the queue using a possible optimization.
1509 * This tries to optimize for the empty queue case and may calls
1510 * the trigger function directly. This is the only difference compared
1511 * to tevent_queue_add_entry().
1513 * The caller needs to be prepared that the trigger function has
1514 * already called tevent_req_notify_callback(), tevent_req_error(),
1515 * tevent_req_done() or a similar function.
1517 * The request can be removed from the queue by calling talloc_free()
1518 * (or a similar function) on the returned queue entry.
1520 * @param[in] queue The queue to add the request.
1522 * @param[in] ev The event handle to use for the request.
1524 * @param[in] req The tevent request to add to the queue.
1526 * @param[in] trigger The function triggered by the queue when the request
1527 * is called. Since tevent 0.9.14 it's possible to
1528 * pass NULL, in order to just add a "blocker" to the
1531 * @param[in] private_data The private data passed to the trigger function.
1533 * @return a pointer to the tevent_queue_entry if the request
1534 * has been successfully added, NULL otherwise.
1536 * @see tevent_queue_add()
1537 * @see tevent_queue_add_entry()
1539 struct tevent_queue_entry
*tevent_queue_add_optimize_empty(
1540 struct tevent_queue
*queue
,
1541 struct tevent_context
*ev
,
1542 struct tevent_req
*req
,
1543 tevent_queue_trigger_fn_t trigger
,
1544 void *private_data
);
1547 * @brief Start a tevent queue.
1549 * The queue is started by default.
1551 * @param[in] queue The queue to start.
1553 void tevent_queue_start(struct tevent_queue
*queue
);
1556 * @brief Stop a tevent queue.
1558 * The queue is started by default.
1560 * @param[in] queue The queue to stop.
1562 void tevent_queue_stop(struct tevent_queue
*queue
);
1565 * @brief Get the length of the queue.
1567 * @param[in] queue The queue to get the length from.
1569 * @return The number of elements.
1571 size_t tevent_queue_length(struct tevent_queue
*queue
);
1574 * @brief Is the tevent queue running.
1576 * The queue is started by default.
1578 * @param[in] queue The queue.
1580 * @return Wether the queue is running or not..
1582 bool tevent_queue_running(struct tevent_queue
*queue
);
1584 typedef int (*tevent_nesting_hook
)(struct tevent_context
*ev
,
1589 const char *location
);
1590 #ifdef TEVENT_DEPRECATED
1591 #ifndef _DEPRECATED_
1592 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1593 #define _DEPRECATED_ __attribute__ ((deprecated))
1595 #define _DEPRECATED_
1598 void tevent_loop_allow_nesting(struct tevent_context
*ev
) _DEPRECATED_
;
1599 void tevent_loop_set_nesting_hook(struct tevent_context
*ev
,
1600 tevent_nesting_hook hook
,
1601 void *private_data
) _DEPRECATED_
;
1602 int _tevent_loop_until(struct tevent_context
*ev
,
1603 bool (*finished
)(void *private_data
),
1605 const char *location
) _DEPRECATED_
;
1606 #define tevent_loop_until(ev, finished, private_data) \
1607 _tevent_loop_until(ev, finished, private_data, __location__)
1610 int tevent_re_initialise(struct tevent_context
*ev
);
1615 * @defgroup tevent_ops The tevent operation functions
1618 * The following structure and registration functions are exclusively
1619 * needed for people writing and pluggin a different event engine.
1620 * There is nothing useful for normal tevent user in here.
1626 int (*context_init
)(struct tevent_context
*ev
);
1628 /* fd_event functions */
1629 struct tevent_fd
*(*add_fd
)(struct tevent_context
*ev
,
1630 TALLOC_CTX
*mem_ctx
,
1631 int fd
, uint16_t flags
,
1632 tevent_fd_handler_t handler
,
1634 const char *handler_name
,
1635 const char *location
);
1636 void (*set_fd_close_fn
)(struct tevent_fd
*fde
,
1637 tevent_fd_close_fn_t close_fn
);
1638 uint16_t (*get_fd_flags
)(struct tevent_fd
*fde
);
1639 void (*set_fd_flags
)(struct tevent_fd
*fde
, uint16_t flags
);
1641 /* timed_event functions */
1642 struct tevent_timer
*(*add_timer
)(struct tevent_context
*ev
,
1643 TALLOC_CTX
*mem_ctx
,
1644 struct timeval next_event
,
1645 tevent_timer_handler_t handler
,
1647 const char *handler_name
,
1648 const char *location
);
1650 /* immediate event functions */
1651 void (*schedule_immediate
)(struct tevent_immediate
*im
,
1652 struct tevent_context
*ev
,
1653 tevent_immediate_handler_t handler
,
1655 const char *handler_name
,
1656 const char *location
);
1658 /* signal functions */
1659 struct tevent_signal
*(*add_signal
)(struct tevent_context
*ev
,
1660 TALLOC_CTX
*mem_ctx
,
1661 int signum
, int sa_flags
,
1662 tevent_signal_handler_t handler
,
1664 const char *handler_name
,
1665 const char *location
);
1667 /* loop functions */
1668 int (*loop_once
)(struct tevent_context
*ev
, const char *location
);
1669 int (*loop_wait
)(struct tevent_context
*ev
, const char *location
);
1672 bool tevent_register_backend(const char *name
, const struct tevent_ops
*ops
);
1677 * @defgroup tevent_compat The tevent compatibility functions
1680 * The following definitions are usueful only for compatibility with the
1681 * implementation originally developed within the samba4 code and will be
1682 * soon removed. Please NEVER use in new code.
1689 #ifdef TEVENT_COMPAT_DEFINES
1691 #define event_context tevent_context
1692 #define event_ops tevent_ops
1693 #define fd_event tevent_fd
1694 #define timed_event tevent_timer
1695 #define signal_event tevent_signal
1697 #define event_fd_handler_t tevent_fd_handler_t
1698 #define event_timed_handler_t tevent_timer_handler_t
1699 #define event_signal_handler_t tevent_signal_handler_t
1701 #define event_context_init(mem_ctx) \
1702 tevent_context_init(mem_ctx)
1704 #define event_context_init_byname(mem_ctx, name) \
1705 tevent_context_init_byname(mem_ctx, name)
1707 #define event_backend_list(mem_ctx) \
1708 tevent_backend_list(mem_ctx)
1710 #define event_set_default_backend(backend) \
1711 tevent_set_default_backend(backend)
1713 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1714 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1716 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1717 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1719 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1720 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1722 #define event_loop_once(ev) \
1723 tevent_loop_once(ev)
1725 #define event_loop_wait(ev) \
1726 tevent_loop_wait(ev)
1728 #define event_get_fd_flags(fde) \
1729 tevent_fd_get_flags(fde)
1731 #define event_set_fd_flags(fde, flags) \
1732 tevent_fd_set_flags(fde, flags)
1734 #define EVENT_FD_READ TEVENT_FD_READ
1735 #define EVENT_FD_WRITE TEVENT_FD_WRITE
1737 #define EVENT_FD_WRITEABLE(fde) \
1738 TEVENT_FD_WRITEABLE(fde)
1740 #define EVENT_FD_READABLE(fde) \
1741 TEVENT_FD_READABLE(fde)
1743 #define EVENT_FD_NOT_WRITEABLE(fde) \
1744 TEVENT_FD_NOT_WRITEABLE(fde)
1746 #define EVENT_FD_NOT_READABLE(fde) \
1747 TEVENT_FD_NOT_READABLE(fde)
1749 #define ev_debug_level tevent_debug_level
1751 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
1752 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
1753 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
1754 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
1756 #define ev_set_debug(ev, debug, context) \
1757 tevent_set_debug(ev, debug, context)
1759 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1761 #endif /* TEVENT_COMPAT_DEFINES */
1765 #endif /* __TEVENT_H__ */