From: Anthony Lineham Refactor previous fix to reduce code duplication. --- src/cfg.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 5dc813d..047d269 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -115,34 +115,73 @@ cfg_bad_hostname_set(GlobalConfig *self, gchar *bad_hostname_re) self->bad_hostname_re = g_strdup(bad_hostname_re); } +/* Checks if a group (source or destination) with the specified name + * already exists. If it does, discards it and returns TRUE. Otherwise + * returns FALSE. */ +gboolean +cfg_group_duplicate(GHashTable *group_hash, gchar *name, LogPipe *pipe) +{ + if (g_hash_table_lookup(group_hash, name)) + { + msg_error("Duplicate item in configuration file", + evt_tag_str("name", name), + NULL); + log_pipe_unref(pipe); + return TRUE; + } + return FALSE; +} + void cfg_add_source(GlobalConfig *cfg, LogSourceGroup *group) { - g_hash_table_insert(cfg->sources, group->name, group); + if (!cfg_group_duplicate(cfg->sources, group->name, (LogPipe *)group)) + g_hash_table_insert(cfg->sources, group->name, group); } void cfg_add_dest(GlobalConfig *cfg, LogDestGroup *group) { - g_hash_table_insert(cfg->destinations, group->name, group); + if (!cfg_group_duplicate(cfg->destinations, group->name, (LogPipe *)group)) + g_hash_table_insert(cfg->destinations, group->name, group); +} + +/* Checks if a process rule (filter, parser, rewrite) with the specified name + * already exists. If it does, discards it and returns TRUE. Otherwise + * returns FALSE. */ +gboolean +cfg_processrule_duplicate(GHashTable *rule_hash, LogProcessRule *rule) +{ + if (g_hash_table_lookup(rule_hash, rule->name)) + { + msg_error("Duplicate item in configuration file", + evt_tag_str("name", rule->name), + NULL); + log_process_rule_unref(rule); + return TRUE; + } + return FALSE; } void cfg_add_filter(GlobalConfig *cfg, LogProcessRule *rule) { - g_hash_table_insert(cfg->filters, rule->name, rule); + if (!cfg_processrule_duplicate(cfg->filters, rule)) + g_hash_table_insert(cfg->filters, rule->name, rule); } void cfg_add_parser(GlobalConfig *cfg, LogProcessRule *rule) { - g_hash_table_insert(cfg->parsers, rule->name, rule); + if (!cfg_processrule_duplicate(cfg->parsers, rule)) + g_hash_table_insert(cfg->parsers, rule->name, rule); } void cfg_add_rewrite(GlobalConfig *cfg, LogProcessRule *rule) { - g_hash_table_insert(cfg->rewriters, rule->name, rule); + if (!cfg_processrule_duplicate(cfg->rewriters, rule)) + g_hash_table_insert(cfg->rewriters, rule->name, rule); } void @@ -151,10 +190,27 @@ cfg_add_connection(GlobalConfig *cfg, LogConnection *conn) g_ptr_array_add(cfg->connections, conn); } +/* Checks if a template with the specified name already exists. + * If it does, discards it and returns TRUE. Otherwise + * returns FALSE. */ +gboolean +cfg_template_duplicate(GHashTable *template_hash, LogTemplate *template) +{ + if (g_hash_table_lookup(template_hash, template->name)) + { + msg_error("Duplicate item in configuration file", + evt_tag_str("template", template->name), + NULL); + log_template_unref(template); + return TRUE; + } + return FALSE; +} void cfg_add_template(GlobalConfig *cfg, LogTemplate *template) { - g_hash_table_insert(cfg->templates, template->name, template); + if (!cfg_template_duplicate(cfg->templates, template)) + g_hash_table_insert(cfg->templates, template->name, template); } LogTemplate *