lsmod01.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2015 Fujitsu Ltd.
  4. # Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
  5. #
  6. # Test basic functionality of lsmod command.
  7. TST_CLEANUP=cleanup
  8. TST_SETUP=setup
  9. TST_TESTFUNC=lsmod_test
  10. TST_NEEDS_TMPDIR=1
  11. TST_NEEDS_CMDS="lsmod"
  12. . tst_test.sh
  13. module_inserted=
  14. setup()
  15. {
  16. if [ -z "$(cat /proc/modules)" ]; then
  17. tst_res TINFO "Loading dummy kernel module"
  18. tst_require_module "ltp_lsmod01.ko"
  19. tst_require_root
  20. tst_require_cmds insmod
  21. insmod "$TST_MODPATH"
  22. if [ $? -ne 0 ]; then
  23. tst_res TBROK "insmod failed"
  24. return
  25. fi
  26. module_inserted=1
  27. fi
  28. }
  29. cleanup()
  30. {
  31. if [ "$module_inserted" = 1 ]; then
  32. tst_res TINFO "Unloading dummy kernel module"
  33. rmmod ltp_lsmod01
  34. if [ $? -ne 0 ]; then
  35. tst_res TWARN "rmmod failed"
  36. fi
  37. fi
  38. }
  39. lsmod_matches_proc_modules()
  40. {
  41. lsmod_output=$(lsmod | awk '!/Module/{print $1, $2, $3}' | sort)
  42. if [ -z "$lsmod_output" ]; then
  43. tst_brk TBROK "Failed to parse the output from lsmod"
  44. fi
  45. modules_output=$(awk '{print $1, $2, $3}' /proc/modules | sort)
  46. if [ -z "$modules_output" ]; then
  47. tst_brk TBROK "Failed to parse /proc/modules"
  48. fi
  49. if [ "$lsmod_output" != "$modules_output" ]; then
  50. tst_res TINFO "lsmod output different from /proc/modules"
  51. echo "$lsmod_output" > temp1
  52. echo "$modules_output" > temp2
  53. if tst_cmd_available diff; then
  54. diff temp1 temp2
  55. else
  56. cat temp1 temp2
  57. fi
  58. return 1
  59. fi
  60. return 0
  61. }
  62. lsmod_test()
  63. {
  64. for i in $(seq 1 5); do
  65. if lsmod_matches_proc_modules; then
  66. tst_res TPASS "'lsmod' passed"
  67. return
  68. fi
  69. tst_res TINFO "Trying again"
  70. sleep 1
  71. done
  72. tst_res TFAIL "'lsmod' doesn't match /proc/modules output"
  73. }
  74. tst_run