rsyslog/backport-imfile-tests-ext-directorys-fd-leak-in-case-of-inotify-on.patch
Qiumiao Zhang b83e0b1c2a backport patches from upstream
Signed-off-by: Qiumiao Zhang <zhangqiumiao1@huawei.com>
2024-03-21 03:38:16 +00:00

198 lines
6.4 KiB
Diff

From e8ac82e09f930bf99421cc323c24a9dbf215f9da Mon Sep 17 00:00:00 2001
From: Sergey Kacheev <s.kacheev@gmail.com>
Date: Wed, 2 Nov 2022 03:45:31 +0700
Subject: [PATCH] imfile tests: ext directory's fd leak in case of inotify on
symlink
Reference:https://github.com/rsyslog/rsyslog/commit/e8ac82e09f930bf99421cc323c24a9dbf215f9da
Conflict:NA
---
tests/Makefile.am | 2 +
tests/README | 12 ++--
tests/diag.sh | 33 ++++++++++
tests/imfile-symlink-ext-tmp-dir-tree.sh | 80 ++++++++++++++++++++++++
4 files changed, 123 insertions(+), 4 deletions(-)
create mode 100755 tests/imfile-symlink-ext-tmp-dir-tree.sh
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 396e159..1063079 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1499,6 +1499,7 @@ TESTS += \
imfile-rename.sh \
imfile-symlink.sh \
imfile-symlink-multi.sh \
+ imfile-symlink-ext-tmp-dir-tree.sh \
imfile-logrotate.sh \
imfile-logrotate-async.sh \
imfile-logrotate-multiple.sh \
@@ -2479,6 +2480,7 @@ EXTRA_DIST= \
imfile-rename.sh \
imfile-symlink.sh \
imfile-symlink-multi.sh \
+ imfile-symlink-ext-tmp-dir-tree.sh \
imfile-logrotate.sh \
imfile-logrotate-async.sh \
imfile-logrotate-copytruncate.sh \
diff --git a/tests/README b/tests/README
index f0d3bb9..63a15d4 100644
--- a/tests/README
+++ b/tests/README
@@ -41,15 +41,19 @@ make check
Running named tests
===================
-make testname.sh.log
+make testname.log
For example, to run the imfile-basic.sh test, use
-make imfile-basic.sh.log
+ make imfile-basic.log
-Test output is in imfile-basic.sh.log
+Test output is in imfile-basic.log
-To re-run the test, first remove imfile-basic.sh.log then make again
+To re-run the test, first remove imfile-basic.log then make again
+
+Or an alternative option is to run
+
+ make check TESTS='imfile-basic.sh'
* Using gdb to debug rsyslog during a test run
diff --git a/tests/diag.sh b/tests/diag.sh
index 8bab35b..14966e0 100755
--- a/tests/diag.sh
+++ b/tests/diag.sh
@@ -889,6 +889,39 @@ check_journal_testmsg_received() {
fi;
}
+# checks that among the open files found in /proc/<PID>/fd/*
+# there is or is not, depending on the calling mode,
+# a link with the specified suffix in the target name
+check_fd_for_pid() {
+ local pid="$1" mode="$2" suffix="$3" target seen
+ seen="false"
+ for fd in $(echo /proc/$pid/fd/*); do
+ target="$(readlink -m "$fd")"
+ if [[ "$target" != *$RSYSLOG_DYNNAME* ]]; then
+ continue
+ fi
+ if ((i % 10 == 0)); then
+ echo "INFO: check target='$target'"
+ fi
+ if [[ "$target" == *$suffix ]]; then
+ seen="true"
+ if [[ "$mode" == "exists" ]]; then
+ echo "PASS: check fd for pid=$pid mode='$mode' suffix='$suffix'"
+ return 0
+ fi
+ fi
+ done
+ if [[ "$seen" == "false" ]] && [[ "$mode" == "absent" ]]; then
+ echo "PASS: check fd for pid=$pid mode='$mode' suffix='$suffix'"
+ return 0
+ fi
+ echo "FAIL: check fd for pid=$pid mode='$mode' suffix='$suffix'"
+ if [[ "$mode" != "ignore" ]]; then
+ return 1
+ fi
+ return 0
+}
+
# wait for main message queue to be empty. $1 is the instance.
# we run in a loop to ensure rsyslog is *really* finished when a
# function for the "finished predicate" is defined. This is done
diff --git a/tests/imfile-symlink-ext-tmp-dir-tree.sh b/tests/imfile-symlink-ext-tmp-dir-tree.sh
new file mode 100755
index 0000000..df15f54
--- /dev/null
+++ b/tests/imfile-symlink-ext-tmp-dir-tree.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+# This test creates multiple symlinks (all watched by rsyslog via wildcard)
+# chained to target files via additional symlinks and checks that all files
+# are recorded with correct corresponding metadata (name of symlink
+# matching configuration).
+# This is part of the rsyslog testbench, released under ASL 2.0
+. "${srcdir:=.}"/diag.sh init
+. "$srcdir"/diag.sh check-inotify
+
+# #define FILE_DELETE_DELAY 5 /* how many seconds to wait before finally deleting a gone file */
+export RSYSLOG_DEBUG="debug nologfuncflow noprintmutexaction nostdout"
+export RSYSLOG_DEBUGLOG="log"
+export TEST_TIMEOUT=30
+
+# generate input files first. Note that rsyslog processes it as
+# soon as it start up (so the file should exist at that point).
+generate_conf
+add_conf '
+# comment out if you need more debug info:
+global( debug.whitelist="on" debug.files=["imfile.c"]
+ workDirectory="./'"$RSYSLOG_DYNNAME"'.work"
+)
+module(load="../plugins/imfile/.libs/imfile" mode="inotify")
+input(type="imfile" File="./'"$RSYSLOG_DYNNAME"'.links/*.log" Tag="file:"
+ Severity="error" Facility="local7" addMetadata="on")
+template(name="outfmt" type="list") {
+ constant(value="HEADER ")
+ property(name="msg" format="json")
+ constant(value=", filename: ")
+ property(name="$!metadata!filename")
+ constant(value=", fileoffset: ")
+ property(name="$!metadata!fileoffset")
+ constant(value="\n")
+}
+if $msg contains "msgnum:" then
+ action( type="omfile" file="'"$RSYSLOG_DYNNAME.out/$RSYSLOG_OUT_LOG"'" template="outfmt")
+'
+
+mkdir "$RSYSLOG_DYNNAME".links "$RSYSLOG_DYNNAME".work "$RSYSLOG_DYNNAME".out
+
+printf '\ncreating %s\n' "$RSYSLOG_DYNNAME".targets/container-1/logs/0.log
+mkdir -p "$RSYSLOG_DYNNAME".targets/container-1/logs
+./inputfilegen -m 1 >"$RSYSLOG_DYNNAME".targets/container-1/logs/0.log
+ls -l "$RSYSLOG_DYNNAME".targets/container-1/logs/0.log
+ln -sv "$PWD/$RSYSLOG_DYNNAME".targets/container-1/logs/0.log "$PWD/$RSYSLOG_DYNNAME".links/container-1.log
+printf '%s generated link %s\n' "$(tb_timestamp)" "container-1"
+ls -l "$RSYSLOG_DYNNAME".links/container-1.log
+
+# Start rsyslog now
+startup
+
+PID=$(cat "$RSYSLOG_PIDBASE".pid)
+echo "Rsyslog pid $RSYSLOG_PIDBASE.pid=$PID"
+if [[ "$PID" == "" ]]; then
+ error_exit 1
+fi
+
+echo "INFO: check files"
+# wait until this files has been opened
+check_fd_for_pid "$PID" exists "container-1/logs/0.log"
+check_fd_for_pid "$PID" exists "container-1/logs"
+
+echo "INFO: remove watched files"
+rm -vr "$RSYSLOG_DYNNAME".targets/container-1
+rm -v "$RSYSLOG_DYNNAME".links/container-1.log
+
+until check_fd_for_pid "$PID" absent "container-1/logs (deleted)"; do
+ if ((_wait_for_absent++ > TEST_TIMEOUT)); then
+ error_exit 1
+ fi
+ echo "INFO: trigger fd unlinking"
+ ./inputfilegen -m 1 >"$RSYSLOG_DYNNAME".links/gogogo.log
+ ./msleep 1000
+ rm -v "$RSYSLOG_DYNNAME".links/gogogo.log
+ ./msleep 10
+done
+
+shutdown_when_empty
+wait_shutdown
+exit_test
--
2.19.1