Blame SOURCES/0045-Be-more-thorough-about-flushing-our-config-file-when.patch

cca0c4
From 7d48cb09b85b81f707565db6d35b1ba0d247b6b4 Mon Sep 17 00:00:00 2001
cca0c4
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@fedoraproject.org>
cca0c4
Date: Fri, 23 Jun 2017 23:56:50 +0200
cca0c4
Subject: [PATCH 45/55] Be more thorough about flushing our config file when
cca0c4
 writing.
cca0c4
cca0c4
Add missing fclose() at the end of writeConfig
cca0c4
Use fflush(out) + fsync(fileno(out) on temporary file
cca0c4
fsync() the destination directory after rename
cca0c4
---
cca0c4
 grubby.c | 41 ++++++++++++++++++++++++++++++++++-------
cca0c4
 1 file changed, 34 insertions(+), 7 deletions(-)
cca0c4
cca0c4
diff --git a/grubby.c b/grubby.c
cca0c4
index 5202485a541..11ee64a02b9 100644
cca0c4
--- a/grubby.c
cca0c4
+++ b/grubby.c
cca0c4
@@ -1789,6 +1789,7 @@ static int writeConfig(struct grubConfig *cfg, char *outName,
cca0c4
 	int needs = MAIN_DEFAULT;
cca0c4
 	struct stat sb;
cca0c4
 	int i;
cca0c4
+	int rc = 0;
cca0c4
 
cca0c4
 	if (!strcmp(outName, "-")) {
cca0c4
 		out = stdout;
cca0c4
@@ -1903,16 +1904,42 @@ static int writeConfig(struct grubConfig *cfg, char *outName,
cca0c4
 	}
cca0c4
 
cca0c4
 	if (tmpOutName) {
cca0c4
-		if (rename(tmpOutName, outName)) {
cca0c4
+		/* write userspace buffers */
cca0c4
+		if (fflush(out))
cca0c4
+			rc = 1;
cca0c4
+
cca0c4
+		/* purge the write-back cache with fsync() */
cca0c4
+		if (fsync(fileno(out)))
cca0c4
+			rc = 1;
cca0c4
+
cca0c4
+		if (fclose(out))
cca0c4
+			rc = 1;
cca0c4
+
cca0c4
+		if (rc == 0 && rename(tmpOutName, outName)) {
cca0c4
+			unlink(tmpOutName);
cca0c4
+			rc = 1;
cca0c4
+		}
cca0c4
+
cca0c4
+		/* fsync() the destination directory after rename */
cca0c4
+		if (rc == 0) {
cca0c4
+			int dirfd;
cca0c4
+
cca0c4
+			dirfd = open(dirname(strdupa(outName)), O_RDONLY);
cca0c4
+			if (dirfd < 0)
cca0c4
+				rc = 1;
cca0c4
+			else if (fsync(dirfd))
cca0c4
+				rc = 1;
cca0c4
+
cca0c4
+			if (dirfd >= 0)
cca0c4
+				close(dirfd);
cca0c4
+		}
cca0c4
+
cca0c4
+		if (rc == 1)
cca0c4
 			fprintf(stderr,
cca0c4
-				_("grubby: error moving %s to %s: %s\n"),
cca0c4
-				tmpOutName, outName, strerror(errno));
cca0c4
-			unlink(outName);
cca0c4
-			return 1;
cca0c4
-		}
cca0c4
+				_("grubby: error flushing data: %m\n"));
cca0c4
 	}
cca0c4
 
cca0c4
-	return 0;
cca0c4
+	return rc;
cca0c4
 }
cca0c4
 
cca0c4
 static int numEntries(struct grubConfig *cfg)
cca0c4
-- 
cca0c4
2.17.1
cca0c4