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

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