summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@kernel.org>2023-11-13 09:08:24 -0800
committerZorro Lang <zlang@kernel.org>2023-11-16 22:45:37 +0800
commit59bf56a1a56e8b588d18c4c39757b439342cd24d (patch)
tree0118d78b68b858028078829d0b0180ef09aa551a /common
parent848c6a882a6faab9d2545a26ad5070a62f7a7bd1 (diff)
common: make helpers for ttyprintk usage
A handful of tests write things to /dev/ttyprintk to make it easier to pinpoint where in a test something went wrong. This isn't entirely robust, however, because ttyprintk is an optional feature. In the grand tradition of kernel design there's also a /dev/kmsg that does nearly the same thing, is also optional, and there's no documentation spelling out when one is supposed to use one or the other. So. Create a pair of helpers to append messages to the kernel log. One simply writes its arguments to the kernel log, and the other writes stdin to the kernel log, stdout, and any other files specified as arguments. Underneath the covers, both functions will send the message to /dev/ttyprintk if available. If it isn't but /dev/kmsg is, they'll send the messages there, prepending a "[U]" to emulate the only discernable difference between ttyprintk and kmsg. If neither are available, then either /dev or the kernel aren't allowing us to write to the kernel log, and the messages are not logged. The second helper will still write the messages to stdout. If this seems like overengineered nonsense, then yes it is. Signed-off-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Zorro Lang <zlang@kernel.org>
Diffstat (limited to 'common')
-rw-r--r--common/fuzzy4
-rw-r--r--common/rc32
2 files changed, 34 insertions, 2 deletions
diff --git a/common/fuzzy b/common/fuzzy
index 72281580..f5d45cb2 100644
--- a/common/fuzzy
+++ b/common/fuzzy
@@ -303,9 +303,9 @@ __scratch_xfs_fuzz_mdrestore()
__fuzz_notify() {
echo '========================================'
- echo "$@"
+ echo "$*"
echo '========================================'
- test -w /dev/ttyprintk && echo "$@" >> /dev/ttyprintk
+ _kernlog "$*"
}
# Perform the online repair part of a fuzz test.
diff --git a/common/rc b/common/rc
index b2e06b12..e64dea10 100644
--- a/common/rc
+++ b/common/rc
@@ -4432,6 +4432,38 @@ _check_dmesg()
fi
}
+# Log the arguments to the kernel log with userspace annotation, if possible.
+# Output is not sent to stdout.
+_kernlog()
+{
+ if [ -w /dev/ttyprintk ]; then
+ echo "$*" >> /dev/ttyprintk
+ return
+ fi
+
+ if [ -w /dev/kmsg ]; then
+ echo "[U] $*" >> /dev/kmsg
+ return
+ fi
+}
+
+# Convey stdin to the kernel log with userspace annotation, if possible.
+# Output will be appended to any file paths provided as arguments.
+_tee_kernlog()
+{
+ if [ -w /dev/ttyprintk ]; then
+ tee -a /dev/ttyprintk "$@"
+ return
+ fi
+
+ if [ -w /dev/kmsg ]; then
+ awk '{printf("[U] %s\n", $0) >> "/dev/kmsg"; printf("%s\n", $0);}' | tee -a "$@"
+ return
+ fi
+
+ tee -a "$@"
+}
+
# Make whatever configuration changes we need ahead of testing fs shutdowns due
# to unexpected IO errors while updating metadata. The sole parameter should
# be the fs device, e.g. $SCRATCH_DEV.