mount_options.tc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. # description: Test tracefs GID mount option
  4. # requires: "[gid=<gid>]":README
  5. fail() {
  6. local msg="$1"
  7. echo "FAILED: $msg"
  8. exit_fail
  9. }
  10. find_alternate_gid() {
  11. local original_gid="$1"
  12. tac /etc/group | grep -v ":$original_gid:" | head -1 | cut -d: -f3
  13. }
  14. remount_tracefs_with_options() {
  15. local mount_point="$1"
  16. local options="$2"
  17. mount -t tracefs -o "remount,$options" nodev "$mount_point"
  18. setup
  19. }
  20. unmount_tracefs() {
  21. local mount_point="$1"
  22. # Need to make sure the mount isn't busy so that we can umount it
  23. (cd $mount_point; finish_ftrace;)
  24. cleanup
  25. }
  26. create_instance() {
  27. local mount_point="$1"
  28. local instance="$mount_point/instances/$(mktemp -u test-XXXXXX)"
  29. mkdir "$instance"
  30. echo "$instance"
  31. }
  32. remove_instance() {
  33. local instance="$1"
  34. rmdir "$instance"
  35. }
  36. check_gid() {
  37. local mount_point="$1"
  38. local expected_gid="$2"
  39. echo "Checking permission group ..."
  40. cd "$mount_point"
  41. for file in "." "events" "events/sched" "events/sched/sched_switch" "events/sched/sched_switch/enable"; do
  42. local gid=`stat -c "%g" $file`
  43. if [ "$gid" -ne "$expected_gid" ]; then
  44. cd - # Return to the previous working directory (tracefs root)
  45. fail "$(realpath $file): Expected group $expected_gid; Got group $gid"
  46. fi
  47. done
  48. cd - # Return to the previous working directory (tracefs root)
  49. }
  50. test_gid_mount_option() {
  51. local mount_point=$(get_mount_point)
  52. local mount_options=$(get_mnt_options "$mount_point")
  53. local original_group=$(stat -c "%g" .)
  54. local other_group=$(find_alternate_gid "$original_group")
  55. # Set up mount options with new GID for testing
  56. local new_options=`echo "$mount_options" | sed -e "s/gid=[0-9]*/gid=$other_group/"`
  57. if [ "$new_options" = "$mount_options" ]; then
  58. new_options="$mount_options,gid=$other_group"
  59. mount_options="$mount_options,gid=$original_group"
  60. fi
  61. # Unmount existing tracefs instance and mount with new GID
  62. unmount_tracefs "$mount_point"
  63. remount_tracefs_with_options "$mount_point" "$new_options"
  64. check_gid "$mount_point" "$other_group"
  65. # Check that files created after the mount inherit the GID
  66. local instance=$(create_instance "$mount_point")
  67. check_gid "$instance" "$other_group"
  68. remove_instance "$instance"
  69. # Unmount and remount with the original GID
  70. unmount_tracefs "$mount_point"
  71. remount_tracefs_with_options "$mount_point" "$mount_options"
  72. check_gid "$mount_point" "$original_group"
  73. }
  74. test_gid_mount_option
  75. exit 0