blob: 37e7963c2ee9d86f17d717651387e93cea94eca3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * rename04
23 *
24 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000025 * This test will verify that rename(2) failed when newpath is
plars865695b2001-08-27 22:15:12 +000026 * 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 <sys/fcntl.h>
62#include <sys/stat.h>
63#include <unistd.h>
64#include <errno.h>
65
66#include "test.h"
plars865695b2001-08-27 22:15:12 +000067
68void setup();
69void cleanup();
70extern void do_file_setup(char *);
71
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020072char *TCID = "rename04";
73int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000074
plars865695b2001-08-27 22:15:12 +000075int fd;
76char tstfile[40];
subrata_modak56207ce2009-03-23 13:35:39 +000077char fdir[255], mdir[255];
78struct stat buf1, buf2;
79dev_t olddev, olddev1;
80ino_t oldino, oldino1;
plars865695b2001-08-27 22:15:12 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000083{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020084 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
plars865695b2001-08-27 22:15:12 +000086
87 /*
88 * parse standard options
89 */
Garrett Cooper53740502010-12-16 00:04:01 -080090 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000092
93 /*
94 * perform global setup for test
95 */
96 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000097
plars865695b2001-08-27 22:15:12 +000098 /*
99 * check looping state if -i option given
100 */
subrata_modak56207ce2009-03-23 13:35:39 +0000101 for (lc = 0; TEST_LOOPING(lc); lc++) {
102
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000104
105 /* rename a directory to a non-empty directory */
106
107 /* Call rename(2) */
108 TEST(rename(fdir, mdir));
109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 if (TEST_RETURN != -1) {
plars865695b2001-08-27 22:15:12 +0000111 tst_resm(TFAIL, "rename(%s, %s) succeeded unexpectedly",
112 fdir, mdir);
113 continue;
114 }
115
nstrazb21c1bc2002-06-19 17:59:51 +0000116 if (TEST_ERRNO == ENOTEMPTY) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TPASS, "rename() returned ENOTEMPTY");
nstrazb21c1bc2002-06-19 17:59:51 +0000118 } else if (TEST_ERRNO == EEXIST) {
subrata_modak56207ce2009-03-23 13:35:39 +0000119 tst_resm(TPASS, "rename() returned EEXIST");
plars865695b2001-08-27 22:15:12 +0000120 } else {
nstrazb21c1bc2002-06-19 17:59:51 +0000121 tst_resm(TFAIL, "Expected ENOTEMPTY or EEXIST got %d",
122 TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000123 }
124
Garrett Cooper2c282152010-12-16 00:55:50 -0800125 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000126
plars865695b2001-08-27 22:15:12 +0000127 /*
128 * cleanup and exit
129 */
130 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800131 tst_exit();
robbiewc2cbe202003-03-27 18:32:02 +0000132
plars865695b2001-08-27 22:15:12 +0000133}
134
135/*
136 * setup() - performs all ONE TIME setup for this test.
137 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400138void setup(void)
plars865695b2001-08-27 22:15:12 +0000139{
Garrett Cooper2c282152010-12-16 00:55:50 -0800140
plars865695b2001-08-27 22:15:12 +0000141 tst_sig(NOFORK, DEF_HANDLER, cleanup);
142
subrata_modak4bb656a2009-02-26 12:02:09 +0000143 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000144
145 /* Create a temporary directory and make it current. */
146 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 sprintf(fdir, "./tdir_%d", getpid());
149 sprintf(mdir, "./rndir_%d", getpid());
150 sprintf(tstfile, "%s/tstfile_%d", mdir, getpid());
plars865695b2001-08-27 22:15:12 +0000151
152 /* create "old" directory */
153 if (mkdir(fdir, 00770) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 }
plars865695b2001-08-27 22:15:12 +0000156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 if (stat(fdir, &buf1) == -1) {
plars865695b2001-08-27 22:15:12 +0000158 tst_brkm(TBROK, cleanup, "failed to stat directory %s"
159 "in rename()", fdir);
Garrett Cooper2c282152010-12-16 00:55:50 -0800160
plars865695b2001-08-27 22:15:12 +0000161 }
162
163 /* save "old"'s dev and ino */
164 olddev = buf1.st_dev;
165 oldino = buf1.st_ino;
166
167 /* create another directory */
168 if (mkdir(mdir, 00770) == -1) {
169 tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 }
subrata_modak56207ce2009-03-23 13:35:39 +0000171
plars865695b2001-08-27 22:15:12 +0000172 /* create a file under "new" directory */
173 do_file_setup(tstfile);
174
175 if (stat(mdir, &buf2) == -1) {
176 tst_brkm(TBROK, cleanup, "failed to stat directory %s "
subrata_modakbdbaec52009-02-26 12:14:51 +0000177 "in rename()", mdir);
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
plars865695b2001-08-27 22:15:12 +0000179 }
180
181 /* save "new"'s dev and ino */
182 olddev1 = buf2.st_dev;
subrata_modak4bb656a2009-02-26 12:02:09 +0000183 oldino1 = buf2.st_ino;
plars865695b2001-08-27 22:15:12 +0000184}
185
186/*
187 * cleanup() - performs all ONE TIME cleanup for this test at
188 * completion or premature exit.
189 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400190void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000191{
plars865695b2001-08-27 22:15:12 +0000192
193 /*
194 * Remove the temporary directory.
195 */
196 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700197}