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

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