cgclear should be able to 'uload' cgroups (and mounts) as specified in
given config file - as exact opposite of cgconfigparser.

This patch adds new options '-c <config_file>' and '-C <directory with config
files> just for this purpose. These options can be specified multiple times.

Signed-off-by: Jan Safranek <jsafr...@redhat.com>
---

 src/tools/Makefile.am |    2 -
 src/tools/cgclear.c   |  106 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index 92a0078..b825dff 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -20,7 +20,7 @@ cgget_SOURCES = cgget.c tools-common.c tools-common.h
 
 cgconfigparser_SOURCES = cgconfig.c
 
-cgclear_SOURCES = cgclear.c
+cgclear_SOURCES = cgclear.c tools-common.c tools-common.h
 
 cgdelete_SOURCES = cgdelete.c tools-common.c tools-common.h
 
diff --git a/src/tools/cgclear.c b/src/tools/cgclear.c
index c286272..1dc3e96 100644
--- a/src/tools/cgclear.c
+++ b/src/tools/cgclear.c
@@ -21,20 +21,114 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <getopt.h>
+#include "tools-common.h"
 
-int main(int argc, char *argv[])
+static struct cgroup_string_list cfg_files;
+
+static void usage(int status, const char *program_name)
 {
-       int error;
+       if (status != 0) {
+               fprintf(stderr, "Wrong input parameters,"
+                       " try %s -h' for more information.\n",
+                       program_name);
+       } else {
+               printf("%s [-e] [-l config file] [-L directory] ...",
+                               program_name);
+       }
+}
 
-       error = cgroup_unload_cgroups();
+static void report_error(int error, const char *program_name)
+{
        /* Don't spit an error when there is nothing to clear. */
        if (error == ECGROUPNOTMOUNTED)
                error = 0;
-
        if (error) {
-               printf("%s failed with %s\n", argv[0], cgroup_strerror(error));
-               exit(3);
+               printf("%s failed with %s\n", program_name,
+                               cgroup_strerror(error));
+       }
+}
+
+
+int main(int argc, char *argv[])
+{
+       int error = 0, ret;
+       int c;
+       int unload_all = 1;
+       int flags = CGFLAG_DELETE_RECURSIVE;
+
+       struct option longopts[] = {
+                       {"load", required_argument, 0,  'l' },
+                       {"load-directory", required_argument, 0,  'L' },
+                       {"only-empty", no_argument, 0,  'e' },
+                       { 0, 0, 0, 0}
+       };
+
+       ret = cgroup_string_list_init(&cfg_files, argc/2);
+       if (ret) {
+               fprintf(stderr, "%s: cannot initialize list of files,"
+                               " out of memory?\n",
+                               argv[0]);
+               exit(1);
+       }
+
+       while ((c = getopt_long(argc, argv, "hl:L:e", longopts, NULL)) > 0) {
+               switch (c) {
+               case 'e':
+                       flags = CGFLAG_DELETE_EMPTY_ONLY;
+                       break;
+
+               case 'l':
+                       unload_all = 0;
+                       ret = cgroup_string_list_add_item(&cfg_files, optarg);
+                       if (ret) {
+                               fprintf(stderr, "%s: cannot add file to list,"\
+                                               " out of memory?\n", argv[0]);
+                               exit(1);
+                       }
+                       break;
+
+               case 'L':
+                       unload_all = 0;
+                       cgroup_string_list_add_directory(&cfg_files, optarg,
+                                       argv[0]);
+                       break;
+
+               case 'h':
+                       usage(0, argv[0]);
+                       exit(0);
+               default:
+                       usage(1, argv[0]);
+                       exit(1);
+               }
+       }
+
+       if (unload_all) {
+               error = cgroup_unload_cgroups();
+               if (error)
+                       report_error(error, argv[0]);
+       } else {
+               int i;
+
+               ret = cgroup_init();
+               if (ret) {
+                       report_error(ret, argv[0]);
+                       exit(4);
+               }
+               /* process the config files in reverse order */
+               for (i = cfg_files.count-1; i >= 0 ; i--) {
+                       ret = cgroup_config_unload_config(cfg_files.items[i],
+                                       flags);
+                       if (ret) {
+                               report_error(ret, argv[0]);
+                               if (!error)
+                                       error = ret;
+                       }
+               }
        }
+       cgroup_string_list_free(&cfg_files);
+       if (error)
+               exit(3);
 
        return 0;
 }


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to