Commit Graph

59 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 65d410c7ca sd-id128: add sd_id128_get_boot_app_specific() 2018-10-02 15:15:10 +02:00
Zbigniew Jędrzejewski-Szmek 6083c4b763 libsystemd: export all functions in sd-hwdb.h
sd-hwdb.h is now also installed in /usr/include/systemd.
2018-09-30 20:39:02 +02:00
Zbigniew Jędrzejewski-Szmek 3f608087bd libsystemd: export all functions in sd-device.h
sd-device.h is now also installed in /usr/include/systemd.
2018-09-30 20:39:02 +02:00
Yu Watanabe 0fbc35524c libsystemd: add missing 'global' specifier in libsystemd.sym 2018-09-25 23:30:58 +02:00
Yu Watanabe 385b2eb262 sd-bus: make BUS_DEFAULT_TIMEOUT configurable
This adds sd_bus_{get,set}_method_call_timeout().
If the timeout is not set or set to 0, then the timeout value is
parsed from $SYSTEMD_BUS_TIMEOUT= environment variable. If the
environment variable is not set, then built-in timeout is used.
2018-07-16 15:11:50 +02:00
Violet Halo 11ce0db2e2 sd-bus: publicize sd_bus_message_readv()
The D-Bus library supplies a va_list variant of
`sd_bus_message_append()` called `sd_bus_message_appendv()`,
but failed to provide a va_list variant of its opposite,
`sd_bus_message_read()`. This commit publicizes a previously static
function as `sd_bus_message_readv()`.
2018-06-29 11:45:40 +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 66223497d0 sd-bus: also add destroy callbacks to sd_bus_track objects
This augments previous work for this for sd_bus_slot objects, and adds
the same concept to sd_bus_track objects, too.
2018-06-07 13:10:56 +02:00
Zbigniew Jędrzejewski-Szmek fa17b4e8d9 bus: optionally call a callbacks for cleanup
This adds a function sd_bus_slot_set_destroy_callback() to set a function
which can free userdata or perform other cleanups.

sd_bus_slot_get_destory_callback() queries the callback, and is included
for completeness.

Without something like this, for floating asynchronous callbacks, which might
be called or not, depending on the sequence of events, it's hard to perform
resource cleanup. The alternative would be to always perform the cleanup from
the caller too, but that requires more coordination and keeping of some shared
state. It's nicer to keep the cleanup contained between the callback and the
function that requests the callback.
2018-06-06 23:01:57 +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
Lennart Poettering 303acb7f2d sd-bus: add new sd_bus_slot_set_floating() call
This new call allows explicit control of the "floating" state of a bus
slot object. This is useful for creating a bus slot object first,
retaining a reference to it, using it for making changes to the slot
object (for example, set a description) and then handing it over to
sd-bus for lifecycle management.

It's also useful to fix #8551.
2018-05-30 17:13:51 +02:00
Zbigniew Jędrzejewski-Szmek 56fbd7187a sd-bus: allow description to be set for system/user busses (#8594)
sd_bus_open/sd_bus_open_system/sd_bus_open_user are convenient, but
don't allow the description to be set. After they return, the bus is
is already started, and sd_bus_set_description() fails with -EBUSY.
It would be possible to allow sd_bus_set_description() to update the
description "live", but messages are already emitted from sd_bus_open
functions, so it's better to allow the description to be set in
sd_bus_open/sd_bus_open_system/sd_bus_open_user.

Fixes message like:
Bus n/a: changing state UNSET → OPENING
2018-03-29 16:14:11 +02:00
Lennart Poettering 2770da027a sd-bus: add APIs to query the current read and write queue size 2018-02-27 19:54:29 +01: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
Lennart Poettering 48ef41a335 sd-bus: add API to optionally set a sender field on all outgoing messages
This is useful on direct connections to generate messages with valid
sender fields.

This is particularly useful for services that are accessible both
through direct connections and the broker, as it allows clients to
install matches on the sender service name, and they work the same in
both cases.
2018-01-05 13:58:33 +01:00
Lennart Poettering b38cc8d563 sd-bus: add new sd_bus_set_connected_signal() API
With this new API sd-bus can synthesize a local "Connected" signal when
the connection is fully established. It mirrors the local "Disconnected"
signal that is already generated when the connection is terminated. This
is useful to be notified when connection setup is done, in order to
start method calls then, in particular when using "slow" connection
methods (for example slow TCP, or most importantly the "watch_bind"
inotify logic).

Note that one could also use hook into the initial NameAcquired signal
received from the bus broker, but that scheme works only if we actually
connect to a bus. The benefit of "Connected" OTOH is that it works with
any kind of connection.

Ideally, we'd just generate this message unconditionally, but in order
not to break clients that do not expect this message it is opt-in.
2018-01-05 13:58:32 +01:00
Lennart Poettering bdbc866914 sd-bus: add new sd_bus_is_ready() API
This new call is much light sd_bus_is_open(), but returns true only if
the connection is fully set up, i.e. after we finished with the
authentication and Hello() phase. This API is useful for clients in
particular when using the "watch_bind" feature, as that way it can be
determined in advance whether it makes sense to sync on some operation.
2018-01-05 13:58:32 +01:00
Lennart Poettering b423e4fb73 sd-bus: add new API sd_bus_match_signal() + sd_bus_match_signal_asnyc()
These are convenience helpers that hide the match string logic (which we
probably should never have exposed), and instead just takes regular C
arguments.
2018-01-05 13:58:32 +01:00
Lennart Poettering 7593c7a495 sd-bus: add asynchronous version of sd_bus_match()
We usually enqueue a number of these calls on each service
initialization. Let's do this asynchronously, and thus remove
synchronization points. This improves both performance behaviour and
reduces the chances to deadlock.
2018-01-05 13:58:32 +01:00
Lennart Poettering 98c5bbc85d sd-bus: add APIs to request/release names asynchronously
They do the same thing as their synchronous counterparts, but only
enqueue the operation, thus removing synchronization points during
service initialization.

If the callback function is passed as NULL we'll fallback to generic
implementations of the reply handlers, that terminate the connection if
the requested name cannot be acquired, under the assumption that not
being able to acquire the name is a technical problem.
2018-01-05 13:58:32 +01:00
Lennart Poettering 8a5cd31e5f sd-bus: optionally, use inotify to wait for bus sockets to appear
This adds a "watch-bind" feature to sd-bus connections. If set and the
AF_UNIX socket we are connecting to doesn't exist yet, we'll establish
an inotify watch instead, and wait for the socket to appear. In other
words, a missing AF_UNIX just makes connecting slower.

This is useful for daemons such as networkd or resolved that shall be
able to run during early-boot, before dbus-daemon is up, and want to
connect to dbus-daemon as soon as it becomes ready.
2018-01-05 13:55:08 +01:00
Zbigniew Jędrzejewski-Szmek d9215cd838 Add SPDX license headers to various assorted files 2017-11-19 19:08:15 +01:00
aeywalee 75bcbcf2e7 Add sd_bus_message_new and sd_bus_message_seal as public functions (#6609) 2017-10-30 16:08:46 +01:00
Federico 19fe49f62c Export sd_bus_message_append_ap. It is renamed to sd_bus_message_appendv to follow systemd naming conventions. (#5753)
Moreover, man page for sd_bus_message_append is updated with reference to new exposed function.
Makefile-man is updated too, to reflect new alias.
2017-04-25 09:32:24 +02:00
Zbigniew Jędrzejewski-Szmek f6f372d2f4 Add sd_is_socket_sockaddr (#4885)
Fixes #1188.
2016-12-14 11:51:26 +01:00
Lennart Poettering 70fc4f5790 sd-id128: add new sd_id128_get_machine_app_specific() API
This adds an API for retrieving an app-specific machine ID to sd-id128.
Internally it calculates HMAC-SHA256 with an 128bit app-specific ID as payload
and the machine ID as key.

(An alternative would have been to use siphash for this, which is also
cryptographically strong. However, as it only generates 64bit hashes it's not
an obvious choice for generating 128bit IDs.)

Fixes: #4667
2016-11-29 15:13:00 +01:00
Lennart Poettering 4b58153dd2 core: add "invocation ID" concept to service manager
This adds a new invocation ID concept to the service manager. The invocation ID
identifies each runtime cycle of a unit uniquely. A new randomized 128bit ID is
generated each time a unit moves from and inactive to an activating or active
state.

The primary usecase for this concept is to connect the runtime data PID 1
maintains about a service with the offline data the journal stores about it.
Previously we'd use the unit name plus start/stop times, which however is
highly racy since the journal will generally process log data after the service
already ended.

The "invocation ID" kinda matches the "boot ID" concept of the Linux kernel,
except that it applies to an individual unit instead of the whole system.

The invocation ID is passed to the activated processes as environment variable.
It is additionally stored as extended attribute on the cgroup of the unit. The
latter is used by journald to automatically retrieve it for each log logged
message and attach it to the log entry. The environment variable is very easily
accessible, even for unprivileged services. OTOH the extended attribute is only
accessible to privileged processes (this is because cgroupfs only supports the
"trusted." xattr namespace, not "user."). The environment variable may be
altered by services, the extended attribute may not be, hence is the better
choice for the journal.

Note that reading the invocation ID off the extended attribute from journald is
racy, similar to the way reading the unit name for a logging process is.

This patch adds APIs to read the invocation ID to sd-id128:
sd_id128_get_invocation() may be used in a similar fashion to
sd_id128_get_boot().

PID1's own logging is updated to always include the invocation ID when it logs
information about a unit.

A new bus call GetUnitByInvocationID() is added that allows retrieving a bus
path to a unit by its invocation ID. The bus path is built using the invocation
ID, thus providing a path for referring to a unit that is valid only for the
current runtime cycleof it.

Outlook for the future: should the kernel eventually allow passing of cgroup
information along AF_UNIX/SOCK_DGRAM messages via a unique cgroup id, then we
can alter the invocation ID to be generated as hash from that rather than
entirely randomly. This way we can derive the invocation race-freely from the
messages.
2016-10-07 20:14:38 +02:00
Lennart Poettering fbb4603d48 sd-bus: optionally, exit process or event loop on disconnect
Old libdbus has a feature that the process is terminated whenever the the bus
connection receives a disconnect. This is pretty useful on desktop apps (where
a disconnect indicates session termination), as well as on command line apps
(where we really shouldn't stay hanging in most cases if dbus daemon goes
down).

Add a similar feature to sd-bus, but make it opt-in rather than opt-out, like
it is on libdbus. Also, if the bus is attached to an event loop just exit the
event loop rather than the the whole process.
2016-08-22 17:31:41 +02:00
Lennart Poettering ae1a2efa8b sd-bus: add a "recursive" mode to sd_bus_track
This adds an optional "recursive" counting mode to sd_bus_track. If enabled
adding the same name multiple times to an sd_bus_track object is counted
individually, so that it also has to be removed the same number of times before
it is gone again from the tracking object.

This functionality is useful for implementing local ref counted objects that
peers make take references on.
2016-08-22 14:17:24 +02: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
Lennart Poettering 5d1ce25728 sd-journal: add API for opening journal files or directories by fd
Also, expose this via the "journalctl --file=-" syntax for STDIN. This feature
remains undocumented though, as it is probably not too useful in real-life as
this still requires fds that support mmaping and seeking, i.e. does not work
for pipes, for which reading from STDIN is most commonly used.
2016-04-25 15:24:46 +02:00
Lennart Poettering 9a07f779bb sd-journal: properly export has_{persistent|runtime}_files()
This was missing in 39fd5b08a7.
2016-02-01 23:15:54 +01:00
Lennart Poettering eb86030ec0 sd-journal: add an API to enumerate known field names of the journal
This adds two new calls to get the list of all journal fields names currently in use.

This is the low-level support to implement the feature requested in #2176 in a more optimized way.
2016-02-01 22:42:33 +01:00
Lennart Poettering 24645b333d build-sys: add sd_listen_fds_with_name() to .sym file 2015-10-07 17:04:39 +02:00
David Herrmann dfb815c36d sd-bus: add sd_bus_path_{encode,decode}_many()
This introduces two new helpers alongside sd_bus_path_{encode,decode}(),
which work similarly to their counterparts, but accept a format-string as
input. This allows encoding and decoding multiple labels of a format
string at the same time.
2015-09-26 16:57:23 +02:00
Lennart Poettering fa2f8973ad sd-bus: introduce new sd_bus_default_flush_close() call
If code enqueues a message on one of the default busses, but doesn't
sync on it, and immediately drops the reference to the bus again, it
will stay queued and consume memory. Intrdouce a new call
sd_bus_default_flush_close() that can be invoked at the end of programs
(or threads) and flushes out all unsent messages on any of the default
busses.
2015-09-22 16:29:10 +02:00
Lennart Poettering f5aaf57562 sd-login: add new sd_pid_get_cgroup() API
This adds a new sd_pid_get_cgroup() call to sd-login which may be used
to query the control path of a process. This is useful for programs when
making use of delegation units, in order to figure out which subtree has
been delegated.

In light of the unified control group hierarchy this is finally safe to
do, hence let's add a proper API for it, to make it easier to use this.
2015-09-05 14:56:23 +02:00
Lennart Poettering 03976f7b4a sd-bus: introduce new sd_bus_flush_close_unref() call
sd_bus_flush_close_unref() is a call that simply combines sd_bus_flush()
(which writes all unwritten messages out) + sd_bus_close() (which
terminates the connection, releasing all unread messages) +
sd_bus_unref() (which frees the connection).

The combination of this call is used pretty frequently in systemd tools
right before exiting, and should also be relevant for most external
clients, and is hence useful to cover in a call of its own.

Previously the combination of the three calls was already done in the
_cleanup_bus_close_unref_ macro, but this was only available internally.

Also see #327
2015-07-03 19:49:03 +02:00
Geert Jansen 969a9685d9 export sd_bus_object_added() / _removed()
Fixes #306.
2015-06-21 22:21:38 -04:00
Kay Sievers 73a7e4615d libsystemd: remove list of symbols to export only in the future 2015-06-17 16:21:13 +02:00
Kay Sievers 2b0a23e864 build-sys: export experimental symbols only with --enable-kdbus 2014-02-19 20:53:18 +01:00
Kay Sievers add00dfd1f build-sys: merge libsystemd-journal into libsystemd 2014-02-19 18:12:58 +01:00
Kay Sievers 0ebee88184 build-sys: merge libsystemd-daemon into libsystemd 2014-02-19 01:09:19 +01:00
David Herrmann a020c2a013 sd-bus: export sd_bus_call{,_async,_async_cancel}
The .sym file somehow lacks these declarations, so add these. You have to
run "make clean" to make sure the sym-test runs fine afterwards.
2014-02-10 23:25:10 +01:00
Lennart Poettering af08d2f9cd bus: add API calls for connecting to starter bus
Add new calls sd_bus_open() and sd_bus_default() for connecting to the
starter bus a service was invoked for, or -- if the process is not a
bus-activated service -- the appropriate bus for the scope the process
has been started in.
2014-01-27 21:34:54 +01:00
Zbigniew Jędrzejewski-Szmek be12791331 build-sys: merge libsystemd-id128 into libsystemd 2014-01-25 18:10:08 -05:00
Zbigniew Jędrzejewski-Szmek 0bba8d6eb7 build-sys: merge libsystemd-login into libsystemd
A compatibility libsystemd-login library is created which uses
.symver and ifunc magic proposed by Lennart to make programs linked
to the old library name continue to work seamlessly.

Unfortunately the bfd linker crashes:
  https://sourceware.org/bugzilla/show_bug.cgi?id=16467
This will be fixed in binutils 2.25.

As a work-around, gold can be used:
  LDFLAGS=-Wl,-fuse-ld=gold

Unfortunately the switch to pick the linker appeared in gcc 4.8.

This also doesn't work with LLVM:
  http://llvm.org/bugs/show_bug.cgi?id=11897
2014-01-25 18:10:08 -05:00
Lennart Poettering eee9ec0e28 bus: change API to expose "inverted" no_reply and no_auto_start message flags non-inverted 2014-01-22 20:45:05 +01:00
Lennart Poettering 766c580959 bus: add sd_bus_process_priority() to support prioq mode of kdbus 2014-01-22 20:26:58 +01:00