resolved: actually unregister DNS-SD services when the bus client vanishes

Since the D-Bus DNS-SD API was introduced in c3036641f0 ("resolved:
implement D-Bus API for DNS-SD", v236), RegisterService() has set up an
sd_bus_track object so that a registered service is destroyed when the
client that registered it disconnects from the bus — there's even a
handler logging "Client of active request vanished, destroying DNS-SD
service."

However, the track object was never stored anywhere: it is declared as
a _cleanup_(sd_bus_track_unrefp) local in bus_method_register_service()
and hence destroyed again when the method handler returns, taking the
tracking down with it. The vanish handler was thus dead code, and
services registered via D-Bus silently persisted after their client
disconnected — unowned, impossible to re-register (the identifier
stays taken, failing with DNSSD_SERVICE_EXISTS), and advertised until
systemd-resolved itself exited.

Store the track object in DnssdRegisteredService, pinning it for the
lifetime of the service, so that client tracking works as originally
intended. Route the vanish path through the full unregistration logic
(goodbye packets + removal from the per-link mDNS zones), since freeing
the service alone would leave its RRs published in the zones.

Note for fire-and-forget clients (e.g. one-shot busctl/gdbus calls):
these were never reliable — the registration silently vanished on any
resolved restart — and now behave as documented for the corresponding
Avahi API as well: the bus connection must be kept open for as long as
the service is to remain registered. Document that in the man page and
NEWS, and point to *.dnssd files for registrations independent of a
client's lifetime.
This commit is contained in:
Stefan Agner
2026-09-02 10:40:34 +02:00
parent a3a8e82f25
commit 11e4e35f4c
5 changed files with 37 additions and 2 deletions
+13
View File
@@ -4,6 +4,19 @@ CHANGES WITH 262:
Feature Removals and Incompatible Changes:
* DNS-SD services registered via systemd-resolved's D-Bus method
org.freedesktop.resolve1.Manager.RegisterService() are now
unregistered automatically when the client that registered them
disconnects from the bus, as originally intended and matching the
behavior of the equivalent Avahi API. Due to a bug the client
tracking never worked, and such services used to stay registered
until systemd-resolved exited (but did not survive a restart of the
daemon either). Clients must now keep their bus connection open for
as long as the service shall remain registered; one-shot
registrations, e.g. via busctl or gdbus, will be withdrawn again as
soon as the tool exits. To register a DNS-SD service independently
of a client's lifetime, use *.dnssd files, see systemd.dnssd(5).
* Meson options '-Dlibiptc=' (deprecated in v259), '-Dlibidn=',
'-Drc-local=', '-Dsysvinit-path=', and '-Dsysvrcnd-path=' (deprecated
in v260) have been removed.
+12
View File
@@ -450,6 +450,18 @@ node /org/freedesktop/resolve1 {
DNS-SD SRV record. Finally, it takes an array of TXT record data. It returns an object path which may be
used as handle to the registered service.</para>
<para>The lifetime of a service registered via this method is bound to the D-Bus connection of the
client that registered it: when the client disconnects from the bus the service is automatically
unregistered and goodbye packets are sent out, as if <function>UnregisterService()</function> had been
called. Clients hence must keep their bus connection open for as long as the service shall remain
registered. The registration also does not survive <filename>systemd-resolved</filename> losing its
own bus connection, in which case tracking clients is not possible anymore and all services registered
via this method are unregistered. To register a DNS-SD service independently of a client's lifetime,
use <filename>*.dnssd</filename> files, see
<citerefentry><refentrytitle>systemd.dnssd</refentrytitle><manvolnum>5</manvolnum></citerefentry>. Note
that prior to version 262, due to a bug, the client's connection was not tracked and services
registered via this method stayed registered until the daemon exited.</para>
<para>The <function>UnregisterService()</function> method undoes the effect of
<function>RegisterService()</function> and deletes a DNS-SD service previously created via IPC
again.</para>
+6 -2
View File
@@ -1894,8 +1894,8 @@ static int dnssd_registered_service_on_bus_track(sd_bus_track *t, void *userdata
assert(t);
log_debug("Client of active request vanished, destroying DNS-SD service.");
dnssd_registered_service_free(s);
log_debug("Client of DNS-SD service '%s' vanished, unregistering.", s->id);
dnssd_registered_service_unregister(s);
return 0;
}
@@ -2063,6 +2063,10 @@ static int bus_method_register_service(sd_bus_message *message, void *userdata,
if (r < 0)
return r;
/* The service is unregistered automatically when the client that registered it disconnects from the
* bus, hence keep the bus track object pinned for the lifetime of the service. */
service->bus_track = TAKE_PTR(bus_track);
service->manager = m;
r = hashmap_ensure_put(&m->dnssd_registered_services, &string_hash_ops, service->id, service);
+2
View File
@@ -57,6 +57,8 @@ DnssdRegisteredService *dnssd_registered_service_free(DnssdRegisteredService *se
if (service->manager)
hashmap_remove(service->manager->dnssd_registered_services, service->id);
sd_bus_track_unref(service->bus_track);
dns_resource_record_unref(service->ptr_rr);
dns_resource_record_unref(service->sub_ptr_rr);
dns_resource_record_unref(service->srv_rr);
+4
View File
@@ -39,6 +39,10 @@ typedef struct DnssdRegisteredService {
Manager *manager;
/* Tracks the D-Bus client that registered the service, so that it can be unregistered when the
* client disconnects from the bus. Only set for services registered via D-Bus. */
sd_bus_track *bus_track;
/* Services registered via D-Bus are not removed on reload */
ResolveConfigSource config_source;