delete_module03.c revision 8fb1cdb0538640f295691929650408688537fb7f
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., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
16 */
17/**********************************************************
18 *
19 *    TEST IDENTIFIER   : delete_module03
20 *
21 *    EXECUTED BY       : root / superuser
22 *
23 *    TEST TITLE        : Checking error conditions for delete_module(2)
24 *
25 *    TEST CASE TOTAL   : 1
26 *
27 *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28 *
29 *    SIGNALS
30 *      Uses SIGUSR1 to pause before test if option set.
31 *      (See the parse_opts(3) man page).
32 *
33 *    DESCRIPTION
34 *      Verify that, delete_module(2) returns -1 and sets errno to EBUSY, if
35 *		 tried to remove a module in-use.
36 *
37 *      Setup:
38 *        Setup signal handling.
39 *        Test caller is super user
40 *        Set expected errnos for logging
41 *        Pause for SIGUSR1 if option specified.
42 *		   Insert loadable modules
43 *
44 *      Test:
45 *       Loop if the proper options are given.
46 *        Execute system call
47 *        Check return code and error number, if matching,
48 *                   Issue PASS message
49 *        Otherwise,
50 *                   Issue FAIL message
51 *
52 *      Cleanup:
53 *        Print errno log and/or timing stats if options given
54 *
55 * USAGE:  <for command-line>
56 *  delete_module03 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
57 *		 		 where,  -c n : Run n copies concurrently. (no
58 *		 		 		effect)
59 *	 		 		 -e   : Turn on errno logging.
60 *	 		 		 -f   : Turn off functional testing
61 *	 		 		 -h   : Show help screen
62 *	 		 		 -i n : Execute test n times.
63 *	 		 		 -I x : Execute test for x seconds.
64 *	 		 		 -p   : Pause for SIGUSR1 before
65 *	 		 		 	starting
66 *	 		 		 -P x : Pause for x seconds between
67 *	 		 		 	iterations.
68 *	 		 		 -t   : Turn on syscall timing.
69 *
70 * RESTRICTIONS
71 *		 -c option has no effect for this testcase, even if used allows
72 *		 only one instance to run at a time.
73 *
74 * CHANGELOG
75 *
76 *  11/22/02 -	Added "--force" to insmod options and redirected output to
77 *  		/dev/null. This was done to allow kernel mismatches, b/c it
78 *  		doesn't matter in this case.
79 *		Robbie Williamson <robbiew@us.ibm.com>
80 *
81 ****************************************************************/
82
83#include <libgen.h>
84#include <errno.h>
85#include <pwd.h>
86#include "test.h"
87#include "usctest.h"
88
89extern int Tst_count;
90
91#define DUMMY_MOD		 "dummy_del_mod"
92#define DUMMY_MOD_DEP		 "dummy_del_mod_dep"
93#define EXP_RET_VAL		 -1
94#define EXP_ERRNO		 EWOULDBLOCK
95/*#define EXP_ERRNO		 EBUSY */
96
97char *TCID = "delete_module03";
98/*static int exp_enos[] = {EBUSY, 0}; */
99static int exp_enos[] = {EWOULDBLOCK, 0};
100int TST_TOTAL = 1;
101
102static int setup(void);
103static void cleanup(void);
104
105int
106main(int argc, char **argv)
107{
108	int lc;		 		 /* loop counter */
109	char *msg;	 		 /* message returned from parse_opts */
110	char cmd[50];
111
112	/* parse standard options */
113	if ((msg = parse_opts(argc, argv, (option_t*) NULL, NULL)) !=
114	    (char *) NULL) {
115		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
116	}
117
118	if (STD_COPIES != 1) {
119		tst_resm(TINFO, "-c option has no effect for this testcase - "
120				"doesn't allow running more than one instance "
121		 		"at a time");
122		STD_COPIES = 1;
123	}
124
125	/* Load first kernel module */
126	if (sprintf(cmd, "/sbin/insmod %s/%s.ko", dirname(argv[0]),
127		DUMMY_MOD) <= 0) {
128		tst_resm(TBROK, "sprintf failed");
129		return 1;
130	}
131	if ((system(cmd)) != 0) {
132		tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD);
133		return 1;
134	}
135
136	/* Load dependant kernel module */
137        if (sprintf(cmd, "/sbin/insmod %s/%s.ko", dirname(argv[0]),
138		DUMMY_MOD_DEP) <= 0) {
139		tst_resm(TBROK, "sprintf failed");
140		goto END;
141	}
142        if ((system(cmd)) != 0) {
143		tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD_DEP);
144		goto END;
145        }
146
147	tst_tmpdir();
148	if (setup() != 0) {
149		return 1;
150	}
151
152	/* check looping state if -i option is given */
153	for (lc = 0; TEST_LOOPING(lc); lc++) {
154		/* reset Tst_count in case we are looping */
155		Tst_count = 0;
156
157		/* Test the system call */
158		TEST(delete_module(DUMMY_MOD));
159
160		TEST_ERROR_LOG(TEST_ERRNO);
161		if ((TEST_RETURN == (int) EXP_RET_VAL) &&
162		     (TEST_ERRNO == EXP_ERRNO) ) {
163			tst_resm(TPASS, "Expected failure for module in-use, "
164		 			"errno: %d", TEST_ERRNO);
165		} else {
166			tst_resm(TFAIL, "Unexpected results for module in-use; "
167		 			"returned %d (expected %d), errno %d "
168					"(expected %d)", TEST_RETURN,
169					EXP_RET_VAL, TEST_ERRNO, EXP_ERRNO);
170		}
171	}
172	cleanup();
173END:
174	if (system("rmmod "DUMMY_MOD) != 0) {
175		tst_resm(TBROK, "Failed to unload %s module", DUMMY_MOD);
176		return 1;
177	}
178
179	/*NOTREACHED*/
180	return 0;
181}
182
183/*
184 * setup()
185 *	performs all ONE TIME setup for this test
186 */
187int
188setup(void)
189{
190	/* capture signals */
191	tst_sig(FORK, DEF_HANDLER, cleanup);
192
193	/* Check whether it is root  */
194	if (geteuid() != 0) {
195		tst_resm(TBROK, "Must be root for this test!");
196		return 1;
197	}
198
199	/*
200	if (tst_kvercmp(2,5,48) >= 0)
201		tst_brkm(TCONF, tst_exit, "This test will not work on "
202					  "kernels after 2.5.48");
203	*/
204
205	/* set the expected errnos... */
206	TEST_EXP_ENOS(exp_enos);
207
208	/* Pause if that option was specified
209	 * TEST_PAUSE contains the code to fork the test with the -c option.
210	 */
211	TEST_PAUSE;
212	return 0;
213
214}
215
216/*
217 * cleanup()
218 *	performs all ONE TIME cleanup for this test at
219 *	completion or premature exit
220 */
221void
222cleanup(void)
223{
224	/* Unload dependent kernel module */
225	if (system("rmmod "DUMMY_MOD_DEP) != 0) {
226		tst_resm(TBROK, "Failed to unload %s module", DUMMY_MOD_DEP);
227	}
228	/* Unload first kernel module */
229	if (system("rmmod "DUMMY_MOD) != 0) {
230		tst_resm(TBROK, "Failed to unload %s module",
231		DUMMY_MOD);
232	}
233	/*
234	 * print timing stats if that option was specified.
235	 * print errno log if that option was specified.
236	 */
237	TEST_CLEANUP;
238	tst_rmdir();
239	/* exit with return code appropriate for results */
240	tst_exit();
241	/*NOTREACHED*/
242}
243