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