[systemd-devel] sd-varlink: how to loop over connections?

Thorsten Kukuk kukuk at suse.com
Wed Nov 20 15:12:19 UTC 2024


On Wed, Nov 20, 2024 at 3:23 PM Lennart Poettering
<lennart at poettering.net> wrote:
>
> On Mi, 20.11.24 14:51, Thorsten Kukuk (kukuk at suse.com) wrote:
>
> > Hi,
> >
> > I'm currently playing with sd-varlink and trying to replace dbus in
> > one of our daemon with sd-varlink. Not that trivial without
> > documentation...
> >
> > My currently biggest question I couldn't solve is: how should a daemon
> > listen to connections?
>
> What kind of event loop are you using inside your daemon? Is this a
> continously running singleton daemon that shall respond to many
> varlink connections simultaneously?

Currently it's a mix of pthreads, timer and a while loop around the
original dbus interface, not sd_bus. It's very old code, which got
enhanced so many times that nobody understands anymore why the code is
working, that's why it's time to do a rewrite.
It should respond to many varlink connections, but it's not very
likely that it will be more than one at a time.

I think it's time to take a deeper look at sd_event then.

Thanks,
  Thorsten

> > There is "sd_varlink_server_loop_auto()", but this exits after the
> > last connection is closed.
>
> Yeah, that has a different usecase.
>
> > This is nice if you start the daemon via
> > systemd socket activation or something similar and the daemon should
> > quit afterwards, but I need an always running daemon.
> > And you cannot call that in a loop ("Failed to run Varlink event loop:
> > Device or resource busy").
>
> Yeah, once that event loop is done it's done.
>
> > Is the expectation to setup an own event loop without "exit_on_idle"?
> > Wouldn't it be better if sd_varlink_server_loop_auto() get's an
> > argument, if it should exit on idle or not? Or do I miss something?
>
> So individual connections you can integrate into any event loop you
> like relatively easily, it works roughly the same way as for sd-bus,
> i.e. every time you enter poll() you query the varlink connection via
> sd_varlink_get_fd(), sd_varlink_get_events(), sd_varlink_get_timeout()
> for the parameters to pass to poll() on. And afterwards you invoke
> sd_varlink_process(). The docs for sd_bus_get_fd() should explain this
> to some degree.
>
> I guess your use case is not just individual connections though: you
> want a full server. For that we currently do not provide such an API
> (because we have many fds to poll on, not just one). For that you can
> allocate an event loop object via sd_event_new(), and attach the
> server to it. You then can either run the event loop directly, or you
> can integrate that even loop into your own event loop of choice
> relatively easily (that works becuase sd-event is epoll internally,
> which itself provides an fd you can poll on). See the
> sd_event_get_fd() man page for details on that. There's even example
> there that shows how to use that to glue this thing into glib's event
> loop.
>
> So you basically can use sd_varlink_server_loop_auto() as blue print
> for this, but you drop the exit on idle stuff, and instead of the
> sd_event_loop() at the end you do what the sd_event_get_fd() man page
> explains.
>
> Lennart
>
> --
> Lennart Poettering, Berlin



-- 
Thorsten Kukuk, Distinguished Engineer, Senior Architect, Future Technologies
SUSE Software Solutions Germany GmbH, Frankenstraße 146, 90461
Nuernberg, Germany
Managing Director: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB
36809, AG Nürnberg)


More information about the systemd-devel mailing list