query_module03.c revision 53740500924f6439623a8ac256b5be2d6c59ed1f
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   : query_module03
20 *
21 *    EXECUTED BY       : root / superuser
22 *
23 *    TEST TITLE        : Checking error conditions for query_module(2)
24 *
25 *    TEST CASE TOTAL   : 4
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) 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.
43 *
44 *	Setup:
45 *	  Setup signal handling.
46 *	  Test caller is superuser
47 *	  Set expected errnos for logging
48 *	  Pause for SIGUSR1 if option specified.
49 *
50 *	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
57 *
58 *	Cleanup:
59 *	  Print errno log and/or timing stats if options given
60 *
61 * 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.
72 *
73 * RESTRICTIONS
74 *	-c option has no effect for this testcase, even if used allows only
75 *	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>
88#include <unistd.h>
89#include <limits.h>
90#include <asm/atomic.h>
91#include <linux/module.h>
92#include <sys/mman.h>
93#include "test.h"
94#include "usctest.h"
95
96#ifndef PAGE_SIZE
97#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
98#endif
99
100#define EXP_RET_VAL	-1
101#define DUMMY_MOD	"dummy_query_mod"
102#define SMALLBUFSIZE	1
103
104extern int Tst_count;
105
106struct test_case_t {			/* test case structure */
107	char 	*modname;
108	int	which;
109	void	*buf;
110	size_t	bufsize;
111	size_t	*ret_size;
112	int	experrno;		/* expected errno */
113	char	*desc;
114	int     (*setup)(void);
115	void    (*cleanup)(void);
116};
117
118char *TCID = "query_module03";
119static int exp_enos[]={ENOSPC, EFAULT, 0};
120static int testno;
121static char out_buf[PAGE_SIZE];
122static size_t ret_size;
123
124char * bad_addr = 0;
125
126static void setup(void);
127static void cleanup(void);
128static int setup1(void);
129static void cleanup1(void);
130
131static struct test_case_t  tdat[] = {
132
133	{ (char *)-1, QM_MODULES, (void *)out_buf, sizeof(out_buf), &ret_size,
134		EFAULT, "results for module name argument outside program's "
135		"accessible address space", NULL, NULL },
136
137	{ NULL, QM_MODULES, (void *)out_buf, sizeof(out_buf), (size_t *)-1,
138		EFAULT, "results for return size argument outside program's "
139		"accessible address space", NULL, NULL },
140
141	{ NULL, QM_MODULES, (void *)-1, sizeof(out_buf), &ret_size, EFAULT,
142		"results for output buffer argument outside program's "
143		"accessible address space", setup1, cleanup1 },
144
145	{ NULL, QM_MODULES, (void *)out_buf, SMALLBUFSIZE, &ret_size, ENOSPC,
146		"results for too small buffer size", setup1, cleanup1 },
147};
148
149int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
150
151int
152main(int argc, char **argv)
153{
154	int lc;				/* loop counter */
155	char *msg;			/* message returned from parse_opts */
156
157	/* parse standard options */
158	if ((msg = parse_opts(argc, argv, NULL, NULL)) !=
159	    (char *)NULL) {
160		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
161	}
162
163	if (STD_COPIES != 1) {
164		tst_resm(TINFO, "-c option has no effect for this testcase - "
165			"doesn't allow running more than one instance "
166			"at a time");
167		STD_COPIES = 1;
168	}
169
170	tst_tmpdir();
171	setup();
172
173	/* check looping state if -i option is given */
174	for (lc = 0; TEST_LOOPING(lc); lc++) {
175		/* reset Tst_count in case we are looping */
176		Tst_count = 0;
177
178		for (testno = 0; testno < TST_TOTAL; ++testno) {
179
180			if ((tdat[testno].setup) && (tdat[testno].setup())) {
181				/* setup() failed, skip this test */
182				continue;
183			}
184			TEST(query_module(tdat[testno].modname,
185				tdat[testno].which, tdat[testno].buf,
186				tdat[testno].bufsize, tdat[testno].ret_size));
187			TEST_ERROR_LOG(TEST_ERRNO);
188			if ((TEST_RETURN == EXP_RET_VAL) &&
189				(TEST_ERRNO == tdat[testno].experrno) ) {
190				tst_resm(TPASS, "Expected %s, errno: %d",
191					tdat[testno].desc, TEST_ERRNO);
192			} else {
193				tst_resm(TFAIL, "Unexpected %s ; returned"
194					" %d (expected %d), errno %d (expected"
195					" %d)", tdat[testno].desc,
196					TEST_RETURN, EXP_RET_VAL,
197					TEST_ERRNO, tdat[testno].experrno);
198			}
199			if (tdat[testno].cleanup) {
200				tdat[testno].cleanup();
201			}
202		}
203	}
204	cleanup();
205	tst_exit();
206}
207
208int
209setup1(void)
210{
211	char cmd[80];
212
213        if (sprintf(cmd, "cp `which %s.o` ./", DUMMY_MOD) == -1) {
214                tst_resm(TBROK, "sprintf failed");
215                return 1;
216        }
217        if (system(cmd) != 0) {
218                tst_resm(TBROK, "Failed to copy %s module", DUMMY_MOD);
219                return 1;
220        }
221
222	/* Should use force to ignore kernel version & insure loading  */
223        /* -RW                                                         */
224        /* if (sprintf(cmd, "insmod %s.o", DUMMY_MOD) == -1) {         */
225	if (sprintf(cmd, "insmod --force -q %s.o >/dev/null 2>&1", DUMMY_MOD) == -1) {
226		tst_resm(TBROK, "sprintf failed");
227		return 1;
228	}
229	if (system(cmd) != 0) {
230		tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD);
231		return 1;
232	}
233	return 0;
234}
235
236void
237cleanup1(void)
238{
239	/* Remove the loadable module - DUMMY_MOD */
240	if (system("rmmod "DUMMY_MOD) != 0) {
241		tst_brkm(TBROK, cleanup, "Failed to unload module %s",
242			DUMMY_MOD);
243	}
244}
245
246
247
248/*
249 * setup()
250 *	performs all ONE TIME setup for this test
251 */
252void
253setup(void)
254{
255	/* capture signals */
256	tst_sig(FORK, DEF_HANDLER, cleanup);
257
258	tst_require_root(NULL);
259
260	if (tst_kvercmp(2,5,48) >= 0)
261		tst_brkm(TCONF, NULL, "This test will not work on "
262				"kernels after 2.5.48");
263
264	/* set the expected errnos... */
265	TEST_EXP_ENOS(exp_enos);
266
267	/* Pause if that option was specified
268	 * TEST_PAUSE contains the code to fork the test with the -c option.
269	 */
270	TEST_PAUSE;
271
272        bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
273        if (bad_addr == MAP_FAILED) {
274                tst_brkm(TBROK, cleanup, "mmap failed");
275        }
276	tdat[0].modname = bad_addr;
277	tdat[2].buf = (void *) bad_addr;
278
279}
280
281/*
282 * cleanup()
283 *	performs all ONE TIME cleanup for this test at
284 *	completion or premature exit
285 */
286void
287cleanup(void)
288{
289	/*
290	 * print timing stats if that option was specified.
291	 * print errno log if that option was specified.
292	 */
293	TEST_CLEANUP;
294	tst_rmdir();
295}
296