mirror of
https://github.com/systemd/systemd.git
synced 2025-01-27 18:04:05 +03:00
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.
This commit is contained in:
parent
f2c49c8658
commit
371c0b794e
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user