]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Allow to set priorities between post init scripts
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Dec 2020 17:23:31 +0000 (17:23 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Dec 2020 17:23:31 +0000 (17:23 +0000)
src/libserver/cfg_file.h
src/lua/lua_common.c
src/lua/lua_config.c

index 723f12c16ad616c84c32bc03a41183836a5ce1ab..50649b1060dfc93b2368d8446ed1f015707fe5e3 100644 (file)
@@ -311,6 +311,7 @@ struct rspamd_action;
 
 struct rspamd_config_cfg_lua_script {
        gint cbref;
+       gint priority;
        struct rspamd_config_cfg_lua_script *prev, *next;
 };
 
index 87474793c573c7cacfe871ea73b1dbffac1de339..482245ac979bb21638d1c6ba94bec2fec3d50318 100644 (file)
@@ -2159,8 +2159,8 @@ rspamd_lua_run_config_post_init (lua_State *L, struct rspamd_config *cfg)
                rspamd_lua_setclass (L, "rspamd{config}", -1);
 
                if (lua_pcall (L, 1, 0, err_idx) != 0) {
-                       msg_err_config ("cannot run config post init script: %s",
-                                       lua_tostring (L, -1));
+                       msg_err_config ("cannot run config post init script: %s; priority = %d",
+                                       lua_tostring (L, -1), sc->priority);
                }
 
                lua_settop (L, err_idx - 1);
index 83665450030bfded5f3836b67a53293eddd7eb68..89f93730de38eadaac28321925968c682f46c5a6 100644 (file)
@@ -3155,21 +3155,36 @@ lua_config_add_on_load (lua_State *L)
        return 0;
 }
 
+static inline int
+rspamd_post_init_sc_sort (const struct rspamd_config_cfg_lua_script *pra,
+                               const struct rspamd_config_cfg_lua_script *prb)
+{
+       /* Inverse sort */
+       return prb->priority - pra->priority;
+}
+
 static gint
 lua_config_add_post_init (lua_State *L)
 {
        LUA_TRACE_POINT;
        struct rspamd_config *cfg = lua_check_config (L, 1);
        struct rspamd_config_cfg_lua_script *sc;
+       guint priority = 0;
 
        if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) {
                return luaL_error (L, "invalid arguments");
        }
 
+       if (lua_type (L, 3) == LUA_TNUMBER) {
+               priority = lua_tointeger (L , 3);
+       }
+
        sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc));
        lua_pushvalue (L, 2);
        sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
+       sc->priority = priority;
        DL_APPEND (cfg->post_init_scripts, sc);
+       DL_SORT (cfg->post_init_scripts, rspamd_post_init_sc_sort);
 
        return 0;
 }