• 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
15module_inserted=
16
17setup()
18{
19	if [ -z "$(cat /proc/modules)"  ]; then
20		tst_res TINFO "Loading dummy kernel module"
21		tst_require_module "ltp_lsmod01.ko"
22		tst_require_root
23		tst_require_cmds insmod
24		ROD insmod "$TST_MODPATH"
25
26		module_inserted=1
27	fi
28}
29
30cleanup()
31{
32	if [ "$module_inserted" = 1 ]; then
33		tst_res TINFO "Unloading dummy kernel module"
34		rmmod ltp_lsmod01
35		if [ $? -ne 0 ]; then
36			tst_res TWARN "rmmod failed"
37		fi
38	fi
39}
40
41lsmod_matches_proc_modules()
42{
43	lsmod_output=$(lsmod \
44			| awk '!/Module/{print $1, $2, ($3==-2) ? "-" : $3}' \
45			| sort)
46	if [ -z "$lsmod_output" ]; then
47		tst_brk TBROK "Failed to parse the output from lsmod"
48	fi
49
50	modules_output=$(awk '{print $1, $2, $3}' /proc/modules | sort)
51	if [ -z "$modules_output" ]; then
52		tst_brk TBROK "Failed to parse /proc/modules"
53	fi
54
55	if [ "$lsmod_output" != "$modules_output" ]; then
56		tst_res TINFO "lsmod output different from /proc/modules"
57
58		echo "$lsmod_output" > temp1
59		echo "$modules_output" > temp2
60		if tst_cmd_available diff; then
61			diff temp1 temp2
62		else
63			cat temp1 temp2
64		fi
65
66		return 1
67	fi
68	return 0
69}
70
71lsmod_test()
72{
73	for i in $(seq 1 5); do
74		if lsmod_matches_proc_modules; then
75			tst_res TPASS "'lsmod' passed"
76			return
77		fi
78		tst_res TINFO "Trying again"
79		sleep 1
80	done
81	tst_res TFAIL "'lsmod' doesn't match /proc/modules output"
82}
83
84. tst_test.sh
85tst_run
86