From 1c961f8bf67a798d51d88195f89d035012a681d4 Mon Sep 17 00:00:00 2001 From: Michal Sekletar Date: Thu, 27 Feb 2014 17:56:16 +0100 Subject: [PATCH] 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 Based-on: 2d4a39e759c4ab846ad8a546abeddd40bc8d736e --- src/core/dbus-scope.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ src/core/dbus-scope.h | 2 ++ src/core/scope.c | 20 +++++++++++++++--- src/core/scope.h | 2 ++ src/run/run.c | 8 +++++++ src/shared/dbus-common.c | 42 ++++++++++++++++++++++++++++++++++++ src/shared/dbus-common.h | 2 ++ 7 files changed, 128 insertions(+), 3 deletions(-) diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c index 783a969..b576f76 100644 --- a/src/core/dbus-scope.c +++ b/src/core/dbus-scope.c @@ -31,10 +31,12 @@ #define BUS_SCOPE_INTERFACE \ " \n" \ BUS_UNIT_CGROUP_INTERFACE \ + " \n"\ " \n" \ BUS_KILL_CONTEXT_INTERFACE \ BUS_CGROUP_CONTEXT_INTERFACE \ " \n" \ + " \n" \ " \n" #define INTROSPECTION \ @@ -56,6 +58,7 @@ const char bus_scope_interface[] _introspect_("Scope") = BUS_SCOPE_INTERFACE; static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_scope_append_scope_result, scope_result, ScopeResult); static const BusProperty bus_scope_properties[] = { + { "Controller", bus_property_append_string, "s", offsetof(Scope, controller) }, { "TimeoutStopUSec", bus_property_append_usec, "t", offsetof(Scope, timeout_stop_usec) }, { "Result", bus_scope_append_scope_result, "s", offsetof(Scope, result) }, {} @@ -127,6 +130,31 @@ static int bus_scope_set_transient_property( return 1; + } else if (streq(name, "Controller")) { + const char *controller; + + if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) + return -EINVAL; + + dbus_message_iter_get_basic(i, &controller); + + if (!isempty(controller) && !bus_service_name_is_valid(controller)) + return -EINVAL; + + if (mode != UNIT_CHECK) { + char *c = NULL; + + if (!isempty(controller)) { + c = strdup(controller); + if (!c) + return -ENOMEM; + } + + free(s->controller); + s->controller = c; + } + + return 1; } else if (streq(name, "TimeoutStopUSec")) { if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64) @@ -187,3 +215,30 @@ int bus_scope_commit_properties(Unit *u) { unit_realize_cgroup(u); return 0; } + +int bus_scope_send_request_stop(Scope *s) { + _cleanup_dbus_message_unref_ DBusMessage *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; + + m = dbus_message_new_signal(p, + "org.freedesktop.systemd1.Scope", + "RequestStop"); + if (!m) + return 0; + + r = dbus_message_set_destination(m, s->controller); + if (!r) + return 0; + + return dbus_connection_send(UNIT(s)->manager->api_bus, m, NULL); +} diff --git a/src/core/dbus-scope.h b/src/core/dbus-scope.h index e6836f1..34720f2 100644 --- a/src/core/dbus-scope.h +++ b/src/core/dbus-scope.h @@ -30,4 +30,6 @@ DBusHandlerResult bus_scope_message_handler(Unit *u, DBusConnection *c, DBusMess int bus_scope_set_property(Unit *u, const char *name, DBusMessageIter *i, UnitSetPropertiesMode mode, DBusError *error); int bus_scope_commit_properties(Unit *u); +int bus_scope_send_request_stop(Scope *s); + extern const char bus_scope_interface[]; diff --git a/src/core/scope.c b/src/core/scope.c index 41da3b9..e75fc2b 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -64,6 +64,9 @@ static void scope_done(Unit *u) { cgroup_context_done(&s->cgroup_context); + free(s->controller); + s->controller = NULL; + set_free(s->pids); s->pids = NULL; @@ -198,6 +201,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); @@ -205,13 +209,23 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { if (f != SCOPE_SUCCESS) s->result = f; - r = unit_kill_context( + /* 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; + + if (r < 0) + goto fail; + } else + r = 1; if (r > 0) { if (s->timeout_stop_usec > 0) { diff --git a/src/core/scope.h b/src/core/scope.h index 2a3dcb7..b4bafa7 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; Watch timer_watch; diff --git a/src/run/run.c b/src/run/run.c index a6abead..93e3f88 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -315,6 +315,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 = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid()); if (r < 0) return r; diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c index 3ba2d87..8a68708 100644 --- a/src/shared/dbus-common.c +++ b/src/shared/dbus-common.c @@ -1428,3 +1428,45 @@ const char *bus_message_get_sender_with_fallback(DBusMessage *m) { return ":no-sender"; } + +bool bus_service_name_is_valid(const char *p) { + const char *q; + bool dot, found_dot = false, unique; + + if (isempty(p)) + return false; + + unique = p[0] == ':'; + + for (dot = true, q = unique ? p+1 : p; *q; q++) + if (*q == '.') { + if (dot) + return false; + + found_dot = dot = true; + } else { + bool good; + + good = + (*q >= 'a' && *q <= 'z') || + (*q >= 'A' && *q <= 'Z') || + ((!dot || unique) && *q >= '0' && *q <= '9') || + *q == '_' || *q == '-'; + + if (!good) + return false; + + dot = false; + } + + if (q - p > 255) + return false; + + if (dot) + return false; + + if (!found_dot) + return false; + + return true; +} diff --git a/src/shared/dbus-common.h b/src/shared/dbus-common.h index 9752f08..8d01d14 100644 --- a/src/shared/dbus-common.h +++ b/src/shared/dbus-common.h @@ -242,5 +242,7 @@ const char *bus_message_get_sender_with_fallback(DBusMessage *m); void bus_message_unrefp(DBusMessage **reply); +bool bus_service_name_is_valid(const char *p); + #define _cleanup_dbus_message_unref_ __attribute__((cleanup(bus_message_unrefp))) #define _cleanup_dbus_error_free_ __attribute__((cleanup(dbus_error_free)))