Email or username:

Password:

Forgot your password?
Lennart Poettering

5️⃣ Here's the 5th installment of posts highlighting key new features of the upcoming v257 release of systemd. #systemd257

Since its beginnings systemd has been a heavy user of the D-Bus IPC system. It provides D-Bus APIs, it calls D-Bus APIs, it schedules activation of the D-Bus broker, and even provides its own C D-Bus client library (sd-bus).

However, since early on our use of D-Bus was not without various major problems. One of the biggest goes something like this:

14 comments
Lennart Poettering

D-Bus' model is built around a central broker daemon, which is started during boot, but unfortunately relatively late (i.e. together with other, regular daemons instead of early boot or the initrd). However, systemd brings up the system as a whole and hence needs IPC from earliest moment on.

And then there are various components of systemd that the D-Bus broker relies on (i.e. consumes functionality of) and hence cannot themselves provide their services on D-Bus, …

Lennart Poettering

…, in order to avoid a chicken/egg problem, a cyclic dependency, and deadlocks. (Example: journald provides logging to the D-Bus broker, and hence cannot provide APIs via D-Bus. Similar PID 1 itself, or systemd-userdb/systemd-homed which provide user record resolution which D-Bus needs for its policies, and so on and so on).

These problems are very hard to tackle. For example in PID 1 itself we provide our D-Bus APIs not just via the broker, but also via another local "direct" socket.

Lennart Poettering

The latter sucks in major ways, since we basically had to reimplement a subset of the broker ourselves, with message multiplexing, subscription, signal matching and a lot of other stuff. Because this was so messy we never did the same for journald, userdbd or homed.

These two are just the biggest issues with D-Bus, but there are a lot more, in my eyes. Hence, quite some time ago we started to use a different type of IPC for these cases, initially just internally.

Lennart Poettering

That alternative IPC is called Varlink (varlink.org/). It has been around for a while, and initially we only adopted it where D-Bus was just too bad to use, and only internally. Over the last couple of releases that changed however: we started to make heavier use of it and provide public interfaces via Varlink in addition or instead of D-Bus.

In many ways Varlink is much nicer to work with than D-Bus: it's a lot simpler, it's brokerless design make it a ton faster, …

Lennart Poettering

…, it's JSON use make it more conceptually compatible with the rest of the world and various other things.

It's also a lot easier to write Varlink services than D-Bus, because it allows you to handle each connection in a different process, thus being compatible with codebases that do not have event loops (D-Bus due to its multiplexing forces you to process all messages within the same process, and due to the global ordering within a single event loop).

Lennart Poettering

To give one example, "bootctl" is a small tool that installs the systemd-boot boot loader into the ESP for you. It's a command line tool that synchronously copies a bunch of files into the target mount. We always had the plan to turn that into a D-Bus service, but never actually did it, because doing that is pain: we'd have to turn it into an event loop driven thing, which is just nasty for something so simple that just copies some files.

In a Varlink world, the problem goes away:

Lennart Poettering

we just let systemd's socket activation logic listen on an AF_UNIX/SOCK_STREAM socket, and then let it fork off a new bootctl instance for each connection. That instance then just processes that connection and is done. And it's easy: it just does what it usually does, but instead of reading the commands to execute from the command line it just reads them from a small JSON object it gets from STDIN. And it just writes its output as JSON to STDOUT, done.

In fact, because bootctl already…

Lennart Poettering

…supported JSON output anyway, the output side was done pretty much anyway.

Anyway, there are many other stories like that.

Suffice to say, in v257 there are now 19 Varlink interfaces/services, which we added in a short time, for various things that never had them before when D-Bus was our sole focus, because it was so nasty to add that.

(For comparison: we provide only 11 D-Bus API services at this time).

Lennart Poettering

There are various bindings for Varlink available from the Varlink project, but with systemd v257 we now make systemd's own C implementation available publically too: sd-varlink.

sd-varlink has been around for quite a while, and is quite well tested (including fuzzed) by now hence. It's already driving your systemd installations, except mostly internally so far.

Since Varlink uses JSON for marshalling its messages, sd-varlink comes with a companion API: sd-json. It's another C library for JSON.

Lennart Poettering

You are of course right if you say that there are already so many of those, why another? And you'd be right. My answer to that is that sd-json is much nicer to use than most, since it doesn't just allow you deal with JSON objects, it also helps you with building them from C data structures, and to map JSON objects back to C data structures. It also helps you with higher level operations that the low-level JSON datatypes leave you in the cold with:

Lennart Poettering replied to Lennart

i.e. for example handles base64 encoding/decoding for handling binary blobs within JSON automatically, or it helps you with dealing with JSON's >53bit integer problem, and various other things.

Right now, documentation for sd-varlink and sd-json is scarce (one could even say "barely existing"), but there are plenty of real-life examples in the systemd source tree, of course.

Lennart Poettering replied to Lennart

At Linux' best conference, All Systems Go! 2024 in Berlin this year I gave a (brief) talk about Varlink, and why you should consider it. If you want to know more about the concept, this might be a good starting point:

media.ccc.de/v/all-systems-go-

And that's all for now, enjoy!

Steven Reed

@pid_eins
Would it be feasible at any point to rip out at least some of the pre-existing native dbus APIs and provide them via a small bridge which does translation to/from varlink?

shine

@pid_eins Oh, I remember Podman using it in its V1 API, I was a bit sad that they dropped it, it was quite nice to use.

Go Up