]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
adds lssubsys tool
authorIvana Hutarova Varekova <varekova@redhat.com>
Mon, 24 Aug 2009 09:46:17 +0000 (11:46 +0200)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Sat, 29 Aug 2009 05:20:26 +0000 (10:50 +0530)
Description:
 Show controller, which are mounted/which are on input
 and if option -m is used shows the mount point on which are mounted:

Changes since V1(the previous versions was not enumerated):
*   fix the size of array cont_name - so no allocation and c_max variable
removed
*   change the sequence of processing of controllers list on input (now
first) and internal controller list(first in the previous version), now the
controllers are output in order in which they are put to input
*   fix return values/error messages - now if the controller is not mount
the tool return error - it returns 0 otherwise

Changes since V2
* fix the format of comments which have more than 1 line

Changes since V3
* printf of help -> fprintf(stdout,

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

 -m - show mount points

Examples:
 $lssubsys -m cpuacct
 cpuset,cpuacct /mnt/cgroups/cpuset

 $ lssubsys
 devices
 cpuset,cpuacct

Signed-off-by: Ivana Hutarova Varekova <varekova@redhat.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
src/tools/Makefile.am
src/tools/lssubsys.c [new file with mode: 0644]

index 138b42b94db58bf3f3c23b32d51ed29db27fc119..e2338bdd370f2315164cda5617ec3d5bcdb3828c 100644 (file)
@@ -3,7 +3,7 @@ LDADD = $(top_srcdir)/src/.libs/libcgroup.la
 
 if WITH_TOOLS
 
-bin_PROGRAMS = cgexec cgclassify cgcreate cgset cgdelete
+bin_PROGRAMS = cgexec cgclassify cgcreate cgset cgdelete lssubsys
 
 sbin_PROGRAMS = cgconfigparser cgclear
 
@@ -21,6 +21,8 @@ cgclear_SOURCES = cgclear.c
 
 cgdelete_SOURCES = cgdelete.c tools-common.c tools-common.h
 
+lssubsys_SOURCES = lssubsys.c
+
 install-exec-hook:
        chmod u+s $(DESTDIR)$(bindir)/cgexec
 
diff --git a/src/tools/lssubsys.c b/src/tools/lssubsys.c
new file mode 100644 (file)
index 0000000..959b9bf
--- /dev/null
@@ -0,0 +1,216 @@
+/* " Copyright (C) 2009 Red Hat, Inc. All Rights Reserved.
+ * " Written by Ivana Hutarova Varekova <varekova@redhat.com>
+ *
+ * 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 <getopt.h>
+
+#include <libcgroup.h>
+
+enum flag{
+    FL_MOUNT = 1,
+    FL_LIST = 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 {
+               fprintf(stdout, "Usage: %s [-m] [controller] [...]\n",
+                       program_name);
+               fprintf(stdout, "list all sybsystems of given controller\n");
+               fprintf(stdout, "  -h, --help           Display this help\n");
+               fprintf(stdout, "  -m, --mount-points   Display mount points\n");
+       }
+}
+
+/* print data about input cont_name controller */
+int print_controller(cont_name_t cont_name, int flags)
+{
+       int ret = 0;
+       char name[FILENAME_MAX];
+       char path[FILENAME_MAX];
+       void *handle;
+       struct cgroup_mount_point controller;
+       int output = 0;
+
+       ret = cgroup_get_controller_begin(&handle, &controller);
+
+       path[0] = '\0';
+       name[0] = '\0';
+
+       /* go through the list of controllers */
+       while (ret == 0) {
+               if (strcmp(path, controller.path) == 0) {
+                       /* if it is still the same mount point */
+                       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);
+                               if ((flags & FL_LIST) != 0) {
+                                       /* we succesfully finish printing */
+                                       output = 0;
+                                       break;
+                               }
+                       }
+
+                       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_LIST) ||
+                       (strcmp(controller.name, cont_name) == 0)))
+                       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 ((flags & FL_LIST) != 0)
+                       ret = 0;
+       }
+
+       cgroup_get_controller_end(&handle);
+       return ret;
+
+}
+
+/* list the controllers */
+int cgroup_list_controllers(char *tname,
+       cont_name_t cont_name[CG_CONTROLLER_MAX], int flags)
+{
+       int ret = 0;
+       int final_ret = 0;
+       int i = 0;
+
+       /* initialize libcgroup */
+       ret = cgroup_init();
+       if (ret) {
+               fprintf(stderr, "controllers can't be listed: %s\n",
+                       cgroup_strerror(ret));
+               return ret;
+       }
+
+       if ((flags & FL_LIST) == 0) {
+               /* we have to print all controllers */
+               ret = print_controller(NULL, flags);
+               if (ret == ECGEOF)
+                       final_ret = 0;
+               else
+                       fprintf(stderr, "controllers can't be listed: %s\n",
+                               cgroup_strerror(ret));
+       } else
+               /* we have he list of controllers which should be print */
+               while ((i < CG_CONTROLLER_MAX) && (cont_name[i][0] != '\0')
+                       && ((ret == ECGEOF) || (ret == 0))) {
+                       ret = print_controller(cont_name[i], flags);
+                       if (ret != 0) {
+                               if (ret == ECGEOF)
+                                       /* controller was not found */
+                                       final_ret = ECGFAIL;
+                               else
+                                       /* other problem */
+                                       final_ret = ret;
+                               fprintf(stderr,
+                                       "%s: cannot find group %s: %s\n",
+                                       tname, cont_name[i],
+                                       cgroup_strerror(final_ret));
+                       }
+                       i++;
+               }
+
+       return final_ret;
+}
+
+int main(int argc, char *argv[])
+{
+
+       int ret = 0;
+       int c;
+
+       int flags = 0;
+
+       int i;
+       int c_number = 0;
+       cont_name_t cont_name[CG_CONTROLLER_MAX];
+
+       static struct option options[] = {
+               {"help", 0, 0, 'h'},
+               {"mount-points", 0, 0, 'm'},
+               {0, 0, 0, 0}
+       };
+
+       for (i = 0; i < CG_CONTROLLER_MAX; i++)
+               cont_name[i][0] = '\0';
+
+       /* parse arguments */
+       while ((c = getopt_long(argc, argv, "mh", options, NULL)) > 0) {
+               switch (c) {
+               case 'h':
+                       usage(0, argv[0]);
+                       return 0;
+               case 'm':
+                       flags |= FL_MOUNT;
+                       break;
+               default:
+                       usage(1, argv[0]);
+                       return -1;
+               }
+       }
+
+       /* read the list of controllers */
+       while (optind < argc) {
+
+               flags |= FL_LIST;
+               strncpy(cont_name[c_number], argv[optind], FILENAME_MAX);
+               cont_name[c_number][FILENAME_MAX-1] = '\0';
+               c_number++;
+               optind++;
+               if (optind == CG_CONTROLLER_MAX) {
+                       fprintf(stderr, "too much parameters\n");
+                       break;
+               }
+       }
+
+       /*
+        * print the information
+        * based on list of input controllers and flags
+        */
+       ret = cgroup_list_controllers(argv[0], cont_name, flags);
+
+       return ret;
+}