xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/rename/rename11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
22*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that,
23*49cdfc7eSAndroid Build Coastguard Worker  *   1. rename() fails with -1 return value and sets errno to ELOOP, if too
24*49cdfc7eSAndroid Build Coastguard Worker  *      many symbolic links were encountered in resolving oldpath or newpath.
25*49cdfc7eSAndroid Build Coastguard Worker  *   2. rename() fails with -1 return value and sets errno to EROFS,
26*49cdfc7eSAndroid Build Coastguard Worker  *      if the file is on a read-only file system.
27*49cdfc7eSAndroid Build Coastguard Worker  *   3. rename() fails with -1 return value and sets errno to EMLINK,
28*49cdfc7eSAndroid Build Coastguard Worker  *	if the file named by old is a directory and the link count of
29*49cdfc7eSAndroid Build Coastguard Worker  *	the parent directory of new would exceed {LINK_MAX}.
30*49cdfc7eSAndroid Build Coastguard Worker  */
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "rename11";
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mntpoint"
45*49cdfc7eSAndroid Build Coastguard Worker #define TEST_EROFS	"mntpoint/test_erofs"
46*49cdfc7eSAndroid Build Coastguard Worker #define TEST_NEW_EROFS	"mntpoint/new_test_erofs"
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #define TEST_EMLINK	"test_emlink"
49*49cdfc7eSAndroid Build Coastguard Worker #define TEST_NEW_EMLINK	"emlink_dir/testdir"
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker #define TEST_NEW_ELOOP	"new_test_eloop"
52*49cdfc7eSAndroid Build Coastguard Worker #define ELOPFILE	"/test_eloop"
53*49cdfc7eSAndroid Build Coastguard Worker static char elooppathname[sizeof(ELOPFILE) * 43] = ".";
54*49cdfc7eSAndroid Build Coastguard Worker static int max_subdirs;
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static const char *device;
57*49cdfc7eSAndroid Build Coastguard Worker static const char *fs_type;
58*49cdfc7eSAndroid Build Coastguard Worker static int mount_flag;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
61*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
62*49cdfc7eSAndroid Build Coastguard Worker static void test_eloop(void);
63*49cdfc7eSAndroid Build Coastguard Worker static void test_erofs(void);
64*49cdfc7eSAndroid Build Coastguard Worker static void test_emlink(void);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker static void (*testfunc[])(void) = { test_eloop, test_erofs, test_emlink };
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(testfunc);
69*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)70*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	int lc, i;
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	setup();
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
79*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++)
82*49cdfc7eSAndroid Build Coastguard Worker 			(*testfunc[i])();
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
86*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	int i;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	fs_type = tst_dev_fs_type();
102*49cdfc7eSAndroid Build Coastguard Worker 	device = tst_acquire_device(cleanup);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	if (!device)
105*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, MNTPOINT, 0755);
110*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
111*49cdfc7eSAndroid Build Coastguard Worker 	mount_flag = 1;
112*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(cleanup, TEST_EROFS, 0644, NULL);
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, TEST_EMLINK, 0755);
115*49cdfc7eSAndroid Build Coastguard Worker 	max_subdirs = tst_fs_fill_subdirs(cleanup, "emlink_dir");
116*49cdfc7eSAndroid Build Coastguard Worker 	/*
117*49cdfc7eSAndroid Build Coastguard Worker 	 * NOTE: the ELOOP test is written based on that the consecutive
118*49cdfc7eSAndroid Build Coastguard Worker 	 * symlinks limits in kernel is hardwired to 40.
119*49cdfc7eSAndroid Build Coastguard Worker 	 */
120*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, "test_eloop", 0644);
121*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
122*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 43; i++)
123*49cdfc7eSAndroid Build Coastguard Worker 		strcat(elooppathname, ELOPFILE);
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker 
check_and_print(int expected_errno)126*49cdfc7eSAndroid Build Coastguard Worker static void check_and_print(int expected_errno)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1) {
129*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_ERRNO == expected_errno) {
130*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS | TTERRNO, "failed as expected");
131*49cdfc7eSAndroid Build Coastguard Worker 		} else {
132*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO,
133*49cdfc7eSAndroid Build Coastguard Worker 				 "failed unexpectedly; expected - %d : %s",
134*49cdfc7eSAndroid Build Coastguard Worker 				 expected_errno, strerror(expected_errno));
135*49cdfc7eSAndroid Build Coastguard Worker 		}
136*49cdfc7eSAndroid Build Coastguard Worker 	} else {
137*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "rename succeeded unexpectedly");
138*49cdfc7eSAndroid Build Coastguard Worker 	}
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
test_eloop(void)141*49cdfc7eSAndroid Build Coastguard Worker static void test_eloop(void)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker 	TEST(rename(elooppathname, TEST_NEW_ELOOP));
144*49cdfc7eSAndroid Build Coastguard Worker 	check_and_print(ELOOP);
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == 0)
147*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNLINK(cleanup, TEST_NEW_ELOOP);
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker 
test_erofs(void)150*49cdfc7eSAndroid Build Coastguard Worker static void test_erofs(void)
151*49cdfc7eSAndroid Build Coastguard Worker {
152*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
153*49cdfc7eSAndroid Build Coastguard Worker 		   NULL);
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	TEST(rename(TEST_EROFS, TEST_NEW_EROFS));
156*49cdfc7eSAndroid Build Coastguard Worker 	check_and_print(EROFS);
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == 0)
159*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNLINK(cleanup, TEST_NEW_EROFS);
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT, NULL);
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker 
test_emlink(void)164*49cdfc7eSAndroid Build Coastguard Worker static void test_emlink(void)
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker 	if (max_subdirs == 0) {
167*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TCONF, "EMLINK test is not appropriate");
168*49cdfc7eSAndroid Build Coastguard Worker 		return;
169*49cdfc7eSAndroid Build Coastguard Worker 	}
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker 	TEST(rename(TEST_EMLINK, TEST_NEW_EMLINK));
172*49cdfc7eSAndroid Build Coastguard Worker 	check_and_print(EMLINK);
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == 0)
175*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_RMDIR(cleanup, TEST_NEW_EMLINK);
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)178*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
179*49cdfc7eSAndroid Build Coastguard Worker {
180*49cdfc7eSAndroid Build Coastguard Worker 	if (mount_flag && tst_umount(MNTPOINT) < 0)
181*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 	if (device)
184*49cdfc7eSAndroid Build Coastguard Worker 		tst_release_device(device);
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
187*49cdfc7eSAndroid Build Coastguard Worker }
188