From: Tom Hromatka Date: Fri, 28 Jan 2022 21:28:57 +0000 (-0700) Subject: cgxget: libcgroup.so: Add cgroup_cgxget() to library X-Git-Tag: v3.0~216 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9cb59526c49d1d698fecfc141072048be74c090d;p=thirdparty%2Flibcgroup.git cgxget: libcgroup.so: Add cgroup_cgxget() to library Add cgroup_cgxget() to libcgroup.so. Create a new header file, libcgroup/tools.h, to hold C APIs that are exported from the tools folder. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/include/Makefile.am b/include/Makefile.am index 32468357..24e5bac9 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,4 +2,4 @@ nobase_include_HEADERS = libcgroup.h libcgroup/error.h libcgroup/init.h \ libcgroup/groups.h libcgroup/tasks.h \ libcgroup/iterators.h libcgroup/config.h \ - libcgroup/log.h + libcgroup/log.h libcgroup/tools.h diff --git a/include/libcgroup.h b/include/libcgroup.h index 1c7d409a..17ab90ea 100644 --- a/include/libcgroup.h +++ b/include/libcgroup.h @@ -25,6 +25,7 @@ #include #include #include +#include #undef _LIBCGROUP_H_INSIDE diff --git a/include/libcgroup/tools.h b/include/libcgroup/tools.h new file mode 100644 index 00000000..aceea243 --- /dev/null +++ b/include/libcgroup/tools.h @@ -0,0 +1,60 @@ +/** + * Libcgroup tools header file + * + * Copyright (c) 2021-2022 Oracle and/or its affiliates. + * Author: Tom Hromatka + */ + +/* + * This library is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License as + * published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see . + */ +#ifndef _LIBCGROUP_TOOLS_H +#define _LIBCGROUP_TOOLS_H + +#ifndef _LIBCGROUP_H_INSIDE +#error "Only should be included directly." +#endif + +#ifndef SWIG +#include +#include +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Read the setting-value pairs in the cgroup sysfs for param cg. + * cgroup_cgxget() will perform the necessary conversions to match + * the "on-disk" format and then convert the data back to the + * requested version. If successful, cg will be populated with + * the setting-value pairs. + * + * @param cg Input/Output cgroup. Must be initialized and freed by the caller + * @param version Cgroup version of cg If set to CGROUP_UNK, the versions + * stored within each controller will be used. Otherwise this + * value will be used to override the cg param's controller + * versions + * @param ignore_unmappable Ignore failures due to settings that cannot be + * converted from one cgroup version to another + */ +int cgroup_cgxget(struct cgroup ** cg, + enum cg_version_t version, bool ignore_unmappable); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* _LIBCGROUP_TOOLS_H */ diff --git a/src/Makefile.am b/src/Makefile.am index f407f609..2ca2ba5f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,14 +19,20 @@ libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c \ libcgroup-internal.h libcgroup.map wrapper.c log.c \ abstraction-common.c abstraction-common.h \ abstraction-map.c abstraction-map.h \ - abstraction-cpu.c abstraction-cpuset.c + abstraction-cpu.c abstraction-cpuset.c \ + tools/cgxget.c libcgroup_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS) -libcgroup_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC=static +libcgroup_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC=static -DLIBCG_LIB \ + -fPIC libcgroup_la_LDFLAGS = -Wl,--version-script,$(srcdir)/libcgroup.map \ -version-number $(VERSION_NUMBER) noinst_LTLIBRARIES = libcgroupfortesting.la -libcgroupfortesting_la_SOURCES = $(libcgroup_la_SOURCES) +libcgroupfortesting_la_SOURCES = parse.h parse.y lex.l api.c config.c \ + libcgroup-internal.h libcgroup.map wrapper.c log.c \ + abstraction-common.c abstraction-common.h \ + abstraction-map.c abstraction-map.h \ + abstraction-cpu.c abstraction-cpuset.c libcgroupfortesting_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS) libcgroupfortesting_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC= -DUNIT_TEST libcgroupfortesting_la_LDFLAGS = -Wl,--version-script,$(TESTING_MAP_FILE) \ diff --git a/src/libcgroup.map b/src/libcgroup.map index d99a2ce7..fb161dae 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -142,4 +142,5 @@ CGROUP_2.0 { CGROUP_3.0 { cgroup_convert_cgroup; + cgroup_cgxget; } CGROUP_2.0; diff --git a/src/tools/cgxget.c b/src/tools/cgxget.c index 1fae5e87..92115af4 100644 --- a/src/tools/cgxget.c +++ b/src/tools/cgxget.c @@ -37,6 +37,7 @@ #define LL_MAX 100 +#ifndef LIBCG_LIB static struct option const long_options[] = { {"v1", no_argument, NULL, '1'}, @@ -497,6 +498,7 @@ static int parse_opts(int argc, char *argv[], struct cgroup **cg_list[], err: return ret; } +#endif /* !LIBCG_LIB */ static int get_cv_value(struct control_value * const cv, const char * const cg_name, @@ -711,6 +713,7 @@ static int get_cgroup_values(struct cgroup * const cg) return ret; } +#ifndef LIBCG_LIB static int get_values(struct cgroup *cg_list[], int cg_list_len) { int ret = 0; @@ -725,7 +728,8 @@ static int get_values(struct cgroup *cg_list[], int cg_list_len) return ret; } -void print_control_values(const struct control_value * const cv, int mode) +static void print_control_values(const struct control_value * const cv, + int mode) { if (mode & MODE_SHOW_NAMES) printf("%s: ", cv->name); @@ -736,7 +740,8 @@ void print_control_values(const struct control_value * const cv, int mode) printf("%s\n", cv->value); } -void print_controller(const struct cgroup_controller * const cgc, int mode) +static void print_controller(const struct cgroup_controller * const cgc, + int mode) { int i; @@ -768,9 +773,9 @@ static void print_cgroups(struct cgroup *cg_list[], int cg_list_len, int mode) } } -int convert_cgroups(struct cgroup **cg_list[], int cg_list_len, - enum cg_version_t in_version, - enum cg_version_t out_version) +static int convert_cgroups(struct cgroup **cg_list[], int cg_list_len, + enum cg_version_t in_version, + enum cg_version_t out_version) { struct cgroup **cg_converted_list; int i = 0, j, ret = 0; @@ -863,3 +868,54 @@ err: return ret; } +#endif /* !LIBCG_LIB */ + +#ifdef LIBCG_LIB +int cgroup_cgxget(struct cgroup **cg, + enum cg_version_t version, bool ignore_unmappable) +{ + struct cgroup *disk_cg, *out_cg; + int ret; + + if (!cg || !(*cg)) { + ret = ECGINVAL; + goto out; + } + + disk_cg = cgroup_new_cgroup((*cg)->name); + if (!disk_cg) { + ret = ECGCONTROLLERCREATEFAILED; + goto out; + } + + ret = cgroup_convert_cgroup(disk_cg, CGROUP_DISK, *cg, version); + if (ret == ECGNOVERSIONCONVERT && ignore_unmappable) + ret = 0; + else if (ret) + goto out; + + ret = get_cgroup_values(disk_cg); + if (ret) + goto out; + + out_cg = cgroup_new_cgroup((*cg)->name); + if (!out_cg) { + ret = ECGCONTROLLERCREATEFAILED; + goto out; + } + + ret = cgroup_convert_cgroup(out_cg, version, disk_cg, CGROUP_DISK); + if (ret) { + cgroup_free(&out_cg); + goto out; + } + + cgroup_free(cg); + *cg = out_cg; + +out: + if (disk_cg) + cgroup_free(&disk_cg); + return ret; +} +#endif /* LIBCG_LIB */