cgroup-util: helper to cg_get_attribute and convert to uint64_t
A common pattern in the codebase is reading a cgroup memory value and converting it to a uint64_t. Let's make it a helper and refactor a few places to use it so it's more concise.
This commit is contained in:
@@ -1639,6 +1639,32 @@ int cg_get_attribute(const char *controller, const char *path, const char *attri
|
||||
return read_one_line_file(p, ret);
|
||||
}
|
||||
|
||||
int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
uint64_t v;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
r = cg_get_attribute(controller, path, attribute, &value);
|
||||
if (r == -ENOENT)
|
||||
return -ENODATA;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (streq(value, "max")) {
|
||||
*ret = CGROUP_LIMIT_MAX;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = safe_atou64(value, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cg_get_keyed_attribute(
|
||||
const char *controller,
|
||||
const char *path,
|
||||
|
||||
@@ -184,6 +184,8 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri
|
||||
int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
|
||||
int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, char **keys, char **values);
|
||||
|
||||
int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret);
|
||||
|
||||
int cg_set_access(const char *controller, const char *path, uid_t uid, gid_t gid);
|
||||
|
||||
int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags);
|
||||
|
||||
@@ -120,16 +120,9 @@ uint64_t system_tasks_max(void) {
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
|
||||
else {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
|
||||
r = cg_get_attribute("pids", root, "pids.max", &value);
|
||||
r = cg_get_attribute_as_uint64("pids", root, "pids.max", &b);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
|
||||
else if (!streq(value, "max")) {
|
||||
r = safe_atou64(value, &b);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to parse pids.max attribute of cgroup root, ignoring: %m");
|
||||
}
|
||||
}
|
||||
|
||||
return MIN3(TASKS_MAX,
|
||||
|
||||
Reference in New Issue
Block a user