summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranck Bui <fbui@suse.com>2017-05-02 09:59:17 +0200
committerSven Eden <yamakuzure@gmx.net>2017-07-25 09:46:52 +0200
commitd5aacbb6077b4e45fd36fbff6843595aa64d2288 (patch)
tree6ab62f5396dba5345dfd9b1be625a9ff7d31e0d1
parent4a121b358bb9d90bd50d73a74a1f9f133f479c5c (diff)
core: introduce cg_mask_from_string()/cg_mask_to_string()
-rw-r--r--src/basic/cgroup-util.c75
-rw-r--r--src/basic/cgroup-util.h2
2 files changed, 59 insertions, 18 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index d30edd41b..ec2c52991 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -2265,6 +2265,60 @@ int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root)
}
#endif // 0
+int cg_mask_to_string(CGroupMask mask, char **ret) {
+ const char *controllers[_CGROUP_CONTROLLER_MAX + 1];
+ CGroupController c;
+ int i = 0;
+ char *s;
+
+ assert(ret);
+
+ if (mask == 0) {
+ *ret = NULL;
+ return 0;
+ }
+
+ for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) {
+
+ if (!(mask & CGROUP_CONTROLLER_TO_MASK(c)))
+ continue;
+
+ controllers[i++] = cgroup_controller_to_string(c);
+ controllers[i] = NULL;
+ }
+
+ s = strv_join((char **)controllers, NULL);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ return 0;
+}
+
+int cg_mask_from_string(const char *value, CGroupMask *mask) {
+ assert(mask);
+ assert(value);
+
+ for (;;) {
+ _cleanup_free_ char *n = NULL;
+ CGroupController v;
+ int r;
+
+ r = extract_first_word(&value, &n, NULL, 0);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
+ v = cgroup_controller_from_string(n);
+ if (v < 0)
+ continue;
+
+ *mask |= CGROUP_CONTROLLER_TO_MASK(v);
+ }
+ return 0;
+}
+
int cg_mask_supported(CGroupMask *ret) {
CGroupMask mask = 0;
int r;
@@ -2278,7 +2332,6 @@ int cg_mask_supported(CGroupMask *ret) {
return r;
if (r > 0) {
_cleanup_free_ char *root = NULL, *controllers = NULL, *path = NULL;
- const char *c;
/* In the unified hierarchy we can read the supported
* and accessible controllers from a the top-level
@@ -2296,23 +2349,9 @@ int cg_mask_supported(CGroupMask *ret) {
if (r < 0)
return r;
- c = controllers;
- for (;;) {
- _cleanup_free_ char *n = NULL;
- CGroupController v;
-
- r = extract_first_word(&c, &n, NULL, 0);
- if (r < 0)
- return r;
- if (r == 0)
- break;
-
- v = cgroup_controller_from_string(n);
- if (v < 0)
- continue;
-
- mask |= CGROUP_CONTROLLER_TO_MASK(v);
- }
+ r = cg_mask_from_string(controllers, &mask);
+ if (r < 0)
+ return r;
/* Currently, we support the cpu, memory, io and pids
* controller in the unified hierarchy, mask
diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h
index ea74df4e6..1ea4d3d08 100644
--- a/src/basic/cgroup-util.h
+++ b/src/basic/cgroup-util.h
@@ -246,6 +246,8 @@ int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p);
#endif // 0
int cg_mask_supported(CGroupMask *ret);
+int cg_mask_from_string(const char *s, CGroupMask *ret);
+int cg_mask_to_string(CGroupMask mask, char **ret);
#if 0 /// UNNEEDED by elogind
int cg_kernel_controllers(Set *controllers);