Blame SOURCES/rsyslog-8.1911.0-rhbz1763757-imfile-statefiles.patch

937096
From ac30968b7858d4ca3743d2b4d296eca543864fe2 Mon Sep 17 00:00:00 2001
937096
From: Jiri Vymazal <jvymazal@redhat.com>
937096
Date: Fri, 22 Nov 2019 14:25:59 +0100
937096
Subject: [PATCH] Thorougher state-file renaming and cleaning
937096
937096
Now checking if file-id changes and reanming - cleaning state file
937096
accordingly and always checking and cleaning old inode-only style
937096
state files.
937096
---
937096
 plugins/imfile/imfile.c | 66 +++++++++++++++++++++++++++--------------
937096
 1 file changed, 43 insertions(+), 23 deletions(-)
937096
937096
diff --git a/plugins/imfile/imfile.c b/plugins/imfile/imfile.c
937096
index d9bf0fbb6d..9db2b47ac9 100644
937096
--- a/plugins/imfile/imfile.c
937096
+++ b/plugins/imfile/imfile.c
937096
@@ -182,6 +182,7 @@ struct act_obj_s {
937096
 	time_t timeoutBase; /* what time to calculate the timeout against? */
937096
 	/* file dynamic data */
937096
 	char file_id[FILE_ID_HASH_SIZE]; /* file id for this entry, once we could obtain it */
937096
+	char file_id_prev[FILE_ID_HASH_SIZE]; /* previous file id for this entry, set if changed */
937096
 	int in_move;	/* workaround for inotify move: if set, state file must not be deleted */
937096
 	ino_t ino;	/* current inode nbr */
937096
 	int fd;		/* fd to file in order to obtain file_id (needs to be preserved across move) */
937096
@@ -711,7 +712,7 @@ act_obj_add(fs_edge_t *const edge, const char *const name, const int is_file,
937096
 		if (is_file) {
937096
 			LogError(errno, RS_RET_ERR, "imfile: error accessing file '%s'", name);
937096
 		} else { /* reporting only in debug for dirs as higher lvl paths are likely blocked by selinux */
937096
-			DBGPRINTF("imfile: error accessing file '%s'", name);
937096
+			DBGPRINTF("imfile: error accessing directory '%s'", name);
937096
 		}
937096
 		FINALIZE;
937096
 	}
937096
@@ -727,6 +728,7 @@ act_obj_add(fs_edge_t *const edge, const char *const name, const int is_file,
937096
 	act->ino = ino;
937096
 	act->fd = fd;
937096
 	act->file_id[0] = '\0';
937096
+	act->file_id_prev[0] = '\0';
937096
 	act->is_symlink = is_symlink;
937096
 	if (source) { /* we are target of symlink */
937096
 		CHKmalloc(act->source_name = strdup(source));
937096
@@ -1256,17 +1258,15 @@ get_file_id_hash(const char *data, size_t lendata,
937096
 static void ATTR_NONNULL(1)
937096
 getFileID(act_obj_t *const act)
937096
 {
937096
-	if(act->file_id[0] != '\0') {
937096
-		return; /* everything already done */
937096
-	}
937096
+	/* save the old id for cleaning purposes */
937096
+	strncpy(act->file_id_prev, (const char*)act->file_id, FILE_ID_HASH_SIZE);
937096
+	act->file_id[0] = '\0';
937096
 	assert(act->fd >= 0); /* fd must have been opened at act_obj_t creation! */
937096
 	char filedata[FILE_ID_SIZE];
937096
+	lseek(act->fd, 0, SEEK_SET); /* Seek to beginning of file so we have correct id */
937096
 	const int r = read(act->fd, filedata, FILE_ID_SIZE);
937096
 	if(r == FILE_ID_SIZE) {
937096
 		get_file_id_hash(filedata, sizeof(filedata), act->file_id, sizeof(act->file_id));
937096
-		dbgprintf("file_id '%s' obtained, closing monitoring file handle\n", act->file_id);
937096
-		close(act->fd); /* we will never go here! */
937096
-		act->fd = -1;
937096
 	} else {
937096
 		DBGPRINTF("getFileID partial or error read, ret %d\n", r);
937096
 	}
937096
@@ -1378,28 +1378,13 @@ openFileWithStateFile(act_obj_t *const act)
937096
 	if(fd < 0) {
937096
 		if(errno == ENOENT) {
937096
 			if(act->file_id[0] != '\0') {
937096
-				const char *pszSFNamHash = strdup((const char*)pszSFNam);
937096
-				CHKmalloc(pszSFNamHash);
937096
 				DBGPRINTF("state file %s for %s does not exist - trying to see if "
937096
 					"inode-only file exists\n", pszSFNam, act->name);
937096
 				getFullStateFileName(statefn, "", pszSFNam, sizeof(pszSFNam));
937096
 				fd = open((char*)pszSFNam, O_CLOEXEC | O_NOCTTY | O_RDONLY, 0600);
937096
 				if(fd >= 0) {
937096
-					dbgprintf("found inode-only state file, renaming it now that we "
937096
-						"know the file_id, new name: %s\n", pszSFNamHash);
937096
-					/* we now can use identify the file, so let's rename it */
937096
-					if(rename((const char*)pszSFNam, pszSFNamHash) != 0) {
937096
-						LogError(errno, RS_RET_IO_ERROR,
937096
-							"imfile error trying to rename state file for '%s' - "
937096
-							"ignoring this error, usually this means a file no "
937096
-							"longer file is left over, but this may also cause "
937096
-							"some real trouble. Still the best we can do ",
937096
-							act->name);
937096
-						free((void*) pszSFNamHash);
937096
-						ABORT_FINALIZE(RS_RET_IO_ERROR);
937096
-					}
937096
+					dbgprintf("found inode-only state file, will be renamed at next persist\n");
937096
 				}
937096
-				free((void*) pszSFNamHash);
937096
 			}
937096
 			if(fd < 0) {
937096
 				DBGPRINTF("state file %s for %s does not exist - trying to see if "
937096
@@ -2609,6 +2594,36 @@ atomicWriteStateFile(const char *fn, const char *content)
937096
 	RETiRet;
937096
 }
937096
 
937096
+/* This function should be called after any file ID change - that is if
937096
+ * file grown from hash-only statefile, or was truncated, this will ensure
937096
+ * we delete the old file so we do not make garbage in our working dir and
937096
+ * there are no leftover statefiles which can in theory later bind to something
937096
+ * and cause data loss.
937096
+ * jvymazal 2019-11-27
937096
+ */
937096
+static void
937096
+removeOldStatefile(const uchar *statefn, const char *hashToDelete)
937096
+{
937096
+	int ret;
937096
+	uchar statefname[MAXFNAME];
937096
+
937096
+	getFullStateFileName(statefn, hashToDelete, statefname, sizeof(statefname));
937096
+	DBGPRINTF("removing old state file: '%s'\n", statefname);
937096
+	ret = unlink((const char*)statefname);
937096
+	if(ret != 0) {
937096
+		if (errno != ENOENT) {
937096
+			LogError(errno, RS_RET_IO_ERROR,
937096
+				"imfile error trying to delete old state file: '%s' - ignoring this "
937096
+				"error, usually this means a file no longer file is left over, but "
937096
+				"this may also cause some real trouble. Still the best we can do ",
937096
+				statefname);
937096
+		} else {
937096
+			DBGPRINTF("trying to delete no longer valid statefile '%s' which no "
937096
+					  "longer exists (probably already deleted)\n", statefname);
937096
+		}
937096
+	}
937096
+}
937096
+
937096
 
937096
 /* This function persists information for a specific file being monitored.
937096
  * To do so, it simply persists the stream object. We do NOT abort on error
937096
@@ -2660,6 +2675,11 @@ persistStrmState(act_obj_t *const act)
937096
 	CHKiRet(atomicWriteStateFile((const char*)statefname, jstr));
937096
 	json_object_put(json);
937096
 
937096
+	/* file-id changed remove the old statefile */
937096
+	if (strncmp((const char *)act->file_id_prev, (const char *)act->file_id, FILE_ID_HASH_SIZE)) {
937096
+		removeOldStatefile(statefn, act->file_id_prev);
937096
+	}
937096
+
937096
 finalize_it:
937096
 	if(iRet != RS_RET_OK) {
937096
 		LogError(0, iRet, "imfile: could not persist state "