blob: ef4b830275fd4739109dd7d079e0484f64706456 [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
nstrazfa31d552002-05-14 16:50:06 +000022 * rename13
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Verify rename() return successfully and performs no other action
26 * when "old" file and "new" file link to the same file.
27 *
28 * ALGORITHM
29 * Setup:
30 * Setup signal handling.
31 * Create temporary directory.
32 * Pause for SIGUSR1 if option specified.
33 *
34 * Test:
35 * Loop if the proper options are given.
subrata_modak4bb656a2009-02-26 12:02:09 +000036 * create the "old" file
plars865695b2001-08-27 22:15:12 +000037 * link the "new" file to the "old" file
38 * rename the "old" to the "new" file
39 * verify the "new" file points to the original file
40 * verify the "old" file exists and points to
41 * the original file
42 * Cleanup:
43 * Print errno log and/or timing stats if options given
44 * Delete the temporary directory created.
45 *
46 * USAGE
nstrazfa31d552002-05-14 16:50:06 +000047 * rename13 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000048 * where, -c n : Run n copies concurrently.
49 * -f : Turn off functionality Testing.
50 * -i n : Execute test n times.
51 * -I x : Execute test for x seconds.
52 * -P x : Pause for x seconds between iterations.
53 * -t : Turn on syscall timing.
54 *
55 * HISTORY
56 * 07/2001 Ported by Wayne Boyer
57 *
58 * RESTRICTIONS
59 * None.
60 */
61#include <sys/types.h>
62#include <sys/fcntl.h>
63#include <sys/stat.h>
64#include <unistd.h>
65#include <errno.h>
66
67#include "test.h"
plars865695b2001-08-27 22:15:12 +000068
69void setup();
70void cleanup();
71extern void do_file_setup(char *);
72
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020073char *TCID = "rename13";
74int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000075
76int fd;
77char fname[255], mname[255];
78struct stat buf1, buf2;
subrata_modak56207ce2009-03-23 13:35:39 +000079dev_t olddev;
80ino_t oldino;
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 /*
106 * TEST rename()works when
107 * both old file and new file link to the same file
108 */
109
110 /* Call rename(2) */
111 TEST(rename(fname, mname));
112
113 if (TEST_RETURN == -1) {
114 tst_resm(TFAIL, "rename(%s, %s) failed", fname, mname);
115 continue;
116 }
117
Cyril Hrubise38b9612014-06-02 17:20:57 +0200118 /* check the existence of "new", and get the status */
119 if (stat(mname, &buf2) == -1) {
120 tst_brkm(TBROK, cleanup, "failed to stat file "
121 "%s in rename()", mname);
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
plars865695b2001-08-27 22:15:12 +0000123 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200124
125 /* check the existence of "old", and get the status */
126 if (stat(fname, &buf1) == -1) {
127 tst_brkm(TBROK, cleanup, "failed to stat file "
128 "%s in rename()", fname);
129
130 }
131
132 /* verify the new file is the same as the original */
133 if (buf2.st_dev != olddev || buf2.st_ino != oldino) {
134 tst_resm(TFAIL,
135 "rename() failed: new file does "
136 "not point to the same file as old "
137 "file");
138 continue;
139 }
140
141 /* verify the old file is unchanged */
142 if (buf1.st_dev != olddev || buf1.st_ino != oldino) {
143 tst_resm(TFAIL,
144 "rename() failed: old file does "
145 "not point to the original file");
146 continue;
147 }
148
149 tst_resm(TPASS, "functionality of rename() is correct");
Garrett Cooper2c282152010-12-16 00:55:50 -0800150 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000151
plars865695b2001-08-27 22:15:12 +0000152 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800153 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800154}
plars865695b2001-08-27 22:15:12 +0000155
156/*
157 * setup() - performs all ONE TIME setup for this test.
158 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400159void setup(void)
plars865695b2001-08-27 22:15:12 +0000160{
Garrett Cooper2c282152010-12-16 00:55:50 -0800161
plars865695b2001-08-27 22:15:12 +0000162 tst_sig(NOFORK, DEF_HANDLER, cleanup);
163
subrata_modak4bb656a2009-02-26 12:02:09 +0000164 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000165
166 /* Create a temporary directory and make it current. */
167 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 sprintf(fname, "./tfile_%d", getpid());
170 sprintf(mname, "./rnfile_%d", getpid());
plars865695b2001-08-27 22:15:12 +0000171
172 /* create the "old" file */
173 do_file_setup(fname);
174
subrata_modak56207ce2009-03-23 13:35:39 +0000175 if (stat(fname, &buf1) == -1) {
plars865695b2001-08-27 22:15:12 +0000176 tst_brkm(TBROK, cleanup, "failed to stat file %s"
subrata_modak56207ce2009-03-23 13:35:39 +0000177 "in rename()", fname);
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
plars865695b2001-08-27 22:15:12 +0000179 }
180
181 /* save the dev and inode */
182 olddev = buf1.st_dev;
183 oldino = buf1.st_ino;
184
185 /* link the "new" file to the "old" file */
subrata_modak56207ce2009-03-23 13:35:39 +0000186 if (link(fname, mname) == -1) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000187 tst_brkm(TBROK, cleanup,
plars865695b2001-08-27 22:15:12 +0000188 "link from %s to %s failed!", fname, mname);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 }
plars865695b2001-08-27 22:15:12 +0000190}
191
192/*
193 * cleanup() - performs all ONE TIME cleanup for this test at
194 * completion or premature exit.
195 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400196void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000197{
plars865695b2001-08-27 22:15:12 +0000198
199 /*
200 * Remove the temporary directory.
201 */
202 tst_rmdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000203
Chris Dearmanec6edca2012-10-17 19:54:01 -0700204}