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 name it.
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 for the tevent context.
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 backent.
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
);
509 * @defgroup tevent_request The tevent request functions.
512 * This represents an async request being processed by callbacks via an event
513 * context. A user can issue for example a write request to a socket, giving
514 * an implementation function the fd, the buffer and the number of bytes to
515 * transfer. The function issuing the request will immediately return without
516 * blocking most likely without having sent anything. The API user then fills
517 * in req->async.fn and req->async.private_data, functions that are called
518 * when the request is finished.
520 * It is up to the user of the async request to talloc_free it after it has
521 * finished. This can happen while the completion function is called.
527 * An async request moves between the following 4 states:
529 enum tevent_req_state
{
531 * we are creating the request
535 * we are waiting the request to complete
537 TEVENT_REQ_IN_PROGRESS
,
539 * the request is finished
543 * A user error has occurred
545 TEVENT_REQ_USER_ERROR
,
549 TEVENT_REQ_TIMED_OUT
,
551 * No memory in between
553 TEVENT_REQ_NO_MEMORY
,
555 * the request is already received by the caller
561 * @brief An async request
566 * @brief A tevent request callback function.
568 * @param[in] req The tevent async request which executed this callback.
570 typedef void (*tevent_req_fn
)(struct tevent_req
*req
);
573 * @brief Set an async request callback.
575 * @param[in] req The async request to set the callback.
577 * @param[in] fn The callback function to set.
579 * @param[in] pvt A pointer to private data to pass to the async request
582 void tevent_req_set_callback(struct tevent_req
*req
, tevent_req_fn fn
, void *pvt
);
586 * @brief Get the private data casted to the given type for a callback from
587 * a tevent request structure.
589 * @param[in] req The structure to get the callback data from.
591 * @param[in] type The type of the private callback data to get.
593 * @return The type casted private data set NULL if not set.
595 void *tevent_req_callback_data(struct tevent_req
*req
, #type);
597 void *_tevent_req_callback_data(struct tevent_req
*req
);
598 #define tevent_req_callback_data(_req, _type) \
599 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
604 * @brief Get the private data for a callback from a tevent request structure.
606 * @param[in] req The structure to get the callback data from.
608 * @param[in] req The structure to get the data from.
610 * @return The private data or NULL if not set.
612 void *tevent_req_callback_data_void(struct tevent_req
*req
);
614 #define tevent_req_callback_data_void(_req) \
615 _tevent_req_callback_data(_req)
620 * @brief Get the private data from a tevent request structure.
622 * @param[in] req The structure to get the private data from.
624 * @return The private data or NULL if not set.
626 void *tevent_req_data(struct tevent_req
*req
);
628 void *_tevent_req_data(struct tevent_req
*req
);
629 #define tevent_req_data(_req, _type) \
630 talloc_get_type_abort(_tevent_req_data(_req), _type)
634 * @brief The print function which can be set for a tevent async request.
636 * @param[in] req The tevent async request.
638 * @param[in] ctx A talloc memory context which can be uses to allocate
641 * @return An allocated string buffer to print.
645 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
647 * struct my_data *data = tevent_req_data(req, struct my_data);
650 * result = tevent_req_default_print(mem_ctx, req);
651 * if (result == NULL) {
655 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
656 * data->foo, data->bar);
660 typedef char *(*tevent_req_print_fn
)(struct tevent_req
*req
, TALLOC_CTX
*ctx
);
663 * @brief This function sets a print function for the given request.
665 * This function can be used to setup a print function for the given request.
666 * This will be triggered if the tevent_req_print() function was
667 * called on the given request.
669 * @param[in] req The request to use.
671 * @param[in] fn A pointer to the print function
673 * @note This function should only be used for debugging.
675 void tevent_req_set_print_fn(struct tevent_req
*req
, tevent_req_print_fn fn
);
678 * @brief The default print function for creating debug messages.
680 * The function should not be used by users of the async API,
681 * but custom print function can use it and append custom text
684 * @param[in] req The request to be printed.
686 * @param[in] mem_ctx The memory context for the result.
688 * @return Text representation of request.
691 char *tevent_req_default_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
);
694 * @brief Print an tevent_req structure in debug messages.
696 * This function should be used by callers of the async API.
698 * @param[in] mem_ctx The memory context for the result.
700 * @param[in] req The request to be printed.
702 * @return Text representation of request.
704 char *tevent_req_print(TALLOC_CTX
*mem_ctx
, struct tevent_req
*req
);
707 * @brief A typedef for a cancel function for a tevent request.
709 * @param[in] req The tevent request calling this function.
711 * @return True if the request could be canceled, false if not.
713 typedef bool (*tevent_req_cancel_fn
)(struct tevent_req
*req
);
716 * @brief This function sets a cancel function for the given tevent request.
718 * This function can be used to setup a cancel function for the given request.
719 * This will be triggered if the tevent_req_cancel() function was
720 * called on the given request.
722 * @param[in] req The request to use.
724 * @param[in] fn A pointer to the cancel function.
726 void tevent_req_set_cancel_fn(struct tevent_req
*req
, tevent_req_cancel_fn fn
);
730 * @brief Try to cancel the given tevent request.
732 * This function can be used to cancel the given request.
734 * It is only possible to cancel a request when the implementation
735 * has registered a cancel function via the tevent_req_set_cancel_fn().
737 * @param[in] req The request to use.
739 * @return This function returns true is the request is cancelable,
740 * othererwise false is returned.
742 * @note Even if the function returns true, the caller need to wait
743 * for the function to complete normally.
744 * Only the _recv() function of the given request indicates
745 * if the request was really canceled.
747 bool tevent_req_cancel(struct tevent_req
*req
);
749 bool _tevent_req_cancel(struct tevent_req
*req
, const char *location
);
750 #define tevent_req_cancel(req) \
751 _tevent_req_cancel(req, __location__)
756 * @brief Create an async tevent request.
758 * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS.
760 * @param[in] mem_ctx The memory context for the result.
762 * @param[in] pstate The private state of the request.
764 * @param[in] state_size The size of the private state of the request.
766 * @param[in] type The name of the request.
768 * @return A new async request. NULL on error.
770 struct tevent_req
*tevent_req_create(TALLOC_CTX
*mem_ctx
,
775 struct tevent_req
*_tevent_req_create(TALLOC_CTX
*mem_ctx
,
779 const char *location
);
781 #define tevent_req_create(_mem_ctx, _pstate, _type) \
782 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
783 #_type, __location__)
787 * @brief Set a timeout for an async request.
789 * @param[in] req The request to set the timeout for.
791 * @param[in] ev The event context to use for the timer.
793 * @param[in] endtime The endtime of the request.
795 * @return True if succeeded, false if not.
797 bool tevent_req_set_endtime(struct tevent_req
*req
,
798 struct tevent_context
*ev
,
799 struct timeval endtime
);
803 * @brief Call the notify callback of the given tevent request manually.
805 * @param[in] req The tevent request to call the notify function from.
807 * @see tevent_req_set_callback()
809 void tevent_req_notify_callback(struct tevent_req
*req
);
811 void _tevent_req_notify_callback(struct tevent_req
*req
, const char *location
);
812 #define tevent_req_notify_callback(req) \
813 _tevent_req_notify_callback(req, __location__)
818 * @brief An async request has successfully finished.
820 * This function is to be used by implementors of async requests. When a
821 * request is successfully finished, this function calls the user's completion
824 * @param[in] req The finished request.
826 void tevent_req_done(struct tevent_req
*req
);
828 void _tevent_req_done(struct tevent_req
*req
,
829 const char *location
);
830 #define tevent_req_done(req) \
831 _tevent_req_done(req, __location__)
836 * @brief An async request has seen an error.
838 * This function is to be used by implementors of async requests. When a
839 * request can not successfully completed, the implementation should call this
840 * function with the appropriate status code.
842 * If error is 0 the function returns false and does nothing more.
844 * @param[in] req The request with an error.
846 * @param[in] error The error code.
848 * @return On success true is returned, false if error is 0.
851 * int error = first_function();
852 * if (tevent_req_error(req, error)) {
856 * error = second_function();
857 * if (tevent_req_error(req, error)) {
861 * tevent_req_done(req);
865 bool tevent_req_error(struct tevent_req
*req
,
868 bool _tevent_req_error(struct tevent_req
*req
,
870 const char *location
);
871 #define tevent_req_error(req, error) \
872 _tevent_req_error(req, error, __location__)
877 * @brief Helper function for nomem check.
879 * Convenience helper to easily check alloc failure within a callback
880 * implementing the next step of an async request.
882 * @param[in] p The pointer to be checked.
884 * @param[in] req The request being processed.
887 * p = talloc(mem_ctx, bla);
888 * if (tevent_req_nomem(p, req)) {
893 bool tevent_req_nomem(const void *p
,
894 struct tevent_req
*req
);
896 bool _tevent_req_nomem(const void *p
,
897 struct tevent_req
*req
,
898 const char *location
);
899 #define tevent_req_nomem(p, req) \
900 _tevent_req_nomem(p, req, __location__)
904 * @brief Finish a request before the caller had the change to set the callback.
906 * An implementation of an async request might find that it can either finish
907 * the request without waiting for an external event, or it can't even start
908 * the engine. To present the illusion of a callback to the user of the API,
909 * the implementation can call this helper function which triggers an
910 * immediate timed event. This way the caller can use the same calling
911 * conventions, independent of whether the request was actually deferred.
913 * @param[in] req The finished request.
915 * @param[in] ev The tevent_context for the timed event.
917 * @return The given request will be returned.
919 struct tevent_req
*tevent_req_post(struct tevent_req
*req
,
920 struct tevent_context
*ev
);
923 * @brief Check if the given request is still in progress.
925 * It is typically used by sync wrapper functions.
927 * This function destroys the attached private data.
929 * @param[in] req The request to poll.
931 * @return The boolean form of "is in progress".
933 bool tevent_req_is_in_progress(struct tevent_req
*req
);
936 * @brief Actively poll for the given request to finish.
938 * This function is typically used by sync wrapper functions.
940 * @param[in] req The request to poll.
942 * @param[in] ev The tevent_context to be used.
944 * @return On success true is returned. If a critical error has
945 * happened in the tevent loop layer false is returned.
946 * This is not the return value of the given request!
948 * @note This should only be used if the given tevent context was created by the
949 * caller, to avoid event loop nesting.
952 * req = tstream_writev_queue_send(mem_ctx,
957 * ok = tevent_req_poll(req, tctx->ev);
958 * rc = tstream_writev_queue_recv(req, &sys_errno);
962 bool tevent_req_poll(struct tevent_req
*req
,
963 struct tevent_context
*ev
);
966 * @brief Get the tevent request and the actual error code you've set.
968 * @param[in] req The tevent request to get the error from.
970 * @param[out] state A pointer to store the tevent request error state.
972 * @param[out] error A pointer to store the error set by tevent_req_error().
974 * @return True if the function could set error and state, false
977 * @see tevent_req_error()
979 bool tevent_req_is_error(struct tevent_req
*req
,
980 enum tevent_req_state
*state
,
984 * @brief Use as the last action of a _recv() function.
986 * This function destroys the attached private data.
988 * @param[in] req The finished request.
990 void tevent_req_received(struct tevent_req
*req
);
993 * @brief Create a tevent subrequest at a given time.
995 * The idea is that always the same syntax for tevent requests.
997 * @param[in] mem_ctx The talloc memory context to use.
999 * @param[in] ev The event handle to setup the request.
1001 * @param[in] wakeup_time The time to wakeup and execute the request.
1003 * @return The new subrequest, NULL on error.
1007 * static my_callback_wakeup_done(tevent_req *req)
1009 * struct tevent_req *req = tevent_req_callback_data(subreq,
1010 * struct tevent_req);
1013 * ok = tevent_wakeup_recv(subreq);
1014 * TALLOC_FREE(subreq);
1016 * tevent_req_error(req, -1);
1024 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1025 * if (tevent_req_nomem(subreq, req)) {
1028 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1031 * @see tevent_wakeup_recv()
1033 struct tevent_req
*tevent_wakeup_send(TALLOC_CTX
*mem_ctx
,
1034 struct tevent_context
*ev
,
1035 struct timeval wakeup_time
);
1038 * @brief Check if the wakeup has been correctly executed.
1040 * This function needs to be called in the callback function set after calling
1041 * tevent_wakeup_send().
1043 * @param[in] req The tevent request to check.
1045 * @return True on success, false otherwise.
1047 * @see tevent_wakeup_recv()
1049 bool tevent_wakeup_recv(struct tevent_req
*req
);
1054 * @defgroup tevent_helpers The tevent helper functiions
1063 * @brief Compare two timeval values.
1065 * @param[in] tv1 The first timeval value to compare.
1067 * @param[in] tv2 The second timeval value to compare.
1069 * @return 0 if they are equal.
1070 * 1 if the first time is greater than the second.
1071 * -1 if the first time is smaller than the second.
1073 int tevent_timeval_compare(const struct timeval
*tv1
,
1074 const struct timeval
*tv2
);
1077 * @brief Get a zero timval value.
1079 * @return A zero timval value.
1081 struct timeval
tevent_timeval_zero(void);
1084 * @brief Get a timeval value for the current time.
1086 * @return A timval value with the current time.
1088 struct timeval
tevent_timeval_current(void);
1091 * @brief Get a timeval structure with the given values.
1093 * @param[in] secs The seconds to set.
1095 * @param[in] usecs The milliseconds to set.
1097 * @return A timeval structure with the given values.
1099 struct timeval
tevent_timeval_set(uint32_t secs
, uint32_t usecs
);
1102 * @brief Get the difference between two timeval values.
1104 * @param[in] tv1 The first timeval.
1106 * @param[in] tv2 The second timeval.
1108 * @return A timeval structure with the difference between the
1109 * first and the second value.
1111 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
1112 const struct timeval
*tv2
);
1115 * @brief Check if a given timeval structure is zero.
1117 * @param[in] tv The timeval to check if it is zero.
1119 * @return True if it is zero, false otherwise.
1121 bool tevent_timeval_is_zero(const struct timeval
*tv
);
1124 * @brief Add the given amount of time to a timeval structure.
1126 * @param[in] tv The timeval structure to add the time.
1128 * @param[in] secs The seconds to add to the timeval.
1130 * @param[in] usecs The milliseconds to add to the timeval.
1132 * @return The timeval structure with the new time.
1134 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
1138 * @brief Get a timeval in the future with a specified offset from now.
1140 * @param[in] secs The seconds of the offset from now.
1142 * @param[in] usecs The milliseconds of the offset from now.
1144 * @return A timval with the given offset in the future.
1146 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
);
1152 * @defgroup tevent_queue The tevent queue functions
1155 * A tevent_queue is used to queue up async requests that must be
1156 * serialized. For example writing buffers into a socket must be
1157 * serialized. Writing a large lump of data into a socket can require
1158 * multiple write(2) or send(2) system calls. If more than one async
1159 * request is outstanding to write large buffers into a socket, every
1160 * request must individually be completed before the next one begins,
1161 * even if multiple syscalls are required.
1163 * Take a look at @ref tevent_queue_tutorial for more details.
1167 struct tevent_queue
;
1171 * @brief Create and start a tevent queue.
1173 * @param[in] mem_ctx The talloc memory context to allocate the queue.
1175 * @param[in] name The name to use to identify the queue.
1177 * @return An allocated tevent queue on success, NULL on error.
1179 * @see tevent_start()
1180 * @see tevent_stop()
1182 struct tevent_queue
*tevent_queue_create(TALLOC_CTX
*mem_ctx
,
1185 struct tevent_queue
*_tevent_queue_create(TALLOC_CTX
*mem_ctx
,
1187 const char *location
);
1189 #define tevent_queue_create(_mem_ctx, _name) \
1190 _tevent_queue_create((_mem_ctx), (_name), __location__)
1194 * @brief A callback trigger function run by the queue.
1196 * @param[in] req The tevent request the trigger function is executed on.
1198 * @param[in] private_data The private data pointer specified by
1199 * tevent_queue_add().
1201 * @see tevent_queue_add()
1203 typedef void (*tevent_queue_trigger_fn_t
)(struct tevent_req
*req
,
1204 void *private_data
);
1207 * @brief Add a tevent request to the queue.
1209 * @param[in] queue The queue to add the request.
1211 * @param[in] ev The event handle to use for the request.
1213 * @param[in] req The tevent request to add to the queue.
1215 * @param[in] trigger The function triggered by the queue when the request
1218 * @param[in] private_data The private data passed to the trigger function.
1220 * @return True if the request has been successfully added, false
1223 bool tevent_queue_add(struct tevent_queue
*queue
,
1224 struct tevent_context
*ev
,
1225 struct tevent_req
*req
,
1226 tevent_queue_trigger_fn_t trigger
,
1227 void *private_data
);
1230 * @brief Start a tevent queue.
1232 * The queue is started by default.
1234 * @param[in] queue The queue to start.
1236 void tevent_queue_start(struct tevent_queue
*queue
);
1239 * @brief Stop a tevent queue.
1241 * The queue is started by default.
1243 * @param[in] queue The queue to stop.
1245 void tevent_queue_stop(struct tevent_queue
*queue
);
1248 * @brief Get the length of the queue.
1250 * @param[in] queue The queue to get the length from.
1252 * @return The number of elements.
1254 size_t tevent_queue_length(struct tevent_queue
*queue
);
1256 typedef int (*tevent_nesting_hook
)(struct tevent_context
*ev
,
1261 const char *location
);
1262 #ifdef TEVENT_DEPRECATED
1263 #ifndef _DEPRECATED_
1264 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1265 #define _DEPRECATED_ __attribute__ ((deprecated))
1267 #define _DEPRECATED_
1270 void tevent_loop_allow_nesting(struct tevent_context
*ev
) _DEPRECATED_
;
1271 void tevent_loop_set_nesting_hook(struct tevent_context
*ev
,
1272 tevent_nesting_hook hook
,
1273 void *private_data
) _DEPRECATED_
;
1274 int _tevent_loop_until(struct tevent_context
*ev
,
1275 bool (*finished
)(void *private_data
),
1277 const char *location
) _DEPRECATED_
;
1278 #define tevent_loop_until(ev, finished, private_data) \
1279 _tevent_loop_until(ev, finished, private_data, __location__)
1282 int tevent_re_initialise(struct tevent_context
*ev
);
1287 * @defgroup tevent_ops The tevent operation functions
1290 * The following structure and registration functions are exclusively
1291 * needed for people writing and pluggin a different event engine.
1292 * There is nothing useful for normal tevent user in here.
1298 int (*context_init
)(struct tevent_context
*ev
);
1300 /* fd_event functions */
1301 struct tevent_fd
*(*add_fd
)(struct tevent_context
*ev
,
1302 TALLOC_CTX
*mem_ctx
,
1303 int fd
, uint16_t flags
,
1304 tevent_fd_handler_t handler
,
1306 const char *handler_name
,
1307 const char *location
);
1308 void (*set_fd_close_fn
)(struct tevent_fd
*fde
,
1309 tevent_fd_close_fn_t close_fn
);
1310 uint16_t (*get_fd_flags
)(struct tevent_fd
*fde
);
1311 void (*set_fd_flags
)(struct tevent_fd
*fde
, uint16_t flags
);
1313 /* timed_event functions */
1314 struct tevent_timer
*(*add_timer
)(struct tevent_context
*ev
,
1315 TALLOC_CTX
*mem_ctx
,
1316 struct timeval next_event
,
1317 tevent_timer_handler_t handler
,
1319 const char *handler_name
,
1320 const char *location
);
1322 /* immediate event functions */
1323 void (*schedule_immediate
)(struct tevent_immediate
*im
,
1324 struct tevent_context
*ev
,
1325 tevent_immediate_handler_t handler
,
1327 const char *handler_name
,
1328 const char *location
);
1330 /* signal functions */
1331 struct tevent_signal
*(*add_signal
)(struct tevent_context
*ev
,
1332 TALLOC_CTX
*mem_ctx
,
1333 int signum
, int sa_flags
,
1334 tevent_signal_handler_t handler
,
1336 const char *handler_name
,
1337 const char *location
);
1339 /* loop functions */
1340 int (*loop_once
)(struct tevent_context
*ev
, const char *location
);
1341 int (*loop_wait
)(struct tevent_context
*ev
, const char *location
);
1344 bool tevent_register_backend(const char *name
, const struct tevent_ops
*ops
);
1349 * @defgroup tevent_compat The tevent compatibility functions
1352 * The following definitions are usueful only for compatibility with the
1353 * implementation originally developed within the samba4 code and will be
1354 * soon removed. Please NEVER use in new code.
1361 #ifdef TEVENT_COMPAT_DEFINES
1363 #define event_context tevent_context
1364 #define event_ops tevent_ops
1365 #define fd_event tevent_fd
1366 #define timed_event tevent_timer
1367 #define signal_event tevent_signal
1369 #define event_fd_handler_t tevent_fd_handler_t
1370 #define event_timed_handler_t tevent_timer_handler_t
1371 #define event_signal_handler_t tevent_signal_handler_t
1373 #define event_context_init(mem_ctx) \
1374 tevent_context_init(mem_ctx)
1376 #define event_context_init_byname(mem_ctx, name) \
1377 tevent_context_init_byname(mem_ctx, name)
1379 #define event_backend_list(mem_ctx) \
1380 tevent_backend_list(mem_ctx)
1382 #define event_set_default_backend(backend) \
1383 tevent_set_default_backend(backend)
1385 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1386 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1388 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1389 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1391 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1392 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1394 #define event_loop_once(ev) \
1395 tevent_loop_once(ev)
1397 #define event_loop_wait(ev) \
1398 tevent_loop_wait(ev)
1400 #define event_get_fd_flags(fde) \
1401 tevent_fd_get_flags(fde)
1403 #define event_set_fd_flags(fde, flags) \
1404 tevent_fd_set_flags(fde, flags)
1406 #define EVENT_FD_READ TEVENT_FD_READ
1407 #define EVENT_FD_WRITE TEVENT_FD_WRITE
1409 #define EVENT_FD_WRITEABLE(fde) \
1410 TEVENT_FD_WRITEABLE(fde)
1412 #define EVENT_FD_READABLE(fde) \
1413 TEVENT_FD_READABLE(fde)
1415 #define EVENT_FD_NOT_WRITEABLE(fde) \
1416 TEVENT_FD_NOT_WRITEABLE(fde)
1418 #define EVENT_FD_NOT_READABLE(fde) \
1419 TEVENT_FD_NOT_READABLE(fde)
1421 #define ev_debug_level tevent_debug_level
1423 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
1424 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
1425 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
1426 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
1428 #define ev_set_debug(ev, debug, context) \
1429 tevent_set_debug(ev, debug, context)
1431 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1433 #endif /* TEVENT_COMPAT_DEFINES */
1437 #endif /* __TEVENT_H__ */