blob: 753b645ca78ee6b6fdcc53473022a08b6fb8f755 [file] [log] [blame]
plars5f30b5d2003-03-12 20:40:32 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars5f30b5d2003-03-12 20:40:32 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
plars5f30b5d2003-03-12 20:40:32 +000019 * TEST IDENTIFIER : query_module01
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
plars5f30b5d2003-03-12 20:40:32 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars5f30b5d2003-03-12 20:40:32 +000023 * TEST TITLE : Checking functionality of query_module(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
plars5f30b5d2003-03-12 20:40:32 +000025 * TEST CASE TOTAL : 6
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
plars5f30b5d2003-03-12 20:40:32 +000027 * AUTHOR : Madhu T L <madhu.tarikere@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
plars5f30b5d2003-03-12 20:40:32 +000029 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
plars5f30b5d2003-03-12 20:40:32 +000048 * Setup:
49 * Setup signal handling.
50 * Test caller is superuser
51 * Initialize long module name
52 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
plars5f30b5d2003-03-12 20:40:32 +000054 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
plars5f30b5d2003-03-12 20:40:32 +000062 * Cleanup:
63 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
plars5f30b5d2003-03-12 20:40:32 +000065 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000076 *
plars5f30b5d2003-03-12 20:40:32 +000077 * RESTRICTIONS
subrata_modak4bb656a2009-02-26 12:02:09 +000078 * -c option has no effect for this testcase, even if used allows only
plars5f30b5d2003-03-12 20:40:32 +000079 * one instance to run at a time.
subrata_modak4bb656a2009-02-26 12:02:09 +000080 *
plars5f30b5d2003-03-12 20:40:32 +000081 * CHANGES
82 *
83 * 12/03/02 Added "force" to insmod to ignore kernel version.
84 * -Robbie Williamson <robbiew@us.ibm.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
plars5f30b5d2003-03-12 20:40:32 +000086 ****************************************************************/
87
88#include <errno.h>
89#include <pwd.h>
90#include <sys/types.h>
subrata_modak024988b2008-09-24 12:55:33 +000091#include <unistd.h>
92#include <limits.h>
plars5f30b5d2003-03-12 20:40:32 +000093#include <asm/atomic.h>
94#include <linux/module.h>
95#include "test.h"
plars5f30b5d2003-03-12 20:40:32 +000096
subrata_modak4bb656a2009-02-26 12:02:09 +000097#ifndef PAGE_SIZE
subrata_modak024988b2008-09-24 12:55:33 +000098#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
99#endif
100
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101#define LONGMODNAMECHAR 'm' /* Arbitrarily selected */
plars5f30b5d2003-03-12 20:40:32 +0000102#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
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111struct 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 */
plars5f30b5d2003-03-12 20:40:32 +0000117};
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
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133static struct test_case_t tdat[] = {
134 {NULL, 0, "module name: NULL, which: 0", NULL, NULL},
plars5f30b5d2003-03-12 20:40:32 +0000135
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 {NULL, QM_MODULES, "NULL module name, which: QM_MODULES",
137 setup1, cleanup1},
plars5f30b5d2003-03-12 20:40:32 +0000138
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 {DUMMY_MOD_DEP, QM_DEPS, "valid module name, which: QM_DEPS",
140 setup2, cleanup2},
plars5f30b5d2003-03-12 20:40:32 +0000141
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 {DUMMY_MOD, QM_REFS, "valid module name, which: QM_REFS",
143 setup2, cleanup2},
plars5f30b5d2003-03-12 20:40:32 +0000144
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 {DUMMY_MOD, QM_INFO, "valid module name, which: QM_INFO",
146 setup1, cleanup1},
plars5f30b5d2003-03-12 20:40:32 +0000147
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 {DUMMY_MOD, QM_SYMBOLS, "valid module name, which: QM_SYMBOLS",
149 setup1, cleanup1},
plars5f30b5d2003-03-12 20:40:32 +0000150};
151
152int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
153
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154int main(int argc, char **argv)
plars5f30b5d2003-03-12 20:40:32 +0000155{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200156 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200157 const char *msg;
plars5f30b5d2003-03-12 20:40:32 +0000158 size_t buflen = sizeof(out_buf);
159
Cyril Hrubiscf0d6262014-09-23 14:03:31 +0200160 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800161 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars5f30b5d2003-03-12 20:40:32 +0000162 }
163
plars5f30b5d2003-03-12 20:40:32 +0000164 tst_tmpdir();
165 setup();
166
plars5f30b5d2003-03-12 20:40:32 +0000167 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800168 /* reset tst_count in case we are looping */
169 tst_count = 0;
plars5f30b5d2003-03-12 20:40:32 +0000170
171 for (testno = 0; testno < TST_TOTAL; ++testno) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800172 if ((tdat[testno].setup) && (tdat[testno].setup())) {
plars5f30b5d2003-03-12 20:40:32 +0000173 /* setup() failed, skip this test */
174 continue;
175 }
176
177 TEST(query_module(tdat[testno].modname,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 tdat[testno].which, (void *)out_buf,
179 buflen, &ret));
plars5f30b5d2003-03-12 20:40:32 +0000180
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800181 if ((TEST_RETURN == EXP_RET_VAL) &&
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 !test_functionality(tdat[testno].which,
183 out_buf, buflen, ret)) {
plars5f30b5d2003-03-12 20:40:32 +0000184 tst_resm(TPASS, "query_module() successful "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 "for %s", tdat[testno].desc);
plars5f30b5d2003-03-12 20:40:32 +0000186 } else {
187 tst_resm(TFAIL, "query_module() failed for "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 "%s ; returned"
189 " %d (expected %d), errno %d (expected"
190 " 0)", tdat[testno].desc,
191 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
plars5f30b5d2003-03-12 20:40:32 +0000192 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800193 if (tdat[testno].cleanup) {
plars5f30b5d2003-03-12 20:40:32 +0000194 tdat[testno].cleanup();
195 }
196 }
197 }
198 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800199 tst_exit();
plars5f30b5d2003-03-12 20:40:32 +0000200}
201
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202int test_functionality(int which, char *buf, size_t bufsize, size_t ret)
plars5f30b5d2003-03-12 20:40:32 +0000203{
204 int i = 0;
205 char *modname;
206 unsigned long *vals;
207
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 switch (which) {
209 case 0:
210 /* Always return SUCCESS */
211 return 0;
212
213 case QM_MODULES:
214 case QM_DEPS:
215 /* Return SUCCESS if found DUMMY_MOD entry */
216 modname = DUMMY_MOD;
217 break;
218
219 case QM_REFS:
220 /* Return SUCCESS if found DUMMY_MOD_DEP entry */
221 modname = DUMMY_MOD_DEP;
222 break;
223
224 case QM_INFO:
225 /*
226 * Since module is already loaded, flags should show
227 * MOD_RUNNING
228 */
229 if (((struct module_info *)buf)->flags & MOD_RUNNING) {
plars5f30b5d2003-03-12 20:40:32 +0000230 return 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 }
232 return 1;
plars5f30b5d2003-03-12 20:40:32 +0000233
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234 case QM_SYMBOLS:
235 vals = (unsigned long *)buf;
plars5f30b5d2003-03-12 20:40:32 +0000236
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 /*
238 * Find entry for atleast one symbol, checking for
239 * EXP_FUNC_NAME symbol, if found return SUCCESS.
240 */
241 for (i = 0; i < ret; i++, vals += 2) {
plars5f30b5d2003-03-12 20:40:32 +0000242
Wanlong Gao354ebb42012-12-07 10:10:04 +0800243 /* buf + vals[1] - address of symbol name */
244 if (!strcmp(buf + vals[1], EXP_FUNC_NAME)) {
plars5f30b5d2003-03-12 20:40:32 +0000245 return 0;
246 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 }
248 return 1;
plars5f30b5d2003-03-12 20:40:32 +0000249
Wanlong Gao354ebb42012-12-07 10:10:04 +0800250 default:
251 /* Unknown which type */
252 return 1;
plars5f30b5d2003-03-12 20:40:32 +0000253 }
254
255 /* Return SUCCESS if found entry */
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800256 for (i = 0; i != ret; i++) {
257 if (strcmp(buf, modname)) {
plars5f30b5d2003-03-12 20:40:32 +0000258 buf += strlen(buf) + 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800259 } else {
plars5f30b5d2003-03-12 20:40:32 +0000260 return 0;
261 }
262 }
263 return 1;
264
265}
266
267/* Insert a module of name mod */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268int insert_mod(char *mod)
plars5f30b5d2003-03-12 20:40:32 +0000269{
270 char cmd[80];
subrata_modakbdbaec52009-02-26 12:14:51 +0000271
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800272 if (sprintf(cmd, "cp `which %s.o` ./", mod) == -1) {
plars5f30b5d2003-03-12 20:40:32 +0000273 tst_resm(TBROK, "sprintf failed");
274 return 1;
275 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800276 if (system(cmd) != 0) {
plars5f30b5d2003-03-12 20:40:32 +0000277 tst_resm(TBROK, "Failed to copy %s module", mod);
278 return 1;
279 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000280
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 /* Should use force to ignore kernel version & insure loading */
282 /* -RW */
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800283 /* if (sprintf(cmd, "insmod %s.o", mod) == -1) { */
284 if (sprintf(cmd, "insmod --force -q %s.o >/dev/null 2>&1", mod) == -1) {
plars5f30b5d2003-03-12 20:40:32 +0000285 tst_resm(TBROK, "sprintf failed");
286 return 1;
287 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800288 if (system(cmd) != 0) {
plars5f30b5d2003-03-12 20:40:32 +0000289 tst_resm(TBROK, "Failed to load %s module", mod);
290 return 1;
291 }
292 return 0;
293}
294
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295int setup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000296{
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800297 if (insert_mod(DUMMY_MOD)) {
plars5f30b5d2003-03-12 20:40:32 +0000298 /* Failed */
299 return 1;
300 } else {
301 return 0;
302 }
303}
304
Wanlong Gao354ebb42012-12-07 10:10:04 +0800305int setup2(void)
plars5f30b5d2003-03-12 20:40:32 +0000306{
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800307 if (insert_mod(DUMMY_MOD)) {
plars5f30b5d2003-03-12 20:40:32 +0000308 /* Failed */
309 return 1;
310 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800311 if (insert_mod(DUMMY_MOD_DEP)) {
plars5f30b5d2003-03-12 20:40:32 +0000312 /* Falied to load DUMMY_MOD_DEP, unload DUMMY_MOD */
313 cleanup1();
314 return 1;
315 }
316 return 0;
317}
318
Wanlong Gao354ebb42012-12-07 10:10:04 +0800319void cleanup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000320{
321 /* Remove the loadable module - DUMMY_MOD */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800322 if (system("rmmod " DUMMY_MOD) != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000323 tst_brkm(TBROK, cleanup, "Failed to unload module %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800324 DUMMY_MOD);
plars5f30b5d2003-03-12 20:40:32 +0000325 }
326}
327
Wanlong Gao354ebb42012-12-07 10:10:04 +0800328void cleanup2(void)
plars5f30b5d2003-03-12 20:40:32 +0000329{
330 /* Remove the loadable module - DUMMY_MOD_DEP */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800331 if (system("rmmod " DUMMY_MOD_DEP) != 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000332 tst_brkm(TBROK, cleanup, "Failed to unload module %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800333 DUMMY_MOD_DEP);
plars5f30b5d2003-03-12 20:40:32 +0000334 }
335 /* Remove the loadable module - DUMMY_MOD */
336 cleanup1();
337}
338
plars5f30b5d2003-03-12 20:40:32 +0000339/*
340 * setup()
341 * performs all ONE TIME setup for this test
342 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800343void setup(void)
plars5f30b5d2003-03-12 20:40:32 +0000344{
Garrett Cooper2c282152010-12-16 00:55:50 -0800345
plars5f30b5d2003-03-12 20:40:32 +0000346 tst_sig(FORK, DEF_HANDLER, cleanup);
347
Garrett Cooper53740502010-12-16 00:04:01 -0800348 tst_require_root(NULL);
plars5f30b5d2003-03-12 20:40:32 +0000349
Wanlong Gao354ebb42012-12-07 10:10:04 +0800350 if (tst_kvercmp(2, 5, 48) >= 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800351 tst_brkm(TCONF, NULL, "This test will not work on "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800352 "kernels after 2.5.48");
plars5f30b5d2003-03-12 20:40:32 +0000353
354 /* Initialize longmodname to LONGMODNAMECHAR character */
355 memset(longmodname, LONGMODNAMECHAR, MODNAMEMAX - 1);
356
357 /* Pause if that option was specified
358 * TEST_PAUSE contains the code to fork the test with the -c option.
359 */
360 TEST_PAUSE;
361}
362
363/*
364 * cleanup()
365 * performs all ONE TIME cleanup for this test at
366 * completion or premature exit
367 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800368void cleanup(void)
plars5f30b5d2003-03-12 20:40:32 +0000369{
370 /*
371 * print timing stats if that option was specified.
372 * print errno log if that option was specified.
373 */
374
plars5f30b5d2003-03-12 20:40:32 +0000375 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700376}