From: Tom Hromatka Date: Fri, 28 Jan 2022 21:29:44 +0000 (-0700) Subject: cgxset: libcgroup.so: Add cgroup_cgxset() to library X-Git-Tag: v3.0~215 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a928a8ef0525aa3ac976c3a874e5ae20efc89b05;p=thirdparty%2Flibcgroup.git cgxset: libcgroup.so: Add cgroup_cgxset() to library Add cgroup_cgxset() to libcgroup.so. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/include/libcgroup/tools.h b/include/libcgroup/tools.h index aceea243..c2dbe3e3 100644 --- a/include/libcgroup/tools.h +++ b/include/libcgroup/tools.h @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 2ca2ba5f..aaecc966 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/libcgroup.map b/src/libcgroup.map index fb161dae..3ce7e1b0 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -143,4 +143,5 @@ CGROUP_2.0 { CGROUP_3.0 { cgroup_convert_cgroup; cgroup_cgxget; + cgroup_cgxset; } CGROUP_2.0; diff --git a/src/tools/cgxset.c b/src/tools/cgxset.c index 95e7d1c0..a1f606e1 100644 --- a/src/tools/cgxset.c +++ b/src/tools/cgxset.c @@ -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 */