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

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