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

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