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

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