Skip to content

Commit 8b55572

Browse files
Kalesh Singhrostedt
authored andcommitted
tracing/selftests: Add tracefs mount options test
Add a selftest to check that the tracefs gid mount option is applied correctly. ./ftracetest test.d/00basic/mount_options.tc Use the new readme string "[gid=<gid>] as a requirement and also update test_ownership.tc requirements to use this. Cc: Eric Sandeen <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Ali Zahraee <[email protected]> Cc: Christian Brauner <[email protected]> Cc: David Howells <[email protected]> Cc: Masami Hiramatsu <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Kalesh Singh <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent fa17cb4 commit 8b55572

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
# description: Test tracefs GID mount option
4+
# requires: "[gid=<gid>]":README
5+
6+
fail() {
7+
local msg="$1"
8+
9+
echo "FAILED: $msg"
10+
exit_fail
11+
}
12+
13+
find_alternate_gid() {
14+
local original_gid="$1"
15+
tac /etc/group | grep -v ":$original_gid:" | head -1 | cut -d: -f3
16+
}
17+
18+
mount_tracefs_with_options() {
19+
local mount_point="$1"
20+
local options="$2"
21+
22+
mount -t tracefs -o "$options" nodev "$mount_point"
23+
24+
setup
25+
}
26+
27+
unmount_tracefs() {
28+
local mount_point="$1"
29+
30+
# Need to make sure the mount isn't busy so that we can umount it
31+
(cd $mount_point; finish_ftrace;)
32+
33+
cleanup
34+
}
35+
36+
create_instance() {
37+
local mount_point="$1"
38+
local instance="$mount_point/instances/$(mktemp -u test-XXXXXX)"
39+
40+
mkdir "$instance"
41+
echo "$instance"
42+
}
43+
44+
remove_instance() {
45+
local instance="$1"
46+
47+
rmdir "$instance"
48+
}
49+
50+
check_gid() {
51+
local mount_point="$1"
52+
local expected_gid="$2"
53+
54+
echo "Checking permission group ..."
55+
56+
cd "$mount_point"
57+
58+
for file in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable"; do
59+
local gid=`stat -c "%g" $file`
60+
if [ "$gid" -ne "$expected_gid" ]; then
61+
cd - # Return to the previous working directory (tracefs root)
62+
fail "$(realpath $file): Expected group $expected_gid; Got group $gid"
63+
fi
64+
done
65+
66+
cd - # Return to the previous working directory (tracefs root)
67+
}
68+
69+
test_gid_mount_option() {
70+
local mount_point=$(get_mount_point)
71+
local mount_options=$(get_mnt_options "$mount_point")
72+
local original_group=$(stat -c "%g" .)
73+
local other_group=$(find_alternate_gid "$original_group")
74+
75+
# Set up mount options with new GID for testing
76+
local new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
77+
if [ "$new_options" = "$mount_options" ]; then
78+
new_options="$mount_options,gid=$other_group"
79+
mount_options="$mount_options,gid=$original_group"
80+
fi
81+
82+
# Unmount existing tracefs instance and mount with new GID
83+
unmount_tracefs "$mount_point"
84+
mount_tracefs_with_options "$mount_point" "$new_options"
85+
86+
check_gid "$mount_point" "$other_group"
87+
88+
# Check that files created after the mount inherit the GID
89+
local instance=$(create_instance "$mount_point")
90+
check_gid "$instance" "$other_group"
91+
remove_instance "$instance"
92+
93+
# Unmount and remount with the original GID
94+
unmount_tracefs "$mount_point"
95+
mount_tracefs_with_options "$mount_point" "$mount_options"
96+
check_gid "$mount_point" "$original_group"
97+
}
98+
99+
test_gid_mount_option
100+
101+
exit 0

tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
#!/bin/sh
22
# SPDX-License-Identifier: GPL-2.0
33
# description: Test file and directory ownership changes for eventfs
4+
# requires: "[gid=<gid>]":README
45

56
original_group=`stat -c "%g" .`
67
original_owner=`stat -c "%u" .`
78

8-
mount_point=`stat -c '%m' .`
9+
local mount_point=$(get_mount_point)
910

10-
# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
11-
# current working directory (which should be a tracefs) as the mount point.
12-
if [ ! -d "$mount_point" ]; then
13-
if mount | grep -qw $PWD ; then
14-
mount_point=$PWD
15-
else
16-
# If PWD doesn't work, that is an environmental problem.
17-
exit_unresolved
18-
fi
19-
fi
20-
21-
mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`
11+
mount_options=$(get_mnt_options "$mount_point")
2212

2313
# find another owner and group that is not the original
2414
other_group=`tac /etc/group | grep -v ":$original_group:" | head -1 | cut -d: -f3`

tools/testing/selftests/ftrace/test.d/functions

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,28 @@ ftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
193193
# " Command: " and "^\n" => 13
194194
test $(expr 13 + $pos) -eq $N
195195
}
196+
197+
# Helper to get the tracefs mount point
198+
get_mount_point() {
199+
local mount_point=`stat -c '%m' .`
200+
201+
# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
202+
# current working directory (which should be a tracefs) as the mount point.
203+
if [ ! -d "$mount_point" ]; then
204+
if mount | grep -qw "$PWD"; then
205+
mount_point=$PWD
206+
else
207+
# If PWD doesn't work, that is an environmental problem.
208+
exit_unresolved
209+
fi
210+
fi
211+
echo "$mount_point"
212+
}
213+
214+
# Helper function to retrieve mount options for a given mount point
215+
get_mnt_options() {
216+
local mnt_point="$1"
217+
local opts=$(mount | grep -m1 "$mnt_point" | sed -e 's/.*(\(.*\)).*/\1/')
218+
219+
echo "$opts"
220+
}

0 commit comments

Comments
 (0)