cgroup-util: add cg_get_keyed_attribute_uint64() helper

Multiple callers of cg_get_keyed_attribute() follow the same pattern of
reading a single keyed attribute and then parsing it as uint64_t with
safe_atou64(). Add a helper that combines both steps.

Convert all existing single-key + uint64 call sites in cgtop, cgroup.c,
and oomd-util.c to use the new helper.
This commit is contained in:
Yaping Li
2026-04-09 14:45:05 -07:00
parent 454d9c1be1
commit 6fe4a16f36
5 changed files with 31 additions and 35 deletions
+18
View File
@@ -1560,6 +1560,24 @@ fail:
return r;
}
int cg_get_keyed_attribute_uint64(const char *path, const char *attribute, const char *key, uint64_t *ret) {
_cleanup_free_ char *val = NULL;
int r;
assert(key);
assert(ret);
r = cg_get_keyed_attribute(path, attribute, STRV_MAKE(key), &val);
if (r < 0)
return r;
r = safe_atou64(val, ret);
if (r < 0)
return log_debug_errno(r, "Failed to parse value '%s' of key '%s' in cgroup attribute '%s': %m", val, key, attribute);
return 0;
}
int cg_mask_to_string(CGroupMask mask, char **ret) {
_cleanup_free_ char *s = NULL;
bool space = false;
+1
View File
@@ -165,6 +165,7 @@ int cg_get_attribute_as_uint64(const char *path, const char *attribute, uint64_t
int cg_get_attribute_as_bool(const char *path, const char *attribute);
int cg_get_keyed_attribute(const char *path, const char *attribute, char * const *keys, char **values);
int cg_get_keyed_attribute_uint64(const char *path, const char *attribute, const char *key, uint64_t *ret);
int cg_get_owner(const char *path, uid_t *ret_uid);