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