Blame SOURCES/rsyslog-8.24.0-rhbz1419228-journal-switch-persistent.patch

a856a8
diff -up ./plugins/imjournal/imjournal.c.journal ./plugins/imjournal/imjournal.c
a856a8
--- ./plugins/imjournal/imjournal.c.journal	2017-03-13 14:38:11.820000000 +0100
a856a8
+++ ./plugins/imjournal/imjournal.c	2017-03-13 14:45:16.006000000 +0100
a856a8
@@ -115,6 +115,25 @@ static const char *pid_field_name;	/* re
a856a8
 static ratelimit_t *ratelimiter = NULL;
a856a8
 static sd_journal *j;
a856a8
 
a856a8
+static rsRetVal persistJournalState(void);
a856a8
+static rsRetVal loadJournalState(void);
a856a8
+
a856a8
+static rsRetVal openJournal(sd_journal** jj) {
a856a8
+	DEFiRet;
a856a8
+
a856a8
+	if (sd_journal_open(jj, SD_JOURNAL_LOCAL_ONLY) < 0)
a856a8
+		iRet = RS_RET_IO_ERROR;
a856a8
+	RETiRet;
a856a8
+}
a856a8
+
a856a8
+static void closeJournal(sd_journal** jj) {
a856a8
+
a856a8
+	if (cs.stateFile) { /* can't persist without a state file */
a856a8
+		persistJournalState();
a856a8
+	}
a856a8
+	sd_journal_close(*jj);
a856a8
+}
a856a8
+
a856a8
 
a856a8
 /* ugly workaround to handle facility numbers; values
a856a8
  * derived from names need to be eight times smaller,
a856a8
@@ -436,20 +455,25 @@ persistJournalState (void)
a856a8
 }
a856a8
 
a856a8
 
a856a8
+static rsRetVal skipOldMessages(void);
a856a8
 /* Polls the journal for new messages. Similar to sd_journal_wait()
a856a8
  * except for the special handling of EINTR.
a856a8
  */
a856a8
+
a856a8
+#define POLL_TIMEOUT 1000 /* timeout for poll is 1s */
a856a8
+
a856a8
 static rsRetVal
a856a8
 pollJournal(void)
a856a8
 {
a856a8
 	DEFiRet;
a856a8
 	struct pollfd pollfd;
a856a8
-	int r;
a856a8
+	int pr = 0;
a856a8
+	int jr = 0;
a856a8
 
a856a8
 	pollfd.fd = sd_journal_get_fd(j);
a856a8
 	pollfd.events = sd_journal_get_events(j);
a856a8
-	r = poll(&pollfd, 1, -1);
a856a8
-	if (r == -1) {
a856a8
+	pr = poll(&pollfd, 1, POLL_TIMEOUT);
a856a8
+	if (pr == -1) {
a856a8
 		if (errno == EINTR) {
a856a8
 			/* EINTR is also received during termination
a856a8
 			 * so return now to check the term state.
a856a8
@@ -465,12 +489,30 @@ pollJournal(void)
a856a8
 		}
a856a8
 	}
a856a8
 
a856a8
-	assert(r == 1);
a856a8
 
a856a8
-	r = sd_journal_process(j);
a856a8
-	if (r < 0) {
a856a8
-		char errStr[256];
a856a8
+	jr = sd_journal_process(j);
a856a8
+	
a856a8
+	if (pr == 1 && jr == SD_JOURNAL_INVALIDATE) {
a856a8
+		/* do not persist stateFile sd_journal_get_cursor will fail! */
a856a8
+		char* tmp = cs.stateFile;
a856a8
+		cs.stateFile = NULL;
a856a8
+		closeJournal(&j);
a856a8
+		cs.stateFile = tmp;
a856a8
+
a856a8
+		iRet = openJournal(&j);
a856a8
+		if (iRet != RS_RET_OK) {
a856a8
+			char errStr[256];
a856a8
+			rs_strerror_r(errno, errStr, sizeof(errStr));
a856a8
+			errmsg.LogError(0, RS_RET_IO_ERROR,
a856a8
+				"sd_journal_open() failed: '%s'", errStr);
a856a8
+			ABORT_FINALIZE(RS_RET_ERR);
a856a8
+		}
a856a8
 
a856a8
+		iRet = loadJournalState();
a856a8
+		errmsg.LogError(0, RS_RET_OK, "imjournal: "
a856a8
+			"journal reloaded...");	
a856a8
+	} else if (jr < 0) {
a856a8
+		char errStr[256];
a856a8
 		rs_strerror_r(errno, errStr, sizeof(errStr));
a856a8
 		errmsg.LogError(0, RS_RET_ERR,
a856a8
 			"sd_journal_process() failed: '%s'", errStr);
a856a8
@@ -694,20 +736,13 @@ ENDfreeCnf
a856a8
 /* open journal */
a856a8
 BEGINwillRun
a856a8
 CODESTARTwillRun
a856a8
-	int ret;
a856a8
-	ret = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
a856a8
-	if (ret < 0) {
a856a8
-		iRet = RS_RET_IO_ERROR;
a856a8
-	}
a856a8
+	iRet = openJournal(&j);
a856a8
 ENDwillRun
a856a8
 
a856a8
 /* close journal */
a856a8
 BEGINafterRun
a856a8
 CODESTARTafterRun
a856a8
-	if (cs.stateFile) { /* can't persist without a state file */
a856a8
-		persistJournalState();
a856a8
-	}
a856a8
-	sd_journal_close(j);
a856a8
+	closeJournal(&j);
a856a8
 	ratelimitDestruct(ratelimiter);
a856a8
 ENDafterRun
a856a8