Skip to content

Commit

Permalink
filter_lua: Provide additional script validator before running as cb_…
Browse files Browse the repository at this point in the history
…pre_run

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 authored and edsiper committed Nov 4, 2023
1 parent fc8b8e8 commit e1034f0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions plugins/filter_lua/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,46 @@
#include "lua_config.h"
#include "mpack/mpack.h"

static int cb_lua_pre_run(struct flb_filter_instance *f_ins,
struct flb_config *config, void *data)
{
int ret;
(void) data;
struct lua_filter *ctx;
struct flb_luajit *lj;

/* Create context */
ctx = lua_config_create(f_ins, config);
if (!ctx) {
flb_error("[filter_lua] filter cannot be loaded");
return -1;
}

/* Create LuaJIT state/vm */
lj = flb_luajit_create(config);
if (!lj) {
lua_config_destroy(ctx);
return -1;
}
ctx->lua = lj;

/* Lua script source code */
if (ctx->code) {
ret = flb_luajit_load_buffer(ctx->lua,
ctx->code, flb_sds_len(ctx->code),
"fluentbit.lua");
}
else {
/* Load Script / file path*/
ret = flb_luajit_load_script(ctx->lua, ctx->script);
}

flb_luajit_destroy(ctx->lua);
lua_config_destroy(ctx);

return ret;
}

static int cb_lua_init(struct flb_filter_instance *f_ins,
struct flb_config *config,
void *data)
Expand Down Expand Up @@ -702,6 +742,7 @@ static struct flb_config_map config_map[] = {
struct flb_filter_plugin filter_lua_plugin = {
.name = "lua",
.description = "Lua Scripting Filter",
.cb_pre_run = cb_lua_pre_run,
.cb_init = cb_lua_init,
#ifdef FLB_FILTER_LUA_USE_MPACK
.cb_filter = cb_lua_filter_mpack,
Expand Down

0 comments on commit e1034f0

Please sign in to comment.