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.
Changelog: /etc/cgconfig.conf and /etc/cgconfig.d/ replaced by their macras in comments all variable definitions move at the beggining of a function all macro definitions for string replaced by multiple fprintf call cgroup_string_list_free called only after succesful initialization replaced cgroup_reload_cached_templates with cgroup_load_templates_cache_from_files in cgre_flash_rules/cgre_flash_templates functions in cgrulesengd.c Signed-off-by: Jan Chaloupka <jchal...@redhat.com> --- src/daemon/cgrulesengd.c | 51 +++++++++++++++++++++++++++++++++++++++++----- src/libcgroup-internal.h | 3 +++ src/tools/tools-common.h | 2 +- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/daemon/cgrulesengd.c b/src/daemon/cgrulesengd.c index 367b898..ea51f11 100644 --- a/src/daemon/cgrulesengd.c +++ b/src/daemon/cgrulesengd.c @@ -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); diff --git a/src/libcgroup-internal.h b/src/libcgroup-internal.h index 4c0f46c..c128788 100644 --- a/src/libcgroup-internal.h +++ b/src/libcgroup-internal.h @@ -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 diff --git a/src/tools/tools-common.h b/src/tools/tools-common.h index e05465f..c723eb4 100644 --- a/src/tools/tools-common.h +++ b/src/tools/tools-common.h @@ -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) ------------------------------------------------------------------------------ Time is money. Stop wasting it! Get your web API in 5 minutes. www.restlet.com/download http://p.sf.net/sfu/restlet _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel