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 <fcntl.h>
62 #include <sys/stat.h>
63 #include <unistd.h>
64 #include <errno.h>
65 
66 #include "test.h"
67 #include "safe_macros.h"
68 
69 void setup();
70 void cleanup();
71 
72 char *TCID = "rename04";
73 int TST_TOTAL = 1;
74 
75 int fd;
76 char tstfile[40];
77 char fdir[255], mdir[255];
78 struct stat buf1, buf2;
79 dev_t olddev, olddev1;
80 ino_t oldino, oldino1;
81 
main(int ac,char ** av)82 int main(int ac, char **av)
83 {
84 	int lc;
85 
86 	/*
87 	 * parse standard options
88 	 */
89 	tst_parse_opts(ac, av, NULL, NULL);
90 
91 	/*
92 	 * perform global setup for test
93 	 */
94 	setup();
95 
96 	/*
97 	 * check looping state if -i option given
98 	 */
99 	for (lc = 0; TEST_LOOPING(lc); lc++) {
100 
101 		tst_count = 0;
102 
103 		/* rename a directory to a non-empty directory */
104 
105 		/* Call rename(2) */
106 		TEST(rename(fdir, mdir));
107 
108 		if (TEST_RETURN != -1) {
109 			tst_resm(TFAIL, "rename(%s, %s) succeeded unexpectedly",
110 				 fdir, mdir);
111 			continue;
112 		}
113 
114 		if (TEST_ERRNO == ENOTEMPTY) {
115 			tst_resm(TPASS, "rename() returned ENOTEMPTY");
116 		} else if (TEST_ERRNO == EEXIST) {
117 			tst_resm(TPASS, "rename() returned EEXIST");
118 		} else {
119 			tst_resm(TFAIL, "Expected ENOTEMPTY or EEXIST got %d",
120 				 TEST_ERRNO);
121 		}
122 
123 	}
124 
125 	/*
126 	 * cleanup and exit
127 	 */
128 	cleanup();
129 	tst_exit();
130 
131 }
132 
133 /*
134  * setup() - performs all ONE TIME setup for this test.
135  */
setup(void)136 void setup(void)
137 {
138 
139 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
140 
141 	TEST_PAUSE;
142 
143 	/* Create a temporary directory and make it current. */
144 	tst_tmpdir();
145 
146 	sprintf(fdir, "./tdir_%d", getpid());
147 	sprintf(mdir, "./rndir_%d", getpid());
148 	sprintf(tstfile, "%s/tstfile_%d", mdir, getpid());
149 
150 	/* create "old" directory */
151 	SAFE_MKDIR(cleanup, fdir, 00770);
152 
153 	SAFE_STAT(cleanup, fdir, &buf1);
154 
155 	/* save "old"'s dev and ino */
156 	olddev = buf1.st_dev;
157 	oldino = buf1.st_ino;
158 
159 	/* create another directory */
160 	SAFE_MKDIR(cleanup, mdir, 00770);
161 
162 	SAFE_TOUCH(cleanup, tstfile, 0700, NULL);
163 
164 	SAFE_STAT(cleanup, mdir, &buf2);
165 
166 	/* save "new"'s dev and ino */
167 	olddev1 = buf2.st_dev;
168 	oldino1 = buf2.st_ino;
169 }
170 
171 /*
172  * cleanup() - performs all ONE TIME cleanup for this test at
173  *              completion or premature exit.
174  */
cleanup(void)175 void cleanup(void)
176 {
177 
178 	/*
179 	 * Remove the temporary directory.
180 	 */
181 	tst_rmdir();
182 }
183