blob: 62378840e6064845147750ee3a42e1125b37427a [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_module03
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 error conditions for query_module(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
plars5f30b5d2003-03-12 20:40:32 +000025 * TEST CASE TOTAL : 4
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) returns -1 and sets errno to EFAULT for module name
36 * argument outside program's accessible address space.
37 * 2. query_module(2) returns -1 and sets errno to EFAULT for return size
38 * argument outside program's accessible address space.
39 * 3. query_module(2) returns -1 and sets errno to EFAULT for output buffer
40 * argument outside program's accessible address space.
41 * 4. query_module(2) returns -1 and sets errno to ENOSPC for too small
42 * buffer size.
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars5f30b5d2003-03-12 20:40:32 +000044 * Setup:
45 * Setup signal handling.
46 * Test caller is superuser
47 * Set expected errnos for logging
48 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
plars5f30b5d2003-03-12 20:40:32 +000050 * Test:
51 * Loop if the proper options are given.
52 * Execute system call
53 * Check return code and error number, if matching,
54 * Issue PASS message
55 * Otherwise,
56 * Issue FAIL message
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
plars5f30b5d2003-03-12 20:40:32 +000058 * Cleanup:
59 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000060 *
plars5f30b5d2003-03-12 20:40:32 +000061 * USAGE: <for command-line>
62 * query_module03 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
63 * where, -c n : Run n copies concurrently.
64 * -e : Turn on errno logging.
65 * -f : Turn off functional testing
66 * -h : Show help screen
67 * -i n : Execute test n times.
68 * -I x : Execute test for x seconds.
69 * -p : Pause for SIGUSR1 before starting
70 * -P x : Pause for x seconds between iterations.
71 * -t : Turn on syscall timing.
subrata_modak4bb656a2009-02-26 12:02:09 +000072 *
plars5f30b5d2003-03-12 20:40:32 +000073 * RESTRICTIONS
subrata_modak4bb656a2009-02-26 12:02:09 +000074 * -c option has no effect for this testcase, even if used allows only
plars5f30b5d2003-03-12 20:40:32 +000075 * one instance to run at a time.
76 *
77 * CHANGES
78 *
79 * 12/03/02 Added "force" to insmod to ignore kernel version.
80 * -Robbie Williamson <robbiew@us.ibm.com>
81 *
82 ****************************************************************/
83
84#include <unistd.h>
85#include <errno.h>
86#include <pwd.h>
87#include <sys/types.h>
subrata_modak024988b2008-09-24 12:55:33 +000088#include <unistd.h>
89#include <limits.h>
plars5f30b5d2003-03-12 20:40:32 +000090#include <asm/atomic.h>
91#include <linux/module.h>
92#include <sys/mman.h>
93#include "test.h"
plars5f30b5d2003-03-12 20:40:32 +000094
subrata_modak4bb656a2009-02-26 12:02:09 +000095#ifndef PAGE_SIZE
subrata_modak024988b2008-09-24 12:55:33 +000096#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
97#endif
98
plars5f30b5d2003-03-12 20:40:32 +000099#define EXP_RET_VAL -1
100#define DUMMY_MOD "dummy_query_mod"
101#define SMALLBUFSIZE 1
102
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103struct test_case_t { /* test case structure */
104 char *modname;
105 int which;
106 void *buf;
107 size_t bufsize;
108 size_t *ret_size;
109 int experrno; /* expected errno */
110 char *desc;
111 int (*setup) (void);
112 void (*cleanup) (void);
plars5f30b5d2003-03-12 20:40:32 +0000113};
114
115char *TCID = "query_module03";
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116
plars5f30b5d2003-03-12 20:40:32 +0000117static int testno;
118static char out_buf[PAGE_SIZE];
119static size_t ret_size;
120
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121char *bad_addr = 0;
plars5f30b5d2003-03-12 20:40:32 +0000122
123static void setup(void);
124static void cleanup(void);
125static int setup1(void);
126static void cleanup1(void);
127
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128static struct test_case_t tdat[] = {
plars5f30b5d2003-03-12 20:40:32 +0000129
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 {(char *)-1, QM_MODULES, (void *)out_buf, sizeof(out_buf), &ret_size,
131 EFAULT, "results for module name argument outside program's "
132 "accessible address space", NULL, NULL}
133 ,
plars5f30b5d2003-03-12 20:40:32 +0000134
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 {NULL, QM_MODULES, (void *)out_buf, sizeof(out_buf), (size_t *) - 1,
136 EFAULT, "results for return size argument outside program's "
137 "accessible address space", NULL, NULL}
138 ,
plars5f30b5d2003-03-12 20:40:32 +0000139
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 {NULL, QM_MODULES, (void *)-1, sizeof(out_buf), &ret_size, EFAULT,
141 "results for output buffer argument outside program's "
142 "accessible address space", setup1, cleanup1}
143 ,
plars5f30b5d2003-03-12 20:40:32 +0000144
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 {NULL, QM_MODULES, (void *)out_buf, SMALLBUFSIZE, &ret_size, ENOSPC,
146 "results for too small buffer size", setup1, cleanup1},
plars5f30b5d2003-03-12 20:40:32 +0000147};
148
149int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
150
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151int main(int argc, char **argv)
plars5f30b5d2003-03-12 20:40:32 +0000152{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200153 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200154 const char *msg;
plars5f30b5d2003-03-12 20:40:32 +0000155
Cyril Hrubiscf0d6262014-09-23 14:03:31 +0200156 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800157 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars5f30b5d2003-03-12 20:40:32 +0000158 }
159
plars5f30b5d2003-03-12 20:40:32 +0000160 tst_tmpdir();
161 setup();
162
plars5f30b5d2003-03-12 20:40:32 +0000163 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800164 /* reset tst_count in case we are looping */
165 tst_count = 0;
plars5f30b5d2003-03-12 20:40:32 +0000166
167 for (testno = 0; testno < TST_TOTAL; ++testno) {
168
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800169 if ((tdat[testno].setup) && (tdat[testno].setup())) {
plars5f30b5d2003-03-12 20:40:32 +0000170 /* setup() failed, skip this test */
171 continue;
172 }
173 TEST(query_module(tdat[testno].modname,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tdat[testno].which, tdat[testno].buf,
175 tdat[testno].bufsize,
176 tdat[testno].ret_size));
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800177 if ((TEST_RETURN == EXP_RET_VAL) &&
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 (TEST_ERRNO == tdat[testno].experrno)) {
plars5f30b5d2003-03-12 20:40:32 +0000179 tst_resm(TPASS, "Expected %s, errno: %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tdat[testno].desc, TEST_ERRNO);
plars5f30b5d2003-03-12 20:40:32 +0000181 } else {
182 tst_resm(TFAIL, "Unexpected %s ; returned"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 " %d (expected %d), errno %d (expected"
184 " %d)", tdat[testno].desc,
185 TEST_RETURN, EXP_RET_VAL,
186 TEST_ERRNO, tdat[testno].experrno);
plars5f30b5d2003-03-12 20:40:32 +0000187 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800188 if (tdat[testno].cleanup) {
plars5f30b5d2003-03-12 20:40:32 +0000189 tdat[testno].cleanup();
190 }
191 }
192 }
193 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800194 tst_exit();
plars5f30b5d2003-03-12 20:40:32 +0000195}
196
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197int setup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000198{
199 char cmd[80];
200
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 if (sprintf(cmd, "cp `which %s.o` ./", DUMMY_MOD) == -1) {
202 tst_resm(TBROK, "sprintf failed");
203 return 1;
204 }
205 if (system(cmd) != 0) {
206 tst_resm(TBROK, "Failed to copy %s module", DUMMY_MOD);
207 return 1;
208 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000209
plars5f30b5d2003-03-12 20:40:32 +0000210 /* Should use force to ignore kernel version & insure loading */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 /* -RW */
212 /* if (sprintf(cmd, "insmod %s.o", DUMMY_MOD) == -1) { */
213 if (sprintf(cmd, "insmod --force -q %s.o >/dev/null 2>&1", DUMMY_MOD) ==
214 -1) {
plars5f30b5d2003-03-12 20:40:32 +0000215 tst_resm(TBROK, "sprintf failed");
216 return 1;
217 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800218 if (system(cmd) != 0) {
plars5f30b5d2003-03-12 20:40:32 +0000219 tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD);
220 return 1;
221 }
222 return 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000223}
plars5f30b5d2003-03-12 20:40:32 +0000224
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225void cleanup1(void)
plars5f30b5d2003-03-12 20:40:32 +0000226{
227 /* Remove the loadable module - DUMMY_MOD */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800228 if (system("rmmod " DUMMY_MOD) != 0) {
plars5f30b5d2003-03-12 20:40:32 +0000229 tst_brkm(TBROK, cleanup, "Failed to unload module %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800230 DUMMY_MOD);
plars5f30b5d2003-03-12 20:40:32 +0000231 }
232}
233
plars5f30b5d2003-03-12 20:40:32 +0000234/*
235 * setup()
236 * performs all ONE TIME setup for this test
237 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800238void setup(void)
plars5f30b5d2003-03-12 20:40:32 +0000239{
Garrett Cooper2c282152010-12-16 00:55:50 -0800240
plars5f30b5d2003-03-12 20:40:32 +0000241 tst_sig(FORK, DEF_HANDLER, cleanup);
242
Garrett Cooper53740502010-12-16 00:04:01 -0800243 tst_require_root(NULL);
plars5f30b5d2003-03-12 20:40:32 +0000244
Wanlong Gao354ebb42012-12-07 10:10:04 +0800245 if (tst_kvercmp(2, 5, 48) >= 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800246 tst_brkm(TCONF, NULL, "This test will not work on "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 "kernels after 2.5.48");
plars5f30b5d2003-03-12 20:40:32 +0000248
plars5f30b5d2003-03-12 20:40:32 +0000249 /* Pause if that option was specified
250 * TEST_PAUSE contains the code to fork the test with the -c option.
251 */
252 TEST_PAUSE;
253
Wanlong Gao354ebb42012-12-07 10:10:04 +0800254 bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
255 if (bad_addr == MAP_FAILED) {
256 tst_brkm(TBROK, cleanup, "mmap failed");
257 }
plars5f30b5d2003-03-12 20:40:32 +0000258 tdat[0].modname = bad_addr;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800259 tdat[2].buf = (void *)bad_addr;
plars5f30b5d2003-03-12 20:40:32 +0000260
261}
262
263/*
264 * cleanup()
265 * performs all ONE TIME cleanup for this test at
266 * completion or premature exit
267 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268void cleanup(void)
plars5f30b5d2003-03-12 20:40:32 +0000269{
270 /*
271 * print timing stats if that option was specified.
272 * print errno log if that option was specified.
273 */
plars5f30b5d2003-03-12 20:40:32 +0000274 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700275}