]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: io.systemd.Unit.List can lookup unit by CGroup
authorIvan Kruglov <mail@ikruglov.com>
Wed, 16 Jul 2025 13:56:59 +0000 (06:56 -0700)
committerIvan Kruglov <mail@ikruglov.com>
Mon, 20 Oct 2025 10:58:39 +0000 (03:58 -0700)
src/core/varlink-unit.c
src/shared/varlink-io.systemd.Unit.c

index 0c4a025b3e14fe0bed903a51d47305cec88f0d6a..5a88bb3840218dd33b079aca5c949b2fbb5f7246 100644 (file)
@@ -10,6 +10,7 @@
 #include "install.h"
 #include "json-util.h"
 #include "manager.h"
+#include "path-util.h"
 #include "pidref.h"
 #include "set.h"
 #include "strv.h"
@@ -356,7 +357,7 @@ static int lookup_unit_by_pidref(sd_varlink *link, Manager *manager, PidRef *pid
 }
 
 typedef struct UnitLookupParameters {
-        const char *name;
+        const char *name, *cgroup;
         PidRef pidref;
 } UnitLookupParameters;
 
@@ -375,9 +376,10 @@ static int varlink_error_no_such_unit(sd_varlink *v, const char *name) {
 static int varlink_error_conflict_lookup_parameters(sd_varlink *v, const UnitLookupParameters *p) {
         log_debug_errno(
                         ESRCH,
-                        "Searching unit by lookup parameters name='%s' pid="PID_FMT" resulted in multiple different units",
+                        "Searching unit by lookup parameters name='%s' pid="PID_FMT" cgroup='%s' resulted in multiple different units",
                         p->name,
-                        p->pidref.pid);
+                        p->pidref.pid,
+                        p->cgroup);
 
         return varlink_error_no_such_unit(v, /* name= */ NULL);
 }
@@ -413,14 +415,28 @@ static int lookup_unit_by_parameters(sd_varlink *link, Manager *manager, UnitLoo
                 unit = pid_unit;
         }
 
+        if (p->cgroup) {
+                if (!path_is_safe(p->cgroup))
+                        return sd_varlink_error_invalid_parameter_name(link, "cgroup");
+
+                Unit *cgroup_unit = manager_get_unit_by_cgroup(manager, p->cgroup);
+                if (!cgroup_unit)
+                        return varlink_error_no_such_unit(link, "cgroup");
+                if (cgroup_unit != unit && unit != NULL)
+                        return varlink_error_conflict_lookup_parameters(link, p);
+
+                unit = cgroup_unit;
+        }
+
         *ret_unit = unit;
         return 0;
 }
 
 int vl_method_list_units(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
         static const sd_json_dispatch_field dispatch_table[] = {
-                { "name", SD_JSON_VARIANT_STRING,        json_dispatch_const_unit_name, offsetof(UnitLookupParameters, name),   0 /* allows UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE */ },
-                { "pid",  _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref,          offsetof(UnitLookupParameters, pidref), SD_JSON_RELAX /* allows PID_AUTOMATIC */            },
+                { "name",         SD_JSON_VARIANT_STRING,        json_dispatch_const_unit_name, offsetof(UnitLookupParameters, name),          0 /* allows UNIT_NAME_PLAIN | UNIT_NAME_INSTANCE */ },
+                { "pid",          _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref,          offsetof(UnitLookupParameters, pidref),        SD_JSON_RELAX /* allows PID_AUTOMATIC */            },
+                { "cgroup",       SD_JSON_VARIANT_STRING,        json_dispatch_const_path,      offsetof(UnitLookupParameters, cgroup),        SD_JSON_STRICT /* require normalized path */        },
                 {}
         };
 
index 3f9e7c40f2b8b4d2e1b58a28c0fef1e07420deb7..b4c7e22b3822839637189df6b05e8a8f689b9454 100644 (file)
@@ -1017,6 +1017,8 @@ static SD_VARLINK_DEFINE_METHOD_FULL(
                 SD_VARLINK_DEFINE_INPUT(name, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("If non-null the PID of a unit. Special value 0 means to take pid of the caller."),
                 SD_VARLINK_DEFINE_INPUT_BY_TYPE(pid, ProcessId, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("If non-null the cgroup of a unit"),
+                SD_VARLINK_DEFINE_INPUT(cgroup, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
                 SD_VARLINK_FIELD_COMMENT("Configuration of the unit"),
                 SD_VARLINK_DEFINE_OUTPUT_BY_TYPE(context, UnitContext, 0),
                 SD_VARLINK_FIELD_COMMENT("Runtime information of the unit"),