logrotate/SOURCES/0017-logrotate-3.18.0-writeState-do-nothing-if-state-file-is-dev-null.patch

81 lines
2.2 KiB
Diff

From d8e171f36aab41182c2cf52d9a33ba478f090490 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Thu, 3 Jun 2021 10:51:07 +0200
Subject: [PATCH] writeState: do nothing if state file is /dev/null
If users do not want to use any state file, they can specify `/dev/null`
as the state file. Without this fix, logrotate would unnecessarily fail
to rename a temporary file to `/dev/null`.
Fixes: https://github.com/logrotate/logrotate/issues/395
(cherry picked from commit 456692644cbf5adb6253cb7ed2d169e950a9e348)
---
logrotate.c | 4 ++++
test/Makefile.am | 1 +
test/test-0089.sh | 14 ++++++++++++++
test/test-config.89.in | 4 ++++
4 files changed, 23 insertions(+)
create mode 100755 test/test-0089.sh
create mode 100644 test/test-config.89.in
diff --git a/logrotate.c b/logrotate.c
index b0f8332..df0eb22 100644
--- a/logrotate.c
+++ b/logrotate.c
@@ -2507,6 +2507,10 @@ static int writeState(const char *stateFilename)
char *prevCtx;
int force_mode = 0;
+ if (!strcmp(stateFilename, "/dev/null"))
+ /* explicitly asked not to write the state file */
+ return 0;
+
localtime_r(&nowSecs, &now);
tmpFilename = malloc(strlen(stateFilename) + 5 );
diff --git a/test/Makefile.am b/test/Makefile.am
index 69a5e45..27c3900 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -87,6 +87,7 @@ TEST_CASES = \
test-0086.sh \
test-0087.sh \
test-0088.sh \
+ test-0089.sh \
test-0092.sh \
test-0100.sh \
test-0101.sh \
diff --git a/test/test-0089.sh b/test/test-0089.sh
new file mode 100755
index 0000000..c586690
--- /dev/null
+++ b/test/test-0089.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+. ./test-common.sh
+
+# skip the test if /dev/null is not readable
+test -r /dev/null || exit 77
+
+# we don't want any stuff left from previous runs
+cleanup 89
+
+# ------------------------------- Test 89 ------------------------------------
+# using /dev/null as state file tells logrotate not to write the state file
+preptest test.log 89 2
+$RLR --state /dev/null test-config.89
diff --git a/test/test-config.89.in b/test/test-config.89.in
new file mode 100644
index 0000000..ec41c37
--- /dev/null
+++ b/test/test-config.89.in
@@ -0,0 +1,4 @@
+&DIR&/test.log {
+ daily
+ rotate 2
+}
--
2.49.0