andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone
1c155e
From ba30cc562f5ebd58955502a19edbf9720a45b655 Mon Sep 17 00:00:00 2001
1c155e
From: Mark Reynolds <mreynolds@redhat.com>
1c155e
Date: Tue, 8 Aug 2017 13:02:53 -0400
1c155e
Subject: [PATCH] Ticket 49298 - force sync() on shutdown
1c155e
1c155e
        Bug Description:  During shutdown on xfs we would occasionally
1c155e
        see a broke dse.ldif (specifically, empty). This happens due to
1c155e
        a bug in xfs where the directory isn't synced on rename().
1c155e
1c155e
        Fix Description:  As we shutdown call sync() to force all our
1c155e
        writes to disk - dse.ldif, logs, db, all of it.
1c155e
1c155e
        https://pagure.io/389-ds-base/issue/49298
1c155e
---
1c155e
 ldap/servers/slapd/dse.c  | 59 +++++++++++++++++++++++++++++------------------
1c155e
 ldap/servers/slapd/main.c |  9 ++++----
1c155e
 2 files changed, 42 insertions(+), 26 deletions(-)
1c155e
1c155e
diff --git a/ldap/servers/slapd/dse.c b/ldap/servers/slapd/dse.c
1c155e
index 5715c83..fa1aacc 100644
1c155e
--- a/ldap/servers/slapd/dse.c
1c155e
+++ b/ldap/servers/slapd/dse.c
1c155e
@@ -40,6 +40,8 @@
1c155e
 #include "slap.h"
1c155e
 #include <pwd.h>
1c155e
 
1c155e
+#include <unistd.h> /* provides fsync/close */
1c155e
+
1c155e
 /* #define SLAPI_DSE_DEBUG */ 	/* define this to force trace log	*/
1c155e
 								/* messages to always be logged		*/
1c155e
 
1c155e
@@ -72,11 +74,11 @@
1c155e
 struct dse_callback
1c155e
 {
1c155e
     int operation;
1c155e
-	int flags;
1c155e
-	Slapi_DN *base;
1c155e
-	int scope;
1c155e
-	char *filter;				/* NULL means match all entries */
1c155e
-    Slapi_Filter *slapifilter;	/* NULL means match all entries */
1c155e
+    int flags;
1c155e
+    Slapi_DN *base;
1c155e
+    int scope;
1c155e
+    char *filter;               /* NULL means match all entries */
1c155e
+    Slapi_Filter *slapifilter;  /* NULL means match all entries */
1c155e
     int (*fn)(Slapi_PBlock *,Slapi_Entry *,Slapi_Entry *,int*,char*,void *);
1c155e
     void *fn_arg;
1c155e
     struct slapdplugin *plugin;
1c155e
@@ -89,13 +91,14 @@ struct dse
1c155e
     char *dse_tmpfile; /* and written to when changes are made via LDAP */
1c155e
     char *dse_fileback; /* contain the latest info, just before a new change */
1c155e
     char *dse_filestartOK; /* contain the latest info with which the server has successfully started */
1c155e
+    char *dse_configdir; /* The location of config files - allows us to fsync the dir post rename */
1c155e
     Avlnode *dse_tree;
1c155e
     struct dse_callback *dse_callback;
1c155e
     Slapi_RWLock *dse_rwlock; /* a read-write lock to protect the whole dse backend */
1c155e
-	char **dse_filelist; /* these are additional read only files used to */
1c155e
-						 /* initialize the dse */
1c155e
-	int  dse_is_updateable; /* if non-zero, this DSE can be written to */
1c155e
-	int  dse_readonly_error_reported; /* used to ensure that read-only errors are logged only once */
1c155e
+    char **dse_filelist; /* these are additional read only files used to */
1c155e
+                         /* initialize the dse */
1c155e
+    int  dse_is_updateable; /* if non-zero, this DSE can be written to */
1c155e
+    int  dse_readonly_error_reported; /* used to ensure that read-only errors are logged only once */
1c155e
 };
1c155e
 
1c155e
 struct dse_node
1c155e
@@ -361,37 +364,39 @@ dse_new( char *filename, char *tmpfilename, char *backfilename, char *startokfil
1c155e
 			if (!strstr(filename, realconfigdir))
1c155e
 			{
1c155e
 				pdse->dse_filename = slapi_ch_smprintf("%s/%s", realconfigdir, filename );
1c155e
-			}
1c155e
-			else
1c155e
+			} else {
1c155e
 				pdse->dse_filename = slapi_ch_strdup(filename);
1c155e
+			}
1c155e
 
1c155e
 			if (!strstr(tmpfilename, realconfigdir)) {
1c155e
 				pdse->dse_tmpfile = slapi_ch_smprintf("%s/%s", realconfigdir, tmpfilename );
1c155e
-			}
1c155e
-			else
1c155e
+			} else {
1c155e
 				pdse->dse_tmpfile = slapi_ch_strdup(tmpfilename);
1c155e
+			}
1c155e
+
1c155e
+			pdse->dse_configdir = slapi_ch_strdup(realconfigdir);
1c155e
 
1c155e
 			if ( backfilename != NULL )
1c155e
 			{
1c155e
 				if (!strstr(backfilename, realconfigdir)) {
1c155e
 					pdse->dse_fileback = slapi_ch_smprintf("%s/%s", realconfigdir, backfilename );
1c155e
-				}
1c155e
-				else
1c155e
+				} else {
1c155e
 					pdse->dse_fileback = slapi_ch_strdup(backfilename);
1c155e
-			}
1c155e
-			else
1c155e
+				}
1c155e
+			} else {
1c155e
 				pdse->dse_fileback = NULL;
1c155e
+            }
1c155e
 
1c155e
 			if ( startokfilename != NULL )
1c155e
 			{
1c155e
 				if (!strstr(startokfilename, realconfigdir)) {
1c155e
 					pdse->dse_filestartOK = slapi_ch_smprintf("%s/%s", realconfigdir, startokfilename );
1c155e
-				}
1c155e
-				else
1c155e
+				} else {
1c155e
 					pdse->dse_filestartOK = slapi_ch_strdup(startokfilename);
1c155e
-			}
1c155e
-			else
1c155e
+				}
1c155e
+			} else {
1c155e
 				pdse->dse_filestartOK = NULL;
1c155e
+			}
1c155e
 
1c155e
 			pdse->dse_tree= NULL;
1c155e
 			pdse->dse_callback= NULL;
1c155e
@@ -440,6 +445,7 @@ dse_destroy(struct dse *pdse)
1c155e
     slapi_ch_free((void **)&(pdse->dse_tmpfile));
1c155e
     slapi_ch_free((void **)&(pdse->dse_fileback));
1c155e
     slapi_ch_free((void **)&(pdse->dse_filestartOK));
1c155e
+    slapi_ch_free((void **)&(pdse->dse_configdir));
1c155e
     dse_callback_deletelist(&pdse->dse_callback);
1c155e
     charray_free(pdse->dse_filelist);
1c155e
     nentries = avl_free(pdse->dse_tree, dse_internal_delete_entry);
1c155e
@@ -991,8 +997,9 @@ dse_write_file_nolock(struct dse* pdse)
1c155e
     FPWrapper	fpw;
1c155e
     int rc = 0;
1c155e
 
1c155e
-	if (dont_ever_write_dse_files)
1c155e
+	if (dont_ever_write_dse_files) {
1c155e
 		return rc;
1c155e
+	}
1c155e
 
1c155e
     fpw.fpw_rc = 0;
1c155e
 	fpw.fpw_prfd = NULL;
1c155e
@@ -1042,6 +1049,14 @@ dse_write_file_nolock(struct dse* pdse)
1c155e
 							pdse->dse_tmpfile, pdse->dse_filename,
1c155e
 							rc, slapd_system_strerror( rc ));
1c155e
                 }
1c155e
+				/*
1c155e
+				 * We have now written to the tmp location, and renamed it
1c155e
+				 * we need to open and fsync the dir to make the rename stick.
1c155e
+				 */
1c155e
+				int fp_configdir = open(pdse->dse_configdir, O_PATH | O_DIRECTORY);
1c155e
+				fsync(fp_configdir);
1c155e
+				close(fp_configdir);
1c155e
+
1c155e
             }
1c155e
         }
1c155e
 		if (fpw.fpw_prfd)
1c155e
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
1c155e
index ba1f5e8..3351464 100644
1c155e
--- a/ldap/servers/slapd/main.c
1c155e
+++ b/ldap/servers/slapd/main.c
1c155e
@@ -1154,11 +1154,12 @@ cleanup:
1c155e
 	ndn_cache_destroy();
1c155e
 	NSS_Shutdown();
1c155e
 	PR_Cleanup();
1c155e
-#if defined( hpux )
1c155e
-	exit( return_value );
1c155e
-#else
1c155e
+	/*
1c155e
+	 * Server has stopped, lets force everything to disk: logs
1c155e
+	 * db, dse.ldif, all of it.
1c155e
+	 */
1c155e
+	sync();
1c155e
 	return return_value;
1c155e
-#endif
1c155e
 }
1c155e
 
1c155e
 
1c155e
-- 
1c155e
2.9.4
1c155e