aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc
blob: 094419e190c29ee2c7f716f914d76226a6ca95a3 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: Test file and directory ownership changes for eventfs

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

mount_point=`stat -c '%m' .`

# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
# current working directory (which should be a tracefs) as the mount point.
if [ ! -d "$mount_point" ]; then
	if mount | grep -qw $PWD ; then
		mount_point=$PWD
	else
		# If PWD doesn't work, that is an environmental problem.
		exit_unresolved
	fi
fi

mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`

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

# Remove any group ownership already
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

canary="events/timer events/timer/timer_cancel events/timer/timer_cancel/format"

test() {
	file=$1
	test_group=$2

	owner=`stat -c "%u" $file`
	group=`stat -c "%g" $file`

	echo "testing $file $owner=$original_owner and $group=$test_group"
	if [ $owner -ne $original_owner ]; then
		exit_fail
	fi
	if [ $group -ne $test_group ]; then
		exit_fail
	fi

	# Note, the remount does not update ownership so test going to and from owner
	echo "test owner $file to $other_owner"
	chown $other_owner $file
	owner=`stat -c "%u" $file`
	if [ $owner -ne $other_owner ]; then
		exit_fail
	fi

	chown $original_owner $file
	owner=`stat -c "%u" $file`
	if [ $owner -ne $original_owner ]; then
		exit_fail
	fi

}

run_tests() {
	for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
		test "$d" $other_group
	done

	chgrp $original_group events
	test "events" $original_group
	for d in "." "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
		test "$d" $other_group
	done

	chgrp $original_group events/sched
	test "events/sched" $original_group
	for d in "." "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
		test "$d" $other_group
	done

	chgrp $original_group events/sched/sched_switch
	test "events/sched/sched_switch" $original_group
	for d in "." "events/sched/sched_switch/enable" $canary; do
		test "$d" $other_group
	done

	chgrp $original_group events/sched/sched_switch/enable
	test "events/sched/sched_switch/enable" $original_group
	for d in "." $canary; do
		test "$d" $other_group
	done
}

# Run the tests twice as leftovers can cause issues
for loop in 1 2 ; do

	echo "Running iteration $loop"

	mount -o remount,"$new_options" .

	run_tests

	mount -o remount,"$mount_options" .

	for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
		test "$d" $original_group
	done

# check instances as well

	chgrp $other_group instances

	instance="$(mktemp -u test-XXXXXX)"

	mkdir instances/$instance

	cd instances/$instance

	run_tests

	cd ../..

	rmdir instances/$instance

	chgrp $original_group instances
done

exit 0