1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00
Commit Graph

74 Commits

Author SHA1 Message Date
Stefan Metzmacher
407cda2f3b tevent: add support for TEVENT_FD_ERROR
After 12 years we finally got TEVENT_FD_ERROR support :-)

TEVENT_FD_WRITE event handlers never get errors reported
instead the event handler is silently disabled.
There are likely callers relying on that behavior, so
we are not able to chance it.

Now TEVENT_FD_WRITE can be used together with TEVENT_FD_ERROR
in order to get errors reported without waiting for TEVENT_FD_READ.

TEVENT_FD_ERROR can also be used alone in order to detect errors
on sockets in order to cleanup resources.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-10-13 09:49:33 +00:00
Stefan Metzmacher
28bf51fc65 tevent: let tevent_epoll.c use new generic mpx infrastructure
This allows any number of event handlers per low level fd.

It means the epoll backend behaves like the poll backend now.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-10-13 09:49:33 +00:00
Stefan Metzmacher
b328e99065 tevent: add tevent_common_fd_mpx infrastructure
Backends may require to map individual tevent_fd instances to
a single low level kernel state (e.g. for epoll).

This generic infrastructure adds helper functions using
a generic (sub)part of struct tevent_fd.

The new code will allow us to support more than 2 tevent_fd
instances per fd, which makes sure all backends can provide
a similar behavior. This will be important when we add
TEVENT_FD_ERROR as a 3rd kind of fd event.

The aim is to use this in order to replace the limited implementation
we already have in tevent_epoll.c.

As these helpers are typically called from within
'void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags)'
there's no way to report errors. So in order avoid additional
error handling complexity the helpers try to avoid
any allocations which may fail. It also means the logic in
tevent_epoll.c doesn't have to change much.

These are implemented as static line functions in order to avoid
the function call overhead, which showed up in profiles of the
early implementation.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-10-13 09:49:33 +00:00
Stefan Metzmacher
95d6600a06 tevent: split out a tevent_common_fd_disarm() helper
It means tevent_trace_fd_callback(TEVENT_EVENT_TRACE_DETACH)
is always called and similar future changes are only
needed in one place.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-10-13 09:49:33 +00:00
Stefan Metzmacher
2645be60d7 tevent: avoid calling epoll_update_event() again if epoll_check_reopen() already did it
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-07-19 08:02:33 +00:00
Stefan Metzmacher
e9d9809734 tevent: let epoll_check_reopen() clear all events before reopening them
This is clearer for multiplexed fdes as it means both sides are
already cleared before we call epoll_update_event() again.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-07-19 08:02:33 +00:00
Stefan Metzmacher
3217d5dc1d tevent: avoid epoll_check_reopen() overhead unless required
The preparation, function call and cleanup for epoll_check_reopen()
is quite some overhead and not needed most of the time!

So check the pid in the caller avoids most of it.

Review with: git show -w

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-07-19 08:02:33 +00:00
Stefan Metzmacher
d94b9c8124 tevent: make use of TEVENT_DEBUG() when using TEVENT_DEBUG_TRACE
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2023-07-19 08:02:33 +00:00
Dmitry Antipov
a665d44f22 tevent: rely on epoll_create1() for epoll interface
Prefer epoll_create1(2) over epoll_create(2) and
always require the former to use epoll(7) interface,
thus saving extra fcntl(2) call to set FD_CLOEXEC.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2023-07-19 08:02:33 +00:00
Stefan Metzmacher
087b1b0efd tevent: add tevent_cached_getpid() helper
This avoids a getpid() syscall per tevent_loop_once() iteration.

We provide tevent_cached_getpid() also as helper for external consumers
in order to have the logic only once.

Note the change to ABI/tevent-0.12.1.sigs will be reverted
with the bump to 0.13.0.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-07-25 17:34:33 +00:00
Stefan Metzmacher
ac9569b1a6 tevent: add tevent_context_wrapper_create() infrastructure
This allows to specify wrapper tevent_contexts, which adds the ability
to run functions before and after the event handler functions.

This can be used to implement impersonation hooks
or advanced debugging/profiling hooks.

We'll undo the 0.9.36 ABI change on the 0.9.37 release
at the end of this patchset.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2018-07-11 23:04:21 +02:00
Stefan Metzmacher
072e3b28ce tevent: split out tevent_common_invoke_fd_handler()
We'll undo the 0.9.36 ABI change on the 0.9.37 release
at the end of this patchset.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2018-07-11 23:04:21 +02:00
Volker Lendecke
f6aaece578 tevent: Add threaded immediate activation
This is infrastructure to improve our async r/w result handling and latency.
The pthreadpool signalling goes through a pipe. This has downsides: The main
event loop has to go through a read on the pipe before it can ship the result.
Also, it is not guaranteed by poll/epoll that the pthreadpool signal pipe is
handled with top priority. When an async pread/pwrite has finished, we should
immediately ship the result to the client, not waiting for anything else.

This patch enables tevent_immediate structs as job signalling. This means a
busy main tevent loop will handle the threaded job completion before any timed
or file descriptor events. Opposite to Jeremy's tevent_thread_proxy this is
done by a modification of the main event loop by looking at a linked list under
a central mutex.

Regarding performance: In a later commit I've created a test that does nothing
but fire one immediate over and over again. If you add a phread_mutex_lock and
unlock pair in the immediate handler, you lose roughly 25% of rounds per
second, so it is measurable. It is questionable that will be measurable in the
real world, but to counter concerns activation of immediates needs to go
through a new struct tevent_threaded_context. Only if such a
tevent_threaded_context exists for a tevent context, the main loop takes the
hit to look at the mutex'ed list of finished jobs.

This patch by design does not care about talloc hierarchies. The idea is that
the main thread owning the tevent context creates a chunk of memory and
prepares the tevent_immediate indication job completion. The main thread hands
the memory chunk together with the immediate as a job description over to a
helper thread. The helper thread does its job and upon completion calls
tevent_threaded_schedule_immediate with the already-prepared immediate. From
that point on memory ownership is again transferred to the main thread.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2016-08-24 01:33:48 +02:00
Jeremy Allison
39d0a81ed8 lib: tevent: Whitespace cleanup.
Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>

Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Nov 18 15:54:03 CET 2015 on sn-devel-104
2015-11-18 15:54:03 +01:00
Simo Sorce
ec499c3d66 Do not return a value from a void function.
Reported by herwin <samba@herwinw.nl>
Resolves: https://bugzilla.samba.org/show_bug.cgi?id=10346

Signed-off-by: Simo Sorce <idra@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-12-26 08:49:00 -08:00
Stefan Metzmacher
36345d44c8 tevent: tevent_epoll_set_panic_fallback() can be a void function
There's no case where this could return an error.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
2013-12-11 22:46:09 +01:00
Stefan Metzmacher
4cc02d7c84 tevent: make use of talloc_get_type_abort() in tevent_epoll.c
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
2013-12-11 22:46:09 +01:00
Stefan Metzmacher
35385a3e28 tevent: optimize adding new zero timer events
Such events were used before we had immediate events.
It's likely that there're a lot of this events
and we need to add new ones in fifo order.

The tricky part is that tevent_common_add_timer()
should not use the optimization as it's used
by broken Samba versions, which don't use
tevent_common_loop_timer_delay() in source3/lib/events.c.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:54 -08:00
Stefan Metzmacher
23640472bc tevent: use DEBUG_ERROR for the fallback message in epoll_panic()
A Samba autobuild passed without a fallback, so this is
really an error.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:26 -08:00
Jeremy Allison
4fcc94d852 tevent: If epoll_ctl(..EPOLL_CTL_ADD,..) failes with EEXIST, merge the two fde's into one epoll event.
Signed-off-by: Jeremy Allison <jra@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:01:24 -08:00
Stefan Metzmacher
16a072ec1c tevent: preferr the write handler if there're two possible handlers registered with epoll
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:21 -08:00
Stefan Metzmacher
fa4f1713f9 tevent: In epoll_event_loop() ensure we trigger the right handler for a multiplexed fde event.
Signed-off-by: Jeremy Allison <jra@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:01:19 -08:00
Jeremy Allison
4057ef2e11 tevent: Add utility function epoll_handle_hup_or_err()
We'll use this to handle the EPOLL_ADDITIONAL_FD_FLAG_GOT_ERROR
and EPOLL_ADDITIONAL_FD_FLAG_REPORT_ERROR flags with multiplexed
events in the event loop.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:01:17 -08:00
Stefan Metzmacher
8b1a43ece8 tevent: handle EPOLL_ADDITIONAL_FD_FLAG_HAS_MPX in epoll_update_event()
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:13 -08:00
Stefan Metzmacher
a347d55f50 tevent: handle multiplexed fde's in epoll_event_fd_destructor()
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:09 -08:00
Stefan Metzmacher
6703c5b49f tevent: Fix epoll_mod_event() to cope with modifying a multiplexed fde event.
Signed-off-by: Jeremy Allison <jra@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:01:07 -08:00
Jeremy Allison
15ca40fb7a tevent: Fix up epoll_del_event to cope with deleting a multiplexed fde event.
Signed-off-by: Jeremy Allison <jra@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:01:05 -08:00
Stefan Metzmacher
809593e7f5 tevent: handle multiplexed fde's in epoll_add_event()
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:01:02 -08:00
Jeremy Allison
e4b496cc09 tevent: Start to fix the epoll backend to support 2 fd events on the same fd correctly.
Add a utility function epoll_add_multiplex_fd() and
a new flag EPOLL_ADDITIONAL_FD_FLAG_HAS_MPX.

This will be called by epoll_add_event() to merge two
fde events with the same file descriptor.

Signed-off-by: Jeremy Allison <jra@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 12:00:59 -08:00
Stefan Metzmacher
7ef688c1dd tevent: ignore EBADF from epoll_ctl() and disable the event
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:56 -08:00
Stefan Metzmacher
2721f0dc5b tevent: unify handling of HAS_EVENT and REPORT_ERROR in epoll_{add,mod,del}_event()
epoll_{add,mod,del}_event() are only called via epoll_update_event()
and epoll_update_event() should not remove REPORT_ERROR itself.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:52 -08:00
Stefan Metzmacher
ec9615c4fc tevent: always go through epoll_update_event()
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:49 -08:00
Stefan Metzmacher
07e0427115 tevent: s/epoll_change_event/epoll_update_event
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:45 -08:00
Stefan Metzmacher
056dcc7a8a tevent: the content of event is ignored by EPOLL_CTL_DEL
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:41 -08:00
Stefan Metzmacher
2b3b06b9b4 tevent: call epoll_panic() if EPOLL_CTL_DEL failed
Only ENOENT is ignored as it is no error.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:37 -08:00
Stefan Metzmacher
31db8a08be tevent: use helper variables to hold the epoll_ctl() result
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:32 -08:00
Stefan Metzmacher
b2114eb3b0 tevent: remove unused if (epoll_ev->epoll_fd == -1) return; checks
We'll never leave epoll_check_reopen() with epoll_fd == -1.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:29 -08:00
Stefan Metzmacher
73dabddbdc tevent: avoid any operation on epoll_ev after a epoll_panic()
This calls TALLOC_FREE(ev->additional_data), which is epoll_ev
within epoll_panic() before calling the fallback handler.

In order to notice that a epoll_panic() happened, a caller can
register a pointer to a bool variable under epoll_ev->panic_state.

As epoll_check_reopen() can fail due to a epoll_panic(),
we need to force the replay flag if we have called any event handler.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:15 -08:00
Stefan Metzmacher
2b05c96217 tevent: add epoll_{create,ctl}_panic_fallback() for testing
This makes sure we only do random panics if a fallback handler
is registered.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 12:00:11 -08:00
Stefan Metzmacher
f86df3e364 tevent: remember the errno from select(), poll() and epoll_wait()
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2013-03-01 11:59:20 -08:00
Michael Adam
5c31b152d2 tevent: fix a comment typo in tevent_epoll.c
Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-03-01 11:59:05 -08:00
Jeremy Allison
1ee428d5ca tevent: Add in the same tevent_re_initialise() fix Metze put in the tevent_poll backend.
We might be called during tevent_re_initialise()
which means we need to free our old additional_data.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Jeremy Allison
06fb88b449 tevent: Add in some test code to allow the panic fallback path to be tested.
Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Jeremy Allison
6f9819229b tevent: Plumb in the panic fallback code into the epoll_panic() runtime call.
Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Jeremy Allison
04ba47e491 tevent: Add an internal function tevent_epoll_set_panic_fallback().
Can be set externally, allows us to fallback if epoll
fails at runtime.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Stefan Metzmacher
e4ef2ecf02 tevent: pass 'bool replay' to epoll_panic()
A fallback panic handler will need to know if
there was an error while waiting for events
(replay=true) or if the error happened on modify
(replay=false).

Signed-off-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Jeremy Allison
129da06d2d tevent: Ensure we return after every call to epoll_panic().
Currently we can't return from this, but the new fallback
code will change this.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
2013-02-14 10:19:38 -08:00
Martin Schwenke
796acbd9ff lib/tevent: Add trace point callback
Set/get a single callback function to be invoked at various trace
points.  Define "before wait" and "after wait" trace points - more
trace points can be added later if required.

CTDB wants this to log long waits and events.

Pair-programmed-with: Amitay Isaacs <amitay@gmail.com>
Signed-off-by: Martin Schwenke <martin@meltin.net>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
2012-06-08 19:00:05 +02:00
Sumit Bose
158b208dfd tevent: Set FD_CLOEXEC on epoll handle
If an application using libtevent starts a new process the epoll file descriptor
is leaked to the new process if the event context is not freed explicitly. By
setting FD_CLOEXEC this is not needed anymore.

Signed-off-by: Simo Sorce <idra@samba.org>
2011-08-11 14:38:53 -04:00
Volker Lendecke
7720facd42 tevent: Fix a comment
Autobuild-User: Volker Lendecke <vlendec@samba.org>
Autobuild-Date: Thu Feb 17 18:16:18 CET 2011 on sn-devel-104
2011-02-17 18:16:18 +01:00