From: Jan Safranek Date: Fri, 23 Sep 2011 11:53:40 +0000 (+0200) Subject: cgconfigparser: Allow multiple config files X-Git-Tag: v0.38~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8736d52b24337ecc2a4f70403488b25cb82895ae;p=thirdparty%2Flibcgroup.git cgconfigparser: Allow multiple config files cgconfigparser can now parse multiple config files, including (alphabetitcally sorted) files from a directory. Signed-off-by: Jan Safranek --- diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am index b825dffc..4cb02ff7 100644 --- a/src/tools/Makefile.am +++ b/src/tools/Makefile.am @@ -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 diff --git a/src/tools/cgconfig.c b/src/tools/cgconfig.c index e426b760..cc2a633e 100644 --- a/src/tools/cgconfig.c +++ b/src/tools/cgconfig.c @@ -31,51 +31,74 @@ #include #include #include +#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; }