]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] More fixes to logger initialization
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 16 Jan 2017 15:03:22 +0000 (15:03 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 16 Jan 2017 15:03:46 +0000 (15:03 +0000)
MFH: true

src/libutil/logger.c
src/rspamd.c

index 923ac8dd9eb0fb7c5beb4aff463c3bb740a46bd1..04291d8e908b940042d4c9ecbdfff4efa57e2c07 100644 (file)
@@ -127,6 +127,14 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
        struct iovec *iov;
        const gchar *line;
        glong r;
+       gint fd;
+
+       if (rspamd_log->type == RSPAMD_LOG_CONSOLE) {
+               fd = STDERR_FILENO;
+       }
+       else {
+               fd = rspamd_log->fd;
+       }
 
        if (rspamd_log->enabled) {
                if (!rspamd_log->no_lock) {
@@ -135,20 +143,20 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
                                rspamd_mempool_lock_mutex (rspamd_log->mtx);
                        }
                        else {
-                               rspamd_file_lock (rspamd_log->fd, FALSE);
+                               rspamd_file_lock (fd, FALSE);
                        }
 #else
-                       rspamd_file_lock (rspamd_log->fd, FALSE);
+                       rspamd_file_lock (fd, FALSE);
 #endif
                }
 
                if (is_iov) {
                        iov = (struct iovec *) data;
-                       r = writev (rspamd_log->fd, iov, count);
+                       r = writev (fd, iov, count);
                }
                else {
                        line = (const gchar *) data;
-                       r = write (rspamd_log->fd, line, count);
+                       r = write (fd, line, count);
                }
 
                if (!rspamd_log->no_lock) {
@@ -157,10 +165,10 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
                                rspamd_mempool_unlock_mutex (rspamd_log->mtx);
                        }
                        else {
-                               rspamd_file_unlock (rspamd_log->fd, FALSE);
+                               rspamd_file_unlock (fd, FALSE);
                        }
 #else
-                       rspamd_file_unlock (rspamd_log->fd, FALSE);
+                       rspamd_file_unlock (fd, FALSE);
 #endif
                }
 
@@ -217,7 +225,7 @@ rspamd_log_open_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
                switch (rspamd_log->cfg->log_type) {
                case RSPAMD_LOG_CONSOLE:
                        /* Do nothing with console */
-                       rspamd_log->enabled = TRUE;
+                       rspamd_log->fd = -1;
                        break;
                case RSPAMD_LOG_SYSLOG:
 #ifdef HAVE_SYSLOG_H
@@ -271,38 +279,38 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
 #endif
                        break;
                case RSPAMD_LOG_FILE:
-                       if (rspamd_log->enabled) {
-                               if (rspamd_log->repeats > REPEATS_MIN) {
-                                       rspamd_snprintf (tmpbuf,
-                                                       sizeof (tmpbuf),
-                                                       "Last message repeated %ud times",
-                                                       rspamd_log->repeats);
-                                       rspamd_log->repeats = 0;
-                                       if (rspamd_log->saved_message) {
-                                               file_log_function (rspamd_log->saved_module,
-                                                               rspamd_log->saved_id,
-                                                               rspamd_log->saved_function,
-                                                               rspamd_log->saved_loglevel | RSPAMD_LOG_FORCED,
-                                                               rspamd_log->saved_message,
-                                                               rspamd_log);
-
-                                               g_free (rspamd_log->saved_message);
-                                               g_free (rspamd_log->saved_function);
-                                               g_free (rspamd_log->saved_module);
-                                               g_free (rspamd_log->saved_id);
-                                               rspamd_log->saved_message = NULL;
-                                               rspamd_log->saved_function = NULL;
-                                               rspamd_log->saved_module = NULL;
-                                               rspamd_log->saved_id = NULL;
-                                       }
-                                       /* It is safe to use temporary buffer here as it is not static */
-                                       file_log_function (NULL, NULL,
-                                                       G_STRFUNC,
+                       if (rspamd_log->repeats > REPEATS_MIN) {
+                               rspamd_snprintf (tmpbuf,
+                                               sizeof (tmpbuf),
+                                               "Last message repeated %ud times",
+                                               rspamd_log->repeats);
+                               rspamd_log->repeats = 0;
+                               if (rspamd_log->saved_message) {
+                                       file_log_function (rspamd_log->saved_module,
+                                                       rspamd_log->saved_id,
+                                                       rspamd_log->saved_function,
                                                        rspamd_log->saved_loglevel | RSPAMD_LOG_FORCED,
-                                                       tmpbuf,
+                                                       rspamd_log->saved_message,
                                                        rspamd_log);
+
+                                       g_free (rspamd_log->saved_message);
+                                       g_free (rspamd_log->saved_function);
+                                       g_free (rspamd_log->saved_module);
+                                       g_free (rspamd_log->saved_id);
+                                       rspamd_log->saved_message = NULL;
+                                       rspamd_log->saved_function = NULL;
+                                       rspamd_log->saved_module = NULL;
+                                       rspamd_log->saved_id = NULL;
                                }
+                               /* It is safe to use temporary buffer here as it is not static */
+                               file_log_function (NULL, NULL,
+                                               G_STRFUNC,
+                                               rspamd_log->saved_loglevel | RSPAMD_LOG_FORCED,
+                                               tmpbuf,
+                                               rspamd_log);
+                       }
 
+                       if (rspamd_log->fd != -1) {
                                if (fsync (rspamd_log->fd) == -1) {
                                        msg_err ("error syncing log file: %s", strerror (errno));
                                }
@@ -400,7 +408,7 @@ rspamd_set_logger (struct rspamd_config *cfg,
        switch (cfg->log_type) {
                case RSPAMD_LOG_CONSOLE:
                        logger->log_func = file_log_function;
-                       logger->fd = STDERR_FILENO;
+                       logger->fd = -1;
                        break;
                case RSPAMD_LOG_SYSLOG:
                        logger->log_func = syslog_log_function;
index 627230d4875c5faeb971bbc88c6efc7821fc5c1e..6f4fc0ba16a32fb81bf63cfed019cd4c0ee673e6 100644 (file)
@@ -249,6 +249,9 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
 {
        struct rspamd_main *rspamd_main = ud;
 
+       rspamd_log_close_priv (rspamd_main->logger,
+                       rspamd_main->workers_uid, rspamd_main->workers_gid);
+
        if (config_test) {
                /* Explicitly set logger type to console in case of config testing */
                rspamd_main->cfg->log_type = RSPAMD_LOG_CONSOLE;
@@ -256,8 +259,6 @@ config_logger (rspamd_mempool_t *pool, gpointer ud)
 
        rspamd_set_logger (rspamd_main->cfg, g_quark_try_string ("main"),
                        &rspamd_main->logger, rspamd_main->server_pool);
-       rspamd_log_close_priv (rspamd_main->logger,
-                       rspamd_main->workers_uid, rspamd_main->workers_gid);
 
        if (rspamd_log_open_priv (rspamd_main->logger,
                        rspamd_main->workers_uid, rspamd_main->workers_gid) == -1) {