]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgconfigparser: Allow multiple config files
authorJan Safranek <jsafrane@redhat.com>
Fri, 23 Sep 2011 11:53:40 +0000 (13:53 +0200)
committerJan Safranek <jsafrane@redhat.com>
Thu, 3 Nov 2011 09:12:47 +0000 (10:12 +0100)
cgconfigparser can now parse multiple config files, including
(alphabetitcally sorted) files from a directory.

Signed-off-by: Jan Safranek <jsafrane@redhat.com>
src/tools/Makefile.am
src/tools/cgconfig.c

index b825dffc6f3a250c8148a15cf81349898f2cf511..4cb02ff7f0d372beff796a1e844d2322e4f6a24a 100644 (file)
@@ -18,7 +18,7 @@ cgset_SOURCES = cgset.c tools-common.c tools-common.h
 
 cgget_SOURCES = cgget.c tools-common.c tools-common.h
 
-cgconfigparser_SOURCES = cgconfig.c
+cgconfigparser_SOURCES = cgconfig.c tools-common.c tools-common.h
 
 cgclear_SOURCES = cgclear.c tools-common.c tools-common.h
 
index e426b760685577760d6455b195db87d04f596c21..cc2a633eda3f13e59383ee9f75764167ffbaa5e2 100644 (file)
 #include <stdlib.h>
 #include <errno.h>
 #include <getopt.h>
+#include "tools-common.h"
+
+static struct cgroup_string_list cfg_files;
 
 
 static void usage(char *progname)
 {
-       printf("Usage: %s [OPTION] [FILE]\n", basename(progname));
+       printf("Usage: %s [-l FILE] ...\n", basename(progname));
        printf("Parse and load the specified cgroups configuration file\n");
        printf("\n");
-       printf("  -h, --help            Display this help\n");
-       printf("  -l, --load=FILE       Parse and load the cgroups configuration file\n");
+       printf("  -h, --help                    Display this help\n");
+       printf("  -l, --load=FILE               Parse and load the cgroups"\
+                       " configuration file\n");
+       printf("  -L, --load-directory=DIR      Parse and load the cgroups"\
+                       " configuration files from a directory\n");
        exit(2);
 }
 
 int main(int argc, char *argv[])
 {
-       int c;
-       char filename[PATH_MAX];
-       int ret;
+       int c, i;
+       int ret, error = 0;
        static struct option options[] = {
                {"help", 0, 0, 'h'},
                {"load", 1, 0, 'l'},
+               {"load-directory", 1, 0, 'L'},
                {0, 0, 0, 0}
        };
 
        if (argc < 2)
                usage(argv[0]); /* usage() exits */
 
-       while ((c = getopt_long(argc, argv, "hl:", options, NULL)) > 0) {
+       ret = cgroup_string_list_init(&cfg_files, argc/2);
+
+       while ((c = getopt_long(argc, argv, "hl:L:", options, NULL)) > 0) {
                switch (c) {
                case 'h':
                        usage(argv[0]);
                        break;
                case 'l':
-                       strncpy(filename, optarg, PATH_MAX);
-                       ret = cgroup_config_load_config(filename);
+                       ret = cgroup_string_list_add_item(&cfg_files, optarg);
                        if (ret) {
-                               printf("Loading configuration file %s "
-                                       "failed\n%s\n", filename,
-                                       cgroup_strerror(ret));
-                               exit(3);
+                               fprintf(stderr, "%s: cannot add file to list,"\
+                                               " out of memory?\n", argv[0]);
+                               exit(1);
                        }
-                       return 0;
+                       break;
+               case 'L':
+                       cgroup_string_list_add_directory(&cfg_files, optarg,
+                                       argv[0]);
+                       break;
                default:
                        usage(argv[0]);
                        break;
                }
        }
-       return 0;
+
+       for (i = 0; i < cfg_files.count; i++) {
+               ret = cgroup_config_load_config(cfg_files.items[i]);
+               if (ret) {
+                       fprintf(stderr, "%s; error loading %s: %s\n", argv[0],
+                                       cfg_files.items[i],
+                                       cgroup_strerror(ret));
+                       if (!error)
+                               error = ret;
+               }
+       }
+
+       cgroup_string_list_free(&cfg_files);
+       return error;
 }