]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Use file lock in logger to avoid deadlocks
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 9 Jun 2016 11:45:22 +0000 (12:45 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 9 Jun 2016 11:45:22 +0000 (12:45 +0100)
src/libutil/logger.c

index f81730448df638500bab95afe822d6d94f19d2b5..7f46c2a503b6e493638f302dfeece15ba39ea3e1 100644 (file)
@@ -61,8 +61,6 @@ struct rspamd_logger_s {
        gchar *saved_module;
        gchar *saved_id;
        guint saved_loglevel;
-       rspamd_mempool_t *pool;
-       rspamd_mempool_mutex_t *mtx;
        guint64 log_cnt[4];
 };
 
@@ -70,18 +68,6 @@ static const gchar lf_chr = '\n';
 
 static rspamd_logger_t *default_logger = NULL;
 
-#define RSPAMD_LOGGER_LOCK(l) do {                             \
-       if ((l) != NULL && !(l)->no_lock) {                     \
-               rspamd_mempool_lock_mutex ((l)->mtx);   \
-       }                                                                                       \
-} while (0)
-
-#define RSPAMD_LOGGER_UNLOCK(l) do {                   \
-       if ((l) != NULL && !(l)->no_lock) {                     \
-               rspamd_mempool_unlock_mutex ((l)->mtx); \
-       }                                                                                       \
-} while (0)
-
 static void
                syslog_log_function (const gchar *log_domain, const gchar *module,
                const gchar *id, const gchar *function,
@@ -118,6 +104,10 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
        glong r;
 
        if (rspamd_log->enabled) {
+               if (!rspamd_log->no_lock) {
+                       rspamd_file_lock (rspamd_log->fd, FALSE);
+               }
+
                if (is_iov) {
                        iov = (struct iovec *) data;
                        r = writev (rspamd_log->fd, iov, count);
@@ -126,6 +116,11 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
                        line = (const gchar *) data;
                        r = write (rspamd_log->fd, line, count);
                }
+
+               if (!rspamd_log->no_lock) {
+                       rspamd_file_unlock (rspamd_log->fd, FALSE);
+               }
+
                if (r == -1) {
                        /* We cannot write message to file, so we need to detect error and make decision */
                        if (errno == EINTR) {
@@ -321,11 +316,7 @@ rspamd_set_logger (struct rspamd_config *cfg,
                struct rspamd_main *rspamd)
 {
        if (rspamd->logger == NULL) {
-               rspamd->logger = g_malloc (sizeof (rspamd_logger_t));
-               memset (rspamd->logger, 0, sizeof (rspamd_logger_t));
-               /* Small pool for interlocking */
-               rspamd->logger->pool = rspamd_mempool_new (512, NULL);
-               rspamd->logger->mtx = rspamd_mempool_get_mutex (rspamd->logger->pool);
+               rspamd->logger = g_slice_alloc0 (sizeof (rspamd_logger_t));
        }
 
        rspamd->logger->type = cfg->log_type;
@@ -468,14 +459,12 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level,
        else {
                if (rspamd_logger_need_log (rspamd_log, log_level, module)) {
                        rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args);
-                       RSPAMD_LOGGER_LOCK (rspamd_log);
                        rspamd_log->log_func (NULL, module, id,
                                        function,
                                        log_level,
                                        logbuf,
                                        FALSE,
                                        rspamd_log);
-                       RSPAMD_LOGGER_UNLOCK (rspamd_log);
                }
 
                switch (log_level) {
@@ -936,7 +925,6 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log,
                        }
                }
 
-               RSPAMD_LOGGER_LOCK (rspamd_log);
                va_start (vp, fmt);
                end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
                *end = '\0';
@@ -947,7 +935,6 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log,
                                logbuf,
                                TRUE,
                                rspamd_log);
-               RSPAMD_LOGGER_UNLOCK (rspamd_log);
        }
 }
 
@@ -964,14 +951,12 @@ rspamd_glib_log_function (const gchar *log_domain,
 
        if (rspamd_log->enabled &&
                        rspamd_logger_need_log (rspamd_log, log_level, NULL)) {
-               RSPAMD_LOGGER_LOCK (rspamd_log);
                rspamd_log->log_func (log_domain, "glib", NULL,
                                NULL,
                                log_level,
                                message,
                                FALSE,
                                rspamd_log);
-               RSPAMD_LOGGER_UNLOCK (rspamd_log);
        }
 }