* it's parent_ctx->data ptr.
*/
typedef struct LogLuaMasterCtx_ {
- char script_dir[PATH_MAX]; /**< contains script-dir */
+ /** \brief Path to script directory. */
+ char script_dir[PATH_MAX];
+
+ /** \brief Lua search path for Lua modules. */
+ char path[PATH_MAX];
+
+ /** \brief Lua search path for C modules. */
+ char cpath[PATH_MAX];
} LogLuaMasterCtx;
typedef struct LogLuaCtx_ {
int stats;
} LogLuaScriptOptions;
+/** \brief Setup or clear Lua module search paths.
+ *
+ * If search paths are provided by the configuration, set them up,
+ * otherwise clear the default search paths.
+ */
+static void LuaSetPaths(lua_State *L, LogLuaMasterCtx *ctx)
+{
+ lua_getglobal(L, "package");
+
+ if (strlen(ctx->path) > 0) {
+ lua_pushstring(L, ctx->path);
+ } else {
+ lua_pushstring(L, "");
+ }
+ lua_setfield(L, -2, "path");
+
+ if (strlen(ctx->cpath) > 0) {
+ lua_pushstring(L, ctx->cpath);
+ } else {
+ lua_pushstring(L, "");
+ }
+ lua_setfield(L, -2, "cpath");
+
+ /* Pop package. */
+ lua_pop(L, 1);
+}
+
/** \brief load and evaluate the script
*
* This function parses the script, checks if all the required functions
* \param options struct to pass script requirements/options back to caller
* \retval errcode 0 ok, -1 error
*/
-static int LuaScriptInit(const char *filename, LogLuaScriptOptions *options) {
+static int LuaScriptInit(const char *filename, LogLuaScriptOptions *options, LogLuaMasterCtx *ctx)
+{
lua_State *luastate = LuaGetState();
if (luastate == NULL)
goto error;
luaL_openlibs(luastate);
SCLuaRequirefBuiltIns(luastate);
+ LuaSetPaths(luastate, ctx);
int status = luaL_loadfile(luastate, filename);
if (status) {
*
* \retval state Returns the set up luastate on success, NULL on error
*/
-static lua_State *LuaScriptSetup(const char *filename)
+static lua_State *LuaScriptSetup(const char *filename, LogLuaMasterCtx *ctx)
{
lua_State *luastate = LuaGetState();
if (luastate == NULL) {
luaL_openlibs(luastate);
SCLuaRequirefBuiltIns(luastate);
+ LuaSetPaths(luastate, ctx);
int status = luaL_loadfile(luastate, filename);
if (status) {
SCMutexInit(&lua_ctx->m, NULL);
- const char *dir = "";
- if (parent_ctx && parent_ctx->data) {
- LogLuaMasterCtx *mc = parent_ctx->data;
- dir = mc->script_dir;
- }
+ BUG_ON(parent_ctx == NULL);
+ LogLuaMasterCtx *mc = parent_ctx->data;
+ BUG_ON(mc == NULL);
+ const char *dir = mc->script_dir;
char path[PATH_MAX] = "";
int ret = snprintf(path, sizeof(path),"%s%s%s", dir, strlen(dir) ? "/" : "", conf->val);
if (ret < 0 || ret == sizeof(path)) {
SCLogDebug("script full path %s", path);
SCMutexLock(&lua_ctx->m);
- lua_ctx->luastate = LuaScriptSetup(path);
+ lua_ctx->luastate = LuaScriptSetup(path, mc);
SCMutexUnlock(&lua_ctx->m);
if (lua_ctx->luastate == NULL)
goto error;
}
LogLuaMasterCtx *master_config = output_ctx->data;
strlcpy(master_config->script_dir, dir, sizeof(master_config->script_dir));
+
+ const char *lua_path = ConfNodeLookupChildValue(conf, "path");
+ if (lua_path && strlen(lua_path) > 0) {
+ strlcpy(master_config->path, lua_path, sizeof(master_config->path));
+ }
+
+ const char *lua_cpath = ConfNodeLookupChildValue(conf, "cpath");
+ if (lua_cpath && strlen(lua_cpath) > 0) {
+ strlcpy(master_config->cpath, lua_cpath, sizeof(master_config->cpath));
+ }
+
TAILQ_INIT(&output_ctx->submodules);
/* check the enables scripts and set them up as submodules */
snprintf(path, sizeof(path),"%s%s%s", dir, strlen(dir) ? "/" : "", script->val);
SCLogDebug("script full path %s", path);
- int r = LuaScriptInit(path, &opts);
+ int r = LuaScriptInit(path, &opts, master_config);
if (r != 0) {
SCLogError("couldn't initialize script");
goto error;