binfmt_misc_lib.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. #
  4. # Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
  5. # Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
  6. TST_SETUP=binfmt_misc_setup
  7. TST_CLEANUP=binfmt_misc_cleanup
  8. TST_NEEDS_DRIVERS="binfmt_misc"
  9. TST_NEEDS_TMPDIR=1
  10. TST_NEEDS_ROOT=1
  11. TST_NEEDS_CMDS="${TST_NEEDS_CMDS} modprobe mount umount mkdir rm"
  12. . tst_test.sh
  13. rmod_binfmt_misc=0
  14. umount_binfmt_misc=0
  15. binfmt_misc_mntpoint="ltp_binfmt_misc"
  16. remove_binary_type()
  17. {
  18. local name=$1
  19. (echo -1 >"$name") 2>/dev/null
  20. [ $? -ne 0 -o -f "$name" ] && \
  21. tst_res TWARN "Fail to remove a binary type"
  22. }
  23. get_binfmt_misc_mntpoint()
  24. {
  25. local mntpoint
  26. mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts)
  27. [ -z "$mntpoint" ] && tst_brk TBROK "Can't get binfmt_misc mntpoint"
  28. echo "$mntpoint"
  29. }
  30. binfmt_misc_setup()
  31. {
  32. local mntpoint
  33. if ! grep -q "binfmt_misc" /proc/filesystems; then
  34. ROD modprobe binfmt_misc
  35. rmod_binfmt_misc=1
  36. fi
  37. # Match fs type accurately, because autofs is also mounted on
  38. # /proc/sys/fs/binfmt_misc on some distros, as below:
  39. # cat /proc/mounts | grep binfmt_misc
  40. # systemd-1 /proc/sys/fs/binfmt_misc autofs ...
  41. # binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc ...
  42. mntpoint=$(awk '/ binfmt_misc / { print $2 }' /proc/mounts)
  43. [ -n "$mntpoint" ] && return
  44. ROD mkdir ${binfmt_misc_mntpoint}
  45. ROD mount -t binfmt_misc none ${binfmt_misc_mntpoint}
  46. umount_binfmt_misc=1
  47. }
  48. binfmt_misc_cleanup()
  49. {
  50. if [ ${umount_binfmt_misc} -ne 0 ]; then
  51. umount ${binfmt_misc_mntpoint}
  52. umount_binfmt_misc=0
  53. fi
  54. [ -d ${binfmt_misc_mntpoint} ] && rm -rf ${binfmt_misc_mntpoint}
  55. if [ ${rmod_binfmt_misc} -ne 0 ]; then
  56. modprobe -r binfmt_misc
  57. rmod_binfmt_misc=0
  58. fi
  59. }