From: Lennart Poettering Date: Fri, 31 Jan 2014 16:45:13 +0000 (+0100) Subject: core: introduce new stop protocol for unit scopes X-Git-Tag: v209~266 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=2d4a39e759c4ab846ad8a546abeddd40bc8d736e core: introduce new stop protocol for unit scopes By specifiy a Controller property when creating the scope a client can specify a bus name that will be notified with a RequestStop bus signal when the scope has been asked to shut down, instead of sending SIGTERM to the scope processes themselves. https://bugzilla.redhat.com/show_bug.cgi?id=1032695 --- diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c index bb251edc7..d5a204851 100644 --- a/src/core/dbus-scope.c +++ b/src/core/dbus-scope.c @@ -26,13 +26,16 @@ #include "dbus-kill.h" #include "dbus-scope.h" #include "bus-util.h" +#include "bus-internal.h" static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult); 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("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), SD_BUS_VTABLE_END }; @@ -86,6 +89,32 @@ static int bus_scope_set_transient_property( return 1; + } else if (streq(name, "Controller")) { + const char *controller; + char *c; + + r = sd_bus_message_read(message, "s", &controller); + if (r < 0) + return r; + + if (!isempty(controller) && !service_name_is_valid(controller)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller); + + if (mode != UNIT_CHECK) { + if (isempty(controller)) + c = NULL; + else { + c = strdup(controller); + if (!c) + return -ENOMEM; + } + + free(s->controller); + s->controller = c; + } + + return 1; + } else if (streq(name, "TimeoutStopUSec")) { if (mode != UNIT_CHECK) { @@ -145,3 +174,29 @@ int bus_scope_commit_properties(Unit *u) { unit_realize_cgroup(u); return 0; } + +int bus_scope_send_request_stop(Scope *s) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + _cleanup_free_ char *p = NULL; + int r; + + assert(s); + + if (!s->controller) + return 0; + + p = unit_dbus_path(UNIT(s)); + if (!p) + return -ENOMEM; + + r = sd_bus_message_new_signal( + UNIT(s)->manager->api_bus, + p, + "org.freedesktop.systemd1.Scope", + "RequestStop", + &m); + if (r < 0) + return r; + + return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL); +} diff --git a/src/core/dbus-scope.h b/src/core/dbus-scope.h index 7e8f0053d..33beda47b 100644 --- a/src/core/dbus-scope.h +++ b/src/core/dbus-scope.h @@ -28,3 +28,5 @@ extern const sd_bus_vtable bus_scope_vtable[]; int bus_scope_set_property(Unit *u, const char *name, sd_bus_message *i, UnitSetPropertiesMode mode, sd_bus_error *error); int bus_scope_commit_properties(Unit *u); + +int bus_scope_send_request_stop(Scope *s); diff --git a/src/core/scope.c b/src/core/scope.c index 5beb4f80c..0c1d17e68 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -64,6 +64,8 @@ static void scope_done(Unit *u) { cgroup_context_done(&s->cgroup_context); + free(s->controller); + set_free(s->pids); s->pids = NULL; @@ -217,6 +219,7 @@ static void scope_enter_dead(Scope *s, ScopeResult f) { } static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { + bool skip_signal = false; int r; assert(s); @@ -224,13 +227,22 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { if (f != SCOPE_SUCCESS) s->result = f; - r = unit_kill_context( - UNIT(s), - &s->kill_context, - state != SCOPE_STOP_SIGTERM, - -1, -1, false); - if (r < 0) - goto fail; + /* If we have a controller set let's ask the controller nicely + * to terminate the scope, instead of us going directly into + * SIGTERM beserk mode */ + if (state == SCOPE_STOP_SIGTERM) + skip_signal = bus_scope_send_request_stop(s) > 0; + + if (!skip_signal) { + r = unit_kill_context( + UNIT(s), + &s->kill_context, + state != SCOPE_STOP_SIGTERM, + -1, -1, false); + if (r < 0) + goto fail; + } else + r = 1; if (r > 0) { r = scope_arm_timer(s); diff --git a/src/core/scope.h b/src/core/scope.h index 4d8a1714b..014b50c76 100644 --- a/src/core/scope.h +++ b/src/core/scope.h @@ -55,6 +55,8 @@ struct Scope { usec_t timeout_stop_usec; + char *controller; + Set *pids; sd_event_source *timer_event_source; diff --git a/src/run/run.c b/src/run/run.c index 1b14e40e4..b9e1700d3 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -356,6 +356,14 @@ static int start_transient_scope( if (r < 0) return r; + { + const char *unique_id; + sd_bus_get_unique_name(bus, &unique_id); + r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique_id); + if (r < 0) + return r; + } + r = message_start_transient_unit_send(bus, m, error, NULL); if (r < 0) return r;