]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/varlink: do not call io.systemd.oom.ReportManagedOOMCGroups() with empty "cgroup...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Sep 2025 09:38:38 +0000 (18:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Sep 2025 10:50:28 +0000 (19:50 +0900)
Fixes the following varlink IDL validation error:
```
systemd-oomd[1687]: varlink-11-11: Parameters for method io.systemd.oom.ReportManagedOOMCGroups()
  didn't pass validation on field 'cgroups': No anode
```

src/core/varlink.c

index 22312a11f765cce2ad7742035bc4beed1991a8bc..17f5ac4fad95c5b88e3d9720453225d1c28f050f 100644 (file)
@@ -69,16 +69,18 @@ static int build_managed_oom_json_array_element(Unit *u, const char *property, s
                               SD_JSON_BUILD_PAIR_CONDITION(use_duration, "duration", SD_JSON_BUILD_UNSIGNED(c->moom_mem_pressure_duration_usec)));
 }
 
-static int build_managed_oom_cgroups_json(Manager *m, sd_json_variant **ret) {
-        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *arr = NULL;
+static int build_managed_oom_cgroups_json(Manager *m, bool allow_empty, sd_json_variant **ret) {
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *arr = NULL;
         int r;
 
         assert(m);
         assert(ret);
 
-        r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
-        if (r < 0)
-                return r;
+        if (allow_empty) {
+                r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
+                if (r < 0)
+                        return r;
+        }
 
         for (UnitType t = 0; t < _UNIT_TYPE_MAX; t++) {
 
@@ -119,12 +121,17 @@ static int build_managed_oom_cgroups_json(Manager *m, sd_json_variant **ret) {
                 }
         }
 
-        r = sd_json_buildo(&v, SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)));
+        if (!arr) {
+                assert(!allow_empty);
+                *ret = NULL;
+                return 0;
+        }
+
+        r = sd_json_buildo(ret, SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)));
         if (r < 0)
                 return r;
 
-        *ret = TAKE_PTR(v);
-        return 0;
+        return 1;
 }
 
 static int manager_varlink_send_managed_oom_initial(Manager *m) {
@@ -141,8 +148,8 @@ static int manager_varlink_send_managed_oom_initial(Manager *m) {
 
         assert(m->managed_oom_varlink);
 
-        r = build_managed_oom_cgroups_json(m, &v);
-        if (r < 0)
+        r = build_managed_oom_cgroups_json(m, /* allow_empty = */ false, &v);
+        if (r <= 0)
                 return r;
 
         return sd_varlink_send(m->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v);
@@ -251,9 +258,11 @@ int manager_varlink_send_managed_oom_update(Unit *u) {
         if (!c)
                 return 0;
 
-        r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
-        if (r < 0)
-                return r;
+        if (MANAGER_IS_SYSTEM(u->manager)) {
+                r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY);
+                if (r < 0)
+                        return r;
+        }
 
         FOREACH_ELEMENT(i, managed_oom_mode_properties) {
                 _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL;
@@ -267,6 +276,12 @@ int manager_varlink_send_managed_oom_update(Unit *u) {
                         return r;
         }
 
+        if (!arr) {
+                /* There is nothing updated. Skip calling method. */
+                assert(!MANAGER_IS_SYSTEM(u->manager));
+                return 0;
+        }
+
         r = sd_json_buildo(&v, SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)));
         if (r < 0)
                 return r;
@@ -320,7 +335,7 @@ static int vl_method_subscribe_managed_oom_cgroups(
 
         _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
-        r = build_managed_oom_cgroups_json(m, &v);
+        r = build_managed_oom_cgroups_json(m, /* allow_empty = */ true, &v);
         if (r < 0)
                 return r;