delete_module03.c revision 1dbf0b4a70c0ef7c849a0b3cd42da2c3874f9432
1/*
2 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17
18/*
19 *    AUTHOR: Madhu T L <madhu.tarikere@wipro.com>
20 *
21 *    DESCRIPTION
22 *      Verify that, delete_module(2) returns -1 and sets errno to EWOULDBLOCK,
23 *      if tried to remove a module while other modules depend on this module.
24 *
25 */
26
27#include <errno.h>
28#include "test.h"
29#include "usctest.h"
30#include "tst_module.h"
31#include "safe_macros.h"
32#include "linux_syscall_numbers.h"
33
34#define DUMMY_MOD		"dummy_del_mod"
35#define DUMMY_MOD_KO		"dummy_del_mod.ko"
36#define DUMMY_MOD_DEP		"dummy_del_mod_dep"
37#define DUMMY_MOD_DEP_KO	"dummy_del_mod_dep.ko"
38
39static int dummy_mod_loaded;
40static int dummy_mod_dep_loaded;
41
42char *TCID = "delete_module03";
43static int exp_enos[] = { EWOULDBLOCK, 0 };
44
45int TST_TOTAL = 1;
46
47static void setup();
48static void cleanup(void);
49
50int main(int argc, char **argv)
51{
52	int lc;
53	const char *msg;
54
55	msg = parse_opts(argc, argv, NULL, NULL);
56	if (msg != NULL)
57		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
58
59	setup();
60
61	for (lc = 0; TEST_LOOPING(lc); lc++) {
62		tst_count = 0;
63
64		TEST(ltp_syscall(__NR_delete_module, DUMMY_MOD, 0));
65		TEST_ERROR_LOG(errno);
66
67		if (TEST_RETURN < 0) {
68			switch (errno) {
69			case EWOULDBLOCK:
70				tst_resm(TPASS | TTERRNO,
71					 "delete_module() failed as expected");
72			break;
73			default:
74				tst_resm(TFAIL | TTERRNO, "delete_module() "
75					 "failed unexpectedly; expected: "
76					 "%d - %s", EWOULDBLOCK,
77					 strerror(EWOULDBLOCK));
78			break;
79			}
80		} else {
81			tst_resm(TFAIL, "delete_module()"
82				 "succeeded unexpectedly");
83			dummy_mod_loaded = 0;
84			/*
85			 * insmod DUMMY_MOD_KO again in case running
86			 * with -i option
87			 */
88			tst_module_load(cleanup, DUMMY_MOD_KO, NULL);
89			dummy_mod_loaded = 1;
90		}
91	}
92
93	cleanup();
94	tst_exit();
95
96}
97
98static void setup(void)
99{
100	tst_sig(NOFORK, DEF_HANDLER, cleanup);
101
102	tst_require_root(NULL);
103
104	TEST_EXP_ENOS(exp_enos);
105
106	/* Load first kernel module */
107	tst_module_load(cleanup, DUMMY_MOD_KO, NULL);
108	dummy_mod_loaded = 1;
109
110	/* Load dependant kernel module */
111	tst_module_load(cleanup, DUMMY_MOD_DEP_KO, NULL);
112	dummy_mod_dep_loaded = 1;
113
114	TEST_PAUSE;
115}
116
117static void cleanup(void)
118{
119	/* Unload dependent kernel module */
120	if (dummy_mod_dep_loaded == 1)
121		tst_module_unload(NULL, DUMMY_MOD_DEP_KO);
122
123	/* Unload first kernel module */
124	if (dummy_mod_loaded == 1)
125		tst_module_unload(NULL, DUMMY_MOD_KO);
126
127	TEST_CLEANUP;
128}
129