query_module01.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 *    TEST IDENTIFIER   : query_module01
20 *
21 *    EXECUTED BY       : root / superuser
22 *
23 *    TEST TITLE        : Checking functionality of query_module(2)
24 *
25 *    TEST CASE TOTAL   : 6
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,
35 *	1. query_module(2) is successful for NULL module name, which argument
36 *	   set to 0.
37 *	2. query_module(2) is successful for NULL module name, which argument
38 *	   set to QM_MODULES.
39 *	3. query_module(2) is successful for valid module name, which argument
40 *	   set to QM_DEPS.
41 *	4. query_module(2) is successful for valid module name, which argument
42 *	   set to QM_REFS.
43 *	5. query_module(2) is successful for valid module name, which argument
44 *	   set to QM_INFO.
45 *	6. query_module(2) is successful for valid module name, which argument
46 *	   set to QM_SYMBOLS.
47 *
48 *      Setup:
49 *	  Setup signal handling.
50 *	  Test caller is superuser
51 *	  Initialize  long module name
52 *	  Pause for SIGUSR1 if option specified.
53 *
54 *	Test:
55 *	 Loop if the proper options are given.
56 *	  Execute system call
57 *	  Check return value and functionality, if success,
58 *		 Issue PASS message
59 *	Otherwise,
60 *		Issue FAIL message
61 *
62 *	Cleanup:
63 *	  Print errno log and/or timing stats if options given
64 *
65 * USAGE:  <for command-line>
66 *  query_module01 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
67 *		where,  -c n : Run n copies concurrently.
68 *			-e   : Turn on errno logging.
69 *			-f   : Turn off functional testing
70 *			-h   : Show help screen
71 *			-i n : Execute test n times.
72 *			-I x : Execute test for x seconds.
73 *			-p   : Pause for SIGUSR1 before starting
74 *			-P x : Pause for x seconds between iterations.
75 *			-t   : Turn on syscall timing.
76 *
77 * RESTRICTIONS
78 *	-c option has no effect for this testcase, even if used allows only
79 *	one instance to run at a time.
80 *
81 * CHANGES
82 *
83 * 12/03/02 Added "force" to insmod to ignore kernel version.
84 *          -Robbie Williamson <robbiew@us.ibm.com>
85 *
86 ****************************************************************/
87
88#include <errno.h>
89#include <pwd.h>
90#include <sys/types.h>
91#include <unistd.h>
92#include <limits.h>
93#include <asm/atomic.h>
94#include <linux/module.h>
95#include "test.h"
96#include "usctest.h"
97
98#ifndef PAGE_SIZE
99#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
100#endif
101
102#define LONGMODNAMECHAR	'm'	/* Arbitrarily selected */
103#define MODNAMEMAX	(PAGE_SIZE + 1)
104#define EXP_RET_VAL	0
105#define DUMMY_MOD	"dummy_query_mod"
106#define DUMMY_MOD_DEP	"dummy_query_mod_dep"
107#define QM_INVALID	(QM_INFO + 100)
108
109/* Name of exported function in DUMMY_MOD */
110#define EXP_FUNC_NAME	"dummy_func_test"
111
112struct test_case_t {		/* test case structure */
113	char *modname;
114	int which;
115	char *desc;
116	int (*setup) (void);	/* Individual setup routine */
117	void (*cleanup) (void);	/* Individual cleanup routine */
118};
119
120char *TCID = "query_module01";
121static char longmodname[MODNAMEMAX];
122static int testno;
123static char out_buf[PAGE_SIZE];
124static size_t ret;
125
126static int test_functionality(int, char *, size_t, size_t);
127static void setup(void);
128static void cleanup(void);
129static int setup1(void);
130static void cleanup1(void);
131static int setup2(void);
132static void cleanup2(void);
133
134static struct test_case_t tdat[] = {
135	{NULL, 0, "module name: NULL, which: 0", NULL, NULL},
136
137	{NULL, QM_MODULES, "NULL module name, which: QM_MODULES",
138	 setup1, cleanup1},
139
140	{DUMMY_MOD_DEP, QM_DEPS, "valid module name, which: QM_DEPS",
141	 setup2, cleanup2},
142
143	{DUMMY_MOD, QM_REFS, "valid module name, which: QM_REFS",
144	 setup2, cleanup2},
145
146	{DUMMY_MOD, QM_INFO, "valid module name, which: QM_INFO",
147	 setup1, cleanup1},
148
149	{DUMMY_MOD, QM_SYMBOLS, "valid module name, which: QM_SYMBOLS",
150	 setup1, cleanup1},
151};
152
153int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
154
155int main(int argc, char **argv)
156{
157	int lc;
158	const char *msg;
159	size_t buflen = sizeof(out_buf);
160
161	if ((msg = parse_opts(argc, argv, NULL, NULL)) != (char *)NULL) {
162		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
163	}
164
165	tst_tmpdir();
166	setup();
167
168	for (lc = 0; TEST_LOOPING(lc); lc++) {
169		/* reset tst_count in case we are looping */
170		tst_count = 0;
171
172		for (testno = 0; testno < TST_TOTAL; ++testno) {
173			if ((tdat[testno].setup) && (tdat[testno].setup())) {
174				/* setup() failed, skip this test */
175				continue;
176			}
177
178			TEST(query_module(tdat[testno].modname,
179					  tdat[testno].which, (void *)out_buf,
180					  buflen, &ret));
181
182			if ((TEST_RETURN == EXP_RET_VAL) &&
183			    !test_functionality(tdat[testno].which,
184						out_buf, buflen, ret)) {
185				tst_resm(TPASS, "query_module() successful "
186					 "for %s", tdat[testno].desc);
187			} else {
188				tst_resm(TFAIL, "query_module() failed for "
189					 "%s ; returned"
190					 " %d (expected %d), errno %d (expected"
191					 " 0)", tdat[testno].desc,
192					 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
193			}
194			if (tdat[testno].cleanup) {
195				tdat[testno].cleanup();
196			}
197		}
198	}
199	cleanup();
200	tst_exit();
201}
202
203int test_functionality(int which, char *buf, size_t bufsize, size_t ret)
204{
205	int i = 0;
206	char *modname;
207	unsigned long *vals;
208
209	switch (which) {
210	case 0:
211		/* Always return SUCCESS */
212		return 0;
213
214	case QM_MODULES:
215	case QM_DEPS:
216		/* Return SUCCESS if found DUMMY_MOD entry */
217		modname = DUMMY_MOD;
218		break;
219
220	case QM_REFS:
221		/* Return SUCCESS if found DUMMY_MOD_DEP entry */
222		modname = DUMMY_MOD_DEP;
223		break;
224
225	case QM_INFO:
226		/*
227		 * Since module is already loaded, flags should show
228		 * MOD_RUNNING
229		 */
230		if (((struct module_info *)buf)->flags & MOD_RUNNING) {
231			return 0;
232		}
233		return 1;
234
235	case QM_SYMBOLS:
236		vals = (unsigned long *)buf;
237
238		/*
239		 * Find entry for atleast one symbol, checking for
240		 * EXP_FUNC_NAME symbol, if found return SUCCESS.
241		 */
242		for (i = 0; i < ret; i++, vals += 2) {
243
244			/* buf + vals[1] - address of symbol name */
245			if (!strcmp(buf + vals[1], EXP_FUNC_NAME)) {
246				return 0;
247			}
248		}
249		return 1;
250
251	default:
252		/* Unknown which type */
253		return 1;
254	}
255
256	/* Return SUCCESS if found entry */
257	for (i = 0; i != ret; i++) {
258		if (strcmp(buf, modname)) {
259			buf += strlen(buf) + 1;
260		} else {
261			return 0;
262		}
263	}
264	return 1;
265
266}
267
268/* Insert a module of name mod */
269int insert_mod(char *mod)
270{
271	char cmd[80];
272
273	if (sprintf(cmd, "cp `which %s.o` ./", mod) == -1) {
274		tst_resm(TBROK, "sprintf failed");
275		return 1;
276	}
277	if (system(cmd) != 0) {
278		tst_resm(TBROK, "Failed to copy %s module", mod);
279		return 1;
280	}
281
282	/* Should use force to ignore kernel version & insure loading  */
283	/* -RW                                                         */
284	/* if (sprintf(cmd, "insmod %s.o", mod) == -1) {               */
285	if (sprintf(cmd, "insmod --force -q %s.o >/dev/null 2>&1", mod) == -1) {
286		tst_resm(TBROK, "sprintf failed");
287		return 1;
288	}
289	if (system(cmd) != 0) {
290		tst_resm(TBROK, "Failed to load %s module", mod);
291		return 1;
292	}
293	return 0;
294}
295
296int setup1(void)
297{
298	if (insert_mod(DUMMY_MOD)) {
299		/* Failed */
300		return 1;
301	} else {
302		return 0;
303	}
304}
305
306int setup2(void)
307{
308	if (insert_mod(DUMMY_MOD)) {
309		/* Failed */
310		return 1;
311	}
312	if (insert_mod(DUMMY_MOD_DEP)) {
313		/* Falied to load DUMMY_MOD_DEP, unload DUMMY_MOD */
314		cleanup1();
315		return 1;
316	}
317	return 0;
318}
319
320void cleanup1(void)
321{
322	/* Remove the loadable module - DUMMY_MOD */
323	if (system("rmmod " DUMMY_MOD) != 0) {
324		tst_brkm(TBROK, cleanup, "Failed to unload module %s",
325			 DUMMY_MOD);
326	}
327}
328
329void cleanup2(void)
330{
331	/* Remove the loadable module - DUMMY_MOD_DEP */
332	if (system("rmmod " DUMMY_MOD_DEP) != 0) {
333		tst_brkm(TBROK, cleanup, "Failed to unload module %s",
334			 DUMMY_MOD_DEP);
335	}
336	/* Remove the loadable module - DUMMY_MOD */
337	cleanup1();
338}
339
340/*
341 * setup()
342 *	performs all ONE TIME setup for this test
343 */
344void setup(void)
345{
346
347	tst_sig(FORK, DEF_HANDLER, cleanup);
348
349	tst_require_root(NULL);
350
351	if (tst_kvercmp(2, 5, 48) >= 0)
352		tst_brkm(TCONF, NULL, "This test will not work on "
353			 "kernels after 2.5.48");
354
355	/* Initialize longmodname to LONGMODNAMECHAR character */
356	memset(longmodname, LONGMODNAMECHAR, MODNAMEMAX - 1);
357
358	/* Pause if that option was specified
359	 * TEST_PAUSE contains the code to fork the test with the -c option.
360	 */
361	TEST_PAUSE;
362}
363
364/*
365 * cleanup()
366 *	performs all ONE TIME cleanup for this test at
367 *	completion or premature exit
368 */
369void cleanup(void)
370{
371	/*
372	 * print timing stats if that option was specified.
373	 * print errno log if that option was specified.
374	 */
375
376	TEST_CLEANUP;
377	tst_rmdir();
378}
379