1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	rename04
23  *
24  * DESCRIPTION
25  *	This test will verify that rename(2) failed when newpath is
26  *      a non-empty directory and return EEXIST or ENOTEMPTY
27  *
28  * ALGORITHM
29  *	Setup:
30  *		Setup signal handling.
31  *		Create temporary directory.
32  *		Pause for SIGUSR1 if option specified.
33  *              create the "old" directory and the "new" directory
34  *              create a file uner the "new" directory
35  *
36  *	Test:
37  *		Loop if the proper options are given.
38  *                  rename the "old" to the "new" directory
39  *                  verify rename() failed and returned ENOTEMPTY
40  *
41  *	Cleanup:
42  *		Print errno log and/or timing stats if options given
43  *		Delete the temporary directory created.
44  *
45  * USAGE
46  *	rename04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
47  *	where,  -c n : Run n copies concurrently.
48  *		-e   : Turn on errno logging.
49  *		-i n : Execute test n times.
50  *		-I x : Execute test for x seconds.
51  *		-P x : Pause for x seconds between iterations.
52  *		-t   : Turn on syscall timing.
53  *
54  * HISTORY
55  *	07/2001 Ported by Wayne Boyer
56  *
57  * RESTRICTIONS
58  *	None.
59  */
60 #include <sys/types.h>
61 #include <sys/fcntl.h>
62 #include <sys/stat.h>
63 #include <unistd.h>
64 #include <errno.h>
65 
66 #include "test.h"
67 
68 void setup();
69 void cleanup();
70 
71 char *TCID = "rename04";
72 int TST_TOTAL = 1;
73 
74 int fd;
75 char tstfile[40];
76 char fdir[255], mdir[255];
77 struct stat buf1, buf2;
78 dev_t olddev, olddev1;
79 ino_t oldino, oldino1;
80 
main(int ac,char ** av)81 int main(int ac, char **av)
82 {
83 	int lc;
84 
85 	/*
86 	 * parse standard options
87 	 */
88 	tst_parse_opts(ac, av, NULL, NULL);
89 
90 	/*
91 	 * perform global setup for test
92 	 */
93 	setup();
94 
95 	/*
96 	 * check looping state if -i option given
97 	 */
98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
99 
100 		tst_count = 0;
101 
102 		/* rename a directory to a non-empty directory */
103 
104 		/* Call rename(2) */
105 		TEST(rename(fdir, mdir));
106 
107 		if (TEST_RETURN != -1) {
108 			tst_resm(TFAIL, "rename(%s, %s) succeeded unexpectedly",
109 				 fdir, mdir);
110 			continue;
111 		}
112 
113 		if (TEST_ERRNO == ENOTEMPTY) {
114 			tst_resm(TPASS, "rename() returned ENOTEMPTY");
115 		} else if (TEST_ERRNO == EEXIST) {
116 			tst_resm(TPASS, "rename() returned EEXIST");
117 		} else {
118 			tst_resm(TFAIL, "Expected ENOTEMPTY or EEXIST got %d",
119 				 TEST_ERRNO);
120 		}
121 
122 	}
123 
124 	/*
125 	 * cleanup and exit
126 	 */
127 	cleanup();
128 	tst_exit();
129 
130 }
131 
132 /*
133  * setup() - performs all ONE TIME setup for this test.
134  */
setup(void)135 void setup(void)
136 {
137 
138 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
139 
140 	TEST_PAUSE;
141 
142 	/* Create a temporary directory and make it current. */
143 	tst_tmpdir();
144 
145 	sprintf(fdir, "./tdir_%d", getpid());
146 	sprintf(mdir, "./rndir_%d", getpid());
147 	sprintf(tstfile, "%s/tstfile_%d", mdir, getpid());
148 
149 	/* create "old" directory */
150 	if (mkdir(fdir, 00770) == -1) {
151 		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
152 	}
153 
154 	if (stat(fdir, &buf1) == -1) {
155 		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
156 			 "in rename()", fdir);
157 
158 	}
159 
160 	/* save "old"'s dev and ino */
161 	olddev = buf1.st_dev;
162 	oldino = buf1.st_ino;
163 
164 	/* create another directory */
165 	if (mkdir(mdir, 00770) == -1) {
166 		tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
167 	}
168 
169 	SAFE_TOUCH(cleanup, tstfile, 0700, NULL);
170 
171 	if (stat(mdir, &buf2) == -1) {
172 		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
173 			 "in rename()", mdir);
174 
175 	}
176 
177 	/* save "new"'s dev and ino */
178 	olddev1 = buf2.st_dev;
179 	oldino1 = buf2.st_ino;
180 }
181 
182 /*
183  * cleanup() - performs all ONE TIME cleanup for this test at
184  *              completion or premature exit.
185  */
cleanup(void)186 void cleanup(void)
187 {
188 
189 	/*
190 	 * Remove the temporary directory.
191 	 */
192 	tst_rmdir();
193 }
194