]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgxget: libcgroup.so: Add cgroup_cgxget() to library
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 28 Jan 2022 21:28:57 +0000 (14:28 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:42:43 +0000 (14:42 -0700)
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 <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
include/Makefile.am
include/libcgroup.h
include/libcgroup/tools.h [new file with mode: 0644]
src/Makefile.am
src/libcgroup.map
src/tools/cgxget.c

index 32468357ff164d66c31e1f880afa106f0a0fdc87..24e5bac91d3e3ab4aba0dd51a11a11ad7c846d33 100644 (file)
@@ -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
index 1c7d409a7ab10972254f426c3f8e19cdf569a585..17ab90eaca5b7493e60fe209d2cb14cc8fd121f9 100644 (file)
@@ -25,6 +25,7 @@
 #include <libcgroup/tasks.h>
 #include <libcgroup/config.h>
 #include <libcgroup/log.h>
+#include <libcgroup/tools.h>
 
 #undef _LIBCGROUP_H_INSIDE
 
diff --git a/include/libcgroup/tools.h b/include/libcgroup/tools.h
new file mode 100644 (file)
index 0000000..aceea24
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * Libcgroup tools header file
+ *
+ * Copyright (c) 2021-2022 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hromatka@oracle.com>
+ */
+
+/*
+ * 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 <http://www.gnu.org/licenses>.
+ */
+#ifndef _LIBCGROUP_TOOLS_H
+#define _LIBCGROUP_TOOLS_H
+
+#ifndef _LIBCGROUP_H_INSIDE
+#error "Only <libcgroup.h> should be included directly."
+#endif
+
+#ifndef SWIG
+#include <features.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#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 */
index f407f609ca73336949688a220d8f03cdbc2d9ebc..2ca2ba5fc246445ffb3c1a41788c61a1d4bab7e2 100644 (file)
@@ -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) \
index d99a2ce73c044c65c39c8138061be52bbd62027d..fb161daeecf20a7151acd665069c9ee0227d571a 100644 (file)
@@ -142,4 +142,5 @@ CGROUP_2.0 {
 
 CGROUP_3.0 {
        cgroup_convert_cgroup;
+       cgroup_cgxget;
 } CGROUP_2.0;
index 1fae5e875471a59cfb369004f79a8a40190b073c..92115af482b4dba92e06de20e7f0ca24bf742bee 100644 (file)
@@ -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 */