|
| 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 |
0 commit comments