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