summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Safranek <jsafrane@redhat.com>2009-12-22 09:15:10 +0100
committerJan Safranek <jsafrane@redhat.com>2009-12-22 09:15:10 +0100
commitfae7c60f41702ff04ccaf81c8825bb63ba1393e7 (patch)
tree8a03a18332c9ee08d949c111cb1c66ba87fdbdbf
parent2f9ed3dcd9cc3a7b73f42a8b85cd7968b36bf01c (diff)
downloadlibcg-fae7c60f41702ff04ccaf81c8825bb63ba1393e7.tar.gz
libcg-fae7c60f41702ff04ccaf81c8825bb63ba1393e7.tar.xz
libcg-fae7c60f41702ff04ccaf81c8825bb63ba1393e7.zip
cgget tool
Add a tool to query for parameter values. The syntax is very similar to cgset, including IMHO ugly group specification without list of controllers. Usage: cgget [-nv] -r <param. name> [-r <param. name>] [...] <path to group> [...] -n - no headers -v - print values only (no parameter names) Example: $ cgget -r cpuset.cpus / /second /: cpuset.cpus=0-1 /second: cpuset.cpus=1 $ cgget -nv -r cpuset.cpus / /second 0-1 1 Signed-off-by: Jan Safranek <jsafrane@redhat.com>
-rw-r--r--src/tools/Makefile.am4
-rw-r--r--src/tools/cgget.c194
2 files changed, 197 insertions, 1 deletions
diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index ccfeeae..f755037 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -3,7 +3,7 @@ LDADD = $(top_builddir)/src/.libs/libcgroup.la
if WITH_TOOLS
-bin_PROGRAMS = cgexec cgclassify cgcreate cgset cgdelete lssubsys lscgroup
+bin_PROGRAMS = cgexec cgclassify cgcreate cgset cgget cgdelete lssubsys lscgroup
sbin_PROGRAMS = cgconfigparser cgclear
@@ -15,6 +15,8 @@ cgcreate_SOURCES = cgcreate.c tools-common.c tools-common.h
cgset_SOURCES = cgset.c tools-common.c tools-common.h
+cgget_SOURCES = cgget.c tools-common.c tools-common.h
+
cgconfigparser_SOURCES = cgconfig.c
cgclear_SOURCES = cgclear.c
diff --git a/src/tools/cgget.c b/src/tools/cgget.c
new file mode 100644
index 0000000..13b7764
--- /dev/null
+++ b/src/tools/cgget.c
@@ -0,0 +1,194 @@
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+
+#include "tools-common.h"
+
+#define MODE_SHOW_HEADERS 1
+#define MODE_SHOW_NAMES 2
+
+void usage(int status, char *program_name)
+{
+ if (status != 0)
+ fprintf(stderr, "Wrong input parameters,"
+ " try %s -h' for more information.\n",
+ program_name);
+ else {
+ printf("Usage: %s [-nv] -r <name> [-r <name>] ... <path> ...\n",
+ program_name);
+ }
+}
+
+int display_values(char **names, int count, const char* group_name,
+ const char *program_name, int mode)
+{
+ int i;
+ struct cgroup_controller *group_controller = NULL;
+ struct cgroup *group = NULL;
+ int ret = 0;
+ char *controller = NULL, *parameter = NULL;
+ char *value = NULL;
+
+ if (mode & MODE_SHOW_HEADERS)
+ printf("%s:\n", group_name);
+
+ group = cgroup_new_cgroup(group_name);
+ if (group == NULL) {
+ fprintf(stderr, "%s: cannot create group '%s'\n", program_name,
+ group_name);
+ return -1;
+ }
+ ret = cgroup_get_cgroup(group);
+ if (ret != 0) {
+ fprintf(stderr, "%s: cannot read group '%s': %s\n",
+ program_name, group_name, cgroup_strerror(ret));
+ goto err;
+ }
+
+ for (i = 0; i < count; i++) {
+ /* Parse the controller out of 'controller.parameter'. */
+ free(controller);
+ controller = strdup(names[i]);
+ if (controller == NULL) {
+ fprintf(stderr, "%s: out of memory\n", program_name);
+ ret = -1;
+ goto err;
+ }
+ parameter = strchr(controller, '.');
+ if (parameter == NULL) {
+ fprintf(stderr, "%s: error parsing parameter name\n" \
+ " '%s'", program_name, names[i]);
+ ret = -1;
+ goto err;
+ }
+ *parameter = '\0';
+ parameter++;
+
+ /* Find appropriate controller. */
+ group_controller = cgroup_get_controller(group, controller);
+ if (group_controller == NULL) {
+ fprintf(stderr, "%s: cannot find controller " \
+ "'%s' in group '%s'\n", program_name,
+ controller, group_name);
+ ret = -1;
+ goto err;
+ }
+
+ /*
+ * Finally read the parameter value.
+ */
+ ret = cgroup_get_value_string(group_controller, names[i],
+ &value);
+ if (ret != 0) {
+ fprintf(stderr, "%s: cannot read parameter '%s.%s' "\
+ "from group '%s': %s\n", program_name,
+ controller, parameter, group_name,
+ cgroup_strerror(ret));
+ goto err;
+ }
+ if (mode & MODE_SHOW_NAMES)
+ printf("%s.%s=", controller, parameter);
+ printf("%s\n", value);
+ free(value);
+ }
+err:
+ if (controller)
+ free(controller);
+ if (group)
+ cgroup_free(&group);
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ int ret = 0;
+ int c, i;
+
+ char **names = NULL;
+ int n_number = 0;
+ int n_max = 0;
+ int mode = MODE_SHOW_NAMES | MODE_SHOW_HEADERS;
+
+ /* No parameter on input? */
+ if (argc < 2) {
+ usage(1, argv[0]);
+ return 1;
+ }
+
+ /* Parse arguments. */
+ while ((c = getopt(argc, argv, "r:hnv")) != -1) {
+ switch (c) {
+ case 'h':
+ usage(0, argv[0]);
+ ret = 0;
+ goto err;
+ break;
+
+ case 'n':
+ /* Do not show headers. */
+ mode = mode & (INT_MAX ^ MODE_SHOW_HEADERS);
+ break;
+
+ case 'v':
+ /* Do not show parameter names. */
+ mode = mode & (INT_MAX ^ MODE_SHOW_NAMES);
+ break;
+
+ case 'r':
+ /* Add name to buffer. */
+ if (n_number >= n_max) {
+ n_max += CG_NV_MAX;
+ names = (char **) realloc(names,
+ n_max * sizeof(char *));
+ if (!names) {
+ fprintf(stderr, "%s: "
+ "not enough memory\n", argv[0]);
+ ret = -1;
+ goto err;
+ }
+ }
+
+ names[n_number] = optarg;
+ n_number++;
+ break;
+ default:
+ usage(1, argv[0]);
+ ret = 1;
+ goto err;
+ break;
+ }
+ }
+
+ if (!argv[optind]) {
+ fprintf(stderr, "%s: no cgroup specified\n", argv[0]);
+ ret = -1;
+ goto err;
+ }
+
+ /* Initialize libcgroup. */
+ ret = cgroup_init();
+ if (ret) {
+ fprintf(stderr, "%s: libcgroup initialization failed: %s\n",
+ argv[0], cgroup_strerror(ret));
+ goto err;
+ }
+
+ /* Parse control groups and print them .*/
+ for (i = optind; i < argc; i++) {
+ ret = display_values(names, n_number, argv[i], argv[0], mode);
+ if (ret)
+ goto err;
+ /* Separate each group with empty line. */
+ if (mode & MODE_SHOW_HEADERS && i != argc-1)
+ printf("\n");
+ }
+
+err:
+ free(names);
+ return ret;
+}