From 267dd427da9f7a215db772a9ac82656dcae683fb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 25 Apr 2018 13:34:47 +0200 Subject: [PATCH] core: add a new GetUnitByControlGroup() bus call This is useful for foreign container runtimes implementing the OCI runtime spec, which only wants to deal with cgroup paths. There's already an API to translate units into cgroup paths, with this we add the reverse. --- src/core/dbus-manager.c | 27 ++++++++++++++++++++++++++ src/core/org.freedesktop.systemd1.conf | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index de3f9db4450..d5a9f524eae 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -544,6 +544,32 @@ static int method_get_unit_by_invocation_id(sd_bus_message *message, void *userd return sd_bus_reply_method_return(message, "o", path); } +static int method_get_unit_by_control_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_free_ char *path = NULL; + Manager *m = userdata; + const char *cgroup; + Unit *u; + int r; + + r = sd_bus_message_read(message, "s", &cgroup); + if (r < 0) + return r; + + u = manager_get_unit_by_cgroup(m, cgroup); + if (!u) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Control group '%s' is not valid or not managed by this instance", cgroup); + + r = mac_selinux_unit_access_check(u, message, "status", error); + if (r < 0) + return r; + + path = unit_dbus_path(u); + if (!path) + return -ENOMEM; + + return sd_bus_reply_method_return(message, "o", path); +} + static int method_load_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_free_ char *path = NULL; Manager *m = userdata; @@ -2539,6 +2565,7 @@ const sd_bus_vtable bus_manager_vtable[] = { SD_BUS_METHOD("GetUnit", "s", "o", method_get_unit, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetUnitByPID", "u", "o", method_get_unit_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetUnitByInvocationID", "ay", "o", method_get_unit_by_invocation_id, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("GetUnitByControlGroup", "s", "o", method_get_unit_by_control_group, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("LoadUnit", "s", "o", method_load_unit, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("StartUnit", "ss", "o", method_start_unit, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("StartUnitReplace", "sss", "o", method_start_unit_replace, SD_BUS_VTABLE_UNPRIVILEGED), diff --git a/src/core/org.freedesktop.systemd1.conf b/src/core/org.freedesktop.systemd1.conf index 5e72fe7e8e7..0114128a573 100644 --- a/src/core/org.freedesktop.systemd1.conf +++ b/src/core/org.freedesktop.systemd1.conf @@ -60,6 +60,10 @@ send_interface="org.freedesktop.systemd1.Manager" send_member="GetUnitByInvocationID"/> + +