]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgrulesengd.c: loading of multiples files during init/reload
authorJan Chaloupka <jchaloup@redhat.com>
Thu, 19 Jun 2014 09:04:26 +0000 (11:04 +0200)
committerJan Chaloupka <jchaloup@redhat.com>
Thu, 19 Jun 2014 09:52:18 +0000 (11:52 +0200)
This patch use new implementation. In cgrulesengd.c, static variable template_files is used to save all config files, i.e. /etc/cgconfig.conf and /etc/cgconfig.d/*. This list of files is then passed to cgroup_templates_cache_set_source_files, which saves files to template_files in config.c module. This list is accessed through invocation of cgroup_init_templates_cache_from_files and cgroup_reload_templates_cache_from_files, which can access variables only from config.c, thus presenting cgroup_templates_cache_set_source_files function. At the end, this list is set free.

Signed-off-by: Jan Chaloupka <jchaloup@redhat.com>
Acked-by: Ivana Hutarova Varekova <varekova@redhat.com>
src/daemon/cgrulesengd.c
src/libcgroup-internal.h
src/tools/tools-common.h

index 367b89850b9777ed0048856c8e4e7ac1d4a8ccf8..ea51f11f43d63e29c52afc1fa9f214ce2e0e9b33 100644 (file)
@@ -34,6 +34,7 @@
 #include "libcgroup.h"
 #include "cgrulesengd.h"
 #include "../libcgroup-internal.h"
+#include "../tools/tools-common.h"
 
 #include <errno.h>
 #include <stdarg.h>
@@ -59,6 +60,9 @@
 
 #define NUM_PER_REALLOCATIOM   (100)
 
+/* list of config files from CGCONFIG_CONF_FILE and CGCONFIG_CONF_DIR */
+static struct cgroup_string_list template_files;
+
 /* Log file, NULL if logging to file is disabled */
 FILE* logfile;
 
@@ -936,6 +940,8 @@ void cgre_flash_rules(int signum)
        /* Current time */
        time_t tm = time(0);
 
+       int fileindex;
+
        flog(LOG_INFO, "Reloading rules configuration\n");
        flog(LOG_DEBUG, "Current time: %s\n", ctime(&tm));
 
@@ -949,7 +955,7 @@ void cgre_flash_rules(int signum)
        }
 
        /* Ask libcgroup to reload the template rules table. */
-       cgroup_reload_cached_templates(CGCONFIG_CONF_FILE);
+       cgroup_load_templates_cache_from_files(&fileindex);
 }
 
 /**
@@ -962,11 +968,13 @@ void cgre_flash_templates(int signum)
        /* Current time */
        time_t tm = time(0);
 
+       int fileindex;
+
        flog(LOG_INFO, "Reloading templates configuration.\n");
        flog(LOG_DEBUG, "Current time: %s\n", ctime(&tm));
 
        /* Ask libcgroup to reload the templates table. */
-       cgroup_reload_cached_templates(CGCONFIG_CONF_FILE);
+       cgroup_load_templates_cache_from_files(&fileindex);
 }
 
 /**
@@ -1069,6 +1077,8 @@ int main(int argc, char *argv[])
                {NULL, 0, NULL, 0}
        };
 
+       int fileindex;
+
        /* Make sure the user is root. */
        if (getuid() != 0) {
                fprintf(stderr, "Error: Only root can start/stop the control"
@@ -1180,6 +1190,25 @@ int main(int argc, char *argv[])
        }
 
        /* Ask libcgroup to load the configuration rules. */
+       ret = cgroup_string_list_init(&template_files,
+               CGCONFIG_CONF_FILES_LIST_MINIMUM_SIZE);
+       if (ret) {
+               fprintf(stderr, "%s: cannot init file list, out of memory?\n",
+                       argv[0]);
+               goto finished_without_temp_files;
+       }
+       /* first add CGCONFIG_CONF_FILE into file list */
+       ret = cgroup_string_list_add_item(&template_files, CGCONFIG_CONF_FILE);
+       if (ret) {
+               fprintf(stderr, "%s: cannot add file to list, out of memory?\n"
+                       , argv[0]);
+               goto finished;
+       }
+
+       /* then read CGCONFIG_CONF_DIR directory for additional config files */
+       cgroup_string_list_add_directory(&template_files, CGCONFIG_CONF_DIR,
+               argv[0]);
+
        if ((ret = cgroup_init_rules_cache()) != 0) {
                fprintf(stderr, "Error: libcgroup failed to initialize rules"
                                "cache from %s. %s\n", CGRULES_CONF_FILE,
@@ -1188,11 +1217,18 @@ int main(int argc, char *argv[])
        }
 
        /* ask libcgroup to load template rules as well */
-       ret = cgroup_init_templates_cache(CGCONFIG_CONF_FILE);
+       cgroup_templates_cache_set_source_files(&template_files);
+       ret = cgroup_load_templates_cache_from_files(&fileindex);
        if (ret != 0) {
-               fprintf(stderr, "Error: libcgroup failed to initialize teplate"\
-                               "rules from %s. %s\n", CGCONFIG_CONF_FILE,
-                               cgroup_strerror(ret));
+               if (fileindex < 0) {
+                       fprintf(stderr, "Error: Template source files ");
+                       fprintf(stderr, "have not been set\n");
+               } else {
+                       fprintf(stderr, "Error: Failed to initialize template");
+                       fprintf(stderr, "rules from %s. ",
+                               template_files.items[fileindex]);
+                       fprintf(stderr, "%s\n", cgroup_strerror(-ret));
+               }
                goto finished;
        }
 
@@ -1259,6 +1295,9 @@ int main(int argc, char *argv[])
        ret =  cgre_create_netlink_socket_process_msg();
 
 finished:
+       cgroup_string_list_free(&template_files);
+
+finished_without_temp_files:
        if (logfile && logfile != stdout)
                fclose(logfile);
 
index 4c0f46c6c6e27c8c0a7cd66237519609202234ae..c128788a964de3aebf411b81b31e78f3c6663ca0 100644 (file)
@@ -48,6 +48,9 @@ __BEGIN_DECLS
 
 
 #define CGCONFIG_CONF_FILE             "/etc/cgconfig.conf"
+/* Minimum number of file in template file list for cgrulesengd */
+#define CGCONFIG_CONF_FILES_LIST_MINIMUM_SIZE   4
+#define CGCONFIG_CONF_DIR               "/etc/cgconfig.d"
 
 #define CGRULES_CONF_FILE       "/etc/cgrules.conf"
 #define CGRULES_MAX_FIELDS_PER_LINE            3
index e05465f78cadc8d19a70f575927e05ee1ed5bd79..c723eb4ceee4d561e973a75f3845765a56d7337c 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "config.h"
 #include <libcgroup.h>
-#include <libcgroup-internal.h>
+#include "../libcgroup-internal.h"
 
 #define cgroup_err(x...) cgroup_log(CGROUP_LOG_ERROR, x)
 #define cgroup_warn(x...) cgroup_log(CGROUP_LOG_WARNING, x)