From 371c0b794e8e677bf792db392991ade30c48245c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 23 Nov 2017 20:15:48 +0100 Subject: [PATCH] core: track scope controllers on the bus This watches controllers on the bus, and unsets them automatically when they disappear. Note that this is primarily a cosmetical fix. Since unique bus names are not recycled, there's strictly no need to forget about them, but it's a lot nicer to do so. --- src/core/dbus-scope.c | 39 ++++++++++++++++++++++++++++++++++++++- src/core/dbus-scope.h | 2 ++ src/core/scope.c | 15 +++++++++++---- src/core/scope.h | 2 ++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c index 46222ef..4737c6d 100644 --- a/src/core/dbus-scope.c +++ b/src/core/dbus-scope.c @@ -61,7 +61,7 @@ static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResu const sd_bus_vtable bus_scope_vtable[] = { SD_BUS_VTABLE_START(0), - SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST), + SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_SIGNAL("RequestStop", NULL, 0), @@ -233,3 +233,40 @@ int bus_scope_send_request_stop(Scope *s) { return sd_bus_send_to(UNIT(s)->manager->api_bus, m, s->controller, NULL); } + +static int on_controller_gone(sd_bus_track *track, void *userdata) { + Scope *s = userdata; + + assert(track); + + if (s->controller) { + log_unit_debug(UNIT(s), "Controller %s disappeared from bus.", s->controller); + unit_add_to_dbus_queue(UNIT(s)); + s->controller = mfree(s->controller); + } + + s->controller_track = sd_bus_track_unref(s->controller_track); + + return 0; +} + +int bus_scope_track_controller(Scope *s) { + int r; + + assert(s); + + if (!s->controller || s->controller_track) + return 0; + + r = sd_bus_track_new(UNIT(s)->manager->api_bus, &s->controller_track, on_controller_gone, s); + if (r < 0) + return r; + + r = sd_bus_track_add_name(s->controller_track, s->controller); + if (r < 0) { + s->controller_track = sd_bus_track_unref(s->controller_track); + return r; + } + + return 0; +} diff --git a/src/core/dbus-scope.h b/src/core/dbus-scope.h index 8e43582..c80317c 100644 --- a/src/core/dbus-scope.h +++ b/src/core/dbus-scope.h @@ -30,3 +30,5 @@ int bus_scope_set_property(Unit *u, const char *name, sd_bus_message *i, UnitSet int bus_scope_commit_properties(Unit *u); int bus_scope_send_request_stop(Scope *s); + +int bus_scope_track_controller(Scope *s); diff --git a/src/core/scope.c b/src/core/scope.c index 05b2ec3..e0e2567 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -59,7 +59,8 @@ static void scope_done(Unit *u) { assert(u); - free(s->controller); + s->controller = mfree(s->controller); + s->controller_track = sd_bus_track_unref(s->controller_track); s->timer_event_source = sd_event_source_unref(s->timer_event_source); } @@ -229,6 +230,8 @@ static int scope_coldplug(Unit *u) { if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) unit_watch_all_pids(UNIT(s)); + bus_scope_track_controller(s); + scope_set_state(s, s->deserialized_state); return 0; } @@ -272,9 +275,8 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { unit_watch_all_pids(UNIT(s)); - /* If we have a controller set let's ask the controller nicely - * to terminate the scope, instead of us going directly into - * SIGTERM berserk mode */ + /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going + * directly into SIGTERM berserk mode */ if (state == SCOPE_STOP_SIGTERM) skip_signal = bus_scope_send_request_stop(s) > 0; @@ -332,6 +334,8 @@ static int scope_start(Unit *u) { if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) return -ENOENT; + (void) bus_scope_track_controller(s); + r = unit_acquire_invocation_id(u); if (r < 0) return r; @@ -536,7 +540,10 @@ int scope_abandon(Scope *s) { return -ESTALE; s->was_abandoned = true; + s->controller = mfree(s->controller); + s->controller_track = sd_bus_track_unref(s->controller_track); + scope_set_state(s, SCOPE_ABANDONED); /* The client is no longer watching the remaining processes, diff --git a/src/core/scope.h b/src/core/scope.h index 7e8c47d..ca7c6c8 100644 --- a/src/core/scope.h +++ b/src/core/scope.h @@ -46,6 +46,8 @@ struct Scope { usec_t timeout_stop_usec; char *controller; + sd_bus_track *controller_track; + bool was_abandoned; sd_event_source *timer_event_source; -- 2.7.4