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
97#ifndef PAGE_SIZE
98#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
99#endif
100
101#define LONGMODNAMECHAR	'm'	/* Arbitrarily selected */
102#define MODNAMEMAX	(PAGE_SIZE + 1)
103#define EXP_RET_VAL	0
104#define DUMMY_MOD	"dummy_query_mod"
105#define DUMMY_MOD_DEP	"dummy_query_mod_dep"
106#define QM_INVALID	(QM_INFO + 100)
107
108/* Name of exported function in DUMMY_MOD */
109#define EXP_FUNC_NAME	"dummy_func_test"
110
111struct test_case_t {		/* test case structure */
112	char *modname;
113	int which;
114	char *desc;
115	int (*setup) (void);	/* Individual setup routine */
116	void (*cleanup) (void);	/* Individual cleanup routine */
117};
118
119char *TCID = "query_module01";
120static char longmodname[MODNAMEMAX];
121static int testno;
122static char out_buf[PAGE_SIZE];
123static size_t ret;
124
125static int test_functionality(int, char *, size_t, size_t);
126static void setup(void);
127static void cleanup(void);
128static int setup1(void);
129static void cleanup1(void);
130static int setup2(void);
131static void cleanup2(void);
132
133static struct test_case_t tdat[] = {
134	{NULL, 0, "module name: NULL, which: 0", NULL, NULL},
135
136	{NULL, QM_MODULES, "NULL module name, which: QM_MODULES",
137	 setup1, cleanup1},
138
139	{DUMMY_MOD_DEP, QM_DEPS, "valid module name, which: QM_DEPS",
140	 setup2, cleanup2},
141
142	{DUMMY_MOD, QM_REFS, "valid module name, which: QM_REFS",
143	 setup2, cleanup2},
144
145	{DUMMY_MOD, QM_INFO, "valid module name, which: QM_INFO",
146	 setup1, cleanup1},
147
148	{DUMMY_MOD, QM_SYMBOLS, "valid module name, which: QM_SYMBOLS",
149	 setup1, cleanup1},
150};
151
152int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
153
154int main(int argc, char **argv)
155{
156	int lc;
157	size_t buflen = sizeof(out_buf);
158
159	tst_parse_opts(argc, argv, NULL, NULL);
160
161	tst_tmpdir();
162	setup();
163
164	for (lc = 0; TEST_LOOPING(lc); lc++) {
165		/* reset tst_count in case we are looping */
166		tst_count = 0;
167
168		for (testno = 0; testno < TST_TOTAL; ++testno) {
169			if ((tdat[testno].setup) && (tdat[testno].setup())) {
170				/* setup() failed, skip this test */
171				continue;
172			}
173
174			TEST(query_module(tdat[testno].modname,
175					  tdat[testno].which, (void *)out_buf,
176					  buflen, &ret));
177
178			if ((TEST_RETURN == EXP_RET_VAL) &&
179			    !test_functionality(tdat[testno].which,
180						out_buf, buflen, ret)) {
181				tst_resm(TPASS, "query_module() successful "
182					 "for %s", tdat[testno].desc);
183			} else {
184				tst_resm(TFAIL, "query_module() failed for "
185					 "%s ; returned"
186					 " %d (expected %d), errno %d (expected"
187					 " 0)", tdat[testno].desc,
188					 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
189			}
190			if (tdat[testno].cleanup) {
191				tdat[testno].cleanup();
192			}
193		}
194	}
195	cleanup();
196	tst_exit();
197}
198
199int test_functionality(int which, char *buf, size_t bufsize, size_t ret)
200{
201	int i = 0;
202	char *modname;
203	unsigned long *vals;
204
205	switch (which) {
206	case 0:
207		/* Always return SUCCESS */
208		return 0;
209
210	case QM_MODULES:
211	case QM_DEPS:
212		/* Return SUCCESS if found DUMMY_MOD entry */
213		modname = DUMMY_MOD;
214		break;
215
216	case QM_REFS:
217		/* Return SUCCESS if found DUMMY_MOD_DEP entry */
218		modname = DUMMY_MOD_DEP;
219		break;
220
221	case QM_INFO:
222		/*
223		 * Since module is already loaded, flags should show
224		 * MOD_RUNNING
225		 */
226		if (((struct module_info *)buf)->flags & MOD_RUNNING) {
227			return 0;
228		}
229		return 1;
230
231	case QM_SYMBOLS:
232		vals = (unsigned long *)buf;
233
234		/*
235		 * Find entry for atleast one symbol, checking for
236		 * EXP_FUNC_NAME symbol, if found return SUCCESS.
237		 */
238		for (i = 0; i < ret; i++, vals += 2) {
239
240			/* buf + vals[1] - address of symbol name */
241			if (!strcmp(buf + vals[1], EXP_FUNC_NAME)) {
242				return 0;
243			}
244		}
245		return 1;
246
247	default:
248		/* Unknown which type */
249		return 1;
250	}
251
252	/* Return SUCCESS if found entry */
253	for (i = 0; i != ret; i++) {
254		if (strcmp(buf, modname)) {
255			buf += strlen(buf) + 1;
256		} else {
257			return 0;
258		}
259	}
260	return 1;
261
262}
263
264/* Insert a module of name mod */
265int insert_mod(char *mod)
266{
267	char cmd[80];
268
269	if (sprintf(cmd, "cp `which %s.o` ./", mod) == -1) {
270		tst_resm(TBROK, "sprintf failed");
271		return 1;
272	}
273	if (system(cmd) != 0) {
274		tst_resm(TBROK, "Failed to copy %s module", mod);
275		return 1;
276	}
277
278	/* Should use force to ignore kernel version & insure loading  */
279	/* -RW                                                         */
280	/* if (sprintf(cmd, "insmod %s.o", mod) == -1) {               */
281	if (sprintf(cmd, "insmod --force -q %s.o >/dev/null 2>&1", mod) == -1) {
282		tst_resm(TBROK, "sprintf failed");
283		return 1;
284	}
285	if (system(cmd) != 0) {
286		tst_resm(TBROK, "Failed to load %s module", mod);
287		return 1;
288	}
289	return 0;
290}
291
292int setup1(void)
293{
294	if (insert_mod(DUMMY_MOD)) {
295		/* Failed */
296		return 1;
297	} else {
298		return 0;
299	}
300}
301
302int setup2(void)
303{
304	if (insert_mod(DUMMY_MOD)) {
305		/* Failed */
306		return 1;
307	}
308	if (insert_mod(DUMMY_MOD_DEP)) {
309		/* Falied to load DUMMY_MOD_DEP, unload DUMMY_MOD */
310		cleanup1();
311		return 1;
312	}
313	return 0;
314}
315
316void cleanup1(void)
317{
318	/* Remove the loadable module - DUMMY_MOD */
319	if (system("rmmod " DUMMY_MOD) != 0) {
320		tst_brkm(TBROK, cleanup, "Failed to unload module %s",
321			 DUMMY_MOD);
322	}
323}
324
325void cleanup2(void)
326{
327	/* Remove the loadable module - DUMMY_MOD_DEP */
328	if (system("rmmod " DUMMY_MOD_DEP) != 0) {
329		tst_brkm(TBROK, cleanup, "Failed to unload module %s",
330			 DUMMY_MOD_DEP);
331	}
332	/* Remove the loadable module - DUMMY_MOD */
333	cleanup1();
334}
335
336/*
337 * setup()
338 *	performs all ONE TIME setup for this test
339 */
340void setup(void)
341{
342
343	tst_sig(FORK, DEF_HANDLER, cleanup);
344
345	tst_require_root();
346
347	if (tst_kvercmp(2, 5, 48) >= 0)
348		tst_brkm(TCONF, NULL, "This test will not work on "
349			 "kernels after 2.5.48");
350
351	/* Initialize longmodname to LONGMODNAMECHAR character */
352	memset(longmodname, LONGMODNAMECHAR, MODNAMEMAX - 1);
353
354	/* Pause if that option was specified
355	 * TEST_PAUSE contains the code to fork the test with the -c option.
356	 */
357	TEST_PAUSE;
358}
359
360/*
361 * cleanup()
362 *	performs all ONE TIME cleanup for this test at
363 *	completion or premature exit
364 */
365void cleanup(void)
366{
367	/*
368	 * print timing stats if that option was specified.
369	 * print errno log if that option was specified.
370	 */
371
372	tst_rmdir();
373}
374