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

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