Commit Graph

57 Commits

Author SHA1 Message Date
Lennart Poettering b778cba4bf sd-event: optionally, if an event source fails, exit the event loop
Currently, if an event source callback returns an error, we'll disable
the event source and continue. This adds a per-event source flag that if
turned on goes further: the event loop is also exited, propagating the
error code.

This is inspired by some patterns repeatedly seen in #15206.

The idea is that event sources that server the "primary" function of a
program are marked like this, so that if they fail the failure is
instantly propagated and terminates the program.
2020-10-07 09:38:41 +02:00
Lennart Poettering d6a83dc48a sd-event: add relative timer calls
We frequently want to set a timer relative to the current time. Let's
add an explicit API for this. This not only saves us a few lines of code
everywhere and simplifies things, but also allows us to do correct
overflow checking.
2020-07-28 11:23:41 +02:00
Lennart Poettering f8f3f9263e sd-event: add pidfd support
This adds support for watching for process exits via Linux new pidfd
concept. This makes watching processes and killing them race-free if
properly used, fixing a long-standing UNIX misdesign.

This patch adds implicit and explicit pidfd support to sd-event: if a
process shall be watched and is specified by PID we will now internally
create a pidfd for it and use that, if available. Alternatively a new
constructor for child process event sources is added that takes pidfds
as input.

Besides mere watching of child processes via pidfd two additional
features are added:

→ sd_event_source_send_child_signal() allows sending a signal to the
  process being watched in the safest way possible (wrapping
  the new pidfd_send_signal() syscall).

→ sd_event_source_set_child_process_own() allows marking a process
  watched for destruction as soon as the event source is freed. This is
  currently implemented in userspace, but hopefully will become a kernel
  feature eventually.

Altogether this means an sd_event_source object is now a safe and stable
concept for referencing processes in race-free way, with automatic
fallback to pre-pidfd kernels.

Note that this patch adds support for this only to sd-event, not to PID
1. That's because PID 1 needs to use waitid(P_ALL) for reaping any
process that might get reparented to it. This currently semantically
conflicts with pidfd use for watching processes since we P_ALL is
undirected and thus might reap process earlier than the pidfd notifies
process end, which is hard to handle. The kernel will likely gain a
concept for excluding specific pidfds from P_ALL watching, as soon as
that is around we can start making use of this in PID 1 too.
2019-12-04 10:34:41 +01:00
Zbigniew Jędrzejewski-Szmek aa7585fd8e sd-event: add sd_event_source_disable_unrefp() too
I do not have any immediate use for it, but let's add it for completeness.
2019-05-10 16:55:37 +02:00
Zbigniew Jędrzejewski-Szmek afd15bbb4b sd-event: add sd_event_source_disable_unref() helper 2019-05-10 16:55:35 +02:00
emersion 772afb7616 libsystemd: check if _POSIX_C_SOURCE is defined
This can cause issues if sd-event.h is included in a source file that doesn't
define _POSIX_C_SOURCE and is compiled with -Wundef.
2019-02-22 10:09:48 +09:00
Yu Watanabe deae09ccc5 libsystemd: introduce _sd_destroy_t 2018-12-02 06:21:11 +01:00
Yu Watanabe 2382c9367b sd-event: add sd_event_source_{get,set}_floating() 2018-11-05 13:19:02 +09:00
Lennart Poettering bde0848d88 sd-event: slightly extend explanatory comment 2018-10-12 12:26:29 +02:00
Yu Watanabe 1e089bf805 tree-wide: drop empty lines in the begining of comments 2018-06-29 10:50:23 +09:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Lennart Poettering 15723a1db0 sd-event: add destroy callback logic to sd-event too
This adds what has been added to sd_bus_slot and sd_bus_track to
sd_event too.
2018-06-07 13:10:56 +02:00
Lennart Poettering 97ef539169 sd-event: add new API for subscribing to inotify events
This adds a new call sd_event_add_inotify() which allows watching for
inotify events on specified paths.

sd-event will try to minimize the number of inotify fds allocated, and
will try to add file watches to the same inotify fd objects as far as
that's possible. Doing this kind of inotify object should optimize
behaviour in programs that watch a limited set of mostly independent
files as in most cases a single inotify object will suffice for watching
all files.

Traditionally, this kind of coalescing logic (i.e. that multiple event
sources are implemented on top of a single inotify object) was very hard
to do, as the inotify API had serious limitations: it only allowed
adding watches by path, and would implicitly merge watches installed on
the same node via different path, without letting the caller know about
whether such merging took place or not.

With the advent of O_PATH this issue can be dealt with to some point:
instead of adding a path to watch to an inotify object with
inotify_add_watch() right away, we can open the path with O_PATH first,
call fstat() on the fd, and check the .st_dev/.st_ino fields of that
against a list of watches we already have in place. If we find one we
know that the inotify_add_watch() will update the watch mask of the
existing watch, otherwise it will create a new watch. To make this
race-free we use inotify_add_watch() on the /proc/self/fd/ path of the
O_PATH fd, instead of the original path, so that we do the checking and
watch updating with guaranteed the same inode.

This approach let's us deal safely with inodes that may appear under
various different paths (due to symlinks, hardlinks, bind mounts, fs
namespaces). However it's not a perfect solution: currently the kernel
has no API for changing the watch mask of an existing watch -- unless
you have a path or fd to the original inode. This means we can "merge"
the watches of the same inode of multiple event sources correctly, but
we cannot "unmerge" it again correctly in many cases, as access to the
original inode might have been lost, due to renames, mount/unmount, or
deletions. We could in theory always keep open an O_PATH fd of the inode
to watch so that we can change the mask anytime we want, but this is
highly problematics, as it would consume too many fds (and in fact the
scarcity of fds is the reason why watch descriptors are a separate
concepts from fds) and would keep the backing mounts busy (wds do not
keep mounts busy, fds do). The current implemented approach to all this:
filter in userspace and accept that the watch mask on some inode might
be higher than necessary due to earlier installed event sources that
might have ceased to exist. This approach while ugly shouldn't be too
bad for most cases as the same inodes are probably wacthed for the same
masks in most implementations.

In order to implement priorities correctly a seperate inotify object is
allocated for each priority that is used. This way we get separate
per-priority event queues, of which we never dequeue more than a few
events at a time.

Fixes: #3982
2018-06-06 10:53:56 +02:00
Nathaniel McCallum ab93297cd0 Add fd close support to sd_event_source
It is often the case that a file descriptor and its corresponding IO
sd_event_source share a life span. When this is the case, developers will
have to unref the event source and close the file descriptor. Instead, we
can just have the event source take ownership of the file descriptor and
close it when the event source is freed. This is especially useful when
combined with cleanup attributes and sd_event_source_unrefp().

This patch adds two new public functions:

    sd_event_source_get_io_fd_own()
    sd_event_source_set_io_fd_own()
2018-01-24 17:57:27 +01:00
Nathaniel McCallum 2b97a9573f Include time.h in sd-event.h
The time-related functions in sd-event.h take as inputs constants (CLOCK_*)
defined in time.h. By including time.h in sd-event.h, we free the developer
from having to do this manually.
2018-01-24 16:34:57 +01:00
Nathaniel McCallum b937d76108 Add support for SD_EVENT_DEFAULT
Currently, sd-event supports the ability to have a thread-local default
event loop. However, this is less useful than it can be since all functions
which require an sd_event* as input require the caller to pass it. This
patch adds a new macro which allows the developer to pass a constant
SD_EVENT_DEFAULT instead. This reduces work for the caller.

For example:

    r = sd_event_default(&e);
    r = sd_event_add_io(e, ...);
    sd_event_unref(e);

Becomes:

    r = sd_event_add_io(SD_EVENT_DEFAULT, ...);

If no thread-local default event loop exists, the function calls will
return -ENOPKG. No event loop will ever be implicitly created.
2018-01-23 09:40:25 -05:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Matija Skala 2797366cd2 check for _POSIX_C_SOURCE instead of __USE_POSIX*
check for _GNU_SOURCE as well as sd_event_child_handler_t needs to be
defined correctly while compiling systemd
2017-03-16 03:29:53 +01:00
Lennart Poettering 60a3b1e11a sd-event: expose the event loop iteration counter via sd_event_get_iteration() (#3631)
This extends the existing event loop iteration counter to 64bit, and exposes it
via a new function sd_event_get_iteration(). This is helpful for cases like
issue #3612. After all, since we maintain the counter anyway, we might as well
expose it.

(This also fixes an unrelated issue in the man page for sd_event_wait() where
micro and milliseconds got mixed up)
2016-06-30 21:25:07 +02:00
Zbigniew Jędrzejewski-Szmek e0c0b07da1 headers: remove commas at end of enum lists
src/systemd/sd-journal.h:75:51: warning: commas at the end of enumerator
      lists are a C99-specific feature [-Wc99-extensions]
2016-03-11 13:50:56 -05:00
Zbigniew Jędrzejewski-Szmek b2542bf9ab headers: do not use siginfo_t if not defined
Simply avoid the trouble and use a void* if the define
is missing. We lose type safety, but who cares.
sigaction(2) says that siginfo_t requires _POSIX_C_SOURCE >= 199309L,
but we can be a bit more generous and use the same define
as /usr/include/signal.h.
2016-03-11 13:46:12 -05:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Lennart Poettering 4afd3348c7 tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.

With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.

The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).

This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.

Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:

       #define _cleanup_(function) __attribute__((cleanup(function)))

Or similar, to make the gcc feature easier to use.

Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.

See #2008.
2015-11-27 19:19:36 +01:00
Lennart Poettering 0be6c2f617 sd-event: define a new PREPARING state
We already have a state RUNNING and EXITING when we dispatch regular and
exit callbacks. Let's introduce a new state called PREPARING that is
active while we invoke preparation callbacks. This way we have a state
each for all three kinds of event handlers.

The states are currently not documented, hence let's add a new state to
the end, before we start documenting this.
2015-11-19 23:36:03 +01:00
Thomas Hindoe Paaboel Andersen 71d35b6b55 tree-wide: sort includes in *.h
This is a continuation of the previous include sort patch, which
only sorted for .c files.
2015-11-18 23:09:02 +01:00
Tom Gundersen 2b0c9ef735 sd-event: rename PASSIVE/PREPARED to INITIAL/ARMED 2015-03-14 12:17:24 +01:00
Lennart Poettering 356779df90 sd-event: rename sd_event_source_set_name() to sd_event_source_get_name()
To mirror the recent name change of the concept for sd_bus objects,
follow the same logic for sd_event_source objects, too.
2014-11-04 18:09:19 +01:00
Tom Gundersen f7f53e9e6e sd-event: allow naming event sources 2014-08-28 21:19:17 +02:00
Tom Gundersen c45a5a7446 sd-event: split run into prepare/wait/dispatch
This will allow sd-event to be integrated into an external event loop, which
in turn will allow (say) glib-based applications to use our various libraries,
without manually integrating each of them (bus, rtnl, dhcp, ...).

The external event-loop should integrate sd-event int he following way:

Every iteration must start with a call to sd_event_prepare(), which will
return 0 if no event sources are ready to be processed, a positive value if
they are and a negative value on error. sd_event_prepare() may only be called
following sd_event_dispatch(); a call to sd_event_wait() indicating that no
sources are ready to be dispatched; or a failed call to sd_event_dispatch() or
sd_event_wait().

A successful call to sd_event_prepare() indicating that no event sources are
ready to be dispatched must be followed by a call to sd_event_wait(),
which will return 0 if it timed out without event sources being ready to
be processed, a negative value on error and a positive value otherwise.
sd_event_wait() may only be called following a successful call to
sd_event_prepare() indicating that no event sources are ready to be dispatched.

If sd_event_wait() indicates that some events sources are ready to be
dispatched, it must be followed by a call to sd_event_dispatch(). This
is the only time sd_event_dispatch() may be called.
2014-08-25 21:52:36 +02:00
Tom Gundersen 9b36454543 sd-event: add API to access epoll_fd
This is a prerequisite for integrating sd-event into an external
event loop.
2014-08-20 17:24:11 +02:00
Lennart Poettering 9a78148e40 sd-event: reorder header slightly 2014-05-15 00:44:29 +02:00
Lennart Poettering b408026b98 man: document sd_event_add_time(3) 2014-05-06 18:51:08 +02:00
Jason St. John 10b17992ee src/systemd: fix grammar and spelling errors in comments 2014-03-25 20:23:33 +01:00
Lennart Poettering 6a0f1f6d5a sd-event: rework API to support CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM, too 2014-03-24 02:58:41 +01:00
Lennart Poettering 6e9feda30d event: add new kind of event source called "post"
This new event source is triggered by the dispatching of any non-post
event source. It can thus be used to do clean-up or recheck work,
triggered by any other event source having been executed.

This is different from "defer" event source which are unconditionally
triggered as long as they are enabled. A "defer" event source that does
nothing will result in the event loop busy looping unless it is turned
off eventually. This is different for "post" event sources that will be
only triggered when some other non-post ran, and will thus not keep the
event loop busy on its own.
2014-02-21 21:13:53 +01:00
Lennart Poettering 151b9b9662 api: in constructor function calls, always put the returned object pointer first (or second)
Previously the returned object of constructor functions where sometimes
returned as last, sometimes as first and sometimes as second parameter.
Let's clean this up a bit. Here are the new rules:

1. The object the new object is derived from is put first, if there is any

2. The object we are creating will be returned in the next arguments

3. This is followed by any additional arguments

Rationale:

For functions that operate on an object we always put that object first.
Constructors should probably not be too different in this regard. Also,
if the additional parameters might want to use varargs which suggests to
put them last.

Note that this new scheme only applies to constructor functions, not to
all other functions. We do give a lot of freedom for those.

Note that this commit only changes the order of the new functions we
added, for old ones we accept the wrong order and leave it like that.
2014-02-20 00:03:10 +01:00
Lennart Poettering 31927c16de event: extend priority raneg to 64bit
The kdbus prioq logic will use 64bit prios too, so let's keep this
similar in style. Using 64bit here has the advantage, that pointers and
pretty much anything else can be mapped naturally to priorities if so
desired.
2014-01-15 20:16:20 +01:00
Lennart Poettering 30caf8f3af event: add ability to change fd of an active event source 2013-12-13 15:30:12 +01:00
Lennart Poettering 8f726607ec event: allow to query userdata and watchdog state 2013-12-13 15:30:12 +01:00
Lennart Poettering 6203e07a83 event: rework sd-event exit logic
With this change a failing event source handler will not cause the
entire event loop to fail. Instead, we just disable the specific event
source, log a message at debug level and go on.

This also introduces a new concept of "exit code" which can be stored in
the event loop and is returned by sd_event_loop(). We also rename "quit"
to "exit" everywhere else.

Altogether this should make things more robus and keep errors local
while still providing a way to return event loop errors in a clear way.
2013-12-13 04:06:43 +01:00
Lennart Poettering cde93897cd event: hook up sd-event with the service watchdog logic
Adds a new call sd_event_set_watchdog() that can be used to hook up the
event loop with the watchdog supervision logic of systemd. If enabled
and $WATCHDOG_USEC is set the event loop will ping the invoking systemd
daemon right after coming back from epoll_wait() but not more often than
$WATCHDOG_USEC/4. The epoll_wait() will sleep no longer than
$WATCHDOG_USEC/4*3, to make sure the service manager is called in time.

This means that setting WatchdogSec= in a .service file and calling
sd_event_set_watchdog() in your daemon is enough to hook it up with the
watchdog logic.
2013-12-11 18:20:09 +01:00
Lennart Poettering adcc4ca30d event: rename sd_event_get() to sd_event_source_get_event() 2013-11-22 01:42:15 +01:00
Lennart Poettering 718db96199 core: convert PID 1 to libsystemd-bus
This patch converts PID 1 to libsystemd-bus and thus drops the
dependency on libdbus. The only remaining code using libdbus is a test
case that validates our bus marshalling against libdbus' marshalling,
and this dependency can be turned off.

This patch also adds a couple of things to libsystem-bus, that are
necessary to make the port work:

- Synthesizing of "Disconnected" messages when bus connections are
  severed.

- Support for attaching multiple vtables for the same interface on the
  same path.

This patch also fixes the SetDefaultTarget() and GetDefaultTarget() bus
calls which used an inappropriate signature.

As a side effect we will now generate PropertiesChanged messages which
carry property contents, rather than just invalidation information.
2013-11-20 20:52:36 +01:00
Lennart Poettering afc6adb5ec bus: introduce concept of a "default" event loop per-thread and make use of it everywhere
Try to emphasize a bit that there should be a mapping between event
loops and threads, hence introduce a logic that there's one "default"
event loop for each thread, that can be queried via
"sd_event_default()".
2013-11-12 00:12:43 +01:00
Lennart Poettering 0095c45415 api: replace manual C++ guards by macros 2013-11-07 16:53:26 +01:00
Lennart Poettering 6695ed7a84 api: add C++ guards 2013-11-07 03:47:42 +01:00
Lennart Poettering 6ee69bab11 event: add some well-defined priority values of event sources 2013-11-05 01:13:05 +01:00
Lennart Poettering 40ca29a137 timedated: use libsystemd-bus instead of libdbus for bus communication
Among other things this also adds a few things necessary for the change:

- Considerably more powerful error returning APIs in libsystemd-bus

- Adapter for connecting an sd_bus to an sd_event

- As I reworked the PolicyKit logic to the new library I also made it
  asynchronous, so that PolicyKit requests of one user cannot block out
  another user anymore.

- We always use the macro names for common bus error. That way it is
  harder to mistype them since the compiler will notice
2013-10-16 06:15:02 +02:00
Lennart Poettering 46e8c8257f event: add apis to query the timestamp of the event currently processed 2013-10-11 04:36:02 +02:00