Fixed overlooked bug - This patch adds lscgroup tool.

Description:
Show controller, which are mounted/which are on input
and if option -m is used shows the mount point on which are mounted:

Syntax:
lscgroup [-m] [controller1] [controller2] [...]

-m - show mount points

Examples:

$lscgroup -m cpuacct
cpuset,cpuacct /mnt/cgroups/cpuset

$ lscgroup
devices
cpuset,cpuacct


Signed-off-by: Ivana Hutarova Varekova <[email protected]>
---

 src/tools/Makefile.am |    4 +
 src/tools/lscgroup.c  |  193 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 196 insertions(+), 1 deletions(-)
 create mode 100644 src/tools/lscgroup.c

diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index 1c2b8e7..c5ec464 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -3,7 +3,7 @@ LDADD = $(top_srcdir)/src/.libs/libcgroup.la
 
 if WITH_TOOLS
 
-bin_PROGRAMS = cgexec cgclassify cgcreate cgset
+bin_PROGRAMS = cgexec cgclassify cgcreate cgset lscgroup
 
 sbin_PROGRAMS = cgconfigparser cgclear
 
@@ -19,6 +19,8 @@ cgconfigparser_SOURCES = cgconfig.c
 
 cgclear_SOURCES = cgclear.c
 
+lscgroup_SOURCES = lscgroup.c
+
 install-exec-hook:
        chmod u+s $(DESTDIR)$(bindir)/cgexec
 
diff --git a/src/tools/lscgroup.c b/src/tools/lscgroup.c
new file mode 100644
index 0000000..dcb2fbc
--- /dev/null
+++ b/src/tools/lscgroup.c
@@ -0,0 +1,193 @@
+/* " Copyright (C) 2009 Red Hat, Inc. All Rights Reserved.
+ * " Written by Ivana Hutarova Varekova <[email protected]>
+ *
+ * This program 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 program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libcgroup.h>
+
+#define CG_C_MAX 10
+
+/* shouw mount points */
+#define FL_MOUNT       1
+/* show all mounted cgroups */
+#define FL_NO          2
+
+typedef char cont_name_t[FILENAME_MAX];
+
+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 [-m] [controller] [...]\n",
+                       program_name);
+       }
+}
+
+/* is controller "name" on list of controllers cont_name? */
+int cont_is_on_list(cont_name_t *cont_name,
+       int c_number, char name[FILENAME_MAX])
+{
+       int i;
+
+       for (i = 0; i < c_number; i++)
+               if (strcmp(name, cont_name[i]) == 0)
+                       return 1;
+
+       return 0;
+}
+
+/* list the controllers */
+int cgroup_list_controllers(cont_name_t *cont_name, int c_number, int flags)
+{
+       int ret = 0;
+       void *handle;
+       struct cgroup_mount_point controller;
+       char name[FILENAME_MAX];
+       char path[FILENAME_MAX];
+       int output = 0;
+
+       strcpy(path, "");
+       strcpy(name, "");
+       ret = cgroup_get_controller_begin(&handle, &controller);
+
+       /* go through the list of controllers */
+       while (ret == 0) {
+
+               /* if it is still the same mount point */
+               if (strcmp(path, controller.path) == 0) {
+                       strncat(name, "," , FILENAME_MAX-strlen(name)-1);
+                       strncat(name, controller.name,
+                               FILENAME_MAX-strlen(name)-1);
+               } else {
+               /* we got new mount point, print the old one if needed */
+                       if (output) {
+                               if ((flags &  FL_MOUNT) != 0)
+                                       printf("%s %s\n", name, path);
+                               else
+                                       printf("%s \n", name);
+                       }
+
+                       output = 0;
+                       strncpy(name, controller.name, FILENAME_MAX);
+                       name[FILENAME_MAX-1] = '\0';
+                       strncpy(path, controller.path, FILENAME_MAX);
+                       path[FILENAME_MAX-1] = '\0';
+               }
+
+               /* set output flag */
+               if ((!output) && ((flags & FL_NO) ||
+                       (cont_is_on_list(cont_name, c_number,
+                               controller.name))))
+                       output = 1;
+
+               /* the actual controller should not be printed */
+               ret = cgroup_get_controller_next(&handle, &controller);
+       }
+
+       if (output) {
+               if ((flags &  FL_MOUNT) != 0)
+                       printf("%s %s\n", name, path);
+               else
+                       printf("%s \n", name);
+       }
+
+       if ((ret != NULL) && (ret != ECGEOF))
+               return ret;
+
+       ret = cgroup_get_controller_end(&handle);
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+
+       int ret = 0;
+       int c;
+
+       int flags = 0;
+
+       cont_name_t *cont_name = NULL;
+       int c_number = 0;
+       int c_max = 0;
+
+       /* parse arguments */
+       while ((c = getopt(argc, argv, "mh")) != -1) {
+               switch (c) {
+               case 'h':
+                       usage(0, argv[0]);
+                       return 0;
+                       break;
+               case 'm':
+                       flags |= FL_MOUNT;
+                       break;
+               default:
+                       usage(1, argv[0]);
+                       return -1;
+                       break;
+               }
+       }
+
+       /* no argument specified -> show all controllers */
+       if (argv[optind] == NULL) {
+               flags |= FL_NO;
+               goto controllers_set;
+       }
+
+       /* read the list of controllers */
+       while (argv[optind]) {
+
+               /* no free space for new controller name */
+               if (c_number >= c_max) {
+                       c_max += CG_C_MAX;
+                       cont_name = (cont_name_t *)realloc(cont_name,
+                               c_max * sizeof(cont_name_t));
+                       if (!cont_name) {
+                               fprintf(stderr, "%s: not enough memory\n",
+                                       argv[0]);
+                               ret = -1;
+                               goto err;
+                       }
+               }
+
+               strncpy(cont_name[c_number], argv[optind], FILENAME_MAX);
+               cont_name[c_number][FILENAME_MAX-1] = '\0';
+               c_number++;
+               /* TODO: check whether each controller is used max. once  */
+               optind++;
+       }
+
+controllers_set:
+       /* initialize libcgroup */
+       ret = cgroup_init();
+       if (ret) {
+               fprintf(stderr, "%s: libcgroup initialization failed: %s\n",
+                       argv[0], cgroup_strerror(ret));
+               goto err;
+       }
+
+       /* print the information, based on list of input controllers and flags*/
+       ret = cgroup_list_controllers(cont_name, c_number, flags);
+       if (ret) {
+               fprintf(stderr, "%s: controllers can't be listed: %s\n",
+                       argv[0], cgroup_strerror(ret));
+       }
+
+err:
+       free(cont_name);
+       return ret;
+}


------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to