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 | * NAME |
| 22 | * creat06.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check creat(2) sets the following errnos correctly: |
| 26 | * 1. EISDIR |
| 27 | * 2. ENAMETOOLONG |
| 28 | * 3. ENOENT |
| 29 | * 4. ENOTDIR |
| 30 | * 5. EFAULT |
| 31 | * 6. EACCES |
| 32 | * |
| 33 | * ALGORITHM |
| 34 | * 1. Attempt to creat(2) an existing directory, and test for |
| 35 | * EISDIR |
| 36 | * 2. Attempt to creat(2) a file whose name is more than |
| 37 | * VFS_MAXNAMLEN and test for ENAMETOOLONG. |
| 38 | * 3. Attempt to creat(2) a file inside a directory which doesn't |
| 39 | * exist, and test for ENOENT |
| 40 | * 4. Attempt to creat(2) a file, the pathname of which comprises |
| 41 | * a component which is a file, test for ENOTDIR. |
| 42 | * 5. Attempt to creat(2) a file with a bad address |
| 43 | * and test for EFAULT |
| 44 | * 6. Attempt to creat(2) a file in a directory with no |
| 45 | * execute permission and test for EACCES |
| 46 | * |
| 47 | * USAGE: <for command-line> |
| 48 | * creat06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 49 | * where, -c n : Run n copies concurrently. |
| 50 | * -e : Turn on errno logging. |
| 51 | * -i n : Execute test n times. |
| 52 | * -I x : Execute test for x seconds. |
| 53 | * -P x : Pause for x seconds between iterations. |
| 54 | * -t : Turn on syscall timing. |
| 55 | * |
| 56 | * HISTORY |
| 57 | * 07/2001 Ported by Wayne Boyer |
| 58 | * |
| 59 | * RESTRICTIONS |
| 60 | * None |
| 61 | */ |
| 62 | |
| 63 | #include <stdio.h> |
| 64 | #include <errno.h> |
| 65 | #include <string.h> |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 66 | #include <pwd.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 67 | #include <sys/mman.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 68 | #include <sys/types.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 69 | #include <sys/stat.h> |
| 70 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | #include "test.h" |
| 72 | #include "usctest.h" |
| 73 | |
| 74 | void setup(void); |
| 75 | void cleanup(void); |
| 76 | |
| 77 | char user1name[] = "nobody"; |
| 78 | |
| 79 | char *TCID = "creat06"; |
mridge | 12952b0 | 2004-08-25 15:17:15 +0000 | [diff] [blame] | 80 | int fileHandle = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | extern int Tst_count; |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int exp_enos[] = { EISDIR, ENAMETOOLONG, ENOENT, ENOTDIR, EFAULT, EACCES, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | #define MODE1 0444 |
| 86 | #define MODE2 0666 |
robbiew | 066dbd4 | 2004-10-28 18:53:39 +0000 | [diff] [blame] | 87 | #define NSIZE 1000 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | char long_name[] = |
| 90 | "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
| 92 | char good_dir[NSIZE]; |
| 93 | char no_dir[] = "testfile/testdir"; |
| 94 | char not_dir[] = "file1/testdir"; |
| 95 | char test6_file[] = "dir6/file6"; |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 96 | char nobody_uid[] = "nobody"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 98 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | struct test_case_t { |
| 100 | char *fname; |
| 101 | int mode; |
| 102 | int error; |
| 103 | } TC[] = { |
| 104 | /* The file name is an existing directory */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | { |
| 106 | good_dir, MODE1, EISDIR}, |
| 107 | /* The file name is too long - ENAMETOOLONG */ |
| 108 | { |
| 109 | long_name, MODE1, ENAMETOOLONG}, |
| 110 | /* Attept to create a file in a directory that doesn't exist - ENOENT */ |
| 111 | { |
| 112 | no_dir, MODE1, ENOENT}, |
| 113 | /* a compent of the file's path is not a directory - ENOTDIR */ |
| 114 | { |
| 115 | not_dir, MODE1, ENOTDIR}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 116 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | /* The file address is bad - EFAULT */ |
| 118 | { |
| 119 | (char *)-1, MODE1, EFAULT}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 120 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | /* The directory lacks execute permission - EACCES */ |
| 122 | { |
| 123 | test6_file, MODE1, EACCES} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 126 | int TST_TOTAL = (sizeof(TC) / sizeof(*TC)); |
| 127 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | int lc; /* loop counter */ |
| 133 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | int i; |
| 135 | |
| 136 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame^] | 137 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 139 | } |
| 140 | |
| 141 | setup(); |
| 142 | |
| 143 | /* set up the expected errnos */ |
| 144 | TEST_EXP_ENOS(exp_enos); |
| 145 | |
| 146 | /* check looping state if -i option given */ |
| 147 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 148 | |
| 149 | /* reset Tst_count in case we are looping */ |
| 150 | Tst_count = 0; |
| 151 | |
| 152 | /* loop through the test cases */ |
| 153 | for (i = 0; i < TST_TOTAL; i++) { |
| 154 | |
| 155 | TEST(creat(TC[i].fname, TC[i].mode)); |
| 156 | |
| 157 | if (TEST_RETURN != -1) { |
| 158 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | TEST_ERROR_LOG(TEST_ERRNO); |
| 163 | |
| 164 | if (TEST_ERRNO == TC[i].error) { |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 165 | tst_resm(TPASS|TTERRNO, "expected failure"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } else { |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 167 | tst_resm(TFAIL|TTERRNO, "wanted errno %d", TC[i].error); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | cleanup(); |
| 172 | |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | /*NOTREACHED*/} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * setup() - performs all ONE TIME setup for this test. |
| 179 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | { |
| 182 | char *cur_dir = NULL; |
| 183 | |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 184 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | if (geteuid() != 0) { |
| 186 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 187 | } |
| 188 | ltpuser = getpwnam(nobody_uid); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 189 | if (seteuid(ltpuser->pw_uid) == -1) |
| 190 | tst_resm(TINFO|TERRNO, "seteuid(%d) failed", ltpuser->pw_uid); |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | /* capture signals */ |
| 193 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 194 | |
| 195 | /* Pause if that option was specified */ |
| 196 | TEST_PAUSE; |
| 197 | |
| 198 | /* make a temporary directory and cd to it */ |
| 199 | tst_tmpdir(); |
| 200 | |
| 201 | /* get the current directory for the first test */ |
| 202 | if ((cur_dir = getcwd(cur_dir, 0)) == NULL) { |
| 203 | tst_brkm(TBROK, cleanup, "getcwd failed"); |
| 204 | } |
| 205 | |
| 206 | if (strncpy(good_dir, cur_dir, NSIZE) == NULL) { |
| 207 | tst_brkm(TBROK, cleanup, "couldn't copy currect directory"); |
| 208 | } |
| 209 | |
| 210 | /* create a file that will be used in test #3 */ |
mridge | 12952b0 | 2004-08-25 15:17:15 +0000 | [diff] [blame] | 211 | if ((fileHandle = creat("file1", MODE1)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | tst_brkm(TBROK, cleanup, "couldn't create a test file"); |
| 213 | } |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 214 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 215 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 217 | if (bad_addr == MAP_FAILED) { |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 218 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 219 | } |
| 220 | TC[4].fname = bad_addr; |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 221 | #endif |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 222 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | /* create a directory that will be used in test #6 */ |
| 224 | if (mkdir("dir6", MODE2) == -1) { |
| 225 | tst_brkm(TBROK, cleanup, "couldn't creat a test directory"); |
| 226 | } |
| 227 | } |
| 228 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | /* |
| 230 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 231 | * completion or premature exit. |
| 232 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | { |
| 235 | /* |
| 236 | * print timing stats if that option was specified. |
| 237 | * print errno log if that option was specified. |
| 238 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | close(fileHandle); |
mridge | 12952b0 | 2004-08-25 15:17:15 +0000 | [diff] [blame] | 240 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | TEST_CLEANUP; |
| 242 | |
| 243 | /* delete the test directory created in setup() */ |
| 244 | tst_rmdir(); |
| 245 | |
| 246 | /* exit with return code appropriate for results */ |
| 247 | tst_exit(); |
| 248 | } |