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 <yangx.jy@cn.fujitsu.com>
5 */
6
7/*
8 * AUTHOR: Madhu T L <madhu.tarikere@wipro.com>
9 *
10 * DESCRIPTION
11 * Verify that, delete_module(2) returns -1 and sets errno to EWOULDBLOCK,
12 * if tried to remove a module while other modules depend on this module.
13 */
14
15#include <errno.h>
16#include "tst_test.h"
17#include "tst_module.h"
18#include "lapi/syscalls.h"
19
20#define DUMMY_MOD		"dummy_del_mod"
21#define DUMMY_MOD_KO		"dummy_del_mod.ko"
22#define DUMMY_MOD_DEP_KO	"dummy_del_mod_dep.ko"
23
24static int dummy_mod_loaded;
25static int dummy_mod_dep_loaded;
26
27static void do_delete_module(void)
28{
29	TEST(tst_syscall(__NR_delete_module, DUMMY_MOD, 0));
30	if (TST_RET < 0) {
31		if (TST_ERR == EWOULDBLOCK) {
32			tst_res(TPASS | TTERRNO,
33				"delete_module() failed as expected");
34		} else {
35			tst_res(TFAIL | TTERRNO, "delete_module() failed "
36			"unexpectedly; expected: %s",
37			tst_strerrno(EWOULDBLOCK));
38		}
39	} else {
40		tst_res(TFAIL, "delete_module() succeeded unexpectedly");
41		dummy_mod_loaded = 0;
42		/*
43		 * insmod DUMMY_MOD_KO again in case running
44		 * with -i option
45		 */
46		tst_module_load(DUMMY_MOD_KO, NULL);
47		dummy_mod_loaded = 1;
48	}
49}
50
51static void setup(void)
52{
53	/* Load first kernel module */
54	tst_module_load(DUMMY_MOD_KO, NULL);
55	dummy_mod_loaded = 1;
56
57	/* Load dependant kernel module */
58	tst_module_load(DUMMY_MOD_DEP_KO, NULL);
59	dummy_mod_dep_loaded = 1;
60}
61
62static void cleanup(void)
63{
64	/* Unload dependent kernel module */
65	if (dummy_mod_dep_loaded == 1)
66		tst_module_unload(DUMMY_MOD_DEP_KO);
67
68	/* Unload first kernel module */
69	if (dummy_mod_loaded == 1)
70		tst_module_unload(DUMMY_MOD_KO);
71}
72
73static struct tst_test test = {
74	.needs_root = 1,
75	/* lockdown and SecureBoot requires signed modules */
76	.skip_in_lockdown = 1,
77	.skip_in_secureboot = 1,
78	.setup = setup,
79	.cleanup = cleanup,
80	.test_all = do_delete_module,
81};
82