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