plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 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 Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | /* |
| 20 | * Description: |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 21 | * 1) create a directory tstdir1, create a file under it. |
| 22 | * call rmdir(tstdir1), verify the return value is -1 |
| 23 | * and the errno is ENOTEMPTY. |
| 24 | * 2) create a directory with long path, call rmdir(tstdir1), |
| 25 | * verify the return value is -1 and the errno is ENAMETOOLONG. |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 26 | * 3) pass a pathname containing non-exist directory component |
| 27 | * to rmdir(), verify the return value is -1 and the errno |
| 28 | * is ENOENT. |
| 29 | * 4) pass a pathname containing a file component to rmdir(), |
| 30 | * verify the return value is -1 and the errno is ENOTDIR. |
| 31 | * 5) attempt to pass an invalid pathname with an address |
| 32 | * pointing outside the address space of the process, as the |
| 33 | * argument to rmdir(), verify the return value is -1 and |
| 34 | * the errno is EFAULT. |
| 35 | * 6) attempt to pass an invalid pathname with NULL, as the |
| 36 | * argument to rmdir(), verify the return value is -1 and |
| 37 | * the errno is EFAULT. |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 38 | * 7) pass a pathname with too many symbolic links to rmdir(), |
| 39 | * verify the return value is -1 and the errno is ELOOP. |
| 40 | * 8) pass a pathname which refers to a directory on a read-only |
| 41 | * file system to rmdir(), verify the return value is -1 and |
| 42 | * the errno is EROFS. |
| 43 | * 9) pass a pathname which is currently used as a mount point |
| 44 | * to rmdir(), verify the return value is -1 and the errno is |
| 45 | * EBUSY. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | #include <errno.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <sys/types.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 51 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <fcntl.h> |
| 53 | #include <unistd.h> |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 54 | #include <pwd.h> |
| 55 | #include <sys/mount.h> |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 56 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 57 | #include "test.h" |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 58 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 60 | #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO) |
| 61 | #define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 63 | #define TESTDIR "testdir" |
| 64 | #define TESTDIR2 "nosuchdir/testdir2" |
| 65 | #define TESTDIR3 "testfile2/testdir3" |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 66 | #define TESTDIR4 "/loopdir" |
| 67 | #define MNTPOINT "mntpoint" |
| 68 | #define TESTDIR5 "mntpoint/testdir5" |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 69 | #define TESTFILE "testdir/testfile" |
| 70 | #define TESTFILE2 "testfile2" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 72 | static char longpathname[PATH_MAX + 2]; |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 73 | static char looppathname[sizeof(TESTDIR4) * 43] = "."; |
| 74 | |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 75 | static const char *device; |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 76 | static int mount_flag; |
| 77 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 78 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | char *dir; |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 80 | int exp_errno; |
| 81 | } test_cases[] = { |
| 82 | {TESTDIR, ENOTEMPTY}, |
| 83 | {longpathname, ENAMETOOLONG}, |
| 84 | {TESTDIR2, ENOENT}, |
| 85 | {TESTDIR3, ENOTDIR}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 86 | #if !defined(UCLINUX) |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 87 | {(char *)-1, EFAULT}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 88 | #endif |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 89 | {NULL, EFAULT}, |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 90 | {looppathname, ELOOP}, |
| 91 | {TESTDIR5, EROFS}, |
| 92 | {MNTPOINT, EBUSY}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 95 | static void setup(void); |
| 96 | static void rmdir_verify(struct test_case_t *tc); |
| 97 | static void cleanup(void); |
| 98 | |
| 99 | char *TCID = "rmdir02"; |
| 100 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | { |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 104 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 105 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 107 | msg = parse_opts(ac, av, NULL, NULL); |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 108 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 109 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 114 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 116 | for (i = 0; i < TST_TOTAL; i++) |
| 117 | rmdir_verify(&test_cases[i]); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 121 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 124 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | { |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 126 | int i; |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 127 | const char *fs_type; |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 128 | |
| 129 | tst_require_root(NULL); |
| 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | TEST_PAUSE; |
| 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_tmpdir(); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 137 | fs_type = tst_dev_fs_type(); |
| 138 | device = tst_acquire_device(cleanup); |
| 139 | |
| 140 | if (!device) |
| 141 | tst_brkm(TCONF, cleanup, "Failed to acquire device"); |
| 142 | |
| 143 | tst_mkfs(cleanup, device, fs_type, NULL); |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 144 | SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE); |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 145 | if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) { |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 146 | tst_brkm(TBROK | TERRNO, cleanup, |
| 147 | "mount device:%s failed", device); |
| 148 | } |
| 149 | SAFE_MKDIR(cleanup, TESTDIR5, DIR_MODE); |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 150 | if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY, |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 151 | NULL) == -1) { |
| 152 | tst_brkm(TBROK | TERRNO, cleanup, |
| 153 | "mount device:%s failed", device); |
| 154 | } |
| 155 | mount_flag = 1; |
| 156 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 157 | SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE); |
| 158 | SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL); |
| 159 | |
| 160 | memset(longpathname, 'a', PATH_MAX + 1); |
| 161 | |
| 162 | SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL); |
| 163 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 164 | #if !defined(UCLINUX) |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 165 | test_cases[4].dir = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 166 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 167 | #endif |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * NOTE: the ELOOP test is written based on that the |
| 171 | * consecutive symlinks limit in kernel is hardwired |
| 172 | * to 40. |
| 173 | */ |
| 174 | SAFE_MKDIR(cleanup, "loopdir", DIR_MODE); |
| 175 | SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir"); |
| 176 | for (i = 0; i < 43; i++) |
| 177 | strcat(looppathname, TESTDIR4); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | } |
| 179 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 180 | static void rmdir_verify(struct test_case_t *tc) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | { |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 182 | TEST(rmdir(tc->dir)); |
| 183 | |
| 184 | if (TEST_RETURN != -1) { |
| 185 | tst_resm(TFAIL, "rmdir() returned %ld, " |
| 186 | "expected -1, errno:%d", TEST_RETURN, |
| 187 | tc->exp_errno); |
| 188 | return; |
| 189 | } |
| 190 | |
gux.fnst@cn.fujitsu.com | 19ec775 | 2014-04-17 10:50:23 +0000 | [diff] [blame] | 191 | if (TEST_ERRNO == tc->exp_errno) { |
| 192 | tst_resm(TPASS | TTERRNO, "rmdir() failed as expected"); |
| 193 | } else { |
| 194 | tst_resm(TFAIL | TTERRNO, |
| 195 | "rmdir() failed unexpectedly; expected: %d - %s", |
| 196 | tc->exp_errno, strerror(tc->exp_errno)); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static void cleanup(void) |
| 201 | { |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 202 | if (mount_flag && tst_umount(MNTPOINT) == -1) |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 203 | tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT); |
| 204 | |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 205 | if (device) |
| 206 | tst_release_device(NULL, device); |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 207 | |
Cyril Hrubis | 7600563 | 2014-06-19 17:51:17 +0200 | [diff] [blame] | 208 | tst_rmdir(); |
gux.fnst@cn.fujitsu.com | cce0640 | 2014-04-17 10:50:24 +0000 | [diff] [blame] | 209 | } |