plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 3 | * Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * 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 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * DESCRIPTION |
| 22 | * Testcase to check creat(2) sets the following errnos correctly: |
| 23 | * 1. EISDIR |
| 24 | * 2. ENAMETOOLONG |
| 25 | * 3. ENOENT |
| 26 | * 4. ENOTDIR |
| 27 | * 5. EFAULT |
| 28 | * 6. EACCES |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 29 | * 7. ELOOP |
| 30 | * 8. EROFS |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 32 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 46 | * 7. Attempt to creat(2) a file which links the other file that |
| 47 | * links the former and test for ELOOP |
| 48 | * 8. Attempt to creat(2) a file in a Read-only file system |
| 49 | * and test for EROFS |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | #include <stdio.h> |
| 53 | #include <errno.h> |
| 54 | #include <string.h> |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 55 | #include <pwd.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 56 | #include <sys/mman.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 58 | #include <sys/stat.h> |
| 59 | #include <fcntl.h> |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 60 | #include <sys/mount.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | #include "test.h" |
| 62 | #include "usctest.h" |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 63 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 65 | #define TEST_FILE "test_dir" |
| 66 | #define NO_DIR "testfile/testdir" |
| 67 | #define NOT_DIR "file1/testdir" |
| 68 | #define TEST6_FILE "dir6/file6" |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 69 | #define TEST7_FILE "file7" |
| 70 | #define TEST8_FILE "mntpoint/tmp" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 72 | #define MODE1 0444 |
| 73 | #define MODE2 0666 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 75 | static void setup(void); |
| 76 | static void cleanup(void); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 77 | static void test6_setup(void); |
| 78 | static void test6_cleanup(void); |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 79 | #if !defined(UCLINUX) |
| 80 | static void bad_addr_setup(int); |
| 81 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 83 | static char long_name[PATH_MAX+2]; |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 84 | static const char *device; |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 85 | static int mount_flag; |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 86 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 87 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | char *fname; |
| 89 | int mode; |
| 90 | int error; |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 91 | void (*setup)(); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 92 | void (*cleanup)(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | } TC[] = { |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 94 | {TEST_FILE, MODE1, EISDIR, NULL, NULL}, |
| 95 | {long_name, MODE1, ENAMETOOLONG, NULL, NULL}, |
| 96 | {NO_DIR, MODE1, ENOENT, NULL, NULL}, |
| 97 | {NOT_DIR, MODE1, ENOTDIR, NULL, NULL}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 98 | #if !defined(UCLINUX) |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 99 | {NULL, MODE1, EFAULT, bad_addr_setup, NULL}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 100 | #endif |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 101 | {TEST6_FILE, MODE1, EACCES, test6_setup, test6_cleanup}, |
| 102 | {TEST7_FILE, MODE1, ELOOP, NULL, NULL}, |
| 103 | {TEST8_FILE, MODE1, EROFS, NULL, NULL}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 106 | char *TCID = "creat06"; |
| 107 | int TST_TOTAL = ARRAY_SIZE(TC); |
| 108 | static int exp_enos[] = { EISDIR, ENAMETOOLONG, ENOENT, ENOTDIR, |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 109 | EFAULT, EACCES, ELOOP, EROFS, 0 }; |
| 110 | static struct passwd *ltpuser; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 114 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 115 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | int i; |
| 117 | |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 118 | msg = parse_opts(ac, av, NULL, NULL); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 119 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 120 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 122 | setup(); |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | TEST_EXP_ENOS(exp_enos); |
| 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 127 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 128 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | for (i = 0; i < TST_TOTAL; i++) { |
| 131 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 132 | if (TC[i].setup != NULL) |
| 133 | TC[i].setup(i); |
| 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | TEST(creat(TC[i].fname, TC[i].mode)); |
| 136 | |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 137 | if (TC[i].cleanup != NULL) |
| 138 | TC[i].cleanup(); |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | if (TEST_RETURN != -1) { |
| 141 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 142 | continue; |
| 143 | } |
| 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | if (TEST_ERRNO == TC[i].error) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | tst_resm(TPASS | TTERRNO, |
| 147 | "got expected failure"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | tst_resm(TFAIL | TTERRNO, "wanted errno %d", |
| 150 | TC[i].error); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | tst_exit(); |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 159 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 161 | const char *fs_type; |
| 162 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 163 | tst_require_root(NULL); |
| 164 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 165 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | TEST_PAUSE; |
| 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | tst_tmpdir(); |
| 172 | |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 173 | fs_type = tst_dev_fs_type(); |
| 174 | device = tst_acquire_device(cleanup); |
| 175 | |
| 176 | if (!device) |
| 177 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 178 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 179 | SAFE_MKDIR(cleanup, TEST_FILE, MODE2); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 181 | memset(long_name, 'a', PATH_MAX+1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 183 | SAFE_TOUCH(cleanup, "file1", MODE1, NULL); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 184 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 185 | SAFE_MKDIR(cleanup, "dir6", MODE2); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 186 | |
| 187 | SAFE_SYMLINK(cleanup, TEST7_FILE, "test_file_eloop2"); |
| 188 | SAFE_SYMLINK(cleanup, "test_file_eloop2", TEST7_FILE); |
| 189 | |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 190 | tst_mkfs(cleanup, device, fs_type, NULL); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 191 | SAFE_MKDIR(cleanup, "mntpoint", 0777); |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 192 | if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) { |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 193 | tst_brkm(TBROK | TERRNO, cleanup, |
| 194 | "mount device:%s failed", device); |
| 195 | } |
| 196 | mount_flag = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 199 | #if !defined(UCLINUX) |
| 200 | static void bad_addr_setup(int i) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 202 | TC[i].fname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 203 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 204 | } |
| 205 | #endif |
mridge | 12952b0 | 2004-08-25 15:17:15 +0000 | [diff] [blame] | 206 | |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 207 | static void test6_setup(void) |
| 208 | { |
| 209 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
| 210 | } |
| 211 | |
| 212 | static void test6_cleanup(void) |
| 213 | { |
| 214 | SAFE_SETEUID(cleanup, 0); |
| 215 | } |
| 216 | |
zenglg.jy | aa23056 | 2013-12-03 15:58:05 +0800 | [diff] [blame] | 217 | static void cleanup(void) |
| 218 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | TEST_CLEANUP; |
| 220 | |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 221 | if (mount_flag && umount("mntpoint") < 0) { |
| 222 | tst_brkm(TBROK | TERRNO, NULL, |
| 223 | "umount device:%s failed", device); |
| 224 | } |
| 225 | |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 226 | if (device) |
| 227 | tst_release_device(NULL, device); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 228 | |
Cyril Hrubis | 4e74c99 | 2014-06-19 16:53:51 +0200 | [diff] [blame^] | 229 | tst_rmdir(); |
Zeng Linggang | 6f0b85f | 2014-01-20 17:16:58 +0800 | [diff] [blame] | 230 | } |