]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgxset: libcgroup.so: Add cgroup_cgxset() to library
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 28 Jan 2022 21:29:44 +0000 (14:29 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:42:43 +0000 (14:42 -0700)
Add cgroup_cgxset() to libcgroup.so.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
include/libcgroup/tools.h
src/Makefile.am
src/libcgroup.map
src/tools/cgxset.c

index aceea24354521d14c60d39a52c44cfe1686dea13..c2dbe3e3e7f05e331d7fa7058ffa07336b3c71ea 100644 (file)
@@ -53,6 +53,19 @@ extern "C" {
 int cgroup_cgxget(struct cgroup ** cg,
                  enum cg_version_t version, bool ignore_unmappable);
 
+/**
+ * Write the setting-value pairs in *cg to the cgroup sysfs.
+ * cgroup_cgxset() will perform the necessary conversions to match the
+ * "on-disk" format prior to writing to the cgroup sysfs.
+ *
+ * @param cg cgroup instance that will be written to the cgroup sysfs
+ * @param version Cgroup version of *cg
+ * @param ignore_unmappable Ignore failures due to settings that cannot be
+ *                          converted from one cgroup version to another
+ */
+int cgroup_cgxset(const struct cgroup * const cg,
+                 enum cg_version_t version, bool ignore_unmappable);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index 2ca2ba5fc246445ffb3c1a41788c61a1d4bab7e2..aaecc966439cc9dded1f4f49fe36901478b66e9e 100644 (file)
@@ -20,7 +20,7 @@ libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c \
                       abstraction-common.c abstraction-common.h \
                       abstraction-map.c abstraction-map.h \
                       abstraction-cpu.c abstraction-cpuset.c \
-                      tools/cgxget.c
+                      tools/cgxget.c tools/cgxset.c
 libcgroup_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS)
 libcgroup_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC=static -DLIBCG_LIB \
                      -fPIC
index fb161daeecf20a7151acd665069c9ee0227d571a..3ce7e1b00c582dba9ce9182bc6f11a88b20ed808 100644 (file)
@@ -143,4 +143,5 @@ CGROUP_2.0 {
 CGROUP_3.0 {
        cgroup_convert_cgroup;
        cgroup_cgxget;
+       cgroup_cgxset;
 } CGROUP_2.0;
index 95e7d1c09662435865a3ff5eb48c00d596c9a3a7..a1f606e189bdcafa7b42e71e8167f09fbd0b6c23 100644 (file)
@@ -345,3 +345,41 @@ err:
        return ret;
 }
 #endif /* !UNIT_TEST */
+
+#ifdef LIBCG_LIB
+int cgroup_cgxset(const struct cgroup * const cgroup,
+                 enum cg_version_t version, bool ignore_unmappable)
+{
+       struct cgroup *converted_cgroup;
+       int ret;
+
+       converted_cgroup = cgroup_new_cgroup(cgroup->name);
+       if (converted_cgroup == NULL) {
+               ret = ECGCONTROLLERCREATEFAILED;
+               goto err;
+       }
+
+       ret = cgroup_convert_cgroup(converted_cgroup, CGROUP_DISK,
+                                   cgroup, version);
+       if (ret == ECGNOVERSIONCONVERT && ignore_unmappable)
+               /* The user has specified that we should ignore
+                * any errors due to being unable to map from v1 to
+                * v2 or vice versa
+                */
+               ret = 0;
+       else if (ret)
+               goto err;
+
+       /* modify cgroup based on values of the new one */
+       ret = cgroup_modify_cgroup(converted_cgroup);
+       if (ret) {
+               goto err;
+       }
+
+err:
+       if (converted_cgroup)
+               cgroup_free(&converted_cgroup);
+
+       return ret;
+}
+#endif /* LIBCG_LIB */