Commit graph

169 commits

Author SHA1 Message Date
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Lennart Poettering 50e0d56cf3 sd-bus: fix error handling of pthread API calls
pthread APIs (unlike the rest of libc) return their errors as positive
error codes directly from the functions, rather than using errno. Let's
make sure we always handle things that way.
2015-10-17 16:48:21 +02:00
David Herrmann 6fe869c251 sd-event: don't provide priority stability
Currently, we guarantee that if two event-sources with the same priority
fire at the same time, they're always dispatched in the same order. While
this might sound nice in theory, there's is little benefit in providing
stability on that level. We have no control over the order the events are
reported, hence, we cannot guarantee that we get notified about both at
the same time.

By dropping the stability guarantee, we loose roughly 10% Heap swaps in
the prioq on a desktop cold-boot. Krzysztof Kotlenga even reported up to
20% on his tests. This sounds worth optimizing, so drop the stability
guarantee.
2015-09-29 20:56:17 +02:00
Krzysztof Kotlenga 8046c4576a sd-event: fix prepare priority queue comparison function
Otherwise a disabled event source can get swapped with an enabled one
and cause a severe sd-event malfunction.

http://lists.freedesktop.org/archives/systemd-devel/2015-September/034356.html
2015-09-24 00:48:30 +02:00
Lennart Poettering 10edebf6cd sd-event: make sure to create a signal queue for the right signal
We should never access the "signal" part of the event source unless the
event source is actually for a signal. In this case it's a child pid
handler however, hence make sure to use the right signal.

This is a fix for PR #1177, which in turn was a fix for
9da4cb2be2.
2015-09-07 00:31:24 +02:00
Thomas Hindoe Paaboel Andersen b8a50a99a6 sd-event: fix call to event_make_signal_data
This looks like a typo from commit 9da4cb2b where it was added.
2015-09-06 22:19:09 +02:00
Lennart Poettering 9da4cb2be2 sd-event: make sure RT signals are not dropped
RT signals operate in a queue, and we should be careful to never merge
two queued signals into one. Hence, makes sure we only ever dequeue a
single signal at a time and leave the remaining ones queued in the
signalfd. In order to implement correct priorities for the signals
introduce one signalfd per priority, so that we only process the highest
priority signal at a time.
2015-09-04 09:07:31 +02:00
Lennart Poettering 556c25cf8c sd-event: improve debug message when we fail to remove and fd from an epoll
Let's help users to debug issues with epoll fd removal by printing the
name of the event source.
2015-08-31 13:20:44 +02:00
Lennart Poettering 8ac43fee1a tree-wide: generate EBADF when we get invalid fds
This is a follow-up to #907, and makes the same change for all our other
public APIs.
2015-08-14 18:23:16 +02:00
Lennart Poettering 38a03f06a7 sd-event: make sure sd_event_now() cannot fail
Previously, if the event loop never ran before sd_event_now() would
fail. With this change it will instead fall back to invoking now(). This
way, the function cannot fail anymore, except for programming error when
invoking it with wrong parameters.

This takes into account the fact that many callers did not handle the
error condition correctly, and if the callers did, then they kept simply
invoking now() as fall back on their own. Hence let's shorten the code
using this call, and make things more robust, and let's just fall back
to now() internally.

Whether now() is used or the cache timestamp may still be detected via
the return value of sd_event_now(). If > 0 is returned, then the fall
back to now() was used, if == 0 is returned, then the cached value was
returned.

This patch also simplifies many of the invocations of sd_event_now():
the manual fall back to now() can be removed. Also, in cases where the
call is invoked withing void functions we can now protect the invocation
via assert_se(), acknowledging the fact that the call cannot fail
anymore except for programming errors with the parameters.

This change is inspired by #841.
2015-08-03 17:34:49 +02:00
David Herrmann 366e641139 sd-event: make errors on EPOLL_CTL_DEL pseudo-fatal
If we call EPOLL_CTL_DEL, we *REALLY* expect the file-descriptor to be
present in that given epoll-set. We actually track such state via our
s->io.registered flag, so it better be true.

Make sure if that's not true, we treat it similar to assert_return() (ie.,
print a loud warning).
2015-06-17 09:13:48 +02:00
Lennart Poettering 72c0a2c255 everywhere: port everything to sigprocmask_many() and friends
This ports a lot of manual code over to sigprocmask_many() and friends.

Also, we now consistly check for sigprocmask() failures with
assert_se(), since the call cannot realistically fail unless there's a
programming error.

Also encloses a few sd_event_add_signal() calls with (void) when we
ignore the return values for it knowingly.
2015-06-15 20:13:23 +02:00
Lennart Poettering cde40acc80 logind,sd-event: drop spurious new-lines 2015-06-10 01:28:58 +02:00
Tom Gundersen f68067348f sd-event: don't touch fd's accross forks
We protect most of the API from use accross forks, but we still allow both
sd_event and sd_event_source objects to be unref'ed. This would cause
problems as it would unregister sources from the underlying eventfd, hence
also affecting the original instance in the parent process.

This fixes the issue by not touching the fds on unref when done accross a fork,
but still free the memory.

This fixes a regression introduced by
        "udevd: move main-loop to sd-event": 693d371d30

where the worker processes were disabling the inotify event source in the
main daemon.
2015-06-04 19:04:26 +02:00
Lennart Poettering 24882e06c1 util: split out signal-util.[ch] from util.[ch]
No functional changes.
2015-05-29 20:14:11 +02:00
Lennart Poettering 53bac4e0e3 sd-event: simplify sd_event_run() 2015-04-10 18:45:39 +02:00
Tom Gundersen 02d30981b1 sd-event: sd_event_run - only return 0 on timeout
sd_event_dispatch() returns 0 on FINISH, so let's eat that up.
2015-03-14 12:17:24 +01:00
Tom Gundersen 2b0c9ef735 sd-event: rename PASSIVE/PREPARED to INITIAL/ARMED 2015-03-14 12:17:24 +01:00
David Herrmann 15411c0cb1 tree-wide: there is no ENOTSUP on linux
Replace ENOTSUP by EOPNOTSUPP as this is what linux actually uses.
2015-03-13 14:10:39 +01:00
Zbigniew Jędrzejewski-Szmek a2360a467b sd-event: fix typo 2015-03-13 00:26:02 -04:00
Hannes Reinecke 1c724e9e0e Remove the cap on epoll events
Currently the code will silently blank out events if there are more
then 512 epoll events, causing them never to be handled at all. This
patch removes the cap on the number of events for epoll_wait, thereby
avoiding this issue.
2015-03-04 21:43:17 -05:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Tom Gundersen c4f1aff230 sd-event: treat NULL callback for timer events like sig events
In both cases exit the event loop.
2015-02-02 11:57:52 +01:00
Ronny Chevalier 0c0cdb06c1 tests: use assert_se instead of assert
Otherwise they can be optimized away with -DNDEBUG
2015-01-22 23:10:56 +01:00
Michal Schmidt da927ba997 treewide: no need to negate errno for log_*_errno()
It corrrectly handles both positive and negative errno values.
2014-11-28 13:29:21 +01:00
Michal Schmidt 0a1beeb642 treewide: auto-convert the simple cases to log_*_errno()
As a followup to 086891e5c1 "log: add an "error" parameter to all
low-level logging calls and intrdouce log_error_errno() as log calls
that take error numbers", use sed to convert the simple cases to use
the new macros:

find . -name '*.[ch]' | xargs sed -r -i -e \
's/log_(debug|info|notice|warning|error|emergency)\("(.*)%s"(.*), strerror\(-([a-zA-Z_]+)\)\);/log_\1_errno(-\4, "\2%m"\3);/'

Multi-line log_*() invocations are not covered.
And we also should add log_unit_*_errno().
2014-11-28 12:04:41 +01:00
Lennart Poettering f4b2933ee7 sd-bus,sd-event: unify error handling of object descriptions
a) When getting the description return ENXIO if none is set

b) Allow setting a description to NULL

c) return ECHILD on fork() like for other calls
2014-11-04 18:09:19 +01:00
Lennart Poettering f1f00dbb7f sd-event: implicitly set signal event source's descriptions to the signal name 2014-11-04 18:09:19 +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
Zbigniew Jędrzejewski-Szmek f95387cda8 sd-event: also update signal mask when disconnecting sources 2014-10-08 19:30:22 -04:00
Zbigniew Jędrzejewski-Szmek 4807d2d068 sd-event: be more careful when enabling/disabling signals
When a child event is disabled (in order to be freed) and there is no
SIGCHLD signal event, sd_event_source_set_enabled will disable SIGCHLD
even if there are other child events.

Also remove some unneeded signalfd updates.

https://bugs.freedesktop.org/show_bug.cgi?id=84659

Based-on-a-patch-by: Hristo Venev <mustrumr97@gmail.com>
2014-10-08 19:30:22 -04:00
Zbigniew Jędrzejewski-Szmek 7057bd9931 sd-event: check the value of received signal
Appease coverity report #1237775.

Also rename ss to n, to make it visually different from ss.
2014-10-03 20:42:09 -04:00
Michal Schmidt d5099efc47 hashmap: introduce hash_ops to make struct Hashmap smaller
It is redundant to store 'hash' and 'compare' function pointers in
struct Hashmap separately. The functions always comprise a pair.
Store a single pointer to struct hash_ops instead.

systemd keeps hundreds of hashmaps, so this saves a little bit of
memory.
2014-09-15 16:08:50 +02:00
David Herrmann 32b13b2f58 sd-event: simplify sd_event_source_set_name()
free_and_strdup() does exactly the same as sd_event_source_set_name(), use
it!
2014-08-28 22:44:28 +02:00
Tom Gundersen 752bbf75b9 sd-event: use event source name rather than address in debug messages 2014-08-28 21:19:17 +02:00
Tom Gundersen f7f53e9e6e sd-event: allow naming event sources 2014-08-28 21:19:17 +02:00
Tom Gundersen 6d148a842e sd-event: sd_event_prepare - stay in PREPARED if sd_event_wait() indicates that no sources are pending 2014-08-26 00:22:06 +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
Tom Gundersen 050f74f286 sd-event: return 'r' rather than '-errno' 2014-08-15 20:32:27 +02:00
Tom Gundersen e07bbb7c50 sd-event: fix missing needs_rearm 2014-08-15 20:20:38 +02:00
Tom Gundersen d06441da04 sd-event: drop _likely_()
This is not certain to be likely.

Lennart says: a frequent usecase is invoking some function regularly in intervals
in such a case every single iteration we'll have to rearm
2014-08-14 02:00:43 +02:00
Tom Gundersen 212bbb1798 sd-event: do not arm timers unnecessarily
Rather than recalculating the next timeout on every loop, we only do it when something changed.
2014-08-14 01:29:51 +02:00
Kay Sievers 3a43da2832 time-util: add and use USEC/NSEC_INFINIY 2014-07-29 13:20:20 +02:00
Tom Gundersen a854881616 sd-event: add support for CLOCK_BOOTTIME
This requires a very recent kernel (3.15), so care should be taken
when using this functionality.
2014-07-24 16:37:01 +02:00
Lennart Poettering 92daebc0d0 sd-event: don't require a signal event source to be enabled for the child event source to work 2014-07-11 16:50:10 +02:00
David Herrmann b63c8d4f03 sd-event: always call epoll_ctl() on mask-updates if edge-triggered
A call to sd_event_source_set_io_events() skipps calling into the kernel
if the new event-mask matches the old one. This is safe for
level-triggered sources as the kernel moves them onto the ready-list
automatically if events change. However, edge-triggered sources might not
be on the ready-list even though events are present.

A call to sd_event_source_set_io_events() with EPOLLET set might thus be
used to just move the io-source onto the ready-list so the next poll
will return it again. This is very useful to avoid starvation in
priority-based event queues.

Imagine a read() loop on an edge-triggered fd. If we cannot read data fast
enough to drain the receive queue, we might decide to skip reading for now
and schedule it for later. On edge-triggered io-sources we have to make
sure it's put on the ready-list so the next dispatch-round will return it
again if it's still the highest priority task. We could make sd-event
handle edge-triggered sources directly and allow marking them ready again.
However, it's much simpler to let the kernel do that for now via
EPOLL_CTL_MOD.
2014-07-11 16:43:53 +02:00
Lennart Poettering 1389925348 event: pull in sd-event.h from event-util.h 2014-07-10 20:12:34 +02:00
Lennart Poettering eec6022cf0 sd-event: restore correct timeout behaviour 2014-06-05 13:50:35 +02:00
Lennart Poettering 4a134c4903 sd-resolve: add "floating" resolve queries
Same story as for sd-bus and sd-event: allow passing NULL to store query
in in which case the query is freed automatically.
2014-05-15 17:13:05 +02:00
Lennart Poettering a71fe8b8ae sd-event: introduce concept of "floating" event sources
These are the counterpart of "floating" bus slots, i.e. event sources
that are bound to the lifetime of the event object itself, and thus
don't require an explicit reference to be kept.
2014-05-15 17:13:04 +02:00
Lennart Poettering cc567911ba sd-event: allow disabling event sources when an event object is dying anyway 2014-05-05 23:48:25 +02:00
Lennart Poettering 59bc1fd776 sd-event: make it easy to bind signal handling to event loop exits
By passing a NULL callback provide a simple way to make event loops exit
on SIGTERM and other signals.
2014-05-05 23:47:59 +02:00
Thomas Hindoe Paaboel Andersen 9f2a50a300 silence warnings 2014-03-27 20:16:49 +01:00
Lennart Poettering 7514578081 sd-event: don't accidentally turn of watchdog timer event if we determine 0 2014-03-25 00:01:51 +01:00
Lennart Poettering 52444dc478 sd-event: initialization perturbation value right before we use it
That way, we don't forget to initialize it when the watchdog is
initialized before all event sources.
2014-03-24 23:54:21 +01:00
Lennart Poettering 79e16ce3bf networkd: fix a couple of memory leaks 2014-03-24 23:46:13 +01:00
Lennart Poettering 3d94f76c99 util: replace close_pipe() with new safe_close_pair()
safe_close_pair() is more like safe_close(), except that it handles
pairs of fds, and doesn't make and misleading allusion, as it works
similarly well for socketpairs() as for pipe()s...
2014-03-24 03:22:44 +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 03e334a1c7 util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:

        fd = safe_close(fd);

Which will close an fd if it is open, and reset the fd variable
correctly.

By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
2014-03-18 19:31:34 +01:00
Zbigniew Jędrzejewski-Szmek bdd13f6be4 Remove dead lines in various places
As pointed-out by clang -Wunreachable-code.

No behaviour changes.
2014-02-24 19:24:14 -05: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
Thomas Bächler 9ac441c0f4 sd-event: Fix systemd crash when using timer units.
There was a copy-paste error introduced in commit c2ba3ad660
which causes the following error when using timer units:

Assertion '(x->type == SOURCE_MONOTONIC && y->type == SOURCE_MONOTONIC) || (x->type == SOURCE_REALTIME && y->type == SOURCE_REALTIME)'
failed at src/libsystemd/sd-event/sd-event.c:264, function latest_time_prioq_compare(). Aborting.
2014-02-21 14:06:50 +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 39883f622f make gcc shut up
If -flto is used then gcc will generate a lot more warnings than before,
among them a number of use-without-initialization warnings. Most of them
without are false positives, but let's make them go away, because it
doesn't really matter.
2014-02-19 17:53:50 +01:00
David Herrmann 7a0d4a3d16 event: fix crash on child-source state modifications
Setting a child-source state to anything but SD_EVENT_OFF currently does
nothing. The condition logic is flawed. Move the state update *below* the
test for the previous state.

Fixes a crash if you call:
  sd_event_source_set_enabled(source, SD_EVENT_ON);
  sd_event_source_unref(source);
on a child-source in its own callback.
2014-02-19 13:24:38 +01:00
Tom Gundersen 607553f930 libsystemd: split up into subdirs
We still only produce on .so, but let's keep the sources separate to make things a bit
less messy.
2014-01-21 14:41:35 +01:00