aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/ftrace/test.d/00basic/mount_options.tc
blob: 35e8d47d6072592f5b43471c0415ffb03779050f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Test tracefs GID mount option
# requires: "[gid=<gid>]":README

fail() {
	local msg="$1"

	echo "FAILED: $msg"
	exit_fail
}

find_alternate_gid() {
	local original_gid="$1"
	tac /etc/group | grep -v ":$original_gid:" | head -1 | cut -d: -f3
}

mount_tracefs_with_options() {
	local mount_point="$1"
	local options="$2"

	mount -t tracefs -o "$options" nodev "$mount_point"

	setup
}

unmount_tracefs() {
	local mount_point="$1"

	# Need to make sure the mount isn't busy so that we can umount it
	(cd $mount_point; finish_ftrace;)

	cleanup
}

create_instance() {
	local mount_point="$1"
	local instance="$mount_point/instances/$(mktemp -u test-XXXXXX)"

	mkdir "$instance"
	echo "$instance"
}

remove_instance() {
	local instance="$1"

	rmdir "$instance"
}

check_gid() {
	local mount_point="$1"
	local expected_gid="$2"

	echo "Checking permission group ..."

	cd "$mount_point"

	for file in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable"; do
		local gid=`stat -c "%g" $file`
		if [ "$gid" -ne "$expected_gid" ]; then
			cd - # Return to the previous working directory (tracefs root)
			fail "$(realpath $file): Expected group $expected_gid; Got group $gid"
		fi
	done

	cd - # Return to the previous working directory (tracefs root)
}

test_gid_mount_option() {
	local mount_point=$(get_mount_point)
	local mount_options=$(get_mnt_options "$mount_point")
	local original_group=$(stat -c "%g" .)
	local other_group=$(find_alternate_gid "$original_group")

	# Set up mount options with new GID for testing
	local new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
	if [ "$new_options" = "$mount_options" ]; then
		new_options="$mount_options,gid=$other_group"
		mount_options="$mount_options,gid=$original_group"
	fi

	# Unmount existing tracefs instance and mount with new GID
	unmount_tracefs "$mount_point"
	mount_tracefs_with_options "$mount_point" "$new_options"

	check_gid "$mount_point" "$other_group"

	# Check that files created after the mount inherit the GID
	local instance=$(create_instance "$mount_point")
	check_gid "$instance" "$other_group"
	remove_instance "$instance"

	# Unmount and remount with the original GID
	unmount_tracefs "$mount_point"
	mount_tracefs_with_options "$mount_point" "$mount_options"
	check_gid "$mount_point" "$original_group"
}

test_gid_mount_option

exit 0