blob: 832a579f94098fa3ad1776e6e6134f64ebcb9169 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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"
plars865695b2001-08-27 22:15:12 +000048
49void setup();
50void cleanup();
51extern void do_file_setup(char *);
52
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020053char *TCID = "rename07";
54int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000055
plars865695b2001-08-27 22:15:12 +000056int fd;
subrata_modak56207ce2009-03-23 13:35:39 +000057char mname[255], fdir[255];
58struct stat buf1, buf2;
59dev_t olddev, olddev1;
60ino_t oldino, oldino1;
plars865695b2001-08-27 22:15:12 +000061
subrata_modak56207ce2009-03-23 13:35:39 +000062int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000063{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
plars865695b2001-08-27 22:15:12 +000066
67 /*
68 * parse standard options
69 */
Garrett Cooper53740502010-12-16 00:04:01 -080070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000072
73 /*
74 * perform global setup for test
75 */
76 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000077
plars865695b2001-08-27 22:15:12 +000078 /*
79 * check looping state if -i option given
80 */
subrata_modak56207ce2009-03-23 13:35:39 +000081 for (lc = 0; TEST_LOOPING(lc); lc++) {
82
Caspar Zhangd59a6592013-03-07 14:59:12 +080083 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000084
85 /* rename a directory to a file */
86 /* Call rename(2) */
87 TEST(rename(fdir, mname));
88
subrata_modak56207ce2009-03-23 13:35:39 +000089 if (TEST_RETURN != -1) {
plars865695b2001-08-27 22:15:12 +000090 tst_resm(TFAIL, "rename(%s, %s) succeeded unexpectedly",
91 fdir, mname);
92 continue;
93 }
94
plars865695b2001-08-27 22:15:12 +000095 if (TEST_ERRNO != ENOTDIR) {
96 tst_resm(TFAIL, "Expected ENOTDIR got %d", TEST_ERRNO);
97 } else {
98 tst_resm(TPASS, "rename() returned ENOTDIR");
99 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800100 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000101
plars865695b2001-08-27 22:15:12 +0000102 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800103 tst_exit();
robbiewc2cbe202003-03-27 18:32:02 +0000104
plars865695b2001-08-27 22:15:12 +0000105}
106
107/*
108 * setup() - performs all ONE TIME setup for this test.
109 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400110void setup(void)
plars865695b2001-08-27 22:15:12 +0000111{
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
plars865695b2001-08-27 22:15:12 +0000113 tst_sig(NOFORK, DEF_HANDLER, cleanup);
114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000116
117 /* Create a temporary directory and make it current. */
118 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 sprintf(fdir, "./rndir_%d", getpid());
121 sprintf(mname, "./tfile_%d", getpid());
plars865695b2001-08-27 22:15:12 +0000122
123 /* create "old" directory */
124 if (stat(fdir, &buf1) != -1) {
125 tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 }
plars865695b2001-08-27 22:15:12 +0000127
128 if (mkdir(fdir, 00770) == -1) {
129 tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 }
plars865695b2001-08-27 22:15:12 +0000131
132 if (stat(fdir, &buf1) == -1) {
133 tst_brkm(TBROK, cleanup, "failed to stat directory %s "
subrata_modakbdbaec52009-02-26 12:14:51 +0000134 "in rename()", fdir);
Garrett Cooper2c282152010-12-16 00:55:50 -0800135
plars865695b2001-08-27 22:15:12 +0000136 }
137
138 /* save "old"'s dev and ino */
139 olddev = buf1.st_dev;
subrata_modak4bb656a2009-02-26 12:02:09 +0000140 oldino = buf1.st_ino;
plars865695b2001-08-27 22:15:12 +0000141
subrata_modak4bb656a2009-02-26 12:02:09 +0000142 /*
plars865695b2001-08-27 22:15:12 +0000143 * create "new" file
144 */
145 do_file_setup(mname);
146
147 if (stat(mname, &buf2) == -1) {
148 tst_brkm(TBROK, cleanup, "failed to stat file %s in rename()",
149 mname);
Garrett Cooper2c282152010-12-16 00:55:50 -0800150
plars865695b2001-08-27 22:15:12 +0000151 }
152
153 /* save "new"'s dev and ino */
154 olddev1 = buf2.st_dev;
155 oldino1 = buf2.st_ino;
156}
157
plars865695b2001-08-27 22:15:12 +0000158/*
159 * cleanup() - performs all ONE TIME cleanup for this test at
160 * completion or premature exit.
161 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000163{
plars865695b2001-08-27 22:15:12 +0000164
165 /*
166 * Remove the temporary directory.
167 */
168 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700169}