plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 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 |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : query_module03 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 23 | * TEST TITLE : Checking error conditions for query_module(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 4 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 27 | * AUTHOR : Madhu T L <madhu.tarikere@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 43 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 44 | * Setup: |
| 45 | * Setup signal handling. |
| 46 | * Test caller is superuser |
| 47 | * Set expected errnos for logging |
| 48 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 49 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 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 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 57 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 58 | * Cleanup: |
| 59 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 60 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 72 | * |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 73 | * RESTRICTIONS |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 74 | * -c option has no effect for this testcase, even if used allows only |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 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> |
subrata_modak | 024988b | 2008-09-24 12:55:33 +0000 | [diff] [blame] | 88 | #include <unistd.h> |
| 89 | #include <limits.h> |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 90 | #include <asm/atomic.h> |
| 91 | #include <linux/module.h> |
| 92 | #include <sys/mman.h> |
| 93 | #include "test.h" |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 94 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 95 | #ifndef PAGE_SIZE |
subrata_modak | 024988b | 2008-09-24 12:55:33 +0000 | [diff] [blame] | 96 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) |
| 97 | #endif |
| 98 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 99 | #define EXP_RET_VAL -1 |
| 100 | #define DUMMY_MOD "dummy_query_mod" |
| 101 | #define SMALLBUFSIZE 1 |
| 102 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | struct 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); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | char *TCID = "query_module03"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 117 | static int testno; |
| 118 | static char out_buf[PAGE_SIZE]; |
| 119 | static size_t ret_size; |
| 120 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | char *bad_addr = 0; |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 122 | |
| 123 | static void setup(void); |
| 124 | static void cleanup(void); |
| 125 | static int setup1(void); |
| 126 | static void cleanup1(void); |
| 127 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | static struct test_case_t tdat[] = { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 129 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | {(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 | , |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 134 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | {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 | , |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 139 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | {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 | , |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 144 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | {NULL, QM_MODULES, (void *)out_buf, SMALLBUFSIZE, &ret_size, ENOSPC, |
| 146 | "results for too small buffer size", setup1, cleanup1}, |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
| 150 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | int main(int argc, char **argv) |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 152 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 153 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 154 | const char *msg; |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 155 | |
Cyril Hrubis | cf0d626 | 2014-09-23 14:03:31 +0200 | [diff] [blame] | 156 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 157 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 158 | } |
| 159 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 160 | tst_tmpdir(); |
| 161 | setup(); |
| 162 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 163 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 164 | /* reset tst_count in case we are looping */ |
| 165 | tst_count = 0; |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 166 | |
| 167 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 168 | |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 169 | if ((tdat[testno].setup) && (tdat[testno].setup())) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 170 | /* setup() failed, skip this test */ |
| 171 | continue; |
| 172 | } |
| 173 | TEST(query_module(tdat[testno].modname, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tdat[testno].which, tdat[testno].buf, |
| 175 | tdat[testno].bufsize, |
| 176 | tdat[testno].ret_size)); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 177 | if ((TEST_RETURN == EXP_RET_VAL) && |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | (TEST_ERRNO == tdat[testno].experrno)) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 179 | tst_resm(TPASS, "Expected %s, errno: %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tdat[testno].desc, TEST_ERRNO); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 181 | } else { |
| 182 | tst_resm(TFAIL, "Unexpected %s ; returned" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | " %d (expected %d), errno %d (expected" |
| 184 | " %d)", tdat[testno].desc, |
| 185 | TEST_RETURN, EXP_RET_VAL, |
| 186 | TEST_ERRNO, tdat[testno].experrno); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 187 | } |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 188 | if (tdat[testno].cleanup) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 189 | tdat[testno].cleanup(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 194 | tst_exit(); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | int setup1(void) |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 198 | { |
| 199 | char cmd[80]; |
| 200 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 209 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 210 | /* Should use force to ignore kernel version & insure loading */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | /* -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) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 215 | tst_resm(TBROK, "sprintf failed"); |
| 216 | return 1; |
| 217 | } |
Garrett Cooper | 8fb1cdb | 2010-11-28 22:56:35 -0800 | [diff] [blame] | 218 | if (system(cmd) != 0) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 219 | tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD); |
| 220 | return 1; |
| 221 | } |
| 222 | return 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 223 | } |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 224 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | void cleanup1(void) |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 226 | { |
| 227 | /* Remove the loadable module - DUMMY_MOD */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 228 | if (system("rmmod " DUMMY_MOD) != 0) { |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 229 | tst_brkm(TBROK, cleanup, "Failed to unload module %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 230 | DUMMY_MOD); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 234 | /* |
| 235 | * setup() |
| 236 | * performs all ONE TIME setup for this test |
| 237 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 238 | void setup(void) |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 239 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 240 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 241 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 242 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 243 | tst_require_root(NULL); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 244 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 245 | if (tst_kvercmp(2, 5, 48) >= 0) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 246 | tst_brkm(TCONF, NULL, "This test will not work on " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 247 | "kernels after 2.5.48"); |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 248 | |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 249 | /* 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 254 | 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 | } |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 258 | tdat[0].modname = bad_addr; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 259 | tdat[2].buf = (void *)bad_addr; |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 260 | |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * cleanup() |
| 265 | * performs all ONE TIME cleanup for this test at |
| 266 | * completion or premature exit |
| 267 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 268 | void cleanup(void) |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 269 | { |
| 270 | /* |
| 271 | * print timing stats if that option was specified. |
| 272 | * print errno log if that option was specified. |
| 273 | */ |
plars | 5f30b5d | 2003-03-12 20:40:32 +0000 | [diff] [blame] | 274 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 275 | } |