plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: mknod06 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1) mknod(2) returns -1 and sets errno to EEXIST if specified path |
| 26 | * already exists. |
| 27 | * 2) mknod(2) returns -1 and sets errno to EFAULT if pathname points |
| 28 | * outside user's accessible address space. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * 3) mknod(2) returns -1 and sets errno to ENOENT if the directory |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * component in pathname does not exist. |
| 31 | * 4) mknod(2) returns -1 and sets errno to ENAMETOOLONG if the pathname |
| 32 | * component was too long. |
| 33 | * 5) mknod(2) returns -1 and sets errno to ENOTDIR if the directory |
| 34 | * component in pathname is not a directory. |
| 35 | * |
| 36 | * Expected Result: |
| 37 | * mknod() should fail with return value -1 and set expected errno. |
| 38 | * |
| 39 | * Algorithm: |
| 40 | * Setup: |
| 41 | * Setup signal handling. |
| 42 | * Create temporary directory. |
| 43 | * Pause for SIGUSR1 if option specified. |
| 44 | * |
| 45 | * Test: |
| 46 | * Loop if the proper options are given. |
| 47 | * Execute system call |
| 48 | * Check return code, if system call failed (return=-1) |
| 49 | * if errno set == expected errno |
| 50 | * Issue sys call fails with expected return value and errno. |
| 51 | * Otherwise, |
| 52 | * Issue sys call fails with unexpected errno. |
| 53 | * Otherwise, |
| 54 | * Issue sys call returns unexpected value. |
| 55 | * |
| 56 | * Cleanup: |
| 57 | * Print errno log and/or timing stats if options given |
| 58 | * Delete the temporary directory(s)/file(s) created. |
| 59 | * |
| 60 | * Usage: <for command-line> |
| 61 | * mknod06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 62 | * where, -c n : Run n copies concurrently. |
| 63 | * -e : Turn on errno logging. |
| 64 | * -i n : Execute test n times. |
| 65 | * -I x : Execute test for x seconds. |
| 66 | * -P x : Pause for x seconds between iterations. |
| 67 | * -t : Turn on syscall timing. |
| 68 | * |
| 69 | * HISTORY |
| 70 | * 07/2001 Ported by Wayne Boyer |
| 71 | * |
| 72 | * RESTRICTIONS: |
| 73 | * This test should be executed by super-user (root) only. |
| 74 | */ |
| 75 | |
| 76 | #include <stdio.h> |
| 77 | #include <stdlib.h> |
| 78 | #include <unistd.h> |
| 79 | #include <errno.h> |
| 80 | #include <string.h> |
| 81 | #include <signal.h> |
| 82 | #include <sys/types.h> |
| 83 | #include <sys/stat.h> |
| 84 | #include <sys/param.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 85 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | #include "test.h" |
| 88 | #include "usctest.h" |
| 89 | |
| 90 | #define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO |
| 91 | |
| 92 | int setup1(); /* setup function to test mknod for EEXIST */ |
| 93 | int setup3(); /* setup function to test mknod for ENOTDIR */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | int longpath_setup(); /* setup function to test mknod for ENAMETOOLONG */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | int no_setup(); /* simply returns 0 to the caller */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | char Longpathname[PATH_MAX + 2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | char High_address_node[64]; |
| 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | char *pathname; |
| 101 | char *desc; |
| 102 | int exp_errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | int (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | { |
| 106 | "tnode_1", "Specified node already exists", EEXIST, setup1}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 107 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | { |
| 109 | (char *)-1, "Negative address", EFAULT, no_setup}, { |
| 110 | High_address_node, "Address beyond address space", EFAULT, |
| 111 | no_setup}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 112 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | { |
| 114 | "testdir_2/tnode_2", "Non-existent file", ENOENT, no_setup}, { |
| 115 | "", "Pathname is empty", ENOENT, no_setup}, { |
| 116 | Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, { |
| 117 | "tnode/tnode_3", "Path contains regular file", ENOTDIR, setup3}, { |
| 118 | NULL, NULL, 0, no_setup} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | char *TCID = "mknod06"; /* Test program identifier. */ |
| 122 | int TST_TOTAL = (sizeof(Test_cases) / sizeof(*Test_cases)); |
| 123 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | extern char *get_high_address(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | int exp_enos[] = { EEXIST, EFAULT, ENOENT, ENAMETOOLONG, ENOTDIR, 0 }; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 127 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | int exp_enos[] = { EEXIST, ENOENT, ENAMETOOLONG, ENOTDIR, 0 }; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 129 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | void setup(); /* setup function for the tests */ |
| 134 | void cleanup(); /* cleanup function for the tests */ |
| 135 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | { |
| 138 | int lc; /* loop counter */ |
| 139 | char *msg; /* message returned from parse_opts */ |
| 140 | char *node_name; /* ptr. for node name created */ |
| 141 | char *test_desc; /* test specific error message */ |
| 142 | int ind; /* counter to test different test conditions */ |
| 143 | |
| 144 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 145 | msg = parse_opts(ac, av, NULL, NULL); |
| 146 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Invoke setup function to call individual test setup functions |
| 153 | * for the test which run as root/super-user. |
| 154 | */ |
| 155 | setup(); |
| 156 | |
| 157 | /* set the expected errnos... */ |
| 158 | TEST_EXP_ENOS(exp_enos); |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
| 164 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 165 | node_name = Test_cases[ind].pathname; |
| 166 | test_desc = Test_cases[ind].desc; |
| 167 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 168 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | if (node_name == High_address_node) { |
| 170 | node_name = get_high_address(); |
| 171 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 172 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Call mknod(2) to test different test conditions. |
| 176 | * verify that it fails with -1 return value and |
| 177 | * sets appropriate errno. |
| 178 | */ |
| 179 | TEST(mknod(node_name, MODE_RWX, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 180 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | /* Check return code from mknod(2) */ |
| 182 | if (TEST_RETURN != -1) { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 183 | tst_resm(TFAIL, "mknod() returned %ld, expected " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | "-1, errno:%d", TEST_RETURN, |
| 185 | Test_cases[ind].exp_errno); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | TEST_ERROR_LOG(TEST_ERRNO); |
| 190 | |
| 191 | if (TEST_ERRNO == Test_cases[ind].exp_errno) { |
| 192 | tst_resm(TPASS, "mknod() fails, %s, errno:%d", |
| 193 | test_desc, TEST_ERRNO); |
| 194 | } else { |
| 195 | tst_resm(TFAIL, "mknod() fails, %s, errno:%d, " |
| 196 | "expected errno:%d", test_desc, |
| 197 | TEST_ERRNO, Test_cases[ind].exp_errno); |
| 198 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 199 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 201 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Invoke cleanup() to delete the test directories created |
| 205 | * in the setup(). |
| 206 | */ |
| 207 | cleanup(); |
| 208 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame^] | 209 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 210 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * setup(void) - performs all ONE TIME setup for this test. |
| 214 | * Exit the test program on receipt of unexpected signals. |
| 215 | * Create a temporary directory used to hold test directories and nodes |
| 216 | * created and change the directory to it. |
| 217 | * Invoke individual test setup functions according to the order |
| 218 | * set in struct. definition. |
| 219 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 222 | int ind; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | |
| 224 | /* Capture unexpected signals */ |
| 225 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 226 | |
| 227 | /* Check that the test process id is super/root */ |
| 228 | if (geteuid() != 0) { |
| 229 | tst_brkm(TBROK, NULL, "Must be super/root for this test!"); |
| 230 | tst_exit(); |
| 231 | } |
| 232 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | TEST_PAUSE; |
| 234 | |
| 235 | /* Make a temp dir and cd to it */ |
| 236 | tst_tmpdir(); |
| 237 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 238 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 239 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 241 | if (bad_addr == MAP_FAILED) { |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 242 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 243 | } |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 244 | Test_cases[2].pathname = bad_addr; |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 245 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | /* call individual setup functions */ |
| 247 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 248 | Test_cases[ind].setupfunc(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * no_setup() - Some test conditions for mknod(2) do not any setup. |
| 254 | * Hence, this function just returns 0. |
| 255 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | int no_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 257 | { |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * longpath_setup() - setup to create a node with a name length exceeding |
| 263 | * the MAX. length of PATH_MAX. |
| 264 | * This function retruns 0. |
| 265 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | int longpath_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | { |
| 268 | int ind; /* counter variable */ |
| 269 | |
| 270 | for (ind = 0; ind <= (PATH_MAX + 1); ind++) { |
| 271 | Longpathname[ind] = 'a'; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 275 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 276 | /* |
| 277 | * setup1() - setup function for a test condition for which mknod(2) |
| 278 | * returns -1 and sets errno to EEXIST. |
| 279 | * This function creates a node using mknod(2) and tries to create |
| 280 | * same node in the test and fails with above errno. |
| 281 | * This function returns 0. |
| 282 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | int setup1() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | { |
| 285 | /* Create a node using mknod */ |
| 286 | if (mknod("tnode_1", MODE_RWX, 0) < 0) { |
| 287 | tst_brkm(TBROK, cleanup, "Fails to create node in setup1()"); |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * setup3() - setup function for a test condition for which mknod(2) |
| 295 | * returns -1 and sets errno to ENOTDIR. |
| 296 | * This function creates a node under temporary directory and the |
| 297 | * test attempts to create another node under under this node and fails |
| 298 | * with ENOTDIR as the node created here is a regular file. |
| 299 | * This function returns 0. |
| 300 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 301 | int setup3() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 302 | { |
| 303 | /* Create a node using mknod */ |
| 304 | if (mknod("tnode", MODE_RWX, 0) < 0) { |
| 305 | tst_brkm(TBROK, cleanup, "Fails to create node in setup3()"); |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * cleanup() - Performs all ONE TIME cleanup for this test at |
| 313 | * completion or premature exit. |
| 314 | * Print test timing stats and errno log if test executed with options. |
| 315 | * Remove temporary directory and sub-directories/files under it |
| 316 | * created during setup(). |
| 317 | * Exit the test program with normal exit code. |
| 318 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 319 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 320 | { |
| 321 | /* |
| 322 | * print timing stats if that option was specified. |
| 323 | * print errno log if that option was specified. |
| 324 | */ |
| 325 | TEST_CLEANUP; |
| 326 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 327 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 328 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 329 | } |