Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: App specific transformations #4683

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
37 changes: 24 additions & 13 deletions lib/timeutils/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,31 +305,42 @@ get_cached_realtime_sec(void)
return (time_t) now.tv_sec;
}

static time_t
_calculate_and_adjust_mktime_result_based_on_cache(struct tm *tm)
{
/* our cached value is valid for 1 hour, as we cache the min==sec==0 value
* for every second */
int sec = tm->tm_sec;
int min = tm->tm_min;
*tm = cache.mktime.mutated_key;
tm->tm_sec = sec;
tm->tm_min = min;
return cache.mktime.value + tm->tm_min * 60 + tm->tm_sec;
}

time_t
cached_mktime(struct tm *tm)
{
_validate_timeutils_cache();
if (G_LIKELY(tm->tm_sec == cache.mktime.key.tm_sec &&
tm->tm_min == cache.mktime.key.tm_min &&
tm->tm_hour == cache.mktime.key.tm_hour &&
if (G_LIKELY(tm->tm_hour == cache.mktime.key.tm_hour &&
tm->tm_mday == cache.mktime.key.tm_mday &&
tm->tm_mon == cache.mktime.key.tm_mon &&
tm->tm_year == cache.mktime.key.tm_year &&
tm->tm_isdst == cache.mktime.key.tm_isdst))
{
*tm = cache.mktime.mutated_key;
return cache.mktime.value;
return _calculate_and_adjust_mktime_result_based_on_cache(tm);
}

/* we need to store the incoming value first, as mktime() might change the
* fields in *tm, for instance in the daylight saving transition hour */
cache.mktime.key = *tm;
cache.mktime.value = mktime(tm);
/* we need to store both the incoming value (key) and the one mutated by
* mktime(), as mktime() might change the fields in *tm for instance in
* the daylight saving transition hour */
cache.mktime.key = cache.mktime.mutated_key = *tm;

/* the result we yield consists of both the return value and the mutated
* key, so we need to save both */
cache.mktime.mutated_key = *tm;
return cache.mktime.value;
/* let's cache the top of the hour */
cache.mktime.mutated_key.tm_sec = 0;
cache.mktime.mutated_key.tm_min = 0;
cache.mktime.value = mktime(&cache.mktime.mutated_key);
return _calculate_and_adjust_mktime_result_based_on_cache(tm);
}

void
Expand Down
2 changes: 2 additions & 0 deletions modules/appmodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ set (APPMODEL_SOURCES
appmodel-parser.c
appmodel-plugin.c
appmodel-context.c
app-object-generator.c
app-parser-generator.c
app-transform-generator.c
application.c
)

Expand Down
6 changes: 6 additions & 0 deletions modules/appmodel/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ modules_appmodel_libappmodel_la_SOURCES = \
modules/appmodel/appmodel-plugin.c \
modules/appmodel/appmodel-context.c \
modules/appmodel/appmodel-context.h \
modules/appmodel/app-object-generator.c \
modules/appmodel/app-object-generator.h \
modules/appmodel/app-parser-generator.c \
modules/appmodel/app-parser-generator.h \
modules/appmodel/app-transform-generator.c \
modules/appmodel/app-transform-generator.h \
modules/appmodel/application.c \
modules/appmodel/application.h \
modules/appmodel/transformation.c \
modules/appmodel/transformation.h \
modules/appmodel/appmodel-grammar.y

modules_appmodel_libappmodel_la_CPPFLAGS = \
Expand Down
113 changes: 113 additions & 0 deletions modules/appmodel/app-object-generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2017 Balabit
* Copyright (c) 2017 Balazs Scheidler <balazs.scheidler@balabit.com>
*
* 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 "app-object-generator.h"
#include "appmodel.h"

#include <string.h>

gboolean
app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name)
{
/* include everything if we don't have the option */
if (!self->included_apps)
return TRUE;
return strstr(self->included_apps, app_name) != NULL;
}

gboolean
app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name)
{
if (!self->excluded_apps)
return FALSE;
return strstr(self->excluded_apps, app_name) != NULL;
}

static gboolean
_parse_auto_parse_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse");

if (v)
self->is_parsing_enabled = cfg_process_yesno(v);
else
self->is_parsing_enabled = TRUE;
return TRUE;
}

static gboolean
_parse_auto_parse_exclude_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-exclude");
if (!v)
return TRUE;
self->excluded_apps = g_strdup(v);
return TRUE;
}

static gboolean
_parse_auto_parse_include_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-include");
if (!v)
return TRUE;
self->included_apps = g_strdup(v);
return TRUE;
}

gboolean
app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
g_assert(args != NULL);

if (!_parse_auto_parse_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_exclude_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_include_arg(self, args, reference))
return FALSE;
return TRUE;
}

static gboolean
_generate(CfgBlockGenerator *s, GlobalConfig *cfg, gpointer args, GString *result, const gchar *reference)
{
AppObjectGenerator *self = (AppObjectGenerator *) s;
CfgArgs *cfgargs = (CfgArgs *)args;

if (!self->parse_arguments(self, cfgargs, reference))
return FALSE;

self->generate_config(self, cfg, result);

return TRUE;
}

void
app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name)
{
cfg_block_generator_init_instance(&self->super, context, name);
self->super.generate = _generate;
self->parse_arguments = app_object_generator_parse_arguments_method;
}
49 changes: 49 additions & 0 deletions modules/appmodel/app-object-generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2017 Balabit
* Copyright (c) 2017 Balazs Scheidler <balazs.scheidler@balabit.com>
*
* 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 APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED
#define APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED

#include "plugin.h"

typedef struct _AppObjectGenerator AppObjectGenerator;

struct _AppObjectGenerator
{
CfgBlockGenerator super;
gboolean (*parse_arguments)(AppObjectGenerator *self, CfgArgs *args, const gchar *reference);
void (*generate_config)(AppObjectGenerator *self, GlobalConfig *cfg, GString *result);
const gchar *included_apps;
const gchar *excluded_apps;
gboolean is_parsing_enabled;
};

gboolean app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name);
gboolean app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name);

gboolean app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference);
void app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name);


#endif
Loading
Loading