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