Skip to content

Commit

Permalink
Merge branch 'persist-tool-integrate'
Browse files Browse the repository at this point in the history
  • Loading branch information
bazsi committed Feb 1, 2014
2 parents a8f4b42 + 94bf21e commit cdbe81d
Show file tree
Hide file tree
Showing 22 changed files with 677 additions and 36 deletions.
5 changes: 5 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ pkginclude_HEADERS += \
lib/msg-format.h \
lib/nvtable.h \
lib/parse-number.h \
lib/pathutils.h \
lib/persist-state.h \
lib/persistable-state-header.h \
lib/persistable-state-presenter.h \
lib/plugin.h \
lib/plugin-types.h \
lib/poll-events.h \
lib/poll-fd-events.h \
lib/pragma-parser.h \
lib/presented-persistable-state.h \
lib/reloc.h \
lib/rcptid.h \
lib/run-id.h \
Expand Down Expand Up @@ -158,11 +161,13 @@ lib_libsyslog_ng_la_SOURCES = \
lib/msg-format.c \
lib/nvtable.c \
lib/parse-number.c \
lib/pathutils.c \
lib/persist-state.c \
lib/plugin.c \
lib/poll-events.c \
lib/poll-fd-events.c \
lib/pragma-parser.c \
lib/persistable-state-presenter.c \
lib/rcptid.c \
lib/reloc.c \
lib/run-id.c \
Expand Down
23 changes: 23 additions & 0 deletions lib/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,29 @@ cfg_dump_processed_config(GString *preprocess_output, gchar *output_filename)
}
}

gboolean
cfg_load_config(GlobalConfig *self, gchar *config_string, gboolean syntax_only, gchar *preprocess_into)
{
gint res;
CfgLexer *lexer;
GString *preprocess_output = g_string_sized_new(8192);

lexer = cfg_lexer_new_buffer(config_string, strlen(config_string));
lexer->preprocess_output = preprocess_output;

res = cfg_run_parser(self, lexer, &main_parser, (gpointer *) &self, NULL);
if (preprocess_into)
{
cfg_dump_processed_config(preprocess_output, preprocess_into);
}
g_string_free(preprocess_output, TRUE);
if (res)
{
return TRUE;
}
return FALSE;
}

gboolean
cfg_read_config(GlobalConfig *self, const gchar *fname, gboolean syntax_only, gchar *preprocess_into)
{
Expand Down
1 change: 1 addition & 0 deletions lib/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void cfg_load_candidate_modules(GlobalConfig *self);
GlobalConfig *cfg_new(gint version);
gboolean cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg);
gboolean cfg_read_config(GlobalConfig *cfg, const gchar *fname, gboolean syntax_only, gchar *preprocess_into);
gboolean cfg_load_config(GlobalConfig *self, gchar *config_string, gboolean syntax_only, gchar *preprocess_into);
void cfg_free(GlobalConfig *self);
gboolean cfg_init(GlobalConfig *cfg);
gboolean cfg_deinit(GlobalConfig *cfg);
Expand Down
15 changes: 0 additions & 15 deletions lib/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,6 @@ find_file_in_path(const gchar *path, const gchar *filename, GFileTest test)
return fullname;
}

gchar *
format_hex_string(gpointer data, gsize data_len, gchar *result, gsize result_len)
{
gint i;
gint pos = 0;
guchar *str = (guchar *) data;

for (i = 0; i < data_len && result_len - pos > 2; i++)
{
g_snprintf(result + pos, 3, "%02x", str[i]);
pos += 2;
}
return result;
}

/**
* Find CR or LF characters in the log message.
*
Expand Down
1 change: 0 additions & 1 deletion lib/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ gboolean resolve_user(const char *user, gint *uid);
gboolean resolve_group(const char *group, gint *gid);
gboolean resolve_user_group(char *arg, gint *uid, gint *gid);

gchar *format_hex_string(gpointer str, gsize str_len, gchar *result, gsize result_len);
gchar *find_cr_or_lf(gchar *s, gsize n);

gchar *find_file_in_path(const gchar *path, const gchar *filename, GFileTest test);
Expand Down
38 changes: 38 additions & 0 deletions lib/pathutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2002-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2013 Viktor Juhasz
* Copyright (c) 2013 Viktor Tusa
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#include "pathutils.h"

gboolean
is_file_directory(const char *filename)
{
return g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
};

gboolean
is_file_regular(const char *filename)
{
return g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR);
};
33 changes: 33 additions & 0 deletions lib/pathutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2002-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2013 Viktor Juhasz
* Copyright (c) 2013 Viktor Tusa
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef _PATHUTILS_H
#define _PATHUTILS_H
#include "syslog-ng.h"

gboolean is_file_regular(const char *filename);
gboolean is_file_directory(const char *filename);

#endif
89 changes: 77 additions & 12 deletions lib/persist-state.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ _map_header_of_entry_from_handle(PersistState *self, PersistEntryHandle handle)
return NULL;
}
return header;
};
}

static void
_free_value(PersistState *self, PersistEntryHandle handle)
Expand Down Expand Up @@ -483,7 +483,7 @@ _load_v23(PersistState *self, gint version, SerializeArchive *sa)
}

static gboolean
_load_v4(PersistState *self)
_load_v4(PersistState *self, gboolean load_all_entries)
{
gint fd;
gint64 file_size;
Expand Down Expand Up @@ -559,7 +559,7 @@ _load_v4(PersistState *self)
}

header = (PersistValueHeader *) ((gchar *) map + entry_ofs - sizeof(PersistValueHeader));
if (header->in_use)
if ((header->in_use) || load_all_entries)
{
gpointer new_block;
PersistEntryHandle new_handle;
Expand Down Expand Up @@ -628,7 +628,7 @@ _load_v4(PersistState *self)
}

static gboolean
_load(PersistState *self)
_load(PersistState *self, gboolean all_errors_are_fatal, gboolean load_all_entries)
{
FILE *persist_file;
gboolean success = FALSE;
Expand All @@ -645,7 +645,7 @@ _load(PersistState *self)
if (memcmp(magic, "SLP", 3) != 0)
{
msg_error("Persistent configuration file is in invalid format, ignoring", NULL);
success = TRUE;
success = all_errors_are_fatal ? FALSE : TRUE;
goto close_and_exit;
}
version = magic[3] - '0';
Expand All @@ -655,7 +655,7 @@ _load(PersistState *self)
}
else if (version == 4)
{
success = _load_v4(self);
success = _load_v4(self, load_all_entries);
}
else
{
Expand All @@ -670,7 +670,16 @@ _load(PersistState *self)
}
else
{
success = TRUE;
if (all_errors_are_fatal)
{
msg_error("Failed to open persist file!",
evt_tag_str("filename", self->commited_filename),
evt_tag_str("error", strerror(errno)),
NULL);
success = FALSE;
}
else
success = TRUE;
}
return success;
}
Expand Down Expand Up @@ -723,7 +732,7 @@ _map_header_of_entry(PersistState *self, const gchar *persist_name, PersistEntry

return _map_header_of_entry_from_handle(self, *handle);

};
}

PersistEntryHandle
persist_state_alloc_entry(PersistState *self, const gchar *persist_name, gsize alloc_size)
Expand Down Expand Up @@ -774,7 +783,42 @@ persist_state_remove_entry(PersistState *self, const gchar *key)

_free_value(self, handle);
return TRUE;
};
}

typedef struct _PersistStateKeysForeachData
{
PersistStateForeachFunc func;
gpointer userdata;
PersistState* storage;
} PersistStateKeysForeachData;

static void
_foreach_entry_func(gpointer key, gpointer value, gpointer userdata)
{
PersistStateKeysForeachData *data = (PersistStateKeysForeachData *) userdata;
PersistEntry *entry = (PersistEntry *) value;
gchar *name = (gchar *) key;

PersistValueHeader *header = persist_state_map_entry(data->storage, entry->ofs - sizeof(PersistValueHeader));
gint size = GUINT32_FROM_BE(header->size);
persist_state_unmap_entry(data->storage, entry->ofs);

gpointer *state = persist_state_map_entry(data->storage, entry->ofs);
data->func(name, size, state, data->userdata);
persist_state_unmap_entry(data->storage, entry->ofs);
}

void
persist_state_foreach_entry(PersistState *self, PersistStateForeachFunc func, gpointer userdata)
{
PersistStateKeysForeachData data;

data.func = func;
data.userdata = userdata;
data.storage = self;

g_hash_table_foreach(self->keys, _foreach_entry_func, &data);
}

/* easier to use string based interface */
gchar *
Expand Down Expand Up @@ -837,12 +881,32 @@ persist_state_get_filename(PersistState *self)
return self->commited_filename;
}

gboolean
persist_state_start_dump(PersistState *self)
{
if (!_create_store(self))
return FALSE;
if (!_load(self, TRUE, TRUE))
return FALSE;
return TRUE;
}

gboolean
persist_state_start_edit(PersistState *self)
{
if (!_create_store(self))
return FALSE;
if (!_load(self, FALSE, TRUE))
return FALSE;
return TRUE;
}

gboolean
persist_state_start(PersistState *self)
{
if (!_create_store(self))
return FALSE;
if (!_load(self))
if (!_load(self, FALSE, FALSE))
return FALSE;
return TRUE;
}
Expand Down Expand Up @@ -880,7 +944,7 @@ _destroy(PersistState *self)
g_free(self->temp_filename);
g_free(self->commited_filename);
g_hash_table_destroy(self->keys);
};
}

static void
_init(PersistState* self, gchar* commited_filename, gchar* temp_filename)
Expand All @@ -893,7 +957,8 @@ _init(PersistState* self, gchar* commited_filename, gchar* temp_filename)
self->fd = -1;
self->commited_filename = commited_filename;
self->temp_filename = temp_filename;
};
}

/*
* This routine should revert to the persist_state_new() state,
* e.g. just like the PersistState object wasn't started yet.
Expand Down
5 changes: 5 additions & 0 deletions lib/persist-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ void persist_state_alloc_string(PersistState *self, const gchar *persist_name, c

void persist_state_free_entry(PersistEntryHandle handle);

typedef void (*PersistStateForeachFunc)(gchar* name, gint entry_size, gpointer entry, gpointer userdata);
void persist_state_foreach_entry(PersistState *self, PersistStateForeachFunc func, gpointer userdata);

gboolean persist_state_start(PersistState *self);
gboolean persist_state_start_edit(PersistState *self);
gboolean persist_state_start_dump(PersistState *self);

const gchar *persist_state_get_filename(PersistState *self);

Expand Down
Loading

0 comments on commit cdbe81d

Please sign in to comment.