]> www.infradead.org Git - users/hch/xfstests-dev.git/commitdiff
common: make helpers for ttyprintk usage
authorDarrick J. Wong <djwong@kernel.org>
Mon, 13 Nov 2023 17:08:24 +0000 (09:08 -0800)
committerZorro Lang <zlang@kernel.org>
Thu, 16 Nov 2023 14:45:37 +0000 (22:45 +0800)
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>
common/fuzzy
common/rc
tests/xfs/329
tests/xfs/434
tests/xfs/435
tests/xfs/436
tests/xfs/444
tests/xfs/516

index 7228158034cf73e09bd674e7721d287d50d727ed..f5d45cb28f07b6d50daeb25452a85f17c644564d 100644 (file)
@@ -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.
index b2e06b12732186c74046aaf4627b66baf32d66cf..e64dea108cf0e164a05f4f299d942ad47236e08c 100644 (file)
--- 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.
index 15dc3c242f92ba76e30bb6fac9897c4569439321..12b7c6084269d500da7c38fabe1724fb0acf5d31 100755 (executable)
@@ -53,11 +53,11 @@ $XFS_FSR_PROG -v -d $testdir/file1 >> $seqres.full 2>&1
 echo "FS should be shut down, touch will fail"
 touch $SCRATCH_MNT/badfs 2>&1 | _filter_scratch
 
-echo "Remount to replay log" | tee /dev/ttyprintk
+echo "Remount to replay log" | _tee_kernlog
 _scratch_remount_dump_log >> $seqres.full
 new_nextents=$(_count_extents $testdir/file1)
 
-echo "Check extent count" | tee /dev/ttyprintk
+echo "Check extent count" | _tee_kernlog
 $XFS_IO_PROG -c 'stat -v' $testdir/file1 >> $seqres.full
 $XFS_IO_PROG -c 'stat -v' $testdir/file2 >> $seqres.full
 echo "extents: $old_nextents -> $new_nextents" >> $seqres.full
index de52531053e1dbb60d4897fc17fb21dc19f13094..12d1a0c9da3393e1c2f984e5767a8e4d12e6fbfd 100755 (executable)
@@ -65,7 +65,7 @@ $XFS_FSR_PROG -v -d $testdir/file1 >> $seqres.full 2>&1
 echo "FS should be shut down, touch will fail"
 touch $SCRATCH_MNT/badfs 2>&1 | _filter_scratch
 
-echo "Remount to replay log" | tee /dev/ttyprintk
+echo "Remount to replay log" | _tee_kernlog
 _scratch_unmount
 _scratch_dump_log >> $seqres.full
 _scratch_xfs_db -x -c 'agf 0' -c 'addr refcntroot' -c 'fuzz -d recs[1].startblock ones' >> $seqres.full
index ded942a12812b1da0a35103a9833978aa71bf635..44135c7653e5f3670b2b1068939fce1ab2834a27 100755 (executable)
@@ -46,7 +46,7 @@ _pwrite_byte 0x62 0 $((blksz * blks)) $testdir/file1 >> $seqres.full
 _pwrite_byte 0x63 0 $blksz $testdir/file2 >> $seqres.full
 _reflink_range $testdir/file2 0 $testdir/file1 $blksz $blksz >> $seqres.full
 
-echo "Remount to check recovery" | tee /dev/ttyprintk
+echo "Remount to check recovery" | _tee_kernlog
 _scratch_unmount
 _scratch_xfs_db -x -c 'agf 0' -c 'addr refcntroot' -c 'fuzz -d recs[1].startblock ones' >> $seqres.full
 _scratch_xfs_db -x -c 'agf 0' -c 'addr refcntroot' -c p >> $seqres.full
index b95da8abf4e0a1dbb0282b8b4cbd3c7da8685246..d010362785b2a9993f2151f0900a540c539d96b6 100755 (executable)
@@ -62,7 +62,7 @@ $XFS_FSR_PROG -v -d $testdir/file1 >> $seqres.full 2>&1
 echo "FS should be shut down, touch will fail"
 touch $SCRATCH_MNT/badfs 2>&1 | _filter_scratch
 
-echo "Remount to replay log" | tee /dev/ttyprintk
+echo "Remount to replay log" | _tee_kernlog
 _scratch_unmount
 _scratch_dump_log >> $seqres.full
 _scratch_xfs_db -x -c 'agf 0' -c 'addr refcntroot' -c 'fuzz -d recs[1].startblock ones' >> $seqres.full
index 8f06d7325940275315afec60203a68565be7f4d9..db7418c55deed315e18ce03a6d0bb47f7d65822f 100755 (executable)
@@ -62,7 +62,7 @@ runtest() {
        cmd="$1"
 
        # Format filesystem
-       echo "TEST $cmd" | tee /dev/ttyprintk
+       echo "TEST $cmd" | _tee_kernlog
        echo "TEST $cmd" >> $seqres.full
        _scratch_mkfs >> $seqres.full
 
index 9e1b993174a2fb25a9e1993aa2780fea7414352f..1bf6f858d5286d775f605f36757d1bd285f8bfa1 100755 (executable)
@@ -31,7 +31,7 @@ _supports_xfs_scrub $TEST_DIR $TEST_DEV && run_scrub=1
 
 log()
 {
-       echo "$@" | tee -a $seqres.full /dev/ttyprintk
+       echo "$*" | _tee_kernlog $seqres.full
 }
 
 __test_mount_opts()