Commit graph

35 commits

Author SHA1 Message Date
Lennart Poettering 12179984a3 event: when unreffing an event source from its own handler, detach fd from epoll
The pattern of unreffing an IO event source and then closing its fd is
frequently seen in even source callbacks. Previously this likely
resultet in us removing the fd from the epoll after it was closed which
is problematic, since while we were dispatching we always kept an extra
reference to event source objects because we might still need it later.
2013-12-13 04:06:43 +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 ba276c8153 sd-event: try to move each wakeup to the same spot within every 10s
In addition to the same spot within every 1min, every 1s, every 250s
2013-12-11 23:41:21 +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 08cd155254 event: when handling SIGCHLD of a child process only reap after dispatching event source
That way the even source callback is run with the zombie process still
around so that it can access /proc/$PID/ and similar, and so that it can
be sure that the PID has not been reused yet.
2013-12-11 18:20:09 +01:00
Lennart Poettering 5b1bc83f81 bus: when we _unref() a NULL pointer, don't use assert_return()
We support unreffing NULL pointers just fine and we shouldn't pay the
_unlikely_() price for it, not get a debug message if we do, hence let's
not use assert_return() here.
2013-12-10 20:38:04 +00:00
David Herrmann 2a16a986ce event: allow EPOLLET as event flag
EPOLLET enables edge-triggered mode (see epoll(7) for more). For most
use-cases, level-triggered is just fine, but for master-TTYs we need
edge-triggered to catch EPOLLHUP. master-TTYs signal EPOLLHUP if no client
is connected, but a client may connect some time later (same happens
during vhangup(2)).

However, epoll doesn't allow masking EPOLLHUP so it's signaled constantly.
To avoid this, edge-triggered mode is needed.
2013-11-28 15:16:49 +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 850516e012 sd-event: try to move timer wakeups to the same spot within each minute 2013-11-21 00:46:13 +01:00
Lennart Poettering 1b5995b039 event: make sure to possibly disarm the timerfds before we reenter epoll_wait 2013-11-20 20:58:17 +01:00
Lennart Poettering c57b5ca301 event: don't disarm invalid timerfd 2013-11-20 20:58:17 +01:00
Lennart Poettering 2576a19ed2 event: whenever a time source changes pending state reshuffle elapsation prioqs 2013-11-20 20:58:17 +01:00
Lennart Poettering 503f39c35e event: when we change the io events to watch we need to figure out if a an event is pending again 2013-11-20 20:58:17 +01:00
Lennart Poettering a99badf5ed sd-event: fix comparison functions 2013-11-20 20:58:17 +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
David Herrmann 0cc1125ae1 event: clear pending-state when re-arming timers
If a timer fires and is marked pending, but an application re-arms it
before it is dispatched, we now clear the pending state.

This fixes a bug where an application arms a timer, which fires and is
marked pending. But before it is dispatched, the application loses
interest in it and disables it. Now if the timer is re-armed and
re-enabled later, it will be immediately dispatched as it is still marked
pending.

This behavior is unexpected, so avoid it by clearing pending state when
re-arming timers. Note that applications have no way to clear pending
state themselves, so there's no current workaround.
2013-11-20 16:19:58 +01:00
Lennart Poettering 76b543756e bus: introduce concept of a default bus for each thread and make use of it everywhere
We want to emphasize bus connections as per-thread communication
primitives, hence introduce a concept of a per-thread default bus, and
make use of it everywhere.
2013-11-12 00:12:43 +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 b7484e2a58 event: make sure we keep a reference to all events we dispatch while we do so. 2013-11-07 00:13:58 +01:00
Zbigniew Jędrzejewski-Szmek f7262a9f28 bus: add public libsystemd-event 2013-11-06 14:56:47 -05:00
David Strauss 9ec9694ce3 sd-event: EPOLLONESHOT only disables event reporting after an event. The fd is still registered. 2013-10-21 17:23:01 -07:00
Dave Reisner fe8245eb3c sd-event: initialize return value
src/libsystemd-bus/sd-event.c:1597:13: warning: 'r' may be used
uninitialized in this function [-Wmaybe-uninitialized]
2013-10-17 16:19:48 -04:00
Lennart Poettering d09d5ecda2 event: handle arbitrary signals received during epoll gracefully 2013-10-16 06:15:00 +02:00
Lennart Poettering 72aedc1e80 event: properly disarm timers when we don't need them anymore 2013-10-16 06:15:00 +02:00
Lennart Poettering e471512768 event: fix sd_event_source_set_io_events() 2013-10-16 06:15:00 +02:00
Zbigniew Jędrzejewski-Szmek 0eb2e0e340 event: avoid derefencing null pointer 2013-10-13 17:56:55 -04:00
Lennart Poettering 46e8c8257f event: add apis to query the timestamp of the event currently processed 2013-10-11 04:36:02 +02:00
Lennart Poettering 305f78bff3 event: move all library calls over to new assert_return() macro 2013-10-11 02:11:30 +02:00
Lennart Poettering 4bee801296 event: add sd_event_source_get_child_pid() call to query the PID of a child event source 2013-10-11 01:33:25 +02:00
Lennart Poettering baf7628332 event: rename the "mute" field of event sources to "enabled"
In addition, the states "UNMUTED" and "MUTED" become "ON" and "OFF".

This has the benefit that a possible value of this field is not
identical to its name, thus minimizing confusion.
2013-10-11 01:08:15 +02:00
Lennart Poettering da7e457c5b event: implement quit handlers
Quit handlers are executed when an event loop is terminated via
sd_event_request_quit(). They are in a way atexit() handlers that are
executed in a well-defined environment, time and thread: from the event
loop thread when the event loop finishes.
2013-10-11 00:49:11 +02:00
Lennart Poettering eaa3cbef3b event: refuse operation if the caller tries to reuse an event loop after a fork() 2013-10-10 21:37:50 +02:00
Lennart Poettering c2ba3ad660 event: add timer accuracy/coalescing logic
In order to improve energy consumption we should minimize our wake-ups
when handling timers.  Hence, for each timer take an accuracy value and
schedule the actual wake-up time somewhere between the specified time
and the specified timer plus the accuracy.

The specified time of timer event sources hence becomes the time the
handler is called the *earliest*, and the specified time plus the accuracy
the time by which it is called the *latest*, leaving the library the
freedom to schedule the wake-up somewhere inbetween.

If the accuracy is specified as 0 the default of 250ms will be used.

When scheduling timeouts we will now try to elapse them at the same
point within each second, across the entire system. We do this by using
a fixed perturbation value keyed off the boot id. If this point within a
second is not in the acceptable range, we try again with a fixed time
within each 250ms time step. If that doesn't work either, we wake up at
the last possible time.
2013-10-10 21:22:59 +02:00
Tero Roponen dfcd88f634 bus: fix duplicate comparisons
Testing for y > x is the same as testing for x < y.
2013-10-10 18:37:48 +02:00
Lennart Poettering fd38203a2a bus: add minimal event loop API
So far we tried to use epoll directly wherever we needed an event loop.
However, that has various shortcomings, such as the inability to handle
larger amounts of timers (since each timerfd costs one fd, which is a
very limited resource, usually bounded to 1024), and inability to do
priorisation between multiple queued events.

Let's add a minimal event loop API around epoll that is suitable for
implementation of our own daemons and maybe one day can become public
API for those who desire it.

This loop is part of libsystemd-bus, but may be used independently of
it.
2013-10-10 04:44:48 +02:00