plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
Cyril Hrubis | 2affa2b | 2014-06-02 15:31:19 +0200 | [diff] [blame] | 3 | * AUTHOR : Bill Branum |
| 4 | * CO-PILOT : Steve Shaw |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Further, this software is distributed without any warranty that it is |
| 15 | * free of the rightful claim of any third person regarding infringement |
| 16 | * or the like. Any license provided herein, whether implied or |
| 17 | * otherwise, applies only to this software file. Patent licenses, if |
| 18 | * any, provided herein do not apply to combinations of this program with |
| 19 | * other software, or any other product whatsoever. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 22 | * with this program; if not, write the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * |
| 25 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 26 | * Mountain View, CA 94043, or: |
| 27 | * |
| 28 | * http://www.sgi.com |
| 29 | * |
| 30 | * For further information regarding this notice, see: |
| 31 | * |
| 32 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 33 | * |
| 34 | */ |
Cyril Hrubis | 2affa2b | 2014-06-02 15:31:19 +0200 | [diff] [blame] | 35 | /* |
| 36 | * TEST CASES |
| 37 | * rmdir(2) test for errno(s) EINVAL, EMLINK, EFAULT |
| 38 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
| 41 | #include <signal.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 44 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | 2affa2b | 2014-06-02 15:31:19 +0200 | [diff] [blame] | 49 | static void setup(void); |
| 50 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 52 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | extern char *get_high_address(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | int TST_TOTAL = 6; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 55 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | int TST_TOTAL = 4; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 57 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 59 | char *TCID = "rmdir05"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
Cyril Hrubis | 2affa2b | 2014-06-02 15:31:19 +0200 | [diff] [blame] | 61 | static struct stat stat_buf; |
| 62 | static char dir_name[256]; |
| 63 | |
| 64 | static char *bad_addr = NULL; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Cyril Hrubis | 2affa2b | 2014-06-02 15:31:19 +0200 | [diff] [blame] | 71 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 77 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | /* |
| 80 | * TEST CASE: 1 |
| 81 | * path points to the current directory |
| 82 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | TEST(rmdir(".")); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 88 | if (TEST_RETURN == -1) { |
| 89 | if (TEST_ERRNO & (EBUSY | ENOTEMPTY)) { |
| 90 | /* For functionality tests, verify that the |
| 91 | * directory wasn't removed. |
| 92 | */ |
| 93 | if (stat(".", &stat_buf) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | tst_resm(TFAIL, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 95 | "rmdir(\".\") removed the current working directory when it should have failed."); |
| 96 | } else { |
| 97 | tst_resm(TPASS, |
| 98 | "rmdir(\".\") failed to remove the current working directory. Returned %d : %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | TEST_ERRNO, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 100 | strerror(TEST_ERRNO)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | } |
| 102 | } else { |
| 103 | tst_resm(TFAIL, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 104 | "rmdir(\".\") failed with errno %d : %s but expected %d (EBUSY)", |
| 105 | TEST_ERRNO, |
| 106 | strerror(TEST_ERRNO), EBUSY); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 108 | } else { |
| 109 | tst_resm(TFAIL, |
| 110 | "rmdir(\".\") succeeded unexpectedly."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | } |
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 | /* |
| 114 | * TEST CASE: 2 |
| 115 | * path points to the "." (dot) entry of a directory |
| 116 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_resm(TCONF, "rmdir on \"dir/.\" supported on Linux"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 118 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | tst_resm(TCONF, |
| 120 | "linked directories test not implemented on Linux"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 121 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | /* |
| 123 | * TEST CASE: 4 |
| 124 | * path argument points below the minimum allocated address space |
| 125 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 126 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | TEST(rmdir(bad_addr)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 132 | if (TEST_RETURN == -1) { |
| 133 | if (TEST_ERRNO == EFAULT) { |
| 134 | tst_resm(TPASS, |
| 135 | "rmdir() - path argument points below the minimum allocated address space failed as expected with errno %d : %s", |
| 136 | TEST_ERRNO, |
| 137 | strerror(TEST_ERRNO)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | } else { |
| 139 | tst_resm(TFAIL, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | "rmdir() - path argument points below the minimum allocated address space failed with errno %d : %s but expected %d (EFAULT)", |
| 141 | TEST_ERRNO, |
| 142 | strerror(TEST_ERRNO), EFAULT); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 144 | } else { |
| 145 | tst_resm(TFAIL, |
| 146 | "rmdir() - path argument points below the minimum allocated address space succeeded unexpectedly."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 148 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | /* |
| 150 | * TEST CASE: 5 |
| 151 | * path argument points above the maximum allocated address space |
| 152 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | TEST(rmdir(get_high_address())); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 159 | if (TEST_RETURN == -1) { |
| 160 | if (TEST_ERRNO == EFAULT) { |
| 161 | tst_resm(TPASS, |
| 162 | "rmdir() - path argument points above the maximum allocated address space failed as expected with errno %d : %s", |
| 163 | TEST_ERRNO, |
| 164 | strerror(TEST_ERRNO)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | } else { |
| 166 | tst_resm(TFAIL, |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 167 | "rmdir() - path argument points above the maximum allocated address space failed with errno %d : %s but expected %d (EFAULT)", |
| 168 | TEST_ERRNO, |
| 169 | strerror(TEST_ERRNO), EFAULT); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 171 | } else { |
| 172 | tst_resm(TFAIL, |
| 173 | "rmdir() - path argument points above the maximum allocated address space succeeded unexpectedly."); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 175 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | /* |
| 178 | * TEST CASE: 6 |
| 179 | * able to remove a directory |
| 180 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | if (mkdir(dir_name, 0777) != 0) { |
| 183 | tst_brkm(TBROK, cleanup, |
| 184 | "mkdir(\"%s\") failed with errno %d : %s", |
| 185 | dir_name, errno, strerror(errno)); |
| 186 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | TEST(rmdir(dir_name)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | tst_resm(TFAIL, |
| 192 | "rmdir(\"%s\") failed when it should have passed. Returned %d : %s", |
| 193 | dir_name, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 194 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 195 | /* Verify the directory was removed. */ |
| 196 | if (stat(dir_name, &stat_buf) != 0) { |
| 197 | tst_resm(TPASS, |
| 198 | "rmdir(\"%s\") removed the directory as expected.", |
| 199 | dir_name); |
| 200 | } else { |
| 201 | tst_resm(TFAIL, |
| 202 | "rmdir(\"%s\") returned a zero exit status but failed to remove the directory.", |
| 203 | dir_name); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 206 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 207 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 209 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 210 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 211 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 213 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | /* Create a directory. */ |
| 222 | if (mkdir("dir1", 0777) == -1) { |
| 223 | tst_brkm(TBROK, cleanup, "mkdir() failed to create dir1."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | /* Create a unique directory name. */ |
| 227 | sprintf(dir_name, "./dir_%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 229 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | bad_addr = mmap(0, 1, PROT_NONE, |
| 231 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| 232 | if (bad_addr == MAP_FAILED) { |
| 233 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 234 | } |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 235 | #endif |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 236 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 238 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 241 | } |