From e7d3f97f16d54b26fe6a7eb62ee90ce2804245bf Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 16 Jan 2017 15:03:22 +0000 Subject: [PATCH] [Fix] More fixes to logger initialization MFH: true --- src/libutil/logger.c | 90 ++++++++++++++++++++++++++------------------ src/rspamd.c | 5 ++- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/src/libutil/logger.c b/src/libutil/logger.c index 8b98e368f..e8af40b58 100644 --- a/src/libutil/logger.c +++ b/src/libutil/logger.c @@ -127,30 +127,48 @@ 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) { #ifndef DISABLE_PTHREAD_MUTEX - rspamd_mempool_lock_mutex (rspamd_log->mtx); + if (rspamd_log->mtx) { + rspamd_mempool_lock_mutex (rspamd_log->mtx); + } + else { + 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) { #ifndef DISABLE_PTHREAD_MUTEX - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + if (rspamd_log->mtx) { + rspamd_mempool_unlock_mutex (rspamd_log->mtx); + } + else { + rspamd_file_unlock (fd, FALSE); + } #else - rspamd_file_unlock (rspamd_log->fd, FALSE); + rspamd_file_unlock (fd, FALSE); #endif } @@ -207,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 @@ -261,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)); } @@ -383,7 +401,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; diff --git a/src/rspamd.c b/src/rspamd.c index 5708b20ef..bea0e8ebd 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -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); - 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) { -- 2.39.5