1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
32affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis *    AUTHOR		: Bill Branum
42affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis *    CO-PILOT		: Steve Shaw
5865695bbc89088b9526ea9045410e5afb70a985cplars *
6865695bbc89088b9526ea9045410e5afb70a985cplars * This program is free software; you can redistribute it and/or modify it
7865695bbc89088b9526ea9045410e5afb70a985cplars * under the terms of version 2 of the GNU General Public License as
8865695bbc89088b9526ea9045410e5afb70a985cplars * published by the Free Software Foundation.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars * This program is distributed in the hope that it would be useful, but
11865695bbc89088b9526ea9045410e5afb70a985cplars * WITHOUT ANY WARRANTY; without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13865695bbc89088b9526ea9045410e5afb70a985cplars *
14865695bbc89088b9526ea9045410e5afb70a985cplars * Further, this software is distributed without any warranty that it is
15865695bbc89088b9526ea9045410e5afb70a985cplars * free of the rightful claim of any third person regarding infringement
16865695bbc89088b9526ea9045410e5afb70a985cplars * or the like.  Any license provided herein, whether implied or
17865695bbc89088b9526ea9045410e5afb70a985cplars * otherwise, applies only to this software file.  Patent licenses, if
18865695bbc89088b9526ea9045410e5afb70a985cplars * any, provided herein do not apply to combinations of this program with
19865695bbc89088b9526ea9045410e5afb70a985cplars * other software, or any other product whatsoever.
20865695bbc89088b9526ea9045410e5afb70a985cplars *
21865695bbc89088b9526ea9045410e5afb70a985cplars * You should have received a copy of the GNU General Public License along
22fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc.,
23fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24865695bbc89088b9526ea9045410e5afb70a985cplars *
25865695bbc89088b9526ea9045410e5afb70a985cplars * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26865695bbc89088b9526ea9045410e5afb70a985cplars * Mountain View, CA  94043, or:
27865695bbc89088b9526ea9045410e5afb70a985cplars *
28865695bbc89088b9526ea9045410e5afb70a985cplars * http://www.sgi.com
29865695bbc89088b9526ea9045410e5afb70a985cplars *
30865695bbc89088b9526ea9045410e5afb70a985cplars * For further information regarding this notice, see:
31865695bbc89088b9526ea9045410e5afb70a985cplars *
32865695bbc89088b9526ea9045410e5afb70a985cplars * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33865695bbc89088b9526ea9045410e5afb70a985cplars *
34865695bbc89088b9526ea9045410e5afb70a985cplars */
352affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis /*
362affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis  * TEST CASES
372affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis  *  rmdir(2) test for errno(s) EINVAL, EMLINK, EFAULT
382affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis  */
39865695bbc89088b9526ea9045410e5afb70a985cplars
40865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
41865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
42865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h>
43865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h>
441ad845172048e19f8669f4f940296afe39c8f43eplars#include <sys/mman.h>
45865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdlib.h>
46865695bbc89088b9526ea9045410e5afb70a985cplars#include <string.h>
47865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h"
48865695bbc89088b9526ea9045410e5afb70a985cplars
492affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubisstatic void setup(void);
502affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubisstatic void cleanup(void);
51865695bbc89088b9526ea9045410e5afb70a985cplars
52d34d581c6a320e356a6cda923c7aa399479e812crobbiew#if !defined(UCLINUX)
53865695bbc89088b9526ea9045410e5afb70a985cplarsextern char *get_high_address();
5456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint TST_TOTAL = 6;
55d34d581c6a320e356a6cda923c7aa399479e812crobbiew#else
5656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint TST_TOTAL = 4;
57d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
58865695bbc89088b9526ea9045410e5afb70a985cplars
59fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "rmdir05";
60865695bbc89088b9526ea9045410e5afb70a985cplars
612affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubisstatic struct stat stat_buf;
622affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubisstatic char dir_name[256];
632affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubis
642affa2bfcaa088605c0256d59a9a8d23ea253cd1Cyril Hrubisstatic char *bad_addr = NULL;
65865695bbc89088b9526ea9045410e5afb70a985cplars
6656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int argc, char **argv)
67865695bbc89088b9526ea9045410e5afb70a985cplars{
6889af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
69865695bbc89088b9526ea9045410e5afb70a985cplars
70d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(argc, argv, NULL, NULL);
712c28215423293e443469a07ae7011135d058b671Garrett Cooper
7256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	setup();
73865695bbc89088b9526ea9045410e5afb70a985cplars
7456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	for (lc = 0; TEST_LOOPING(lc); lc++) {
75d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
76865695bbc89088b9526ea9045410e5afb70a985cplars
7756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/*
7856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * TEST CASE: 1
7956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * path points to the current directory
8056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 */
8156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		TEST(rmdir("."));
82bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
8356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (TEST_RETURN == -1) {
84e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			if (TEST_ERRNO & (EBUSY | ENOTEMPTY)) {
85e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				/* For functionality tests, verify that the
86e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				 * directory wasn't removed.
87e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				 */
88e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				if (stat(".", &stat_buf) == -1) {
8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					tst_resm(TFAIL,
90e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis						 "rmdir(\".\") removed the current working directory when it should have failed.");
91e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				} else {
92e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					tst_resm(TPASS,
93e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis						 "rmdir(\".\") failed to remove the current working directory. Returned %d : %s",
9456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak						 TEST_ERRNO,
95e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis						 strerror(TEST_ERRNO));
9656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				}
9756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			} else {
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
99e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir(\".\") failed with errno %d : %s but expected %d (EBUSY)",
100e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEST_ERRNO,
101e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 strerror(TEST_ERRNO), EBUSY);
10256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
103e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		} else {
104e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			tst_resm(TFAIL,
105e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				 "rmdir(\".\") succeeded unexpectedly.");
10656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
10756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
10856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/*
10956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * TEST CASE: 2
11056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * path points to the "." (dot) entry of a directory
11156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 */
11256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_resm(TCONF, "rmdir on \"dir/.\" supported on Linux");
113bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
11456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_resm(TCONF,
11556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			 "linked directories test not implemented on Linux");
11656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
11756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/*
11856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * TEST CASE: 4
11956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * path argument points below the minimum allocated address space
12056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 */
121bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak#if !defined(UCLINUX)
12256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		TEST(rmdir(bad_addr));
123bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
12456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (TEST_RETURN == -1) {
12556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
126865695bbc89088b9526ea9045410e5afb70a985cplars
127e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		if (TEST_RETURN == -1) {
128e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			if (TEST_ERRNO == EFAULT) {
129e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TPASS,
130e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir() - path argument points below the minimum allocated address space failed as expected with errno %d : %s",
131e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEST_ERRNO,
132e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 strerror(TEST_ERRNO));
13356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			} else {
13456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
135e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir() - path argument points below the minimum allocated address space failed with errno %d : %s but expected %d (EFAULT)",
136e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEST_ERRNO,
137e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 strerror(TEST_ERRNO), EFAULT);
13856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
139e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		} else {
140e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			tst_resm(TFAIL,
141e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				 "rmdir() - path argument points below the minimum allocated address space succeeded unexpectedly.");
14256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
14356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
14456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/*
14556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * TEST CASE: 5
14656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * path argument points above the maximum allocated address space
14756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 */
14856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
14956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		TEST(rmdir(get_high_address()));
15056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
15156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (TEST_RETURN == -1) {
15256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
153865695bbc89088b9526ea9045410e5afb70a985cplars
154e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		if (TEST_RETURN == -1) {
155e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			if (TEST_ERRNO == EFAULT) {
156e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TPASS,
157e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir() - path argument points above the maximum allocated address space failed as expected with errno %d : %s",
158e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEST_ERRNO,
159e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 strerror(TEST_ERRNO));
16056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			} else {
16156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL,
162e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir() - path argument points above the maximum allocated address space failed with errno %d : %s but expected %d (EFAULT)",
163e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 TEST_ERRNO,
164e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 strerror(TEST_ERRNO), EFAULT);
16556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
166e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		} else {
167e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			tst_resm(TFAIL,
168e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				 "rmdir() - path argument points above the maximum allocated address space succeeded unexpectedly.");
16956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
170d34d581c6a320e356a6cda923c7aa399479e812crobbiew#endif
171865695bbc89088b9526ea9045410e5afb70a985cplars
17256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/*
17356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * TEST CASE: 6
17456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 * able to remove a directory
17556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		 */
17656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
17756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (mkdir(dir_name, 0777) != 0) {
17856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			tst_brkm(TBROK, cleanup,
17956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 "mkdir(\"%s\") failed with errno %d : %s",
18056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 dir_name, errno, strerror(errno));
18156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
18256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
18356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		TEST(rmdir(dir_name));
18456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
18556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (TEST_RETURN == -1) {
18656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			tst_resm(TFAIL,
18756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 "rmdir(\"%s\") failed when it should have passed. Returned %d : %s",
18856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 dir_name, TEST_ERRNO, strerror(TEST_ERRNO));
18956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		} else {
190e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			/* Verify the directory was removed. */
191e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			if (stat(dir_name, &stat_buf) != 0) {
192e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TPASS,
193e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir(\"%s\") removed the directory as expected.",
194e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 dir_name);
195e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			} else {
196e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis				tst_resm(TFAIL,
197e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 "rmdir(\"%s\") returned a zero exit status but failed to remove the directory.",
198e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis					 dir_name);
19956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
20056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		}
20156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
2022c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
203865695bbc89088b9526ea9045410e5afb70a985cplars
20456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	cleanup();
2051e6f5a673655551de5734ff31ef48cd63b604e6dGarrett Cooper	tst_exit();
2062c28215423293e443469a07ae7011135d058b671Garrett Cooper}
207865695bbc89088b9526ea9045410e5afb70a985cplars
208c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid setup(void)
209865695bbc89088b9526ea9045410e5afb70a985cplars{
21056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_sig(FORK, DEF_HANDLER, cleanup);
211865695bbc89088b9526ea9045410e5afb70a985cplars
21256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	TEST_PAUSE;
213865695bbc89088b9526ea9045410e5afb70a985cplars
21456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_tmpdir();
215865695bbc89088b9526ea9045410e5afb70a985cplars
21656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* Create a directory. */
21756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (mkdir("dir1", 0777) == -1) {
21856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TBROK, cleanup, "mkdir() failed to create dir1.");
21956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	}
220865695bbc89088b9526ea9045410e5afb70a985cplars
22156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* Create a unique directory name. */
22256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	sprintf(dir_name, "./dir_%d", getpid());
223865695bbc89088b9526ea9045410e5afb70a985cplars
22462b16cfc513f6d873f3ba8fa6d19d308c8500557vapier#if !defined(UCLINUX)
22556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	bad_addr = mmap(0, 1, PROT_NONE,
22656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
22756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (bad_addr == MAP_FAILED) {
22856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TBROK, cleanup, "mmap failed");
22956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	}
23062b16cfc513f6d873f3ba8fa6d19d308c8500557vapier#endif
2312c28215423293e443469a07ae7011135d058b671Garrett Cooper}
232865695bbc89088b9526ea9045410e5afb70a985cplars
233c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid cleanup(void)
234865695bbc89088b9526ea9045410e5afb70a985cplars{
23556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_rmdir();
236ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
237