blob: af6dab325955fa546cbf8563669285b0b80e7354 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubis2affa2b2014-06-02 15:31:19 +02003 * AUTHOR : Bill Branum
4 * CO-PILOT : Steve Shaw
plars865695b2001-08-27 22:15:12 +00005 *
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 Gaofed96412012-10-24 10:10:29 +080022 * with this program; if not, write the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000024 *
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 Hrubis2affa2b2014-06-02 15:31:19 +020035 /*
36 * TEST CASES
37 * rmdir(2) test for errno(s) EINVAL, EMLINK, EFAULT
38 */
plars865695b2001-08-27 22:15:12 +000039
40#include <errno.h>
41#include <signal.h>
42#include <sys/types.h>
43#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000044#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000045#include <stdlib.h>
46#include <string.h>
47#include "test.h"
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubis2affa2b2014-06-02 15:31:19 +020049static void setup(void);
50static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000051
robbiewd34d5812005-07-11 22:28:09 +000052#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +000053extern char *get_high_address();
subrata_modak56207ce2009-03-23 13:35:39 +000054int TST_TOTAL = 6;
robbiewd34d5812005-07-11 22:28:09 +000055#else
subrata_modak56207ce2009-03-23 13:35:39 +000056int TST_TOTAL = 4;
robbiewd34d5812005-07-11 22:28:09 +000057#endif
plars865695b2001-08-27 22:15:12 +000058
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020059char *TCID = "rmdir05";
plars865695b2001-08-27 22:15:12 +000060
Cyril Hrubis2affa2b2014-06-02 15:31:19 +020061static struct stat stat_buf;
62static char dir_name[256];
63
64static char *bad_addr = NULL;
plars865695b2001-08-27 22:15:12 +000065
subrata_modak56207ce2009-03-23 13:35:39 +000066int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000067{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
plars865695b2001-08-27 22:15:12 +000070
Cyril Hrubis2affa2b2014-06-02 15:31:19 +020071 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080073
subrata_modak56207ce2009-03-23 13:35:39 +000074 setup();
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079 /*
80 * TEST CASE: 1
81 * path points to the current directory
82 */
subrata_modak56207ce2009-03-23 13:35:39 +000083 TEST(rmdir("."));
subrata_modakbdbaec52009-02-26 12:14:51 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +000086 }
plars865695b2001-08-27 22:15:12 +000087
Cyril Hrubise38b9612014-06-02 17:20:57 +020088 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_modak56207ce2009-03-23 13:35:39 +000094 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +020095 "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_modak56207ce2009-03-23 13:35:39 +000099 TEST_ERRNO,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200100 strerror(TEST_ERRNO));
subrata_modak56207ce2009-03-23 13:35:39 +0000101 }
102 } else {
103 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200104 "rmdir(\".\") failed with errno %d : %s but expected %d (EBUSY)",
105 TEST_ERRNO,
106 strerror(TEST_ERRNO), EBUSY);
subrata_modak56207ce2009-03-23 13:35:39 +0000107 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200108 } else {
109 tst_resm(TFAIL,
110 "rmdir(\".\") succeeded unexpectedly.");
subrata_modak56207ce2009-03-23 13:35:39 +0000111 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 /*
114 * TEST CASE: 2
115 * path points to the "." (dot) entry of a directory
116 */
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TCONF, "rmdir on \"dir/.\" supported on Linux");
subrata_modakbdbaec52009-02-26 12:14:51 +0000118
subrata_modak56207ce2009-03-23 13:35:39 +0000119 tst_resm(TCONF,
120 "linked directories test not implemented on Linux");
subrata_modakbdbaec52009-02-26 12:14:51 +0000121
subrata_modak56207ce2009-03-23 13:35:39 +0000122 /*
123 * TEST CASE: 4
124 * path argument points below the minimum allocated address space
125 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000126#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000127 TEST(rmdir(bad_addr));
subrata_modakbdbaec52009-02-26 12:14:51 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000130 }
plars865695b2001-08-27 22:15:12 +0000131
Cyril Hrubise38b9612014-06-02 17:20:57 +0200132 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_modak56207ce2009-03-23 13:35:39 +0000138 } else {
139 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200140 "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_modak56207ce2009-03-23 13:35:39 +0000143 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200144 } else {
145 tst_resm(TFAIL,
146 "rmdir() - path argument points below the minimum allocated address space succeeded unexpectedly.");
subrata_modak56207ce2009-03-23 13:35:39 +0000147 }
robbiewd34d5812005-07-11 22:28:09 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 /*
150 * TEST CASE: 5
151 * path argument points above the maximum allocated address space
152 */
plars865695b2001-08-27 22:15:12 +0000153
subrata_modak56207ce2009-03-23 13:35:39 +0000154 TEST(rmdir(get_high_address()));
plars865695b2001-08-27 22:15:12 +0000155
subrata_modak56207ce2009-03-23 13:35:39 +0000156 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000157 }
plars865695b2001-08-27 22:15:12 +0000158
Cyril Hrubise38b9612014-06-02 17:20:57 +0200159 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_modak56207ce2009-03-23 13:35:39 +0000165 } else {
166 tst_resm(TFAIL,
Cyril Hrubise38b9612014-06-02 17:20:57 +0200167 "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_modak56207ce2009-03-23 13:35:39 +0000170 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200171 } else {
172 tst_resm(TFAIL,
173 "rmdir() - path argument points above the maximum allocated address space succeeded unexpectedly.");
subrata_modak56207ce2009-03-23 13:35:39 +0000174 }
robbiewd34d5812005-07-11 22:28:09 +0000175#endif
plars865695b2001-08-27 22:15:12 +0000176
subrata_modak56207ce2009-03-23 13:35:39 +0000177 /*
178 * TEST CASE: 6
179 * able to remove a directory
180 */
plars865695b2001-08-27 22:15:12 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 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 }
plars865695b2001-08-27 22:15:12 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 TEST(rmdir(dir_name));
subrata_modakbdbaec52009-02-26 12:14:51 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000191 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 Hrubise38b9612014-06-02 17:20:57 +0200195 /* 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_modak56207ce2009-03-23 13:35:39 +0000204 }
205 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000206
Garrett Cooper2c282152010-12-16 00:55:50 -0800207 }
plars865695b2001-08-27 22:15:12 +0000208
subrata_modak56207ce2009-03-23 13:35:39 +0000209 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800210 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800211}
plars865695b2001-08-27 22:15:12 +0000212
Mike Frysingerc57fba52014-04-09 18:56:30 -0400213void setup(void)
plars865695b2001-08-27 22:15:12 +0000214{
subrata_modak56207ce2009-03-23 13:35:39 +0000215 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000216
subrata_modak56207ce2009-03-23 13:35:39 +0000217 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000218
subrata_modak56207ce2009-03-23 13:35:39 +0000219 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000220
subrata_modak56207ce2009-03-23 13:35:39 +0000221 /* Create a directory. */
222 if (mkdir("dir1", 0777) == -1) {
223 tst_brkm(TBROK, cleanup, "mkdir() failed to create dir1.");
plars865695b2001-08-27 22:15:12 +0000224 }
plars865695b2001-08-27 22:15:12 +0000225
subrata_modak56207ce2009-03-23 13:35:39 +0000226 /* Create a unique directory name. */
227 sprintf(dir_name, "./dir_%d", getpid());
plars865695b2001-08-27 22:15:12 +0000228
vapier62b16cf2007-02-09 20:48:23 +0000229#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000230 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 }
vapier62b16cf2007-02-09 20:48:23 +0000235#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800236}
plars865695b2001-08-27 22:15:12 +0000237
Mike Frysingerc57fba52014-04-09 18:56:30 -0400238void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000239{
subrata_modak56207ce2009-03-23 13:35:39 +0000240 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700241}