From: Ivana Hutarova Varekova Date: Mon, 28 Jan 2013 13:41:38 +0000 (+0100) Subject: config.h: create function for init/reload cgconfig template cache X-Git-Tag: v0.41~89 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=361be8239c5beb0b5a45f18e39c8a317eac7a1d4;p=thirdparty%2Flibcgroup.git config.h: create function for init/reload cgconfig template cache Two new functions maintain the templates cache: int cgroup_init_templates_cache(char *pathname); int cgroup_reload_cached_templates(char *pathname); their are analogous to cgroup_init_rules_cache and cgroup_reload_cached_rules the only difference is there can be set configuration file as a parameter Changelog: remove useless comments Signed-off-by: Ivana Hutarova Varekova Acked-By: Jan Safranek --- diff --git a/include/libcgroup/config.h b/include/libcgroup/config.h index 2dfdd32a..d18634ef 100644 --- a/include/libcgroup/config.h +++ b/include/libcgroup/config.h @@ -73,6 +73,16 @@ int cgroup_config_unload_config(const char *pathname, int flags); */ int cgroup_config_set_default(struct cgroup *new_default); +/** + * Initializes the templates cache and load it from file pathname. + */ +int cgroup_init_templates_cache(char *pathname); + +/** + * Reloads the templates list from file pathname. + */ +int cgroup_reload_cached_templates(char *pathname); + /** * @} * @} diff --git a/src/config.c b/src/config.c index 62817673..72755dae 100644 --- a/src/config.c +++ b/src/config.c @@ -82,6 +82,15 @@ static int cgroup_table_index; static struct cgroup *config_template_table; static int config_template_table_index; +/* + * template structures used for templates cache, config_template_table and + * cgroup_template_table_index are rewritten in each cgroup_parse_config + * thus not only if we want to reload template cache + */ +static struct cgroup *template_table; +static int template_table_index; + + /* * Needed for the type while mounting cgroupfs. */ @@ -1408,3 +1417,87 @@ int cgroup_config_set_default(struct cgroup *new_default) return 0; } + +/** + * Reloads the templates list, using the given configuration file. + * @return 0 on success, > 0 on failure + */ +int cgroup_reload_cached_templates(char *pathname) +{ + int i; + int ret = 0; + + if (template_table) { + /* template structures have to be free */ + for (i = 0; i < template_table_index; i++) + cgroup_free_controllers(&template_table[i]); + free(template_table); + template_table = NULL; + } + template_table_index = 0; + + if (config_template_table_index != 0) { + /* config template structures have to be free as well*/ + cgroup_free_config(); + } + + /* reloading data to config template structures */ + cgroup_dbg("Reloading cached templates from %s.\n", pathname); + ret = cgroup_parse_config(pathname); + if (ret) { + cgroup_dbg("Could not reload template cache, error was: %d\n", + ret); + return ret; + } + + /* copy data to templates cache structures */ + template_table_index = config_template_table_index; + template_table = calloc(template_table_index, sizeof(struct cgroup)); + if (template_table == NULL) { + ret = ECGOTHER; + return ret; + } + + memcpy(template_table, config_template_table, + template_table_index * sizeof(struct cgroup)); + + return ret; +} + +/** + * Initializes the templates cache. + * @return 0 on success, > 0 on error + */ +int cgroup_init_templates_cache(char *pathname) +{ + int ret = 0; + + if (config_template_table_index != 0) { + /* config structures have to be clean */ + cgroup_free_config(); + } + + cgroup_dbg("Loading cached templates from %s.\n", pathname); + /* Attempt to read the configuration file and cache the rules. */ + ret = cgroup_parse_config(pathname); + if (ret) { + cgroup_dbg("Could not initialize rule cache, error was: %d\n", + ret); + return ret; + } + + /* copy template data to templates cache structures */ + template_table_index = config_template_table_index; + template_table = calloc(template_table_index, sizeof(struct cgroup)); + if (template_table == NULL) { + ret = ECGOTHER; + return ret; + } + + memcpy(template_table, config_template_table, + template_table_index * sizeof(struct cgroup)); + + return ret; + + +} diff --git a/src/libcgroup-internal.h b/src/libcgroup-internal.h index e59a59e1..dbb8e9a2 100644 --- a/src/libcgroup-internal.h +++ b/src/libcgroup-internal.h @@ -47,6 +47,8 @@ __BEGIN_DECLS #define CGRULE_SUCCESS_STORE_PID "SUCCESS_STORE_PID" +#define CGCONFIG_CONF_FILE "/etc/cgconfig.conf" + #define CGRULES_CONF_FILE "/etc/cgrules.conf" #define CGRULES_MAX_FIELDS_PER_LINE 3 diff --git a/src/libcgroup.map b/src/libcgroup.map index e73dd6e6..e29f8879 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -105,3 +105,8 @@ CGROUP_0.38 { cgroup_config_unload_config; cgroup_config_set_default; } CGROUP_0.37; + +CGROUP_0.39 { + cgroup_reload_cached_templates; + cgroup_init_templates_cache; +} CGROUP_0.38; \ No newline at end of file