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