Blame SOURCES/0025-Issue-50984-Memory-leaks-in-disk-monitoring.patch

ab20a9
From 68ca1de0f39c11056a57b03a544520bd6708d855 Mon Sep 17 00:00:00 2001
ab20a9
From: Simon Pichugin <simon.pichugin@gmail.com>
ab20a9
Date: Thu, 11 Jun 2020 15:39:59 +0200
ab20a9
Subject: [PATCH] Issue 50984 - Memory leaks in disk monitoring
ab20a9
ab20a9
Description: Fix the rest of the leaks in disk monitoring
ab20a9
which are present when we shutdown while being below half
ab20a9
of the threshold (at the start-up in main.c).
ab20a9
ab20a9
Free directories, sockets and ports before going to cleanup.
ab20a9
ab20a9
https://pagure.io/389-ds-base/issue/50984
ab20a9
ab20a9
Reviewed by: mhonek, tbordaz (Thanks!)
ab20a9
---
ab20a9
 ldap/servers/slapd/daemon.c | 75 ++++++++++++++++++++-----------------
ab20a9
 ldap/servers/slapd/fe.h     |  1 +
ab20a9
 ldap/servers/slapd/main.c   | 49 +++++++++++++-----------
ab20a9
 3 files changed, 70 insertions(+), 55 deletions(-)
ab20a9
ab20a9
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
ab20a9
index a70f40316..7091b570d 100644
ab20a9
--- a/ldap/servers/slapd/daemon.c
ab20a9
+++ b/ldap/servers/slapd/daemon.c
ab20a9
@@ -884,6 +884,46 @@ convert_pbe_des_to_aes(void)
ab20a9
     charray_free(attrs);
ab20a9
 }
ab20a9
 
ab20a9
+void
ab20a9
+slapd_sockets_ports_free(daemon_ports_t *ports_info)
ab20a9
+{
ab20a9
+    /* freeing PRFileDescs */
ab20a9
+    PRFileDesc **fdesp = NULL;
ab20a9
+    for (fdesp = ports_info->n_socket; fdesp && *fdesp; fdesp++) {
ab20a9
+        PR_Close(*fdesp);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->n_socket);
ab20a9
+
ab20a9
+    for (fdesp = ports_info->s_socket; fdesp && *fdesp; fdesp++) {
ab20a9
+        PR_Close(*fdesp);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->s_socket);
ab20a9
+#if defined(ENABLE_LDAPI)
ab20a9
+    for (fdesp = ports_info->i_socket; fdesp && *fdesp; fdesp++) {
ab20a9
+        PR_Close(*fdesp);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->i_socket);
ab20a9
+#endif /* ENABLE_LDAPI */
ab20a9
+
ab20a9
+    /* freeing NetAddrs */
ab20a9
+    PRNetAddr **nap;
ab20a9
+    for (nap = ports_info->n_listenaddr; nap && *nap; nap++) {
ab20a9
+        slapi_ch_free((void **)nap);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->n_listenaddr);
ab20a9
+
ab20a9
+    for (nap = ports_info->s_listenaddr; nap && *nap; nap++) {
ab20a9
+        slapi_ch_free((void **)nap);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->s_listenaddr);
ab20a9
+#if defined(ENABLE_LDAPI)
ab20a9
+    for (nap = ports_info->i_listenaddr; nap && *nap; nap++) {
ab20a9
+        slapi_ch_free((void **)nap);
ab20a9
+    }
ab20a9
+    slapi_ch_free((void **)&ports_info->i_listenaddr);
ab20a9
+#endif
ab20a9
+}
ab20a9
+
ab20a9
 void
ab20a9
 slapd_daemon(daemon_ports_t *ports)
ab20a9
 {
ab20a9
@@ -1099,40 +1139,7 @@ slapd_daemon(daemon_ports_t *ports)
ab20a9
     /* free the listener indexes */
ab20a9
     slapi_ch_free((void **)&listener_idxs);
ab20a9
 
ab20a9
-    for (fdesp = n_tcps; fdesp && *fdesp; fdesp++) {
ab20a9
-        PR_Close(*fdesp);
ab20a9
-    }
ab20a9
-    slapi_ch_free((void **)&n_tcps);
ab20a9
-
ab20a9
-    for (fdesp = i_unix; fdesp && *fdesp; fdesp++) {
ab20a9
-        PR_Close(*fdesp);
ab20a9
-    }
ab20a9
-    slapi_ch_free((void **)&i_unix);
ab20a9
-
ab20a9
-    for (fdesp = s_tcps; fdesp && *fdesp; fdesp++) {
ab20a9
-        PR_Close(*fdesp);
ab20a9
-    }
ab20a9
-    slapi_ch_free((void **)&s_tcps);
ab20a9
-
ab20a9
-    /* freeing NetAddrs */
ab20a9
-    {
ab20a9
-        PRNetAddr **nap;
ab20a9
-        for (nap = ports->n_listenaddr; nap && *nap; nap++) {
ab20a9
-            slapi_ch_free((void **)nap);
ab20a9
-        }
ab20a9
-        slapi_ch_free((void **)&ports->n_listenaddr);
ab20a9
-
ab20a9
-        for (nap = ports->s_listenaddr; nap && *nap; nap++) {
ab20a9
-            slapi_ch_free((void **)nap);
ab20a9
-        }
ab20a9
-        slapi_ch_free((void **)&ports->s_listenaddr);
ab20a9
-#if defined(ENABLE_LDAPI)
ab20a9
-        for (nap = ports->i_listenaddr; nap && *nap; nap++) {
ab20a9
-            slapi_ch_free((void **)nap);
ab20a9
-        }
ab20a9
-        slapi_ch_free((void **)&ports->i_listenaddr);
ab20a9
-#endif
ab20a9
-    }
ab20a9
+    slapd_sockets_ports_free(ports);
ab20a9
 
ab20a9
     op_thread_cleanup();
ab20a9
     housekeeping_stop(); /* Run this after op_thread_cleanup() logged sth */
ab20a9
diff --git a/ldap/servers/slapd/fe.h b/ldap/servers/slapd/fe.h
ab20a9
index 2d9a0931b..9cd122881 100644
ab20a9
--- a/ldap/servers/slapd/fe.h
ab20a9
+++ b/ldap/servers/slapd/fe.h
ab20a9
@@ -120,6 +120,7 @@ int connection_table_iterate_active_connections(Connection_Table *ct, void *arg,
ab20a9
  */
ab20a9
 int signal_listner(void);
ab20a9
 int daemon_pre_setuid_init(daemon_ports_t *ports);
ab20a9
+void slapd_sockets_ports_free(daemon_ports_t *ports_info);
ab20a9
 void slapd_daemon(daemon_ports_t *ports);
ab20a9
 void daemon_register_connection(void);
ab20a9
 int slapd_listenhost2addr(const char *listenhost, PRNetAddr ***addr);
ab20a9
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
ab20a9
index e54b8e1c5..9e5219c4a 100644
ab20a9
--- a/ldap/servers/slapd/main.c
ab20a9
+++ b/ldap/servers/slapd/main.c
ab20a9
@@ -734,7 +734,6 @@ main(int argc, char **argv)
ab20a9
      * etc the backends need to start
ab20a9
      */
ab20a9
 
ab20a9
-
ab20a9
     /* Important: up 'till here we could be running as root (on unix).
ab20a9
      * we believe that we've not created any files before here, otherwise
ab20a9
      * they'd be owned by root, which is bad. We're about to change identity
ab20a9
@@ -891,6 +890,34 @@ main(int argc, char **argv)
ab20a9
     }
ab20a9
     }
ab20a9
 
ab20a9
+    if (config_get_disk_monitoring()) {
ab20a9
+        char **dirs = NULL;
ab20a9
+        char *dirstr = NULL;
ab20a9
+        uint64_t disk_space = 0;
ab20a9
+        int64_t threshold = 0;
ab20a9
+        uint64_t halfway = 0;
ab20a9
+        threshold = config_get_disk_threshold();
ab20a9
+        halfway = threshold / 2;
ab20a9
+        disk_mon_get_dirs(&dirs);
ab20a9
+        dirstr = disk_mon_check_diskspace(dirs, threshold, &disk_space);
ab20a9
+        if (dirstr != NULL && disk_space < halfway) {
ab20a9
+            slapi_log_err(SLAPI_LOG_EMERG, "main",
ab20a9
+                          "Disk Monitoring is enabled and disk space on (%s) is too far below the threshold(%" PRIu64 " bytes). Exiting now.\n",
ab20a9
+                          dirstr, threshold);
ab20a9
+            slapi_ch_array_free(dirs);
ab20a9
+            /*
ab20a9
+             * We should free the structs we allocated for sockets and addresses
ab20a9
+             * as they would be freed at the slapd_daemon but it was not initiated
ab20a9
+             * at that point of start-up.
ab20a9
+             */
ab20a9
+            slapd_sockets_ports_free(&ports_info);
ab20a9
+            return_value = 1;
ab20a9
+            goto cleanup;
ab20a9
+        }
ab20a9
+        slapi_ch_array_free(dirs);
ab20a9
+        dirs = NULL;
ab20a9
+    }
ab20a9
+
ab20a9
     /* initialize the normalized DN cache */
ab20a9
     if (ndn_cache_init() != 0) {
ab20a9
         slapi_log_err(SLAPI_LOG_EMERG, "main", "Unable to create ndn cache\n");
ab20a9
@@ -940,26 +967,6 @@ main(int argc, char **argv)
ab20a9
         slapi_ch_free((void **)&versionstring);
ab20a9
     }
ab20a9
 
ab20a9
-    if (config_get_disk_monitoring()) {
ab20a9
-        char **dirs = NULL;
ab20a9
-        char *dirstr = NULL;
ab20a9
-        uint64_t disk_space = 0;
ab20a9
-        int64_t threshold = 0;
ab20a9
-        uint64_t halfway = 0;
ab20a9
-        threshold = config_get_disk_threshold();
ab20a9
-        halfway = threshold / 2;
ab20a9
-        disk_mon_get_dirs(&dirs);
ab20a9
-        dirstr = disk_mon_check_diskspace(dirs, threshold, &disk_space);
ab20a9
-        if (dirstr != NULL && disk_space < halfway) {
ab20a9
-            slapi_log_err(SLAPI_LOG_EMERG, "main",
ab20a9
-                          "Disk Monitoring is enabled and disk space on (%s) is too far below the threshold(%" PRIu64 " bytes). Exiting now.\n",
ab20a9
-                          dirstr, threshold);
ab20a9
-            return_value = 1;
ab20a9
-            goto cleanup;
ab20a9
-        }
ab20a9
-        slapi_ch_array_free(dirs);
ab20a9
-        dirs = NULL;
ab20a9
-    }
ab20a9
     /* log the max fd limit as it is typically set in env/systemd */
ab20a9
     slapi_log_err(SLAPI_LOG_INFO, "main",
ab20a9
             "Setting the maximum file descriptor limit to: %ld\n",
ab20a9
-- 
ab20a9
2.26.2
ab20a9