xref: /linux/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: Test file and directory ownership changes for eventfs
4
5original_group=`stat -c "%g" .`
6original_owner=`stat -c "%u" .`
7
8mount_point=`stat -c '%m' .`
9
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.
12if [ ! -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
19fi
20
21mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`
22
23# find another owner and group that is not the original
24other_group=`tac /etc/group | grep -v ":$original_group:" | head -1 | cut -d: -f3`
25other_owner=`tac /etc/passwd | grep -v ":$original_owner:" | head -1 | cut -d: -f3`
26
27# Remove any group ownership already
28new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
29
30if [ "$new_options" = "$mount_options" ]; then
31	new_options="$mount_options,gid=$other_group"
32	mount_options="$mount_options,gid=$original_group"
33fi
34
35canary="events/timer events/timer/timer_cancel events/timer/timer_cancel/format"
36
37test() {
38	file=$1
39	test_group=$2
40
41	owner=`stat -c "%u" $file`
42	group=`stat -c "%g" $file`
43
44	echo "testing $file $owner=$original_owner and $group=$test_group"
45	if [ $owner -ne $original_owner ]; then
46		exit_fail
47	fi
48	if [ $group -ne $test_group ]; then
49		exit_fail
50	fi
51
52	# Note, the remount does not update ownership so test going to and from owner
53	echo "test owner $file to $other_owner"
54	chown $other_owner $file
55	owner=`stat -c "%u" $file`
56	if [ $owner -ne $other_owner ]; then
57		exit_fail
58	fi
59
60	chown $original_owner $file
61	owner=`stat -c "%u" $file`
62	if [ $owner -ne $original_owner ]; then
63		exit_fail
64	fi
65
66}
67
68run_tests() {
69	for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
70		test "$d" $other_group
71	done
72
73	chgrp $original_group events
74	test "events" $original_group
75	for d in "." "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
76		test "$d" $other_group
77	done
78
79	chgrp $original_group events/sched
80	test "events/sched" $original_group
81	for d in "." "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
82		test "$d" $other_group
83	done
84
85	chgrp $original_group events/sched/sched_switch
86	test "events/sched/sched_switch" $original_group
87	for d in "." "events/sched/sched_switch/enable" $canary; do
88		test "$d" $other_group
89	done
90
91	chgrp $original_group events/sched/sched_switch/enable
92	test "events/sched/sched_switch/enable" $original_group
93	for d in "." $canary; do
94		test "$d" $other_group
95	done
96}
97
98# Run the tests twice as leftovers can cause issues
99for loop in 1 2 ; do
100
101	echo "Running iteration $loop"
102
103	mount -o remount,"$new_options" .
104
105	run_tests
106
107	mount -o remount,"$mount_options" .
108
109	for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
110		test "$d" $original_group
111	done
112
113# check instances as well
114
115	chgrp $other_group instances
116
117	instance="$(mktemp -u test-XXXXXX)"
118
119	mkdir instances/$instance
120
121	cd instances/$instance
122
123	run_tests
124
125	cd ../..
126
127	rmdir instances/$instance
128
129	chgrp $original_group instances
130done
131
132exit 0
133