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