Blame SOURCES/rsyslog-8.2102.0-rhbz2064318-errfile-maxsize.patch

9315e5
--- rsyslog-8.2102.0-ori/action.c	2021-02-15 12:06:16.000000000 +0100
9315e5
+++ rsyslog-8.2102.0-changes/action.c	2022-03-10 11:00:11.027242300 +0100
9315e5
@@ -198,6 +198,7 @@
9315e5
 	{ "name", eCmdHdlrGetWord, 0 }, /* legacy: actionname */
9315e5
 	{ "type", eCmdHdlrString, CNFPARAM_REQUIRED }, /* legacy: actionname */
9315e5
 	{ "action.errorfile", eCmdHdlrString, 0 },
9315e5
+	{ "action.errorfile.maxsize", eCmdHdlrInt, 0 },
9315e5
 	{ "action.writeallmarkmessages", eCmdHdlrBinary, 0 }, /* legacy: actionwriteallmarkmessages */
9315e5
 	{ "action.execonlyeverynthtime", eCmdHdlrInt, 0 }, /* legacy: actionexeconlyeverynthtime */
9315e5
 	{ "action.execonlyeverynthtimetimeout", eCmdHdlrInt, 0 }, /* legacy: actionexeconlyeverynthtimetimeout */
9315e5
@@ -400,6 +401,8 @@
9315e5
 	pThis->iResumeRetryCount = 0;
9315e5
 	pThis->pszName = NULL;
9315e5
 	pThis->pszErrFile = NULL;
9315e5
+	pThis->maxErrFileSize = 0;
9315e5
+	pThis->errFileWritten = 0;
9315e5
 	pThis->pszExternalStateFile = NULL;
9315e5
 	pThis->fdErrFile = -1;
9315e5
 	pThis->bWriteAllMarkMsgs = 1;
9315e5
@@ -1436,6 +1439,14 @@
9315e5
 				pThis->pszName, pThis->pszErrFile);
9315e5
 			goto done;
9315e5
 		}
9315e5
+		if (pThis->maxErrFileSize > 0) {
9315e5
+			struct stat statbuf;
9315e5
+			if (fstat(pThis->fdErrFile, &statbuf) == -1) {
9315e5
+				LogError(errno, RS_RET_ERR, "failed to fstat %s", pThis->pszErrFile);
9315e5
+				goto done;
9315e5
+			}
9315e5
+			pThis->errFileWritten += statbuf.st_size;
9315e5
+		}
9315e5
 	}
9315e5
 
9315e5
 	for(int i = 0 ; i < nparams ; ++i) {
9315e5
@@ -1454,16 +1465,26 @@
9315e5
 		char *const rendered = strdup((char*)fjson_object_to_json_string(etry));
9315e5
 		if(rendered == NULL)
9315e5
 			goto done;
9315e5
-		const size_t toWrite = strlen(rendered) + 1;
9315e5
-		/* note: we use the '\0' inside the string to store a LF - we do not
9315e5
-		 * otherwise need it and it safes us a copy/realloc.
9315e5
-		 */
9315e5
-		rendered[toWrite-1] = '\n'; /* NO LONGER A STRING! */
9315e5
-		const ssize_t wrRet = write(pThis->fdErrFile, rendered, toWrite);
9315e5
-		if(wrRet != (ssize_t) toWrite) {
9315e5
-			LogError(errno, RS_RET_IO_ERROR,
9315e5
-				"action %s: error writing errorFile %s, write returned %lld",
9315e5
-				pThis->pszName, pThis->pszErrFile, (long long) wrRet);
9315e5
+		size_t toWrite = strlen(rendered) + 1;
9315e5
+		// Check if need to truncate the amount of bytes to write
9315e5
+		if (pThis->maxErrFileSize > 0) {
9315e5
+			if (pThis->errFileWritten + toWrite > pThis->maxErrFileSize) {
9315e5
+				// Truncate to the pending available
9315e5
+				toWrite = pThis->maxErrFileSize - pThis->errFileWritten;
9315e5
+			}
9315e5
+			pThis->errFileWritten += toWrite;
9315e5
+		}
9315e5
+		if(toWrite > 0) {
9315e5
+			/* note: we use the '\0' inside the string to store a LF - we do not
9315e5
+			 * otherwise need it and it safes us a copy/realloc.
9315e5
+			 */
9315e5
+			rendered[toWrite-1] = '\n'; /* NO LONGER A STRING! */
9315e5
+			const ssize_t wrRet = write(pThis->fdErrFile, rendered, toWrite);
9315e5
+			if(wrRet != (ssize_t) toWrite) {
9315e5
+				LogError(errno, RS_RET_IO_ERROR,
9315e5
+					"action %s: error writing errorFile %s, write returned %lld",
9315e5
+					pThis->pszName, pThis->pszErrFile, (long long) wrRet);
9315e5
+			}
9315e5
 		}
9315e5
 		free(rendered);
9315e5
 
9315e5
@@ -2048,6 +2069,8 @@
9315e5
 			continue; /* this is handled seperately during module select! */
9315e5
 		} else if(!strcmp(pblk.descr[i].name, "action.errorfile")) {
9315e5
 			pAction->pszErrFile = es_str2cstr(pvals[i].val.d.estr, NULL);
9315e5
+		} else if(!strcmp(pblk.descr[i].name, "action.errorfile.maxsize")) {
9315e5
+			pAction->maxErrFileSize = pvals[i].val.d.n;
9315e5
 		} else if(!strcmp(pblk.descr[i].name, "action.externalstate.file")) {
9315e5
 			pAction->pszExternalStateFile = es_str2cstr(pvals[i].val.d.estr, NULL);
9315e5
 		} else if(!strcmp(pblk.descr[i].name, "action.writeallmarkmessages")) {
9315e5
--- rsyslog-8.2102.0-ori/action.h	2020-10-03 19:06:47.000000000 +0200
9315e5
+++ rsyslog-8.2102.0-changes/action.h	2022-03-04 11:36:47.024588972 +0100
9315e5
@@ -77,6 +77,8 @@
9315e5
 	/* error file */
9315e5
 	const char *pszErrFile;
9315e5
 	int fdErrFile;
9315e5
+	size_t maxErrFileSize;
9315e5
+	size_t errFileWritten;
9315e5
 	pthread_mutex_t mutErrFile;
9315e5
 	/* external stat file system */
9315e5
 	const char *pszExternalStateFile;
9315e5
--- rsyslog-8.2102.0-ori/tests/Makefile.am	2021-02-15 12:06:16.000000000 +0100
9315e5
+++ rsyslog-8.2102.0-changes/tests/Makefile.am	2022-03-04 11:38:01.625095709 +0100
9315e5
@@ -695,7 +695,8 @@
9315e5
 	mysql-actq-mt.sh \
9315e5
 	mysql-actq-mt-withpause.sh \
9315e5
 	action-tx-single-processing.sh \
9315e5
-	action-tx-errfile.sh
9315e5
+	action-tx-errfile.sh \
9315e5
+	action-tx-errfile-maxsize.sh
9315e5
 
9315e5
 mysql-basic.log: mysqld-start.log
9315e5
 mysql-basic-cnf6.log: mysqld-start.log
9315e5
@@ -2156,6 +2157,8 @@
9315e5
 	sndrcv_omudpspoof_nonstdpt.sh \
9315e5
 	sndrcv_gzip.sh \
9315e5
 	action-tx-single-processing.sh \
9315e5
+	omfwd-errfile-maxsize.sh \
9315e5
+	action-tx-errfile-maxsize.sh \
9315e5
 	action-tx-errfile.sh \
9315e5
 	testsuites/action-tx-errfile.result \
9315e5
 	pipeaction.sh \
9315e5
--- rsyslog-8.2102.0-ori/tests/omfwd-errfile-maxsize.sh	1970-01-01 01:00:00.000000000 +0100
9315e5
+++ rsyslog-8.2102.0-changes/tests/omfwd-errfile-maxsize.sh	2022-03-04 11:39:02.060506234 +0100
9315e5
@@ -0,0 +1,17 @@
9315e5
+#!/bin/bash
9315e5
+# part of the rsyslog project, released under ASL 2.0
9315e5
+. ${srcdir:=.}/diag.sh init
9315e5
+
9315e5
+export MAX_ERROR_SIZE=1999
9315e5
+
9315e5
+generate_conf
9315e5
+add_conf '
9315e5
+action(type="omfwd" target="1.2.3.4" port="1234" Protocol="tcp" NetworkNamespace="doesNotExist"
9315e5
+       action.errorfile="'$RSYSLOG2_OUT_LOG'" action.errorfile.maxsize="'$MAX_ERROR_SIZE'")
9315e5
+'
9315e5
+startup
9315e5
+shutdown_when_empty
9315e5
+wait_shutdown
9315e5
+check_file_exists ${RSYSLOG2_OUT_LOG}
9315e5
+file_size_check ${RSYSLOG2_OUT_LOG} ${MAX_ERROR_SIZE}
9315e5
+exit_test
9315e5
--- rsyslog-8.2102.0-ori/tests/action-tx-errfile-maxsize.sh	1970-01-01 01:00:00.000000000 +0100
9315e5
+++ rsyslog-8.2102.0-changes/tests/action-tx-errfile-maxsize.sh	2022-03-04 11:59:22.592796989 +0100
9315e5
@@ -0,0 +1,35 @@
9315e5
+#!/bin/bash
9315e5
+# part of the rsyslog project, released under ASL 2.0
9315e5
+
9315e5
+. ${srcdir:=.}/diag.sh init
9315e5
+
9315e5
+export NUMMESSAGES=50 # enough to generate big file
9315e5
+export MAX_ERROR_SIZE=100
9315e5
+
9315e5
+generate_conf
9315e5
+add_conf '
9315e5
+$ModLoad ../plugins/ommysql/.libs/ommysql
9315e5
+global(errormessagestostderr.maxnumber="5")
9315e5
+
9315e5
+template(type="string" name="tpl" string="insert into SystemEvents (Message, Facility) values (\"%msg%\", %$!facility%)" option.sql="on")
9315e5
+
9315e5
+if((not($msg contains "error")) and ($msg contains "msgnum:")) then {
9315e5
+	set $.num = field($msg, 58, 2);
9315e5
+	if $.num % 2 == 0 then {
9315e5
+		set $!facility = $syslogfacility;
9315e5
+	} else {
9315e5
+		set $/cntr = 0;
9315e5
+	}
9315e5
+	action(type="ommysql" name="mysql_action_errfile_maxsize" server="127.0.0.1" template="tpl"
9315e5
+	       db="'$RSYSLOG_DYNNAME'" uid="rsyslog" pwd="testbench" action.errorfile="'$RSYSLOG2_OUT_LOG'" action.errorfile.maxsize="'$MAX_ERROR_SIZE'")
9315e5
+}
9315e5
+'
9315e5
+mysql_prep_for_test
9315e5
+startup
9315e5
+injectmsg
9315e5
+shutdown_when_empty
9315e5
+wait_shutdown
9315e5
+mysql_get_data
9315e5
+check_file_exists ${RSYSLOG2_OUT_LOG}
9315e5
+file_size_check ${RSYSLOG2_OUT_LOG} ${MAX_ERROR_SIZE}
9315e5
+exit_test
9315e5
--- rsyslog-8.2102.0/tests/omfwd-errfile-maxsize-filled.sh	1970-01-01 01:00:00.000000000 +0100
9315e5
+++ rsyslog-8.2102.0-changes/tests/omfwd-errfile-maxsize-filled.sh	2022-03-08 16:24:01.174365289 +0100
9315e5
@@ -0,0 +1,19 @@
9315e5
+#!/bin/bash
9315e5
+# part of the rsyslog project, released under ASL 2.0
9315e5
+. ${srcdir:=.}/diag.sh init
9315e5
+ERRFILE=$(mktemp)
9315e5
+export MAX_ERROR_SIZE=1999
9315e5
+export INITIAL_FILE_SIZE=$((MAX_ERROR_SIZE - 100))
9315e5
+dd if=/dev/urandom of=${ERRFILE}  bs=1 count=${INITIAL_FILE_SIZE}
9315e5
+generate_conf
9315e5
+add_conf '
9315e5
+action(type="omfwd" target="1.2.3.4" port="1234" Protocol="tcp" NetworkNamespace="doesNotExist"
9315e5
+       action.errorfile="'$ERRFILE'" action.errorfile.maxsize="'$MAX_ERROR_SIZE'")
9315e5
+'
9315e5
+startup
9315e5
+shutdown_when_empty
9315e5
+wait_shutdown
9315e5
+check_file_exists ${ERRFILE}
9315e5
+file_size_check ${ERRFILE} ${MAX_ERROR_SIZE}
9315e5
+exit_test
9315e5
+rm ${ERRFILE}