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 List available backends.
131 * @param[in] mem_ctx The memory context to use.
133 * @return A string vector with a terminating NULL element, NULL
136 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
139 * @brief Set the default tevent backend.
141 * @param[in] backend The name of the backend to set.
143 void tevent_set_default_backend(const char *backend);
147 * @brief Add a file descriptor based event.
149 * @param[in] ev The event context to work on.
151 * @param[in] mem_ctx The talloc memory context to use.
153 * @param[in] fd The file descriptor to base the event on.
155 * @param[in] flags #TEVENT_FD_READ or #TEVENT_FD_WRITE
157 * @param[in] handler The callback handler for the event.
159 * @param[in] private_data The private data passed to the callback handler.
161 * @return The file descriptor based event, NULL on error.
163 * @note To cancel the monitoring of a file descriptor, call talloc_free()
164 * on the object returned by this function.
166 struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
170 tevent_fd_handler_t handler,
173 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
177 tevent_fd_handler_t handler,
179 const char *handler_name,
180 const char *location);
181 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
182 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
183 #handler, __location__)
188 * @brief Add a timed event
190 * @param[in] ev The event context to work on.
192 * @param[in] mem_ctx The talloc memory context to use.
194 * @param[in] next_event Timeval specifying the absolute time to fire this
195 * event. This is not an offset.
197 * @param[in] handler The callback handler for the event.
199 * @param[in] private_data The private data passed to the callback handler.
201 * @return The newly-created timer event, or NULL on error.
203 * @note To cancel a timer event before it fires, call talloc_free() on the
204 * event returned from this function. This event is automatically
205 * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
207 * @note Unlike some mainloops, tevent timers are one-time events. To set up
208 * a recurring event, it is necessary to call tevent_add_timer() again during
209 * the handler processing.
211 * @note Due to the internal mainloop processing, a timer set to run
212 * immediately will do so after any other pending timers fire, but before
213 * any further file descriptor or signal handling events fire. Callers should
214 * not rely on this behavior!
216 struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
218 struct timeval next_event,
219 tevent_timer_handler_t handler,
222 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
224 struct timeval next_event,
225 tevent_timer_handler_t handler,
227 const char *handler_name,
228 const char *location);
229 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
230 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
231 #handler, __location__)
236 * Initialize an immediate event object
238 * This object can be used to trigger an event to occur immediately after
239 * returning from the current event (before any other event occurs)
241 * @param[in] mem_ctx The talloc memory context to use as the parent
243 * @return An empty tevent_immediate object. Use tevent_schedule_immediate
244 * to populate and use it.
246 * @note Available as of tevent 0.9.8
248 struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
250 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
251 const char *location);
252 #define tevent_create_immediate(mem_ctx) \
253 _tevent_create_immediate(mem_ctx, __location__)
259 * Schedule an event for immediate execution. This event will occur
260 * immediately after returning from the current event (before any other
263 * @param[in] im The tevent_immediate object to populate and use
264 * @param[in] ctx The tevent_context to run this event
265 * @param[in] handler The event handler to run when this event fires
266 * @param[in] private_data Data to pass to the event handler
268 void tevent_schedule_immediate(struct tevent_immediate *im,
269 struct tevent_context *ctx,
270 tevent_immediate_handler_t handler,
273 void _tevent_schedule_immediate(struct tevent_immediate *im,
274 struct tevent_context *ctx,
275 tevent_immediate_handler_t handler,
277 const char *handler_name,
278 const char *location);
279 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
280 _tevent_schedule_immediate(im, ctx, handler, private_data, \
281 #handler, __location__);
286 * @brief Add a tevent signal handler
288 * tevent_add_signal() creates a new event for handling a signal the next
289 * time through the mainloop. It implements a very simple traditional signal
290 * handler whose only purpose is to add the handler event into the mainloop.
292 * @param[in] ev The event context to work on.
294 * @param[in] mem_ctx The talloc memory context to use.
296 * @param[in] signum The signal to trap
298 * @param[in] handler The callback handler for the signal.
300 * @param[in] sa_flags sigaction flags for this signal handler.
302 * @param[in] private_data The private data passed to the callback handler.
304 * @return The newly-created signal handler event, or NULL on error.
306 * @note To cancel a signal handler, call talloc_free() on the event returned
307 * from this function.
309 struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
313 tevent_signal_handler_t handler,
316 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
320 tevent_signal_handler_t handler,
322 const char *handler_name,
323 const char *location);
324 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
325 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
326 #handler, __location__)
331 * @brief Pass a single time through the mainloop
333 * This will process any appropriate signal, immediate, fd and timer events
335 * @param[in] ev The event context to process
337 * @return Zero on success, nonzero if an internal error occurred
339 int tevent_loop_once(struct tevent_context *ev);
341 int _tevent_loop_once(struct tevent_context *ev, const char *location);
342 #define tevent_loop_once(ev) \
343 _tevent_loop_once(ev, __location__)
348 * @brief Run the mainloop
350 * The mainloop will run until there are no events remaining to be processed
352 * @param[in] ev The event context to process
354 * @return Zero if all events have been processed. Nonzero if an internal
357 int tevent_loop_wait(struct tevent_context *ev);
359 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
360 #define tevent_loop_wait(ev) \
361 _tevent_loop_wait(ev, __location__)
366 * Assign a function to run when a tevent_fd is freed
368 * This function is a destructor for the tevent_fd. It does not automatically
369 * close the file descriptor. If this is the desired behavior, then it must be
370 * performed by the close_fn.
372 * @param[in] fde File descriptor event on which to set the destructor
373 * @param[in] close_fn Destructor to execute when fde is freed
375 void tevent_fd_set_close_fn(struct tevent_fd *fde,
376 tevent_fd_close_fn_t close_fn);
379 * Automatically close the file descriptor when the tevent_fd is freed
381 * This function calls close(fd) internally.
383 * @param[in] fde File descriptor event to auto-close
385 void tevent_fd_set_auto_close(struct tevent_fd *fde);
388 * Return the flags set on this file descriptor event
390 * @param[in] fde File descriptor event to query
392 * @return The flags set on the event. See #TEVENT_FD_READ and
395 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
398 * Set flags on a file descriptor event
400 * @param[in] fde File descriptor event to set
401 * @param[in] flags Flags to set on the event. See #TEVENT_FD_READ and
404 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
407 * Query whether tevent supports signal handling
409 * @param[in] ev An initialized tevent context
411 * @return True if this platform and tevent context support signal handling
413 bool tevent_signal_support(struct tevent_context *ev);
415 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
417 /* bits for file descriptor event flags */
420 * Monitor a file descriptor for write availability
422 #define TEVENT_FD_READ 1
424 * Monitor a file descriptor for data to be read
426 #define TEVENT_FD_WRITE 2
429 * Convenience function for declaring a tevent_fd writable
431 #define TEVENT_FD_WRITEABLE(fde) \
432 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
435 * Convenience function for declaring a tevent_fd readable
437 #define TEVENT_FD_READABLE(fde) \
438 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
441 * Convenience function for declaring a tevent_fd non-writable
443 #define TEVENT_FD_NOT_WRITEABLE(fde) \
444 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
447 * Convenience function for declaring a tevent_fd non-readable
449 #define TEVENT_FD_NOT_READABLE(fde) \
450 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
453 * Debug level of tevent
455 enum tevent_debug_level {
458 TEVENT_DEBUG_WARNING,
463 * @brief The tevent debug callbac.
465 * @param[in] context The memory context to use.
467 * @param[in] level The debug level.
469 * @param[in] fmt The format string.
471 * @param[in] ap The arguments for the format string.
473 typedef void (*tevent_debug_fn)(void *context,
474 enum tevent_debug_level level,
476 va_list ap) PRINTF_ATTRIBUTE(3,0);
479 * Set destination for tevent debug messages
481 * @param[in] ev Event context to debug
482 * @param[in] debug Function to handle output printing
483 * @param[in] context The context to pass to the debug function.
485 * @return Always returns 0 as of version 0.9.8
487 * @note Default is to emit no debug messages
489 int tevent_set_debug(struct tevent_context *ev,
490 tevent_debug_fn debug,
494 * Designate stderr for debug message output
496 * @param[in] ev Event context to debug
498 * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
499 * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
500 * function for tevent_set_debug()
502 int tevent_set_debug_stderr(struct tevent_context *ev);
504 enum tevent_trace_point {
506 * Corresponds to a trace point just before waiting
508 TEVENT_TRACE_BEFORE_WAIT,
510 * Corresponds to a trace point just after waiting
512 TEVENT_TRACE_AFTER_WAIT,
515 typedef void (*tevent_trace_callback_t)(enum tevent_trace_point,
519 * Register a callback to be called at certain trace points
521 * @param[in] ev Event context
522 * @param[in] cb Trace callback
523 * @param[in] private_data Data to be passed to callback
525 * @note The callback will be called at trace points defined by
526 * tevent_trace_point. Call with NULL to reset.
528 void tevent_set_trace_callback(struct tevent_context *ev,
529 tevent_trace_callback_t cb,
533 * Retrieve the current trace callback
535 * @param[in] ev Event context
536 * @param[out] cb Registered trace callback
537 * @param[out] private_data Registered data to be passed to callback
539 * @note This can be used to allow one component that wants to
540 * register a callback to respect the callback that another component
541 * has already registered.
543 void tevent_get_trace_callback(struct tevent_context *ev,
544 tevent_trace_callback_t *cb,
552 * @defgroup tevent_request The tevent request functions.
555 * A tevent_req represents an asynchronous computation.
557 * The tevent_req group of API calls is the recommended way of
558 * programming async computations within tevent. In particular the
559 * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
560 * are considered too low-level to be used in larger computations. To
561 * read and write from and to sockets, Samba provides two calls on top
562 * of tevent_add_fd: read_packet_send/recv and writev_send/recv. These
563 * requests are much easier to compose than the low-level event
564 * handlers called from tevent_add_fd.
566 * A lot of the simplicity tevent_req has brought to the notoriously
567 * hairy async programming came via a set of conventions that every
568 * async computation programmed should follow. One central piece of
569 * these conventions is the naming of routines and variables.
571 * Every async computation needs a name (sensibly called "computation"
572 * down from here). From this name quite a few naming conventions are
575 * Every computation that requires local state needs a
577 * struct computation_state {
581 * Even if no local variables are required, such a state struct should
582 * be created containing a dummy variable. Quite a few helper
583 * functions and macros (for example tevent_req_create()) assume such
586 * An async computation is started by a computation_send
587 * function. When it is finished, its result can be received by a
588 * computation_recv function. For an example how to set up an async
589 * computation, see the code example in the documentation for
590 * tevent_req_create() and tevent_req_post(). The prototypes for _send
591 * and _recv functions should follow some conventions:
594 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
595 * struct tevent_req *ev,
597 * int computation_recv(struct tevent_req *req, ... further output args);
600 * The "int" result of computation_recv() depends on the result the
601 * sync version of the function would have, "int" is just an example
604 * Another important piece of the conventions is that the program flow
605 * is interrupted as little as possible. Because a blocking
606 * sub-computation requires that the flow needs to continue in a
607 * separate function that is the logical sequel of some computation,
608 * it should lexically follow sending off the blocking
609 * sub-computation. Setting the callback function via
610 * tevent_req_set_callback() requires referencing a function lexically
611 * below the call to tevent_req_set_callback(), forward declarations
612 * are required. A lot of the async computations thus begin with a
613 * sequence of declarations such as
616 * static void computation_step1_done(struct tevent_req *subreq);
617 * static void computation_step2_done(struct tevent_req *subreq);
618 * static void computation_step3_done(struct tevent_req *subreq);
621 * It really helps readability a lot to do these forward declarations,
622 * because the lexically sequential program flow makes the async
623 * computations almost as clear to read as a normal, sync program
626 * It is up to the user of the async computation to talloc_free it
627 * after it has finished. If an async computation should be aborted,
628 * the tevent_req structure can be talloc_free'ed. After it has
629 * finished, it should talloc_free'ed by the API user.
635 * An async request moves from TEVENT_REQ_INIT to
636 * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
639 enum tevent_req_state {
641 * We are creating the request
645 * We are waiting the request to complete
647 TEVENT_REQ_IN_PROGRESS,
649 * The request is finished successfully
653 * A user error has occurred. The user error has been
654 * indicated by tevent_req_error(), it can be retrieved via
655 * tevent_req_is_error().
657 TEVENT_REQ_USER_ERROR,
659 * Request timed out after the timeout set by tevent_req_set_endtime.
661 TEVENT_REQ_TIMED_OUT,
663 * An internal allocation has failed, or tevent_req_nomem has
664 * been given a NULL pointer as the first argument.
666 TEVENT_REQ_NO_MEMORY,
668 * The request has been received by the caller. No further
675 * @brief An async request
680 * @brief A tevent request callback function.
682 * @param[in] req The tevent async request which executed this callback.
684 typedef void (*tevent_req_fn)(struct tevent_req *req);
687 * @brief Set an async request callback.
689 * See the documentation of tevent_req_post() for an example how this
690 * is supposed to be used.
692 * @param[in] req The async request to set the callback.
694 * @param[in] fn The callback function to set.
696 * @param[in] pvt A pointer to private data to pass to the async request
699 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
703 * @brief Get the private data cast to the given type for a callback from
704 * a tevent request structure.
707 * static void computation_done(struct tevent_req *subreq) {
708 * struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
709 * struct computation_state *state = tevent_req_data(req, struct computation_state);
710 * .... more things, eventually maybe call tevent_req_done(req);
714 * @param[in] req The structure to get the callback data from.
716 * @param[in] type The type of the private callback data to get.
718 * @return The type casted private data set NULL if not set.
720 void *tevent_req_callback_data(struct tevent_req *req, #type);
722 void *_tevent_req_callback_data(struct tevent_req *req);
723 #define tevent_req_callback_data(_req, _type) \
724 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
729 * @brief Get the private data for a callback from a tevent request structure.
731 * @param[in] req The structure to get the callback data from.
733 * @param[in] req The structure to get the data from.
735 * @return The private data or NULL if not set.
737 void *tevent_req_callback_data_void(struct tevent_req *req);
739 #define tevent_req_callback_data_void(_req) \
740 _tevent_req_callback_data(_req)
745 * @brief Get the private data from a tevent request structure.
747 * When the tevent_req has been created by tevent_req_create, the
748 * result of tevent_req_data() is the state variable created by
749 * tevent_req_create() as a child of the req.
751 * @param[in] req The structure to get the private data from.
753 * @param[in] type The type of the private data
755 * @return The private data or NULL if not set.
757 void *tevent_req_data(struct tevent_req *req, #type);
759 void *_tevent_req_data(struct tevent_req *req);
760 #define tevent_req_data(_req, _type) \
761 talloc_get_type_abort(_tevent_req_data(_req), _type)
765 * @brief The print function which can be set for a tevent async request.
767 * @param[in] req The tevent async request.
769 * @param[in] ctx A talloc memory context which can be uses to allocate
772 * @return An allocated string buffer to print.
776 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
778 * struct my_data *data = tevent_req_data(req, struct my_data);
781 * result = tevent_req_default_print(mem_ctx, req);
782 * if (result == NULL) {
786 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
787 * data->foo, data->bar);
791 typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
794 * @brief This function sets a print function for the given request.
796 * This function can be used to setup a print function for the given request.
797 * This will be triggered if the tevent_req_print() function was
798 * called on the given request.
800 * @param[in] req The request to use.
802 * @param[in] fn A pointer to the print function
804 * @note This function should only be used for debugging.
806 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
809 * @brief The default print function for creating debug messages.
811 * The function should not be used by users of the async API,
812 * but custom print function can use it and append custom text
815 * @param[in] req The request to be printed.
817 * @param[in] mem_ctx The memory context for the result.
819 * @return Text representation of request.
822 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
825 * @brief Print an tevent_req structure in debug messages.
827 * This function should be used by callers of the async API.
829 * @param[in] mem_ctx The memory context for the result.
831 * @param[in] req The request to be printed.
833 * @return Text representation of request.
835 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
838 * @brief A typedef for a cancel function for a tevent request.
840 * @param[in] req The tevent request calling this function.
842 * @return True if the request could be canceled, false if not.
844 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
847 * @brief This function sets a cancel function for the given tevent request.
849 * This function can be used to setup a cancel function for the given request.
850 * This will be triggered if the tevent_req_cancel() function was
851 * called on the given request.
853 * @param[in] req The request to use.
855 * @param[in] fn A pointer to the cancel function.
857 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
861 * @brief Try to cancel the given tevent request.
863 * This function can be used to cancel the given request.
865 * It is only possible to cancel a request when the implementation
866 * has registered a cancel function via the tevent_req_set_cancel_fn().
868 * @param[in] req The request to use.
870 * @return This function returns true is the request is cancelable,
871 * othererwise false is returned.
873 * @note Even if the function returns true, the caller need to wait
874 * for the function to complete normally.
875 * Only the _recv() function of the given request indicates
876 * if the request was really canceled.
878 bool tevent_req_cancel(struct tevent_req *req);
880 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
881 #define tevent_req_cancel(req) \
882 _tevent_req_cancel(req, __location__)
887 * @brief Create an async tevent request.
889 * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
892 * struct tevent_req *req;
893 * struct computation_state *state;
894 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
897 * Tevent_req_create() creates the state variable as a talloc child of
898 * its result. The state variable should be used as the talloc parent
899 * for all temporary variables that are allocated during the async
900 * computation. This way, when the user of the async computation frees
901 * the request, the state as a talloc child will be free'd along with
902 * all the temporary variables hanging off the state.
904 * @param[in] mem_ctx The memory context for the result.
905 * @param[in] pstate Pointer to the private request state.
906 * @param[in] type The name of the request.
908 * @return A new async request. NULL on error.
910 struct tevent_req *tevent_req_create(TALLOC_CTX *mem_ctx,
911 void **pstate, #type);
913 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
917 const char *location);
919 #define tevent_req_create(_mem_ctx, _pstate, _type) \
920 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
921 #_type, __location__)
925 * @brief Set a timeout for an async request.
927 * @param[in] req The request to set the timeout for.
929 * @param[in] ev The event context to use for the timer.
931 * @param[in] endtime The endtime of the request.
933 * @return True if succeeded, false if not.
935 bool tevent_req_set_endtime(struct tevent_req *req,
936 struct tevent_context *ev,
937 struct timeval endtime);
941 * @brief Call the notify callback of the given tevent request manually.
943 * @param[in] req The tevent request to call the notify function from.
945 * @see tevent_req_set_callback()
947 void tevent_req_notify_callback(struct tevent_req *req);
949 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
950 #define tevent_req_notify_callback(req) \
951 _tevent_req_notify_callback(req, __location__)
956 * @brief An async request has successfully finished.
958 * This function is to be used by implementors of async requests. When a
959 * request is successfully finished, this function calls the user's completion
962 * @param[in] req The finished request.
964 void tevent_req_done(struct tevent_req *req);
966 void _tevent_req_done(struct tevent_req *req,
967 const char *location);
968 #define tevent_req_done(req) \
969 _tevent_req_done(req, __location__)
974 * @brief An async request has seen an error.
976 * This function is to be used by implementors of async requests. When a
977 * request can not successfully completed, the implementation should call this
978 * function with the appropriate status code.
980 * If error is 0 the function returns false and does nothing more.
982 * @param[in] req The request with an error.
984 * @param[in] error The error code.
986 * @return On success true is returned, false if error is 0.
989 * int error = first_function();
990 * if (tevent_req_error(req, error)) {
994 * error = second_function();
995 * if (tevent_req_error(req, error)) {
999 * tevent_req_done(req);
1003 bool tevent_req_error(struct tevent_req *req,
1006 bool _tevent_req_error(struct tevent_req *req,
1008 const char *location);
1009 #define tevent_req_error(req, error) \
1010 _tevent_req_error(req, error, __location__)
1015 * @brief Helper function for nomem check.
1017 * Convenience helper to easily check alloc failure within a callback
1018 * implementing the next step of an async request.
1020 * @param[in] p The pointer to be checked.
1022 * @param[in] req The request being processed.
1025 * p = talloc(mem_ctx, bla);
1026 * if (tevent_req_nomem(p, req)) {
1031 bool tevent_req_nomem(const void *p,
1032 struct tevent_req *req);
1034 bool _tevent_req_nomem(const void *p,
1035 struct tevent_req *req,
1036 const char *location);
1037 #define tevent_req_nomem(p, req) \
1038 _tevent_req_nomem(p, req, __location__)
1043 * @brief Indicate out of memory to a request
1045 * @param[in] req The request being processed.
1047 void tevent_req_oom(struct tevent_req *req);
1049 void _tevent_req_oom(struct tevent_req *req,
1050 const char *location);
1051 #define tevent_req_oom(req) \
1052 _tevent_req_oom(req, __location__)
1056 * @brief Finish a request before the caller had the change to set the callback.
1058 * An implementation of an async request might find that it can either finish
1059 * the request without waiting for an external event, or it can not even start
1060 * the engine. To present the illusion of a callback to the user of the API,
1061 * the implementation can call this helper function which triggers an
1062 * immediate event. This way the caller can use the same calling
1063 * conventions, independent of whether the request was actually deferred.
1066 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1067 * struct tevent_context *ev)
1069 * struct tevent_req *req, *subreq;
1070 * struct computation_state *state;
1071 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1072 * if (req == NULL) {
1075 * subreq = subcomputation_send(state, ev);
1076 * if (tevent_req_nomem(subreq, req)) {
1077 * return tevent_req_post(req, ev);
1079 * tevent_req_set_callback(subreq, computation_done, req);
1084 * @param[in] req The finished request.
1086 * @param[in] ev The tevent_context for the immediate event.
1088 * @return The given request will be returned.
1090 struct tevent_req *tevent_req_post(struct tevent_req *req,
1091 struct tevent_context *ev);
1094 * @brief Finish multiple requests within one function
1096 * Normally tevent_req_notify_callback() and all wrappers
1097 * (e.g. tevent_req_done() and tevent_req_error())
1098 * need to be the last thing an event handler should call.
1099 * This is because the callback is likely to destroy the
1100 * context of the current function.
1102 * If a function wants to notify more than one caller,
1103 * it is dangerous if it just triggers multiple callbacks
1104 * in a row. With tevent_req_defer_callback() it is possible
1105 * to set an event context that will be used to defer the callback
1106 * via an immediate event (similar to tevent_req_post()).
1109 * struct complete_state {
1110 * struct tevent_context *ev;
1112 * struct tevent_req **reqs;
1115 * void complete(struct complete_state *state)
1117 * size_t i, c = talloc_array_length(state->reqs);
1119 * for (i=0; i < c; i++) {
1120 * tevent_req_defer_callback(state->reqs[i], state->ev);
1121 * tevent_req_done(state->reqs[i]);
1126 * @param[in] req The finished request.
1128 * @param[in] ev The tevent_context for the immediate event.
1130 * @return The given request will be returned.
1132 void tevent_req_defer_callback(struct tevent_req *req,
1133 struct tevent_context *ev);
1136 * @brief Check if the given request is still in progress.
1138 * It is typically used by sync wrapper functions.
1140 * @param[in] req The request to poll.
1142 * @return The boolean form of "is in progress".
1144 bool tevent_req_is_in_progress(struct tevent_req *req);
1147 * @brief Actively poll for the given request to finish.
1149 * This function is typically used by sync wrapper functions.
1151 * @param[in] req The request to poll.
1153 * @param[in] ev The tevent_context to be used.
1155 * @return On success true is returned. If a critical error has
1156 * happened in the tevent loop layer false is returned.
1157 * This is not the return value of the given request!
1159 * @note This should only be used if the given tevent context was created by the
1160 * caller, to avoid event loop nesting.
1163 * req = tstream_writev_queue_send(mem_ctx,
1168 * ok = tevent_req_poll(req, tctx->ev);
1169 * rc = tstream_writev_queue_recv(req, &sys_errno);
1173 bool tevent_req_poll(struct tevent_req *req,
1174 struct tevent_context *ev);
1177 * @brief Get the tevent request state and the actual error set by
1181 * int computation_recv(struct tevent_req *req, uint64_t *perr)
1183 * enum tevent_req_state state;
1185 * if (tevent_req_is_error(req, &state, &err)) {
1193 * @param[in] req The tevent request to get the error from.
1195 * @param[out] state A pointer to store the tevent request error state.
1197 * @param[out] error A pointer to store the error set by tevent_req_error().
1199 * @return True if the function could set error and state, false
1202 * @see tevent_req_error()
1204 bool tevent_req_is_error(struct tevent_req *req,
1205 enum tevent_req_state *state,
1209 * @brief Use as the last action of a _recv() function.
1211 * This function destroys the attached private data.
1213 * @param[in] req The finished request.
1215 void tevent_req_received(struct tevent_req *req);
1218 * @brief Create a tevent subrequest at a given time.
1220 * The idea is that always the same syntax for tevent requests.
1222 * @param[in] mem_ctx The talloc memory context to use.
1224 * @param[in] ev The event handle to setup the request.
1226 * @param[in] wakeup_time The time to wakeup and execute the request.
1228 * @return The new subrequest, NULL on error.
1232 * static void my_callback_wakeup_done(tevent_req *subreq)
1234 * struct tevent_req *req = tevent_req_callback_data(subreq,
1235 * struct tevent_req);
1238 * ok = tevent_wakeup_recv(subreq);
1239 * TALLOC_FREE(subreq);
1241 * tevent_req_error(req, -1);
1249 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1250 * if (tevent_req_nomem(subreq, req)) {
1253 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1256 * @see tevent_wakeup_recv()
1258 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
1259 struct tevent_context *ev,
1260 struct timeval wakeup_time);
1263 * @brief Check if the wakeup has been correctly executed.
1265 * This function needs to be called in the callback function set after calling
1266 * tevent_wakeup_send().
1268 * @param[in] req The tevent request to check.
1270 * @return True on success, false otherwise.
1272 * @see tevent_wakeup_recv()
1274 bool tevent_wakeup_recv(struct tevent_req *req);
1279 * @defgroup tevent_helpers The tevent helper functiions
1288 * @brief Compare two timeval values.
1290 * @param[in] tv1 The first timeval value to compare.
1292 * @param[in] tv2 The second timeval value to compare.
1294 * @return 0 if they are equal.
1295 * 1 if the first time is greater than the second.
1296 * -1 if the first time is smaller than the second.
1298 int tevent_timeval_compare(const struct timeval *tv1,
1299 const struct timeval *tv2);
1302 * @brief Get a zero timval value.
1304 * @return A zero timval value.
1306 struct timeval tevent_timeval_zero(void);
1309 * @brief Get a timeval value for the current time.
1311 * @return A timval value with the current time.
1313 struct timeval tevent_timeval_current(void);
1316 * @brief Get a timeval structure with the given values.
1318 * @param[in] secs The seconds to set.
1320 * @param[in] usecs The microseconds to set.
1322 * @return A timeval structure with the given values.
1324 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
1327 * @brief Get the difference between two timeval values.
1329 * @param[in] tv1 The first timeval.
1331 * @param[in] tv2 The second timeval.
1333 * @return A timeval structure with the difference between the
1334 * first and the second value.
1336 struct timeval tevent_timeval_until(const struct timeval *tv1,
1337 const struct timeval *tv2);
1340 * @brief Check if a given timeval structure is zero.
1342 * @param[in] tv The timeval to check if it is zero.
1344 * @return True if it is zero, false otherwise.
1346 bool tevent_timeval_is_zero(const struct timeval *tv);
1349 * @brief Add the given amount of time to a timeval structure.
1351 * @param[in] tv The timeval structure to add the time.
1353 * @param[in] secs The seconds to add to the timeval.
1355 * @param[in] usecs The microseconds to add to the timeval.
1357 * @return The timeval structure with the new time.
1359 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
1363 * @brief Get a timeval in the future with a specified offset from now.
1365 * @param[in] secs The seconds of the offset from now.
1367 * @param[in] usecs The microseconds of the offset from now.
1369 * @return A timval with the given offset in the future.
1371 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
1377 * @defgroup tevent_queue The tevent queue functions
1380 * A tevent_queue is used to queue up async requests that must be
1381 * serialized. For example writing buffers into a socket must be
1382 * serialized. Writing a large lump of data into a socket can require
1383 * multiple write(2) or send(2) system calls. If more than one async
1384 * request is outstanding to write large buffers into a socket, every
1385 * request must individually be completed before the next one begins,
1386 * even if multiple syscalls are required.
1388 * Take a look at @ref tevent_queue_tutorial for more details.
1392 struct tevent_queue;
1393 struct tevent_queue_entry;
1397 * @brief Create and start a tevent queue.
1399 * @param[in] mem_ctx The talloc memory context to allocate the queue.
1401 * @param[in] name The name to use to identify the queue.
1403 * @return An allocated tevent queue on success, NULL on error.
1405 * @see tevent_queue_start()
1406 * @see tevent_queue_stop()
1408 struct tevent_queue *tevent_queue_create(TALLOC_CTX *mem_ctx,
1411 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
1413 const char *location);
1415 #define tevent_queue_create(_mem_ctx, _name) \
1416 _tevent_queue_create((_mem_ctx), (_name), __location__)
1420 * @brief A callback trigger function run by the queue.
1422 * @param[in] req The tevent request the trigger function is executed on.
1424 * @param[in] private_data The private data pointer specified by
1425 * tevent_queue_add().
1427 * @see tevent_queue_add()
1428 * @see tevent_queue_add_entry()
1429 * @see tevent_queue_add_optimize_empty()
1431 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
1432 void *private_data);
1435 * @brief Add a tevent request to the queue.
1437 * @param[in] queue The queue to add the request.
1439 * @param[in] ev The event handle to use for the request.
1441 * @param[in] req The tevent request to add to the queue.
1443 * @param[in] trigger The function triggered by the queue when the request
1444 * is called. Since tevent 0.9.14 it's possible to
1445 * pass NULL, in order to just add a "blocker" to the
1448 * @param[in] private_data The private data passed to the trigger function.
1450 * @return True if the request has been successfully added, false
1453 bool tevent_queue_add(struct tevent_queue *queue,
1454 struct tevent_context *ev,
1455 struct tevent_req *req,
1456 tevent_queue_trigger_fn_t trigger,
1457 void *private_data);
1460 * @brief Add a tevent request to the queue.
1462 * The request can be removed from the queue by calling talloc_free()
1463 * (or a similar function) on the returned queue entry. This
1464 * is the only difference to tevent_queue_add().
1466 * @param[in] queue The queue to add the request.
1468 * @param[in] ev The event handle to use for the request.
1470 * @param[in] req The tevent request to add to the queue.
1472 * @param[in] trigger The function triggered by the queue when the request
1473 * is called. Since tevent 0.9.14 it's possible to
1474 * pass NULL, in order to just add a "blocker" to the
1477 * @param[in] private_data The private data passed to the trigger function.
1479 * @return a pointer to the tevent_queue_entry if the request
1480 * has been successfully added, NULL otherwise.
1482 * @see tevent_queue_add()
1483 * @see tevent_queue_add_optimize_empty()
1485 struct tevent_queue_entry *tevent_queue_add_entry(
1486 struct tevent_queue *queue,
1487 struct tevent_context *ev,
1488 struct tevent_req *req,
1489 tevent_queue_trigger_fn_t trigger,
1490 void *private_data);
1493 * @brief Add a tevent request to the queue using a possible optimization.
1495 * This tries to optimize for the empty queue case and may calls
1496 * the trigger function directly. This is the only difference compared
1497 * to tevent_queue_add_entry().
1499 * The caller needs to be prepared that the trigger function has
1500 * already called tevent_req_notify_callback(), tevent_req_error(),
1501 * tevent_req_done() or a similar function.
1503 * The request can be removed from the queue by calling talloc_free()
1504 * (or a similar function) on the returned queue entry.
1506 * @param[in] queue The queue to add the request.
1508 * @param[in] ev The event handle to use for the request.
1510 * @param[in] req The tevent request to add to the queue.
1512 * @param[in] trigger The function triggered by the queue when the request
1513 * is called. Since tevent 0.9.14 it's possible to
1514 * pass NULL, in order to just add a "blocker" to the
1517 * @param[in] private_data The private data passed to the trigger function.
1519 * @return a pointer to the tevent_queue_entry if the request
1520 * has been successfully added, NULL otherwise.
1522 * @see tevent_queue_add()
1523 * @see tevent_queue_add_entry()
1525 struct tevent_queue_entry *tevent_queue_add_optimize_empty(
1526 struct tevent_queue *queue,
1527 struct tevent_context *ev,
1528 struct tevent_req *req,
1529 tevent_queue_trigger_fn_t trigger,
1530 void *private_data);
1533 * @brief Start a tevent queue.
1535 * The queue is started by default.
1537 * @param[in] queue The queue to start.
1539 void tevent_queue_start(struct tevent_queue *queue);
1542 * @brief Stop a tevent queue.
1544 * The queue is started by default.
1546 * @param[in] queue The queue to stop.
1548 void tevent_queue_stop(struct tevent_queue *queue);
1551 * @brief Get the length of the queue.
1553 * @param[in] queue The queue to get the length from.
1555 * @return The number of elements.
1557 size_t tevent_queue_length(struct tevent_queue *queue);
1560 * @brief Is the tevent queue running.
1562 * The queue is started by default.
1564 * @param[in] queue The queue.
1566 * @return Wether the queue is running or not..
1568 bool tevent_queue_running(struct tevent_queue *queue);
1570 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
1575 const char *location);
1576 #ifdef TEVENT_DEPRECATED
1577 #ifndef _DEPRECATED_
1578 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1579 #define _DEPRECATED_ __attribute__ ((deprecated))
1581 #define _DEPRECATED_
1584 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
1585 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
1586 tevent_nesting_hook hook,
1587 void *private_data) _DEPRECATED_;
1588 int _tevent_loop_until(struct tevent_context *ev,
1589 bool (*finished)(void *private_data),
1591 const char *location) _DEPRECATED_;
1592 #define tevent_loop_until(ev, finished, private_data) \
1593 _tevent_loop_until(ev, finished, private_data, __location__)
1596 int tevent_re_initialise(struct tevent_context *ev);
1601 * @defgroup tevent_ops The tevent operation functions
1604 * The following structure and registration functions are exclusively
1605 * needed for people writing and pluggin a different event engine.
1606 * There is nothing useful for normal tevent user in here.
1612 int (*context_init)(struct tevent_context *ev);
1614 /* fd_event functions */
1615 struct tevent_fd *(*add_fd)(struct tevent_context *ev,
1616 TALLOC_CTX *mem_ctx,
1617 int fd, uint16_t flags,
1618 tevent_fd_handler_t handler,
1620 const char *handler_name,
1621 const char *location);
1622 void (*set_fd_close_fn)(struct tevent_fd *fde,
1623 tevent_fd_close_fn_t close_fn);
1624 uint16_t (*get_fd_flags)(struct tevent_fd *fde);
1625 void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
1627 /* timed_event functions */
1628 struct tevent_timer *(*add_timer)(struct tevent_context *ev,
1629 TALLOC_CTX *mem_ctx,
1630 struct timeval next_event,
1631 tevent_timer_handler_t handler,
1633 const char *handler_name,
1634 const char *location);
1636 /* immediate event functions */
1637 void (*schedule_immediate)(struct tevent_immediate *im,
1638 struct tevent_context *ev,
1639 tevent_immediate_handler_t handler,
1641 const char *handler_name,
1642 const char *location);
1644 /* signal functions */
1645 struct tevent_signal *(*add_signal)(struct tevent_context *ev,
1646 TALLOC_CTX *mem_ctx,
1647 int signum, int sa_flags,
1648 tevent_signal_handler_t handler,
1650 const char *handler_name,
1651 const char *location);
1653 /* loop functions */
1654 int (*loop_once)(struct tevent_context *ev, const char *location);
1655 int (*loop_wait)(struct tevent_context *ev, const char *location);
1658 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
1663 * @defgroup tevent_compat The tevent compatibility functions
1666 * The following definitions are usueful only for compatibility with the
1667 * implementation originally developed within the samba4 code and will be
1668 * soon removed. Please NEVER use in new code.
1675 #ifdef TEVENT_COMPAT_DEFINES
1677 #define event_context tevent_context
1678 #define event_ops tevent_ops
1679 #define fd_event tevent_fd
1680 #define timed_event tevent_timer
1681 #define signal_event tevent_signal
1683 #define event_fd_handler_t tevent_fd_handler_t
1684 #define event_timed_handler_t tevent_timer_handler_t
1685 #define event_signal_handler_t tevent_signal_handler_t
1687 #define event_context_init(mem_ctx) \
1688 tevent_context_init(mem_ctx)
1690 #define event_context_init_byname(mem_ctx, name) \
1691 tevent_context_init_byname(mem_ctx, name)
1693 #define event_backend_list(mem_ctx) \
1694 tevent_backend_list(mem_ctx)
1696 #define event_set_default_backend(backend) \
1697 tevent_set_default_backend(backend)
1699 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1700 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1702 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1703 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1705 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1706 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1708 #define event_loop_once(ev) \
1709 tevent_loop_once(ev)
1711 #define event_loop_wait(ev) \
1712 tevent_loop_wait(ev)
1714 #define event_get_fd_flags(fde) \
1715 tevent_fd_get_flags(fde)
1717 #define event_set_fd_flags(fde, flags) \
1718 tevent_fd_set_flags(fde, flags)
1720 #define EVENT_FD_READ TEVENT_FD_READ
1721 #define EVENT_FD_WRITE TEVENT_FD_WRITE
1723 #define EVENT_FD_WRITEABLE(fde) \
1724 TEVENT_FD_WRITEABLE(fde)
1726 #define EVENT_FD_READABLE(fde) \
1727 TEVENT_FD_READABLE(fde)
1729 #define EVENT_FD_NOT_WRITEABLE(fde) \
1730 TEVENT_FD_NOT_WRITEABLE(fde)
1732 #define EVENT_FD_NOT_READABLE(fde) \
1733 TEVENT_FD_NOT_READABLE(fde)
1735 #define ev_debug_level tevent_debug_level
1737 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
1738 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
1739 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
1740 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
1742 #define ev_set_debug(ev, debug, context) \
1743 tevent_set_debug(ev, debug, context)
1745 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1747 #endif /* TEVENT_COMPAT_DEFINES */
1751 #endif /* __TEVENT_H__ */