Blame SOURCES/0017-Issue-4814-_cl5_get_tod_expiration-may-crash-at-star.patch

6d0b66
From 59266365eda8130abf6901263efae4c87586376a Mon Sep 17 00:00:00 2001
6d0b66
From: Thierry Bordaz <tbordaz@redhat.com>
6d0b66
Date: Mon, 28 Jun 2021 16:40:15 +0200
6d0b66
Subject: [PATCH] Issue 4814 - _cl5_get_tod_expiration may crash at startup
6d0b66
6d0b66
Bug description:
6d0b66
	This bug exist only in 1.4.3 branch
6d0b66
	In 1.4.3, CL open as a separated database so
6d0b66
        compaction mechanism is started along a CL
6d0b66
        mechanism (CL trimming).
6d0b66
        The problem is that the configuration of the CL
6d0b66
        compaction is done after the compaction mechanism
6d0b66
        (is started). Depending on thread scheduling it
6d0b66
        crashes
6d0b66
6d0b66
Fix description:
6d0b66
        Make sure configuration of compaction thread is
6d0b66
        taken into account (cl5ConfigSetCompaction) before
6d0b66
	the compaction thread starts (cl5open)
6d0b66
6d0b66
relates: https://github.com/389ds/389-ds-base/issues/4814
6d0b66
6d0b66
Reviewed by: Mark Reynolds, Simon Pichugin (thanks !)
6d0b66
6d0b66
Platforms tested: 8.5
6d0b66
---
6d0b66
 ldap/servers/plugins/replication/cl5_api.c    | 24 ++++++++++++-------
6d0b66
 ldap/servers/plugins/replication/cl5_api.h    | 10 +++++++-
6d0b66
 ldap/servers/plugins/replication/cl5_config.c |  8 +++++--
6d0b66
 ldap/servers/plugins/replication/cl5_init.c   |  4 +++-
6d0b66
 ldap/servers/plugins/replication/cl5_test.c   |  2 +-
6d0b66
 .../servers/plugins/replication/repl_shared.h |  2 +-
6d0b66
 6 files changed, 35 insertions(+), 15 deletions(-)
6d0b66
6d0b66
diff --git a/ldap/servers/plugins/replication/cl5_api.c b/ldap/servers/plugins/replication/cl5_api.c
6d0b66
index 4c5077b48..954b6b9e3 100644
6d0b66
--- a/ldap/servers/plugins/replication/cl5_api.c
6d0b66
+++ b/ldap/servers/plugins/replication/cl5_api.c
6d0b66
@@ -1016,6 +1016,20 @@ cl5GetState()
6d0b66
     return s_cl5Desc.dbState;
6d0b66
 }
6d0b66
 
6d0b66
+void
6d0b66
+cl5ConfigSetCompaction(int compactInterval, char *compactTime)
6d0b66
+{
6d0b66
+
6d0b66
+    if (compactInterval != CL5_NUM_IGNORE) {
6d0b66
+        s_cl5Desc.dbTrim.compactInterval = compactInterval;
6d0b66
+    }
6d0b66
+
6d0b66
+    if (strcmp(compactTime, CL5_STR_IGNORE) != 0) {
6d0b66
+        s_cl5Desc.dbTrim.compactTime = slapi_ch_strdup(compactTime);
6d0b66
+    }
6d0b66
+
6d0b66
+}
6d0b66
+
6d0b66
 /* Name:        cl5ConfigTrimming
6d0b66
    Description:    sets changelog trimming parameters; changelog must be open.
6d0b66
    Parameters:  maxEntries - maximum number of entries in the chnagelog (in all files);
6d0b66
@@ -1026,7 +1040,7 @@ cl5GetState()
6d0b66
                 CL5_BAD_STATE if changelog is not open
6d0b66
  */
6d0b66
 int
6d0b66
-cl5ConfigTrimming(int maxEntries, const char *maxAge, int compactInterval, char *compactTime, int trimInterval)
6d0b66
+cl5ConfigTrimming(int maxEntries, const char *maxAge, int trimInterval)
6d0b66
 {
6d0b66
     if (s_cl5Desc.dbState == CL5_STATE_NONE) {
6d0b66
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name_cl,
6d0b66
@@ -1058,14 +1072,6 @@ cl5ConfigTrimming(int maxEntries, const char *maxAge, int compactInterval, char
6d0b66
         s_cl5Desc.dbTrim.maxEntries = maxEntries;
6d0b66
     }
6d0b66
 
6d0b66
-    if (compactInterval != CL5_NUM_IGNORE) {
6d0b66
-        s_cl5Desc.dbTrim.compactInterval = compactInterval;
6d0b66
-    }
6d0b66
-
6d0b66
-    if (strcmp(compactTime, CL5_STR_IGNORE) != 0) {
6d0b66
-        s_cl5Desc.dbTrim.compactTime = slapi_ch_strdup(compactTime);
6d0b66
-    }
6d0b66
-
6d0b66
     if (trimInterval != CL5_NUM_IGNORE) {
6d0b66
         s_cl5Desc.dbTrim.trimInterval = trimInterval;
6d0b66
     }
6d0b66
diff --git a/ldap/servers/plugins/replication/cl5_api.h b/ldap/servers/plugins/replication/cl5_api.h
6d0b66
index 11db771f2..6aa48aec4 100644
6d0b66
--- a/ldap/servers/plugins/replication/cl5_api.h
6d0b66
+++ b/ldap/servers/plugins/replication/cl5_api.h
6d0b66
@@ -227,6 +227,14 @@ int cl5ImportLDIF(const char *clDir, const char *ldifFile, Replica **replicas);
6d0b66
 
6d0b66
 int cl5GetState(void);
6d0b66
 
6d0b66
+/* Name:        cl5ConfigSetCompaction
6d0b66
+ * Description: sets the database compaction parameters
6d0b66
+ * Parameters:  compactInterval - Interval for compaction default is 30days
6d0b66
+ *              compactTime - Compact time default is 23:59
6d0b66
+ * Return:      void
6d0b66
+ */
6d0b66
+void cl5ConfigSetCompaction(int compactInterval, char *compactTime);
6d0b66
+
6d0b66
 /* Name:        cl5ConfigTrimming
6d0b66
    Description:    sets changelog trimming parameters
6d0b66
    Parameters:  maxEntries - maximum number of entries in the log;
6d0b66
@@ -236,7 +244,7 @@ int cl5GetState(void);
6d0b66
    Return:        CL5_SUCCESS if successful;
6d0b66
                 CL5_BAD_STATE if changelog has not been open
6d0b66
  */
6d0b66
-int cl5ConfigTrimming(int maxEntries, const char *maxAge, int compactInterval, char *compactTime, int trimInterval);
6d0b66
+int cl5ConfigTrimming(int maxEntries, const char *maxAge, int trimInterval);
6d0b66
 
6d0b66
 void cl5DestroyIterator(void *iterator);
6d0b66
 
6d0b66
diff --git a/ldap/servers/plugins/replication/cl5_config.c b/ldap/servers/plugins/replication/cl5_config.c
6d0b66
index b32686788..a43534c9b 100644
6d0b66
--- a/ldap/servers/plugins/replication/cl5_config.c
6d0b66
+++ b/ldap/servers/plugins/replication/cl5_config.c
6d0b66
@@ -197,6 +197,8 @@ changelog5_config_add(Slapi_PBlock *pb __attribute__((unused)),
6d0b66
 
6d0b66
         goto done;
6d0b66
     }
6d0b66
+    /* Set compaction parameters */
6d0b66
+    cl5ConfigSetCompaction(config.compactInterval, config.compactTime);
6d0b66
 
6d0b66
     /* start the changelog */
6d0b66
     rc = cl5Open(config.dir, &config.dbconfig);
6d0b66
@@ -212,7 +214,7 @@ changelog5_config_add(Slapi_PBlock *pb __attribute__((unused)),
6d0b66
     }
6d0b66
 
6d0b66
     /* set trimming parameters */
6d0b66
-    rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.compactInterval, config.compactTime, config.trimInterval);
6d0b66
+    rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.trimInterval);
6d0b66
     if (rc != CL5_SUCCESS) {
6d0b66
         *returncode = 1;
6d0b66
         if (returntext) {
6d0b66
@@ -548,6 +550,8 @@ changelog5_config_modify(Slapi_PBlock *pb,
6d0b66
                 slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name_cl,
6d0b66
                               "changelog5_config_modify - Deleted the changelog at %s\n", currentDir);
6d0b66
             }
6d0b66
+            /* Set compaction parameters */
6d0b66
+            cl5ConfigSetCompaction(config.compactInterval, config.compactTime);
6d0b66
 
6d0b66
             rc = cl5Open(config.dir, &config.dbconfig);
6d0b66
             if (rc != CL5_SUCCESS) {
6d0b66
@@ -575,7 +579,7 @@ changelog5_config_modify(Slapi_PBlock *pb,
6d0b66
     if (config.maxEntries != CL5_NUM_IGNORE ||
6d0b66
         config.trimInterval != CL5_NUM_IGNORE ||
6d0b66
         strcmp(config.maxAge, CL5_STR_IGNORE) != 0) {
6d0b66
-        rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.compactInterval, config.compactTime, config.trimInterval);
6d0b66
+        rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.trimInterval);
6d0b66
         if (rc != CL5_SUCCESS) {
6d0b66
             *returncode = 1;
6d0b66
             if (returntext) {
6d0b66
diff --git a/ldap/servers/plugins/replication/cl5_init.c b/ldap/servers/plugins/replication/cl5_init.c
6d0b66
index 251859714..567e0274c 100644
6d0b66
--- a/ldap/servers/plugins/replication/cl5_init.c
6d0b66
+++ b/ldap/servers/plugins/replication/cl5_init.c
6d0b66
@@ -45,6 +45,8 @@ changelog5_init()
6d0b66
         rc = 0; /* OK */
6d0b66
         goto done;
6d0b66
     }
6d0b66
+    /* Set compaction parameters */
6d0b66
+    cl5ConfigSetCompaction(config.compactInterval, config.compactTime);
6d0b66
 
6d0b66
     /* start changelog */
6d0b66
     rc = cl5Open(config.dir, &config.dbconfig);
6d0b66
@@ -57,7 +59,7 @@ changelog5_init()
6d0b66
     }
6d0b66
 
6d0b66
     /* set trimming parameters */
6d0b66
-    rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.compactInterval, config.compactTime, config.trimInterval);
6d0b66
+    rc = cl5ConfigTrimming(config.maxEntries, config.maxAge, config.trimInterval);
6d0b66
     if (rc != CL5_SUCCESS) {
6d0b66
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name_cl,
6d0b66
                       "changelog5_init: failed to configure changelog trimming\n");
6d0b66
diff --git a/ldap/servers/plugins/replication/cl5_test.c b/ldap/servers/plugins/replication/cl5_test.c
6d0b66
index d6656653c..efb8c543a 100644
6d0b66
--- a/ldap/servers/plugins/replication/cl5_test.c
6d0b66
+++ b/ldap/servers/plugins/replication/cl5_test.c
6d0b66
@@ -281,7 +281,7 @@ testTrimming()
6d0b66
         rc = populateChangelog(300, NULL);
6d0b66
 
6d0b66
         if (rc == 0)
6d0b66
-            rc = cl5ConfigTrimming(300, "1d", CHANGELOGDB_COMPACT_INTERVAL, CHANGELOGDB_TRIM_INTERVAL);
6d0b66
+            rc = cl5ConfigTrimming(300, "1d", CHANGELOGDB_TRIM_INTERVAL);
6d0b66
 
6d0b66
         interval = PR_SecondsToInterval(300); /* 5 min is default trimming interval */
6d0b66
         slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name_cl,
6d0b66
diff --git a/ldap/servers/plugins/replication/repl_shared.h b/ldap/servers/plugins/replication/repl_shared.h
6d0b66
index 6708e12f7..b59b2bd27 100644
6d0b66
--- a/ldap/servers/plugins/replication/repl_shared.h
6d0b66
+++ b/ldap/servers/plugins/replication/repl_shared.h
6d0b66
@@ -26,7 +26,7 @@
6d0b66
 
6d0b66
 #define CHANGELOGDB_TRIM_INTERVAL 300        /* 5 minutes */
6d0b66
 #define CHANGELOGDB_COMPACT_INTERVAL 2592000 /* 30 days */
6d0b66
-#define CHANGELOGDB_COMPACT_TIME "23:55" /* 30 days */
6d0b66
+#define CHANGELOGDB_COMPACT_TIME "23:59"     /* around midnight */
6d0b66
 
6d0b66
 #define CONFIG_CHANGELOG_DIR_ATTRIBUTE "nsslapd-changelogdir"
6d0b66
 #define CONFIG_CHANGELOG_MAXENTRIES_ATTRIBUTE "nsslapd-changelogmaxentries"
6d0b66
-- 
6d0b66
2.31.1
6d0b66