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