xref: /linux/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
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' .`
9mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`
10
11# find another owner and group that is not the original
12other_group=`tac /etc/group | grep -v ":$original_group:" | head -1 | cut -d: -f3`
13other_owner=`tac /etc/passwd | grep -v ":$original_owner:" | head -1 | cut -d: -f3`
14
15# Remove any group ownership already
16new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
17
18if [ "$new_options" = "$mount_options" ]; then
19	new_options="$mount_options,gid=$other_group"
20	mount_options="$mount_options,gid=$original_group"
21fi
22
23canary="events/timer events/timer/timer_cancel events/timer/timer_cancel/format"
24
25test() {
26	file=$1
27	test_group=$2
28
29	owner=`stat -c "%u" $file`
30	group=`stat -c "%g" $file`
31
32	echo "testing $file $owner=$original_owner and $group=$test_group"
33	if [ $owner -ne $original_owner ]; then
34		exit_fail
35	fi
36	if [ $group -ne $test_group ]; then
37		exit_fail
38	fi
39
40	# Note, the remount does not update ownership so test going to and from owner
41	echo "test owner $file to $other_owner"
42	chown $other_owner $file
43	owner=`stat -c "%u" $file`
44	if [ $owner -ne $other_owner ]; then
45		exit_fail
46	fi
47
48	chown $original_owner $file
49	owner=`stat -c "%u" $file`
50	if [ $owner -ne $original_owner ]; then
51		exit_fail
52	fi
53
54}
55
56run_tests() {
57	for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
58		test "$d" $other_group
59	done
60
61	chgrp $original_group events
62	test "events" $original_group
63	for d in "." "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
64		test "$d" $other_group
65	done
66
67	chgrp $original_group events/sched
68	test "events/sched" $original_group
69	for d in "." "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
70		test "$d" $other_group
71	done
72
73	chgrp $original_group events/sched/sched_switch
74	test "events/sched/sched_switch" $original_group
75	for d in "." "events/sched/sched_switch/enable" $canary; do
76		test "$d" $other_group
77	done
78
79	chgrp $original_group events/sched/sched_switch/enable
80	test "events/sched/sched_switch/enable" $original_group
81	for d in "." $canary; do
82		test "$d" $other_group
83	done
84}
85
86mount -o remount,"$new_options" .
87
88run_tests
89
90mount -o remount,"$mount_options" .
91
92for d in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable" $canary; do
93	test "$d" $original_group
94done
95
96# check instances as well
97
98chgrp $other_group instances
99
100instance="$(mktemp -u test-XXXXXX)"
101
102mkdir instances/$instance
103
104cd instances/$instance
105
106run_tests
107
108cd ../..
109
110rmdir instances/$instance
111
112chgrp $original_group instances
113
114exit 0
115