]> source.dussan.org Git - rspamd.git/commitdiff
[CritFix] Fix descriptors leak on reload
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 16 Jan 2017 14:27:39 +0000 (14:27 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 16 Jan 2017 14:27:39 +0000 (14:27 +0000)
MFH: true

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

index 8707acb39b47d8faafe4fe4bc7c6ff4e537245bb..923ac8dd9eb0fb7c5beb4aff463c3bb740a46bd1 100644 (file)
@@ -73,6 +73,7 @@ struct rspamd_logger_s {
        gboolean is_debug;
        gboolean throttling;
        gboolean no_lock;
+       gboolean opened;
        time_t throttling_time;
        enum rspamd_log_type type;
        pid_t pid;
@@ -212,18 +213,18 @@ rspamd_escape_log_string (gchar *str)
 gint
 rspamd_log_open_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
 {
-       switch (rspamd_log->cfg->log_type) {
+       if (!rspamd_log->opened) {
+               switch (rspamd_log->cfg->log_type) {
                case RSPAMD_LOG_CONSOLE:
                        /* Do nothing with console */
                        rspamd_log->enabled = TRUE;
-                       return 0;
+                       break;
                case RSPAMD_LOG_SYSLOG:
 #ifdef HAVE_SYSLOG_H
                        openlog ("rspamd", LOG_NDELAY | LOG_PID,
                                        rspamd_log->cfg->log_facility);
-                       rspamd_log->enabled = TRUE;
 #endif
-                       return 0;
+                       break;
                case RSPAMD_LOG_FILE:
                        rspamd_log->fd = open (rspamd_log->cfg->log_file,
                                        O_CREAT | O_WRONLY | O_APPEND,
@@ -241,10 +242,16 @@ rspamd_log_open_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
                                close (rspamd_log->fd);
                                return -1;
                        }
-                       rspamd_log->enabled = TRUE;
-                       return 0;
+                       break;
+               default:
+                       return -1;
+               }
+
+               rspamd_log->opened = TRUE;
+               rspamd_log->enabled = TRUE;
        }
-       return -1;
+
+       return 0;
 }
 
 void
@@ -253,7 +260,8 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
        gchar tmpbuf[256];
        rspamd_log_flush (rspamd_log);
 
-       switch (rspamd_log->type) {
+       if (rspamd_log->opened) {
+               switch (rspamd_log->type) {
                case RSPAMD_LOG_CONSOLE:
                        /* Do nothing special */
                        break;
@@ -293,7 +301,6 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
                                                        rspamd_log->saved_loglevel | RSPAMD_LOG_FORCED,
                                                        tmpbuf,
                                                        rspamd_log);
-                                       return;
                                }
 
                                if (fsync (rspamd_log->fd) == -1) {
@@ -302,15 +309,18 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
                                close (rspamd_log->fd);
                        }
                        break;
-       }
+               }
 
-       rspamd_log->enabled = FALSE;
+               rspamd_log->enabled = FALSE;
+               rspamd_log->opened = FALSE;
+       }
 }
 
 gint
 rspamd_log_reopen_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
 {
        rspamd_log_close_priv (rspamd_log, uid, gid);
+
        if (rspamd_log_open_priv (rspamd_log, uid, gid) == 0) {
                msg_info ("log file reopened");
                return 0;
index a6142614bfcf3b2a779ffe13fb892a3e259e14a7..627230d4875c5faeb971bbc88c6efc7821fc5c1e 100644 (file)
@@ -256,6 +256,9 @@ 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) {
                fprintf (stderr, "Fatal error, cannot open logfile, exiting\n");