commit fbe966f: [Rework] Change lua global variables registration

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Mar 26 18:28:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-03-26 16:38:27 +0000
URL: https://github.com/rspamd/rspamd/commit/fbe966f7b9d6f1655cbce792fb60d2d1b4633616

[Rework] Change lua global variables registration

---
 src/libserver/cfg_rcl.c |   4 +-
 src/lua/lua_common.c    | 147 ++++++++++++++++++++++++++++++++----------------
 src/lua/lua_common.h    |   4 +-
 src/rspamadm/rspamadm.c |   3 +-
 test/rspamd_lua_test.c  |   3 +-
 5 files changed, 106 insertions(+), 55 deletions(-)

diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index 9a2d7cd19..c6d2f3572 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -3678,6 +3678,8 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
 	struct rspamd_rcl_section *top, *logger_section;
 	const ucl_object_t *logger_obj;
 
+	rspamd_lua_set_env (cfg->lua_state, vars);
+
 	if (!rspamd_config_parse_ucl (cfg, filename, vars, NULL, NULL, &err)) {
 		msg_err_config_forced ("failed to load config: %e", err);
 		g_error_free (err);
@@ -3687,7 +3689,7 @@ rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
 
 	top = rspamd_rcl_config_init (cfg, NULL);
 	rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, vars);
-	rspamd_lua_set_globals (cfg, cfg->lua_state, vars);
+	rspamd_lua_set_globals (cfg, cfg->lua_state);
 	rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_rcl_section_free, top);
 	err = NULL;
 
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index cff684aad..7f49c090d 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -538,59 +538,10 @@ rspamd_lua_rspamd_version (lua_State *L)
 }
 
 void
-rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L,
-							GHashTable *vars)
+rspamd_lua_set_env (lua_State *L, GHashTable *vars)
 {
-	struct rspamd_config **pcfg;
 	gint orig_top = lua_gettop (L);
 
-	/* First check for global variable 'config' */
-	lua_getglobal (L, "config");
-	if (lua_isnil (L, -1)) {
-		/* Assign global table to set up attributes */
-		lua_newtable (L);
-		lua_setglobal (L, "config");
-	}
-
-	lua_getglobal (L, "metrics");
-	if (lua_isnil (L, -1)) {
-		lua_newtable (L);
-		lua_setglobal (L, "metrics");
-	}
-
-	lua_getglobal (L, "composites");
-	if (lua_isnil (L, -1)) {
-		lua_newtable (L);
-		lua_setglobal (L, "composites");
-	}
-
-	lua_getglobal (L, "rspamd_classifiers");
-	if (lua_isnil (L, -1)) {
-		lua_newtable (L);
-		lua_setglobal (L, "rspamd_classifiers");
-	}
-
-	lua_getglobal (L, "classifiers");
-	if (lua_isnil (L, -1)) {
-		lua_newtable (L);
-		lua_setglobal (L, "classifiers");
-	}
-
-	lua_getglobal (L, "rspamd_version");
-	if (lua_isnil (L, -1)) {
-		lua_pushcfunction (L, rspamd_lua_rspamd_version);
-		lua_setglobal (L, "rspamd_version");
-	}
-
-	if (cfg != NULL) {
-		pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
-		rspamd_lua_setclass (L, "rspamd{config}", -1);
-		*pcfg = cfg;
-		lua_setglobal (L, "rspamd_config");
-	}
-
-	lua_settop (L, orig_top);
-
 	/* Set known paths as rspamd_paths global */
 	lua_getglobal (L, "rspamd_paths");
 	if (lua_isnil (L, -1)) {
@@ -728,6 +679,102 @@ rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L,
 		lua_setglobal (L, "rspamd_paths");
 	}
 
+	lua_getglobal (L, "rspamd_env");
+	if (lua_isnil (L, -1)) {
+		lua_newtable (L);
+
+		if (vars != NULL) {
+			GHashTableIter it;
+			gpointer k, v;
+
+			g_hash_table_iter_init (&it, vars);
+
+			while (g_hash_table_iter_next (&it, &k, &v)) {
+				rspamd_lua_table_set (L, k, v);
+			}
+		}
+
+		gint hostlen = sysconf (_SC_HOST_NAME_MAX);
+
+		if (hostlen <= 0) {
+			hostlen = 256;
+		}
+		else {
+			hostlen ++;
+		}
+
+		gchar *hostbuf = g_alloca (hostlen);
+		memset (hostbuf, 0, hostlen);
+		gethostname (hostbuf, hostlen - 1);
+
+		rspamd_lua_table_set (L, "hostname", hostbuf);
+
+		rspamd_lua_table_set (L, "version", RVERSION);
+		rspamd_lua_table_set (L, "ver_major", RSPAMD_VERSION_MAJOR);
+		rspamd_lua_table_set (L, "ver_minor", RSPAMD_VERSION_MINOR);
+		rspamd_lua_table_set (L, "ver_patch", RSPAMD_VERSION_PATCH);
+		rspamd_lua_table_set (L, "ver_id", RID);
+		lua_pushstring (L, "ver_num");
+		lua_pushinteger (L, RSPAMD_VERSION_NUM);
+		lua_settable (L, -3);
+
+		lua_setglobal (L, "rspamd_env");
+	}
+
+	lua_settop (L, orig_top);
+}
+
+void
+rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L)
+{
+	struct rspamd_config **pcfg;
+	gint orig_top = lua_gettop (L);
+
+	/* First check for global variable 'config' */
+	lua_getglobal (L, "config");
+	if (lua_isnil (L, -1)) {
+		/* Assign global table to set up attributes */
+		lua_newtable (L);
+		lua_setglobal (L, "config");
+	}
+
+	lua_getglobal (L, "metrics");
+	if (lua_isnil (L, -1)) {
+		lua_newtable (L);
+		lua_setglobal (L, "metrics");
+	}
+
+	lua_getglobal (L, "composites");
+	if (lua_isnil (L, -1)) {
+		lua_newtable (L);
+		lua_setglobal (L, "composites");
+	}
+
+	lua_getglobal (L, "rspamd_classifiers");
+	if (lua_isnil (L, -1)) {
+		lua_newtable (L);
+		lua_setglobal (L, "rspamd_classifiers");
+	}
+
+	lua_getglobal (L, "classifiers");
+	if (lua_isnil (L, -1)) {
+		lua_newtable (L);
+		lua_setglobal (L, "classifiers");
+	}
+
+	lua_getglobal (L, "rspamd_version");
+	if (lua_isnil (L, -1)) {
+		lua_pushcfunction (L, rspamd_lua_rspamd_version);
+		lua_setglobal (L, "rspamd_version");
+	}
+
+	if (cfg != NULL) {
+		pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
+		rspamd_lua_setclass (L, "rspamd{config}", -1);
+		*pcfg = cfg;
+		lua_setglobal (L, "rspamd_config");
+	}
+
 	lua_settop (L, orig_top);
 }
 
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index c8858571a..520f5920d 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -306,8 +306,8 @@ void rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj,
 		GHashTable *vars);
 
 /* Set some lua globals */
-void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L,
-							 GHashTable *vars);
+void rspamd_lua_set_env (lua_State *L, GHashTable *vars);
+void rspamd_lua_set_globals (struct rspamd_config *cfg, lua_State *L);
 
 struct memory_pool_s * rspamd_lua_check_mempool (lua_State * L, gint pos);
 struct rspamd_config * lua_check_config (lua_State * L, gint pos);
diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c
index 0c6ffd419..227cbc86b 100644
--- a/src/rspamadm/rspamadm.c
+++ b/src/rspamadm/rspamadm.c
@@ -458,8 +458,9 @@ main (gint argc, gchar **argv, gchar **env)
 	setproctitle ("rspamdadm");
 
 	L = cfg->lua_state;
+	rspamd_lua_set_env (L, ucl_vars);
 	rspamd_lua_set_path (L, NULL, ucl_vars);
-	rspamd_lua_set_globals (cfg, L, ucl_vars);
+	rspamd_lua_set_globals (cfg, L);
 	rspamadm_add_lua_globals ();
 
 #ifdef WITH_HIREDIS
diff --git a/test/rspamd_lua_test.c b/test/rspamd_lua_test.c
index 15dd3923b..2af0d9aab 100644
--- a/test/rspamd_lua_test.c
+++ b/test/rspamd_lua_test.c
@@ -64,7 +64,8 @@ rspamd_lua_test_func (void)
 	glob_t globbuf;
 	gint i, len;
 
-	rspamd_lua_set_globals (rspamd_main->cfg, L, NULL);
+	rspamd_lua_set_env (L, NULL);
+	rspamd_lua_set_globals (rspamd_main->cfg, L);
 
 	if (lua_test_case) {
 		lua_pushstring (L, lua_test_case);


More information about the Commits mailing list