1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-28 11:55:44 +03:00

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
This commit is contained in:
Lennart Poettering 2014-01-31 17:45:13 +01:00
parent fb818b2ea1
commit 2d4a39e759
5 changed files with 86 additions and 7 deletions

View File

@ -26,13 +26,16 @@
#include "dbus-kill.h" #include "dbus-kill.h"
#include "dbus-scope.h" #include "dbus-scope.h"
#include "bus-util.h" #include "bus-util.h"
#include "bus-internal.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult); static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
const sd_bus_vtable bus_scope_vtable[] = { const sd_bus_vtable bus_scope_vtable[] = {
SD_BUS_VTABLE_START(0), 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("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_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 SD_BUS_VTABLE_END
}; };
@ -86,6 +89,32 @@ static int bus_scope_set_transient_property(
return 1; 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")) { } else if (streq(name, "TimeoutStopUSec")) {
if (mode != UNIT_CHECK) { if (mode != UNIT_CHECK) {
@ -145,3 +174,29 @@ int bus_scope_commit_properties(Unit *u) {
unit_realize_cgroup(u); unit_realize_cgroup(u);
return 0; 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);
}

View File

@ -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_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_commit_properties(Unit *u);
int bus_scope_send_request_stop(Scope *s);

View File

@ -64,6 +64,8 @@ static void scope_done(Unit *u) {
cgroup_context_done(&s->cgroup_context); cgroup_context_done(&s->cgroup_context);
free(s->controller);
set_free(s->pids); set_free(s->pids);
s->pids = NULL; 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) { static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
bool skip_signal = false;
int r; int r;
assert(s); assert(s);
@ -224,13 +227,22 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
if (f != SCOPE_SUCCESS) if (f != SCOPE_SUCCESS)
s->result = f; s->result = f;
r = unit_kill_context( /* If we have a controller set let's ask the controller nicely
UNIT(s), * to terminate the scope, instead of us going directly into
&s->kill_context, * SIGTERM beserk mode */
state != SCOPE_STOP_SIGTERM, if (state == SCOPE_STOP_SIGTERM)
-1, -1, false); skip_signal = bus_scope_send_request_stop(s) > 0;
if (r < 0)
goto fail; 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) { if (r > 0) {
r = scope_arm_timer(s); r = scope_arm_timer(s);

View File

@ -55,6 +55,8 @@ struct Scope {
usec_t timeout_stop_usec; usec_t timeout_stop_usec;
char *controller;
Set *pids; Set *pids;
sd_event_source *timer_event_source; sd_event_source *timer_event_source;

View File

@ -356,6 +356,14 @@ static int start_transient_scope(
if (r < 0) if (r < 0)
return r; 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); r = message_start_transient_unit_send(bus, m, error, NULL);
if (r < 0) if (r < 0)
return r; return r;