xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/renameat2/renameat202.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker  /* Description:
20*49cdfc7eSAndroid Build Coastguard Worker  *   Calls renameat2(2) with the flag RENAME_EXCHANGE and check that
21*49cdfc7eSAndroid Build Coastguard Worker  *   the content was swapped
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "renameat2.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "test_dir/"
32*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR2 "test_dir2/"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
35*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE2 "test_file2"
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "renameat202";
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static int olddirfd;
40*49cdfc7eSAndroid Build Coastguard Worker static int newdirfd;
41*49cdfc7eSAndroid Build Coastguard Worker static int fd = -1;
42*49cdfc7eSAndroid Build Coastguard Worker static int cnt;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static const char content[] = "content";
45*49cdfc7eSAndroid Build Coastguard Worker static long fs_type;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
51*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
52*49cdfc7eSAndroid Build Coastguard Worker static void renameat2_verify(void);
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)55*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	setup();
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 		TEST(renameat2(olddirfd, TEST_FILE,
68*49cdfc7eSAndroid Build Coastguard Worker 				newdirfd, TEST_FILE2, RENAME_EXCHANGE));
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 		cnt++;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 		renameat2_verify();
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
76*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	if ((tst_kvercmp(3, 15, 0)) < 0) {
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, NULL,
83*49cdfc7eSAndroid Build Coastguard Worker 			"This test can only run on kernels that are 3.15. and higher");
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	fs_type = tst_fs_type(cleanup, ".");
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
91*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL);
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	olddirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
97*49cdfc7eSAndroid Build Coastguard Worker 	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(cleanup, TEST_DIR TEST_FILE, "%s", content);
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)103*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker 	if (olddirfd > 0 && close(olddirfd) < 0)
106*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close olddirfd failed");
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	if (newdirfd > 0 && close(newdirfd) < 0)
109*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close newdirfd failed");
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0 && close(fd) < 0)
112*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close fd failed");
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker 
renameat2_verify(void)118*49cdfc7eSAndroid Build Coastguard Worker static void renameat2_verify(void)
119*49cdfc7eSAndroid Build Coastguard Worker {
120*49cdfc7eSAndroid Build Coastguard Worker 	char str[BUFSIZ] = { 0 };
121*49cdfc7eSAndroid Build Coastguard Worker 	struct stat st;
122*49cdfc7eSAndroid Build Coastguard Worker 	char *emptyfile;
123*49cdfc7eSAndroid Build Coastguard Worker 	char *contentfile;
124*49cdfc7eSAndroid Build Coastguard Worker 	int readn, data_len;
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_ERRNO == EINVAL && TST_BTRFS_MAGIC == fs_type) {
127*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, cleanup,
128*49cdfc7eSAndroid Build Coastguard Worker 			"RENAME_EXCHANGE flag is not implemeted on %s",
129*49cdfc7eSAndroid Build Coastguard Worker 			tst_fs_type_name(fs_type));
130*49cdfc7eSAndroid Build Coastguard Worker 	}
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN != 0) {
133*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TTERRNO, "renameat2() failed unexpectedly");
134*49cdfc7eSAndroid Build Coastguard Worker 		return;
135*49cdfc7eSAndroid Build Coastguard Worker 	}
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 	if (cnt % 2 == 1) {
138*49cdfc7eSAndroid Build Coastguard Worker 		emptyfile = TEST_DIR TEST_FILE;
139*49cdfc7eSAndroid Build Coastguard Worker 		contentfile = TEST_DIR2 TEST_FILE2;
140*49cdfc7eSAndroid Build Coastguard Worker 	} else {
141*49cdfc7eSAndroid Build Coastguard Worker 		emptyfile = TEST_DIR2 TEST_FILE2;
142*49cdfc7eSAndroid Build Coastguard Worker 		contentfile = TEST_DIR TEST_FILE;
143*49cdfc7eSAndroid Build Coastguard Worker 	}
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(cleanup, contentfile, O_RDONLY);
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(cleanup, emptyfile, &st);
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	readn = SAFE_READ(cleanup, 0, fd, str, BUFSIZ);
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 	if (close(fd) < 0)
152*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TERRNO | TFAIL, cleanup, "close fd failed");
153*49cdfc7eSAndroid Build Coastguard Worker 	fd = 0;
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	data_len = sizeof(content) - 1;
156*49cdfc7eSAndroid Build Coastguard Worker 	if (readn != data_len) {
157*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Wrong number of bytes read after renameat2(). "
158*49cdfc7eSAndroid Build Coastguard Worker 				"Expect %d, got %d", data_len, readn);
159*49cdfc7eSAndroid Build Coastguard Worker 		return;
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 	if (strncmp(content, str, data_len)) {
162*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "File content changed after renameat2(). "
163*49cdfc7eSAndroid Build Coastguard Worker 				"Expect '%s', got '%s'", content, str);
164*49cdfc7eSAndroid Build Coastguard Worker 		return;
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 	if (st.st_size) {
167*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "emptyfile has non-zero file size");
168*49cdfc7eSAndroid Build Coastguard Worker 		return;
169*49cdfc7eSAndroid Build Coastguard Worker 	}
170*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "renameat2() test passed");
171*49cdfc7eSAndroid Build Coastguard Worker }
172