]> source.dussan.org Git - rspamd.git/commitdiff
Remove global rspamd_main.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 9 Oct 2015 16:35:44 +0000 (17:35 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 9 Oct 2015 16:35:44 +0000 (17:35 +0100)
src/CMakeLists.txt
src/controller.c
src/fuzzy_storage.c
src/http_proxy.c
src/libserver/rspamd_control.c [new file with mode: 0644]
src/libserver/rspamd_control.h [new file with mode: 0644]
src/lua_worker.c
src/rspamd.c
src/rspamd.h
src/smtp_proxy.c
src/worker.c

index 0490186dfbcd4e4e241f5c1f0120470be5da3669..40882da54dba2b1db331604c919e900556f9e86e 100644 (file)
@@ -85,7 +85,7 @@ SET(PLUGINSSRC        plugins/surbl.c
                                plugins/chartable.c
                                plugins/fuzzy_check.c
                                plugins/spf.c
-                               plugins/dkim_check.c libutil/unix-std.h)
+                               plugins/dkim_check.c libutil/unix-std.h libserver/rspamd_control.c libserver/rspamd_control.h)
                                
 SET(MODULES_LIST surbl regexp chartable fuzzy_check spf dkim)
 SET(WORKERS_LIST normal controller smtp_proxy fuzzy lua http_proxy)
index 6d14342d7b925238a43ab2c7f22d03afd3709344..7ad504f30e3f882448aa20819f737b682f4484a7 100644 (file)
@@ -107,7 +107,6 @@ const struct timeval rrd_update_time = {
                .tv_usec = 0
 };
 
-extern struct rspamd_main *rspamd_main;
 gpointer init_controller_worker (struct rspamd_config *cfg);
 void start_controller_worker (struct rspamd_worker *worker);
 
@@ -2545,7 +2544,7 @@ start_controller_worker (struct rspamd_worker *worker)
        g_mime_shutdown ();
        rspamd_stat_close ();
        rspamd_http_router_free (ctx->http);
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
        rspamd_controller_store_saved_stats (ctx);
 
        if (ctx->rrd) {
index 873221d8cedd2b1a4bca1708d7130ed588e0f7e1..51bcc1001e0d81c700b0450a0d0cb5f4dfd0c5e4 100644 (file)
@@ -47,7 +47,6 @@
 
 #define INVALID_NODE_TIME (guint64) - 1
 
-extern struct rspamd_main *rspamd_main;
 /* Init functions */
 gpointer init_fuzzy (struct rspamd_config *cfg);
 void start_fuzzy (struct rspamd_worker *worker);
@@ -559,7 +558,7 @@ start_fuzzy (struct rspamd_worker *worker)
 
        rspamd_fuzzy_backend_sync (ctx->backend, ctx->expire, TRUE);
        rspamd_fuzzy_backend_close (ctx->backend);
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
 
        if (ctx->keypair_cache) {
                rspamd_keypair_cache_destroy (ctx->keypair_cache);
index 572d78578541e7383953de51f32f8950d4e57a67..c35b771acf5b6e471fa1525f1521a43eb49a44be 100644 (file)
@@ -39,7 +39,7 @@
 
 /* Rotate keys each minute by default */
 #define DEFAULT_ROTATION_TIME 60.0
-extern struct rspamd_main *rspamd_main;
+
 gpointer init_http_proxy (struct rspamd_config *cfg);
 void start_http_proxy (struct rspamd_worker *worker);
 
@@ -473,7 +473,7 @@ start_http_proxy (struct rspamd_worker *worker)
        event_base_loop (ctx->ev_base, 0);
 
        g_mime_shutdown ();
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
 
        if (ctx->key) {
                rspamd_http_connection_key_unref (ctx->key);
diff --git a/src/libserver/rspamd_control.c b/src/libserver/rspamd_control.c
new file mode 100644 (file)
index 0000000..99b7605
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *      * Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ *      * Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */#include "rspamd_control.h"
diff --git a/src/libserver/rspamd_control.h b/src/libserver/rspamd_control.h
new file mode 100644 (file)
index 0000000..d8a2599
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *      * Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ *      * Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef RSPAMD_RSPAMD_CONTROL_H
+#define RSPAMD_RSPAMD_CONTROL_H
+
+
+
+#endif
index b2a3ffc193945e761011983cdd48d2cdd8276adf..7e15c632a791f153fc479fa77cfb7cae9b77a45a 100644 (file)
@@ -43,7 +43,7 @@
 
 /* 60 seconds for worker's IO */
 #define DEFAULT_WORKER_IO_TIMEOUT 60000
-extern struct rspamd_main *rspamd_main;
+
 gpointer init_lua_worker (struct rspamd_config *cfg);
 void start_lua_worker (struct rspamd_worker *worker);
 
@@ -409,7 +409,7 @@ start_lua_worker (struct rspamd_worker *worker)
                luaL_unref (L, LUA_REGISTRYINDEX, ctx->cbref_fin);
        }
 
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
        exit (EXIT_SUCCESS);
 }
 
index 01416b593c605f9058a11c409aa6b43ae50b7035..596a2511c37d08503ae13c7417f746a33f44a73f 100644 (file)
@@ -28,6 +28,7 @@
 #include "fuzzy_storage.h"
 #include "lua/lua_common.h"
 #include "libserver/worker_util.h"
+#include "libserver/rspamd_control.h"
 #include "ottery.h"
 #include "xxhash.h"
 #include "utlist.h"
 #include <openssl/evp.h>
 #endif
 
-#define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
-        rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
-        G_STRFUNC, \
-        __VA_ARGS__)
-#define msg_warn_main(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
-        rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
-        G_STRFUNC, \
-        __VA_ARGS__)
-#define msg_info_main(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
-        rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
-        G_STRFUNC, \
-        __VA_ARGS__)
-#define msg_debug_main(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
-        rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
-        G_STRFUNC, \
-        __VA_ARGS__)
-
 /* 2 seconds to fork new process in place of dead one */
 #define SOFT_FORK_TIME 2
 
 /* 10 seconds after getting termination signal to terminate all workers with SIGKILL */
 #define TERMINATION_ATTEMPTS 40
 
-static struct rspamd_worker * fork_worker (struct rspamd_main *,
-       struct rspamd_worker_conf *, guint);
-static gboolean load_rspamd_config (struct rspamd_config *cfg,
-       gboolean init_modules);
+static gboolean load_rspamd_config (struct rspamd_main *rspamd_main,
+               struct rspamd_config *cfg,
+               gboolean init_modules);
 
 /* Control socket */
 static gint control_fd;
@@ -130,8 +113,6 @@ static GArray *other_workers = NULL;
 /* List of active listen sockets indexed by worker type */
 static GHashTable *listen_sockets = NULL;
 
-struct rspamd_main *rspamd_main;
-
 /* Defined in modules.c */
 extern module_t *modules[];
 extern worker_t *workers[];
@@ -224,7 +205,7 @@ read_cmd_line (gint *argc, gchar ***argv, struct rspamd_config *cfg)
 
 /* Detect privilleged mode */
 static void
-detect_priv (struct rspamd_main *rspamd)
+detect_priv (struct rspamd_main *rspamd_main)
 {
        struct passwd *pwd;
        struct group *grp;
@@ -233,104 +214,82 @@ detect_priv (struct rspamd_main *rspamd)
        euid = geteuid ();
 
        if (euid == 0) {
-               if (!rspamd->cfg->rspamd_user && !is_insecure) {
+               if (!rspamd_main->cfg->rspamd_user && !is_insecure) {
                        msg_err_main (
                                "cannot run rspamd workers as root user, please add -u and -g options to select a proper unprivilleged user or specify --insecure flag");
                        exit (EXIT_FAILURE);
                }
                else if (is_insecure) {
-                       rspamd->is_privilleged = TRUE;
-                       rspamd->workers_uid = 0;
-                       rspamd->workers_gid = 0;
+                       rspamd_main->is_privilleged = TRUE;
+                       rspamd_main->workers_uid = 0;
+                       rspamd_main->workers_gid = 0;
                }
                else {
-                       rspamd->is_privilleged = TRUE;
-                       pwd = getpwnam (rspamd->cfg->rspamd_user);
+                       rspamd_main->is_privilleged = TRUE;
+                       pwd = getpwnam (rspamd_main->cfg->rspamd_user);
                        if (pwd == NULL) {
                                msg_err_main ("user specified does not exists (%s), aborting",
                                        strerror (errno));
                                exit (-errno);
                        }
-                       if (rspamd->cfg->rspamd_group) {
-                               grp = getgrnam (rspamd->cfg->rspamd_group);
+                       if (rspamd_main->cfg->rspamd_group) {
+                               grp = getgrnam (rspamd_main->cfg->rspamd_group);
                                if (grp == NULL) {
                                        msg_err_main ("group specified does not exists (%s), aborting",
                                                strerror (errno));
                                        exit (-errno);
                                }
-                               rspamd->workers_gid = grp->gr_gid;
+                               rspamd_main->workers_gid = grp->gr_gid;
                        }
                        else {
-                               rspamd->workers_gid = (gid_t)-1;
+                               rspamd_main->workers_gid = (gid_t)-1;
                        }
-                       rspamd->workers_uid = pwd->pw_uid;
+                       rspamd_main->workers_uid = pwd->pw_uid;
                }
        }
        else {
-               rspamd->is_privilleged = FALSE;
-               rspamd->workers_uid = (uid_t)-1;
-               rspamd->workers_gid = (gid_t)-1;
-       }
-}
-
-static void
-drop_priv (struct rspamd_main *rspamd)
-{
-       if (rspamd->is_privilleged) {
-               if (setgid (rspamd->workers_gid) == -1) {
-                       msg_err_main ("cannot setgid to %d (%s), aborting",
-                               (gint)rspamd->workers_gid,
-                               strerror (errno));
-                       exit (-errno);
-               }
-               if (rspamd->cfg->rspamd_user &&
-                       initgroups (rspamd->cfg->rspamd_user, rspamd->workers_gid) == -1) {
-                       msg_err_main ("initgroups failed (%s), aborting", strerror (errno));
-                       exit (-errno);
-               }
-               if (setuid (rspamd->workers_uid) == -1) {
-                       msg_err_main ("cannot setuid to %d (%s), aborting",
-                               (gint)rspamd->workers_uid,
-                               strerror (errno));
-                       exit (-errno);
-               }
+               rspamd_main->is_privilleged = FALSE;
+               rspamd_main->workers_uid = (uid_t)-1;
+               rspamd_main->workers_gid = (gid_t)-1;
        }
 }
 
 static void
 config_logger (rspamd_mempool_t *pool, gpointer ud)
 {
-       struct rspamd_main *rm = ud;
+       struct rspamd_main *rspamd_main = ud;
 
        if (config_test) {
                /* Explicitly set logger type to console in case of config testing */
-               rm->cfg->log_type = RSPAMD_LOG_CONSOLE;
+               rspamd_main->cfg->log_type = RSPAMD_LOG_CONSOLE;
        }
 
-       rspamd_set_logger (rm->cfg, g_quark_try_string ("main"), rm);
-       if (rspamd_log_open_priv (rm->logger, rm->workers_uid, rm->workers_gid) == -1) {
+       rspamd_set_logger (rspamd_main->cfg, g_quark_try_string ("main"),
+                       rspamd_main);
+       if (rspamd_log_open_priv (rspamd_main->logger,
+                       rspamd_main->workers_uid, rspamd_main->workers_gid) == -1) {
                fprintf (stderr, "Fatal error, cannot open logfile, exiting\n");
                exit (EXIT_FAILURE);
        }
 }
 
 static void
-reread_config (struct rspamd_main *rspamd)
+reread_config (struct rspamd_main *rspamd_main)
 {
        struct rspamd_config *tmp_cfg;
        gchar *cfg_file;
 
        tmp_cfg = (struct rspamd_config *)g_malloc0 (sizeof (struct rspamd_config));
-       tmp_cfg->c_modules = g_hash_table_ref (rspamd->cfg->c_modules);
-       rspamd_set_logger (tmp_cfg,  g_quark_try_string ("main"), rspamd);
+       tmp_cfg->c_modules = g_hash_table_ref (rspamd_main->cfg->c_modules);
+       rspamd_set_logger (tmp_cfg,  g_quark_try_string ("main"), rspamd_main);
        rspamd_init_cfg (tmp_cfg, TRUE);
        cfg_file = rspamd_mempool_strdup (tmp_cfg->cfg_pool,
-                       rspamd->cfg->cfg_name);
+                       rspamd_main->cfg->cfg_name);
        tmp_cfg->cache = rspamd_symbols_cache_new (tmp_cfg);
        /* Save some variables */
        tmp_cfg->cfg_name = cfg_file;
 
-       if (!load_rspamd_config (tmp_cfg, FALSE)) {
+       if (!load_rspamd_config (rspamd_main, tmp_cfg, FALSE)) {
                rspamd_set_logger (rspamd_main->cfg, g_quark_try_string (
                                "main"), rspamd_main);
                msg_err_main ("cannot parse new config file, revert to old one");
@@ -339,128 +298,24 @@ reread_config (struct rspamd_main *rspamd)
        else {
                msg_debug_main ("replacing config");
                rspamd_symbols_cache_destroy (rspamd_main->cfg->cache);
-               rspamd_config_free (rspamd->cfg);
-               g_free (rspamd->cfg);
+               rspamd_config_free (rspamd_main->cfg);
+               g_free (rspamd_main->cfg);
 
-               rspamd->cfg = tmp_cfg;
-               rspamd_set_logger (tmp_cfg,  g_quark_try_string ("main"), rspamd);
+               rspamd_main->cfg = tmp_cfg;
+               rspamd_set_logger (tmp_cfg,  g_quark_try_string ("main"), rspamd_main);
                /* Force debug log */
                if (is_debug) {
-                       rspamd->cfg->log_level = G_LOG_LEVEL_DEBUG;
+                       rspamd_main->cfg->log_level = G_LOG_LEVEL_DEBUG;
                }
 
-               rspamd_init_filters (rspamd->cfg, TRUE);
-               rspamd_symbols_cache_init (rspamd->cfg->cache);
+               rspamd_init_filters (rspamd_main->cfg, TRUE);
+               rspamd_symbols_cache_init (rspamd_main->cfg->cache);
                msg_info_main ("config has been reread successfully");
        }
 }
 
-static void
-set_worker_limits (struct rspamd_worker_conf *cf)
-{
-       struct rlimit rlmt;
-
-       if (cf->rlimit_nofile != 0) {
-               rlmt.rlim_cur = (rlim_t) cf->rlimit_nofile;
-               rlmt.rlim_max = (rlim_t) cf->rlimit_nofile;
-
-               if (setrlimit (RLIMIT_NOFILE, &rlmt) == -1) {
-                       msg_warn_main ("cannot set files rlimit: %d, %s",
-                               cf->rlimit_nofile,
-                               strerror (errno));
-               }
-       }
-
-       if (cf->rlimit_maxcore != 0) {
-               rlmt.rlim_cur = (rlim_t) cf->rlimit_maxcore;
-               rlmt.rlim_max = (rlim_t) cf->rlimit_maxcore;
-
-               if (setrlimit (RLIMIT_CORE, &rlmt) == -1) {
-                       msg_warn_main ("cannot set max core rlimit: %d, %s",
-                               cf->rlimit_maxcore,
-                               strerror (errno));
-               }
-       }
-}
-
-static struct rspamd_worker *
-fork_worker (struct rspamd_main *rspamd, struct rspamd_worker_conf *cf,
-               guint index)
-{
-       struct rspamd_worker *cur;
-       /* Starting worker process */
-       cur = (struct rspamd_worker *)g_malloc0 (sizeof (struct rspamd_worker));
-
-       if (!rspamd_socketpair (cur->control_pipe)) {
-               msg_err ("socketpair failure: %s", strerror (errno));
-               exit (-errno);
-       }
-
-       cur->srv = rspamd;
-       cur->type = cf->type;
-       cur->cf = g_malloc (sizeof (struct rspamd_worker_conf));
-       memcpy (cur->cf, cf, sizeof (struct rspamd_worker_conf));
-       cur->index = index;
-       cur->ctx = cf->ctx;
-
-       cur->pid = fork ();
-
-       switch (cur->pid) {
-       case 0:
-               /* Update pid for logging */
-               rspamd_log_update_pid (cf->type, rspamd->logger);
-               /* Lock statfile pool if possible XXX */
-               /* Init PRNG after fork */
-               ottery_init (NULL);
-               g_random_set_seed (ottery_rand_uint32 ());
-               /* Drop privilleges */
-               drop_priv (rspamd);
-               /* Set limits */
-               set_worker_limits (cf);
-               setproctitle ("%s process", cf->worker->name);
-               rspamd_pidfile_close (rspamd->pfh);
-               /* Do silent log reopen to avoid collisions */
-               rspamd_log_close (rspamd->logger);
-               rspamd_log_open (rspamd->logger);
-#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION <= 30))
-               # if (GLIB_MINOR_VERSION > 20)
-                       /* Ugly hack for old glib */
-                       if (!g_thread_get_initialized ()) {
-                               g_thread_init (NULL);
-                       }
-# else
-                       g_thread_init (NULL);
-# endif
-#endif
-               msg_info_main ("starting %s process %P", cf->worker->name, getpid ());
-               /* Close parent part of socketpair */
-               close (cur->control_pipe[0]);
-               /* Set non-blocking on the worker part of socketpair */
-               rspamd_socket_nonblocking (cur->control_pipe[1]);
-               /* Execute worker */
-               cf->worker->worker_start_func (cur);
-               break;
-       case -1:
-               msg_err_main ("cannot fork main process. %s", strerror (errno));
-               rspamd_pidfile_remove (rspamd->pfh);
-               exit (-errno);
-               break;
-       default:
-               /* Close worker part of socketpair */
-               close (cur->control_pipe[1]);
-               /* Set blocking on the main part of socketpair */
-               rspamd_socket_nonblocking (cur->control_pipe[0]);
-               /* Insert worker into worker's table, pid is index */
-               g_hash_table_insert (rspamd->workers, GSIZE_TO_POINTER (
-                               cur->pid), cur);
-               break;
-       }
-
-       return cur;
-}
-
 struct waiting_worker {
-       struct event_base *ev_base;
+       struct rspamd_main *rspamd_main;
        struct event wait_ev;
        struct rspamd_worker_conf *cf;
        guint oldindex;
@@ -472,14 +327,14 @@ rspamd_fork_delayed_cb (gint signo, short what, gpointer arg)
        struct waiting_worker *w = arg;
 
        event_del (&w->wait_ev);
-       fork_worker (rspamd_main, w->cf, w->oldindex);
+       rspamd_fork_worker (w->rspamd_main, w->cf, w->oldindex);
        g_slice_free1 (sizeof (*w), w);
 }
 
 static void
 rspamd_fork_delayed (struct rspamd_worker_conf *cf,
                guint index,
-               struct event_base *ev_base)
+               struct rspamd_main *rspamd_main)
 {
        struct waiting_worker *nw;
        struct timeval tv;
@@ -487,11 +342,11 @@ rspamd_fork_delayed (struct rspamd_worker_conf *cf,
        nw = g_slice_alloc (sizeof (*nw));
        nw->cf = cf;
        nw->oldindex = index;
-       nw->ev_base = ev_base;
+       nw->rspamd_main = rspamd_main;
        tv.tv_sec = SOFT_FORK_TIME;
        tv.tv_usec = 0;
        event_set (&nw->wait_ev, -1, EV_TIMEOUT, rspamd_fork_delayed_cb, nw);
-       event_base_set (ev_base, &nw->wait_ev);
+       event_base_set (rspamd_main->ev_base, &nw->wait_ev);
        event_add (&nw->wait_ev, &tv);
 }
 
@@ -515,7 +370,7 @@ create_listen_socket (GPtrArray *addrs, guint cnt, gint listen_type)
 }
 
 static GList *
-systemd_get_socket (gint number)
+systemd_get_socket (struct rspamd_main *rspamd_main, gint number)
 {
        int sock, num_passed, flags;
        GList *result = NULL;
@@ -590,7 +445,7 @@ make_listen_key (struct rspamd_worker_bind_conf *cf)
 }
 
 static void
-spawn_workers (struct rspamd_main *rspamd)
+spawn_workers (struct rspamd_main *rspamd_main)
 {
        GList *cur, *ls;
        struct rspamd_worker_conf *cf;
@@ -600,7 +455,7 @@ spawn_workers (struct rspamd_main *rspamd)
        struct rspamd_worker_bind_conf *bcf;
        gboolean listen_ok = FALSE;
 
-       cur = rspamd->cfg->workers;
+       cur = rspamd_main->cfg->workers;
 
        while (cur) {
                cf = cur->data;
@@ -622,7 +477,7 @@ spawn_workers (struct rspamd_main *rspamd)
                                                                        cf->worker->listen_type);
                                                }
                                                else {
-                                                       ls = systemd_get_socket (bcf->cnt);
+                                                       ls = systemd_get_socket (rspamd_main, bcf->cnt);
                                                }
                                                if (ls == NULL) {
                                                        msg_err_main ("cannot listen on socket %s: %s",
@@ -652,14 +507,14 @@ spawn_workers (struct rspamd_main *rspamd)
                                                msg_warn_main ("cannot spawn more than 1 %s worker, so spawn one",
                                                                cf->worker->name);
                                        }
-                                       fork_worker (rspamd, cf, 0);
+                                       rspamd_fork_worker (rspamd_main, cf, 0);
                                }
                                else if (cf->worker->threaded) {
-                                       fork_worker (rspamd, cf, 0);
+                                       rspamd_fork_worker (rspamd_main, cf, 0);
                                }
                                else {
                                        for (i = 0; i < cf->count; i++) {
-                                               fork_worker (rspamd, cf, i);
+                                               rspamd_fork_worker (rspamd_main, cf, i);
                                        }
                                }
                        }
@@ -679,7 +534,9 @@ static void
 kill_old_workers (gpointer key, gpointer value, gpointer unused)
 {
        struct rspamd_worker *w = value;
+       struct rspamd_main *rspamd_main;
 
+       rspamd_main = w->srv;
        kill (w->pid, SIGUSR2);
        msg_info_main ("send signal to worker %P", w->pid);
 }
@@ -688,8 +545,11 @@ static gboolean
 wait_for_workers (gpointer key, gpointer value, gpointer unused)
 {
        struct rspamd_worker *w = value;
+       struct rspamd_main *rspamd_main;
        gint res = 0;
 
+       rspamd_main = w->srv;
+
        if (waitpid (w->pid, &res, WNOHANG) <= 0) {
                if (term_attempts == 0) {
                        if (w->cf->worker->killable) {
@@ -718,6 +578,9 @@ static void
 reopen_log_handler (gpointer key, gpointer value, gpointer unused)
 {
        struct rspamd_worker *w = value;
+       struct rspamd_main *rspamd_main;
+
+       rspamd_main = w->srv;
 
        if (kill (w->pid, SIGUSR1) == -1) {
                msg_err_main ("kill failed for pid %P: %s", w->pid, strerror (errno));
@@ -725,7 +588,8 @@ reopen_log_handler (gpointer key, gpointer value, gpointer unused)
 }
 
 static gboolean
-load_rspamd_config (struct rspamd_config *cfg, gboolean init_modules)
+load_rspamd_config (struct rspamd_main *rspamd_main,
+               struct rspamd_config *cfg, gboolean init_modules)
 {
        cfg->cache = rspamd_symbols_cache_new (cfg);
        cfg->compiled_modules = modules;
@@ -782,17 +646,19 @@ do_encrypt_password (void)
 static void
 rspamd_term_handler (gint signo, short what, gpointer arg)
 {
-       struct event_base *ev_base = arg;
+       struct rspamd_main *rspamd_main = arg;
 
        msg_info_main ("catch termination signal, waiting for children");
        rspamd_pass_signal (rspamd_main->workers, SIGTERM);
 
-       event_base_loopexit (ev_base, NULL);
+       event_base_loopexit (rspamd_main->ev_base, NULL);
 }
 
 static void
 rspamd_usr1_handler (gint signo, short what, gpointer arg)
 {
+       struct rspamd_main *rspamd_main = arg;
+
        rspamd_log_reopen_priv (rspamd_main->logger,
                        rspamd_main->workers_uid,
                        rspamd_main->workers_gid);
@@ -803,6 +669,8 @@ rspamd_usr1_handler (gint signo, short what, gpointer arg)
 static void
 rspamd_hup_handler (gint signo, short what, gpointer arg)
 {
+       struct rspamd_main *rspamd_main = arg;
+
        rspamd_log_reopen_priv (rspamd_main->logger,
                        rspamd_main->workers_uid,
                        rspamd_main->workers_gid);
@@ -818,7 +686,7 @@ rspamd_hup_handler (gint signo, short what, gpointer arg)
 static void
 rspamd_cld_handler (gint signo, short what, gpointer arg)
 {
-       struct event_base *ev_base = arg;
+       struct rspamd_main *rspamd_main = arg;
        guint i;
        gint res = 0;
        struct rspamd_worker *cur;
@@ -876,7 +744,7 @@ rspamd_cld_handler (gint signo, short what, gpointer arg)
                                                WEXITSTATUS (res));
                        }
                        /* Fork another worker in replace of dead one */
-                       rspamd_fork_delayed (cur->cf, cur->index, ev_base);
+                       rspamd_fork_delayed (cur->cf, cur->index, rspamd_main);
                }
 
                g_free (cur);
@@ -894,14 +762,14 @@ rspamd_cld_handler (gint signo, short what, gpointer arg)
 static void
 rspamd_final_term_handler (gint signo, short what, gpointer arg)
 {
-       struct event_base *ev_base = arg;
+       struct rspamd_main *rspamd_main = arg;
 
        term_attempts --;
 
        g_hash_table_foreach_remove (rspamd_main->workers, wait_for_workers, NULL);
 
        if (g_hash_table_size (rspamd_main->workers) == 0) {
-               event_base_loopexit (ev_base, NULL);
+               event_base_loopexit (rspamd_main->ev_base, NULL);
        }
 }
 
@@ -909,6 +777,7 @@ rspamd_final_term_handler (gint signo, short what, gpointer arg)
 static void
 rspamd_control_handler (gint fd, short what, gpointer arg)
 {
+       struct rspamd_main *rspamd_main = arg;
        rspamd_inet_addr_t *addr;
        gint nfd;
 
@@ -937,6 +806,7 @@ main (gint argc, gchar **argv, gchar **env)
        struct event_base *ev_base;
        struct event term_ev, int_ev, cld_ev, hup_ev, usr1_ev, control_ev;
        struct timeval term_tv;
+       struct rspamd_main *rspamd_main;
 
 #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION <= 30))
        g_thread_init (NULL);
@@ -1041,7 +911,7 @@ main (gint argc, gchar **argv, gchar **env)
        }
 
        /* Load config */
-       if (!load_rspamd_config (rspamd_main->cfg, TRUE)) {
+       if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE)) {
                exit (EXIT_FAILURE);
        }
 
@@ -1140,24 +1010,25 @@ main (gint argc, gchar **argv, gchar **env)
 
        /* Init event base */
        ev_base = event_init ();
+       rspamd_main->ev_base = ev_base;
        /* Unblock signals */
        sigemptyset (&signals.sa_mask);
        sigprocmask (SIG_SETMASK, &signals.sa_mask, NULL);
 
        /* Set events for signals */
-       evsignal_set (&term_ev, SIGTERM, rspamd_term_handler, ev_base);
+       evsignal_set (&term_ev, SIGTERM, rspamd_term_handler, rspamd_main);
        event_base_set (ev_base, &term_ev);
        event_add (&term_ev, NULL);
-       evsignal_set (&int_ev, SIGINT, rspamd_term_handler, ev_base);
+       evsignal_set (&int_ev, SIGINT, rspamd_term_handler, rspamd_main);
        event_base_set (ev_base, &int_ev);
        event_add (&int_ev, NULL);
-       evsignal_set (&hup_ev, SIGHUP, rspamd_hup_handler, ev_base);
+       evsignal_set (&hup_ev, SIGHUP, rspamd_hup_handler, rspamd_main);
        event_base_set (ev_base, &hup_ev);
        event_add (&hup_ev, NULL);
-       evsignal_set (&cld_ev, SIGCHLD, rspamd_cld_handler, ev_base);
+       evsignal_set (&cld_ev, SIGCHLD, rspamd_cld_handler, rspamd_main);
        event_base_set (ev_base, &cld_ev);
        event_add (&cld_ev, NULL);
-       evsignal_set (&usr1_ev, SIGUSR1, rspamd_usr1_handler, ev_base);
+       evsignal_set (&usr1_ev, SIGUSR1, rspamd_usr1_handler, rspamd_main);
        event_base_set (ev_base, &usr1_ev);
        event_add (&usr1_ev, NULL);
 
@@ -1165,7 +1036,7 @@ main (gint argc, gchar **argv, gchar **env)
                msg_info_main ("listening for control commands on %s",
                                rspamd_inet_address_to_string (control_addr));
                event_set (&control_ev, control_fd, EV_READ|EV_PERSIST,
-                               rspamd_control_handler, ev_base);
+                               rspamd_control_handler, rspamd_main);
                event_base_set (ev_base, &control_ev);
                event_add (&control_ev, NULL);
        }
@@ -1194,7 +1065,7 @@ main (gint argc, gchar **argv, gchar **env)
 
        event_del (&term_ev);
        event_set (&term_ev, -1, EV_TIMEOUT|EV_PERSIST,
-                       rspamd_final_term_handler, ev_base);
+                       rspamd_final_term_handler, rspamd_main);
        event_base_set (ev_base, &term_ev);
        event_add (&term_ev, &term_tv);
 
index 4e8201d83b6e5ea0a0b1abee51046e4e3e9d0366..1b20b765d872e3863ed24cc3d35f1f57f6ddee2e 100644 (file)
@@ -166,6 +166,7 @@ struct rspamd_main {
        gid_t workers_gid;                                          /**< worker's gid running to                                                */
        gboolean is_privilleged;                                    /**< true if run in privilleged mode                */
        struct roll_history *history;                               /**< rolling history                                                                */
+       struct event_base *ev_base;
 };
 
 /**
index e93ef69ad4b388ef823e178f83415fd02691e928..40e5ce4737b8b8a0afa38622255ee4aaed4171ef 100644 (file)
@@ -43,9 +43,6 @@
 #define DEFAULT_PROXY_BUF_LEN 100 * 1024
 
 #define SMTP_MAXERRORS 15
-
-extern struct rspamd_main *rspamd_main;
-
 /* Init functions */
 gpointer init_smtp_proxy (struct rspamd_config *cfg);
 void start_smtp_proxy (struct rspamd_worker *worker);
@@ -659,7 +656,7 @@ smtp_dns_cb (struct rdns_reply *reply, void *arg)
        case SMTP_PROXY_STATE_RESOLVE_REVERSE:
                /* Parse reverse reply and start resolve of this ip */
                if (reply->code != RDNS_RC_NOERROR) {
-                       rspamd_conditional_debug (rspamd_main->logger,
+                       rspamd_conditional_debug (session->worker->srv->logger,
                                NULL, "rdns", NULL, G_STRFUNC, "DNS error: %s",
                                rdns_strerror (reply->code));
 
@@ -688,7 +685,7 @@ smtp_dns_cb (struct rdns_reply *reply, void *arg)
                break;
        case SMTP_PROXY_STATE_RESOLVE_NORMAL:
                if (reply->code != RDNS_RC_NOERROR) {
-                       rspamd_conditional_debug (rspamd_main->logger,
+                       rspamd_conditional_debug (session->worker->srv->logger,
                                NULL, "rdns", NULL, G_STRFUNC, "DNS error: %s",
                                rdns_strerror (reply->code));
 
@@ -1083,7 +1080,7 @@ start_smtp_proxy (struct rspamd_worker *worker)
 
        event_base_loop (ctx->ev_base, 0);
 
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
        exit (EXIT_SUCCESS);
 }
 
index a127d14d882ba34632c413428301b37a38b9871a..d103d16601e1f52f73a54738c5737c83309475b3 100644 (file)
@@ -44,7 +44,6 @@
 
 /* 60 seconds for worker's IO */
 #define DEFAULT_WORKER_IO_TIMEOUT 60000
-extern struct rspamd_main *rspamd_main;
 
 gpointer init_worker (struct rspamd_config *cfg);
 void start_worker (struct rspamd_worker *worker);
@@ -334,7 +333,7 @@ start_worker (struct rspamd_worker *worker)
 
        g_mime_shutdown ();
        rspamd_stat_close ();
-       rspamd_log_close (rspamd_main->logger);
+       rspamd_log_close (worker->srv->logger);
 
        if (ctx->key) {
                rspamd_http_connection_key_unref (ctx->key);