xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lchown/lchown03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2014 Fujitsu Ltd.
3  * Author: Zeng Linggang <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 /*
18  * Test Description:
19  *  Verify that,
20  *   1. lchown() fails with -1 return value and sets errno to ELOOP
21  *      if too many symbolic links were encountered in resolving path.
22  *   2. lchown() fails with -1 return value and sets errno to EROFS
23  *      if the file is on a read-only file system.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <grp.h>
34 #include <pwd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <sys/mount.h>
39 
40 #include "test.h"
41 #include "safe_macros.h"
42 
43 /*
44  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
45  * rewriting all tests to the new API.
46  */
47 #include "compat_16.h"
48 
49 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
50 			 S_IXGRP|S_IROTH|S_IXOTH)
51 #define TEST_EROFS	"mntpoint"
52 
53 static char test_eloop[PATH_MAX] = ".";
54 static const char *device;
55 static int mount_flag;
56 
57 static struct test_case_t {
58 	char *pathname;
59 	int exp_errno;
60 } test_cases[] = {
61 	{test_eloop, ELOOP},
62 	{TEST_EROFS, EROFS},
63 };
64 
65 TCID_DEFINE(lchown03);
66 int TST_TOTAL = ARRAY_SIZE(test_cases);
67 
68 static void setup(void);
69 static void lchown_verify(const struct test_case_t *);
70 static void cleanup(void);
71 
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 	int lc;
75 	int i;
76 
77 	tst_parse_opts(argc, argv, NULL, NULL);
78 
79 	setup();
80 
81 	for (lc = 0; TEST_LOOPING(lc); lc++) {
82 		tst_count = 0;
83 		for (i = 0; i < TST_TOTAL; i++)
84 			lchown_verify(&test_cases[i]);
85 	}
86 
87 	cleanup();
88 	tst_exit();
89 }
90 
setup(void)91 static void setup(void)
92 {
93 	int i;
94 	const char *fs_type;
95 
96 	tst_require_root();
97 
98 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
99 
100 	TEST_PAUSE;
101 
102 	tst_tmpdir();
103 
104 	fs_type = tst_dev_fs_type();
105 	device = tst_acquire_device(cleanup);
106 
107 	if (!device)
108 		tst_brkm(TCONF, cleanup, "Failed to acquire device");
109 
110 	SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
111 	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
112 	for (i = 0; i < 43; i++)
113 		strcat(test_eloop, "/test_eloop");
114 
115 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
116 	SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
117 	SAFE_MOUNT(cleanup, device, TEST_EROFS, fs_type, MS_RDONLY, NULL);
118 	mount_flag = 1;
119 }
120 
lchown_verify(const struct test_case_t * test)121 static void lchown_verify(const struct test_case_t *test)
122 {
123 	UID16_CHECK(geteuid(), "lchown", cleanup)
124 	GID16_CHECK(getegid(), "lchown", cleanup)
125 
126 	TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid()));
127 
128 	if (TEST_RETURN != -1) {
129 		tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d",
130 			 TEST_RETURN, test->exp_errno);
131 		return;
132 	}
133 
134 	if (TEST_ERRNO == test->exp_errno) {
135 		tst_resm(TPASS | TTERRNO, "lchown() failed as expected");
136 	} else {
137 		tst_resm(TFAIL | TTERRNO,
138 			 "lchown() failed unexpectedly; expected: %d - %s",
139 			 test->exp_errno,
140 			 strerror(test->exp_errno));
141 	}
142 }
143 
cleanup(void)144 static void cleanup(void)
145 {
146 	if (mount_flag && tst_umount(TEST_EROFS) < 0)
147 		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
148 
149 	if (device)
150 		tst_release_device(device);
151 
152 	tst_rmdir();
153 }
154