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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | #define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO |
| 90 | |
| 91 | int setup1(); /* setup function to test mknod for EEXIST */ |
| 92 | int setup3(); /* setup function to test mknod for ENOTDIR */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int longpath_setup(); /* setup function to test mknod for ENAMETOOLONG */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | int no_setup(); /* simply returns 0 to the caller */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | char Longpathname[PATH_MAX + 2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | char High_address_node[64]; |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | char *pathname; |
| 100 | char *desc; |
| 101 | int exp_errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | { |
| 105 | "tnode_1", "Specified node already exists", EEXIST, setup1}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 106 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | { |
| 108 | (char *)-1, "Negative address", EFAULT, no_setup}, { |
| 109 | High_address_node, "Address beyond address space", EFAULT, |
| 110 | no_setup}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 111 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | { |
| 113 | "testdir_2/tnode_2", "Non-existent file", ENOENT, no_setup}, { |
| 114 | "", "Pathname is empty", ENOENT, no_setup}, { |
| 115 | Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, { |
| 116 | "tnode/tnode_3", "Path contains regular file", ENOTDIR, setup3}, { |
| 117 | NULL, NULL, 0, no_setup} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 120 | char *TCID = "mknod06"; |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 121 | int TST_TOTAL = ARRAY_SIZE(Test_cases); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 122 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | extern char *get_high_address(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | #else |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 125 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 128 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | void setup(); /* setup function for the tests */ |
| 130 | void cleanup(); /* cleanup function for the tests */ |
| 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 134 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 135 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | char *node_name; /* ptr. for node name created */ |
| 137 | char *test_desc; /* test specific error message */ |
| 138 | int ind; /* counter to test different test conditions */ |
| 139 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 140 | msg = parse_opts(ac, av, NULL, NULL); |
| 141 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Invoke setup function to call individual test setup functions |
| 148 | * for the test which run as root/super-user. |
| 149 | */ |
| 150 | setup(); |
| 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 153 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 154 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
| 156 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 157 | node_name = Test_cases[ind].pathname; |
| 158 | test_desc = Test_cases[ind].desc; |
| 159 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 160 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | if (node_name == High_address_node) { |
| 162 | node_name = get_high_address(); |
| 163 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 164 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Call mknod(2) to test different test conditions. |
| 168 | * verify that it fails with -1 return value and |
| 169 | * sets appropriate errno. |
| 170 | */ |
| 171 | TEST(mknod(node_name, MODE_RWX, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | /* Check return code from mknod(2) */ |
| 174 | if (TEST_RETURN != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_resm(TFAIL, |
| 176 | "mknod() returned %ld, expected " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | "-1, errno:%d", TEST_RETURN, |
| 178 | Test_cases[ind].exp_errno); |
| 179 | continue; |
| 180 | } |
| 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | if (TEST_ERRNO == Test_cases[ind].exp_errno) { |
| 183 | tst_resm(TPASS, "mknod() fails, %s, errno:%d", |
| 184 | test_desc, TEST_ERRNO); |
| 185 | } else { |
| 186 | tst_resm(TFAIL, "mknod() fails, %s, errno:%d, " |
| 187 | "expected errno:%d", test_desc, |
| 188 | TEST_ERRNO, Test_cases[ind].exp_errno); |
| 189 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 190 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 192 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Invoke cleanup() to delete the test directories created |
| 196 | * in the setup(). |
| 197 | */ |
| 198 | cleanup(); |
| 199 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 200 | tst_exit(); |
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 | * setup(void) - performs all ONE TIME setup for this test. |
| 205 | * Exit the test program on receipt of unexpected signals. |
| 206 | * Create a temporary directory used to hold test directories and nodes |
| 207 | * created and change the directory to it. |
| 208 | * Invoke individual test setup functions according to the order |
| 209 | * set in struct. definition. |
| 210 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 211 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 213 | int ind; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 215 | tst_require_root(NULL); |
| 216 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | /* Capture unexpected signals */ |
| 218 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 219 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | TEST_PAUSE; |
| 221 | |
| 222 | /* Make a temp dir and cd to it */ |
| 223 | tst_tmpdir(); |
| 224 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 225 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 226 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 228 | if (bad_addr == MAP_FAILED) { |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 229 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 230 | } |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 231 | Test_cases[2].pathname = bad_addr; |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 232 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | /* call individual setup functions */ |
| 234 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 235 | Test_cases[ind].setupfunc(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * no_setup() - Some test conditions for mknod(2) do not any setup. |
| 241 | * Hence, this function just returns 0. |
| 242 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 243 | int no_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 244 | { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * longpath_setup() - setup to create a node with a name length exceeding |
| 250 | * the MAX. length of PATH_MAX. |
| 251 | * This function retruns 0. |
| 252 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 253 | int longpath_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | { |
| 255 | int ind; /* counter variable */ |
| 256 | |
| 257 | for (ind = 0; ind <= (PATH_MAX + 1); ind++) { |
| 258 | Longpathname[ind] = 'a'; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 262 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 263 | /* |
| 264 | * setup1() - setup function for a test condition for which mknod(2) |
| 265 | * returns -1 and sets errno to EEXIST. |
| 266 | * This function creates a node using mknod(2) and tries to create |
| 267 | * same node in the test and fails with above errno. |
| 268 | * This function returns 0. |
| 269 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 270 | int setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | { |
| 272 | /* Create a node using mknod */ |
| 273 | if (mknod("tnode_1", MODE_RWX, 0) < 0) { |
| 274 | tst_brkm(TBROK, cleanup, "Fails to create node in setup1()"); |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * setup3() - setup function for a test condition for which mknod(2) |
| 282 | * returns -1 and sets errno to ENOTDIR. |
| 283 | * This function creates a node under temporary directory and the |
| 284 | * test attempts to create another node under under this node and fails |
| 285 | * with ENOTDIR as the node created here is a regular file. |
| 286 | * This function returns 0. |
| 287 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 288 | int setup3(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 289 | { |
| 290 | /* Create a node using mknod */ |
| 291 | if (mknod("tnode", MODE_RWX, 0) < 0) { |
| 292 | tst_brkm(TBROK, cleanup, "Fails to create node in setup3()"); |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * cleanup() - Performs all ONE TIME cleanup for this test at |
| 300 | * completion or premature exit. |
| 301 | * Print test timing stats and errno log if test executed with options. |
| 302 | * Remove temporary directory and sub-directories/files under it |
| 303 | * created during setup(). |
| 304 | * Exit the test program with normal exit code. |
| 305 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 306 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 307 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 308 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 309 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 310 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 311 | } |