xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/delete_module/delete_module01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  * Copyright (c) 2018 Xiao Yang <[email protected]>
5  * Copyright (c) Linux Test Project, 2002-2023
6  * Author: Madhu T L <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Basic test for delete_module(2).
13  *
14  * Install dummy_del_mod.ko and delete it with delete_module(2).
15  */
16 
17 #include <stdlib.h>
18 #include "tst_test.h"
19 #include "tst_module.h"
20 #include "tst_kconfig.h"
21 #include "lapi/syscalls.h"
22 
23 #define MODULE_NAME	"dummy_del_mod"
24 #define MODULE_NAME_KO	MODULE_NAME ".ko"
25 
26 static int module_loaded;
27 
do_delete_module(void)28 static void do_delete_module(void)
29 {
30 	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
31 
32 	tst_kcmdline_parse(&params, 1);
33 	if (atoi(params.value) == 1)
34 		tst_brk(TCONF, "module signature is enforced, skip test");
35 
36 	if (!module_loaded) {
37 		tst_module_load(MODULE_NAME_KO, NULL);
38 		module_loaded = 1;
39 	}
40 
41 	TEST(tst_syscall(__NR_delete_module, MODULE_NAME, 0));
42 	if (TST_RET == -1) {
43 		tst_res(TFAIL | TTERRNO,
44 			"delete_module() failed to remove module entry for %s",
45 			MODULE_NAME);
46 		return;
47 	}
48 
49 	tst_res(TPASS, "delete_module() successful");
50 	module_loaded = 0;
51 }
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	if (module_loaded)
56 		tst_module_unload(MODULE_NAME_KO);
57 }
58 
59 static struct tst_test test = {
60 	.needs_root = 1,
61 	/* lockdown and SecureBoot requires signed modules */
62 	.skip_in_lockdown = 1,
63 	.skip_in_secureboot = 1,
64 	.cleanup = cleanup,
65 	.test_all = do_delete_module,
66 };
67