xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lchown/lchown01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program;  if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * Test Description:
21  *  Verify that, lchown(2) succeeds to change the owner and group of a file
22  *  specified by path to any numeric owner(uid)/group(gid) values when invoked
23  *  by super-user.
24  *
25  * Expected Result:
26  *  lchown(2) should return 0 and the ownership set on the file should match
27  *  the numeric values contained in owner and group respectively.
28  *
29  * HISTORY
30  *	07/2001 Ported by Wayne Boyer
31  *	11/2010 Code cleanup by Cyril Hrubis [email protected]
32  */
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <signal.h>
41 
42 #include "test.h"
43 #include "safe_macros.h"
44 
45 /*
46  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
47  * rewriting all tests to the new API.
48  */
49 #include "compat_16.h"
50 
51 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
52 #define TESTFILE	"testfile"
53 #define SFILE		"slink_file"
54 
55 TCID_DEFINE(lchown01);
56 int TST_TOTAL = 5;
57 
58 struct test_case_t {
59 	char *desc;
60 	uid_t user_id;
61 	gid_t group_id;
62 };
63 
64 static struct test_case_t test_cases[] = {
65 	{"Change Owner/Group ids", 700, 701},
66 	{"Change Owner id only", 702, -1},
67 	{"Change Owner/Group ids", 703, 701},
68 	{"Change Group id only", -1, 704},
69 	{"Change Group/Group ids", 703, 705},
70 	{"Change none", -1, -1},
71 	{NULL, 0, 0}
72 };
73 
74 static void setup(void);
75 static void cleanup(void);
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 	struct stat stat_buf;
80 	int lc;
81 	int i;
82 
83 	tst_parse_opts(argc, argv, NULL, NULL);
84 
85 	setup();
86 
87 	for (lc = 0; TEST_LOOPING(lc); lc++) {
88 
89 		tst_count = 0;
90 
91 		for (i = 0; test_cases[i].desc != NULL; i++) {
92 			uid_t user_id = test_cases[i].user_id;
93 			gid_t group_id = test_cases[i].group_id;
94 			char *test_desc = test_cases[i].desc;
95 
96 			/*
97 			 * Call lchown(2) with different user id and
98 			 * group id (numeric values) to set it on
99 			 * symlink of testfile.
100 			 */
101 			TEST(LCHOWN(cleanup, SFILE, user_id, group_id));
102 
103 			if (TEST_RETURN == -1) {
104 				tst_resm(TFAIL,
105 					 "lchown() Fails to %s, errno %d",
106 					 test_desc, TEST_ERRNO);
107 				continue;
108 			}
109 
110 			if (lstat(SFILE, &stat_buf) < 0) {
111 				tst_brkm(TFAIL, cleanup, "lstat(2) "
112 					 "%s failed, errno %d",
113 					 SFILE, TEST_ERRNO);
114 			}
115 
116 			if ((int)user_id == -1) {
117 				if (i > 0)
118 					user_id =
119 					    test_cases[i - 1].user_id;
120 				else
121 					user_id = geteuid();
122 			}
123 
124 			if ((int)group_id == -1) {
125 				if (i > 0)
126 					group_id =
127 					    test_cases[i - 1].group_id;
128 				else
129 					group_id = getegid();
130 			}
131 
132 			/*
133 			 * Check for expected Ownership ids
134 			 * set on testfile.
135 			 */
136 			if ((stat_buf.st_uid != user_id) ||
137 			    (stat_buf.st_gid != group_id)) {
138 				tst_resm(TFAIL,
139 					 "%s: incorrect ownership set, "
140 					 "Expected %d %d", SFILE,
141 					 user_id, group_id);
142 			} else {
143 				tst_resm(TPASS, "lchown() succeeds to "
144 					 "%s of %s", test_desc, SFILE);
145 			}
146 		}
147 	}
148 
149 	cleanup();
150 	tst_exit();
151 }
152 
setup(void)153 static void setup(void)
154 {
155 	int fd;
156 
157 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
158 
159 	tst_require_root();
160 
161 	TEST_PAUSE;
162 	tst_tmpdir();
163 
164 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
165 		tst_brkm(TBROK, cleanup, "open failed");
166 	}
167 	SAFE_CLOSE(cleanup, fd);
168 
169 	SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
170 }
171 
cleanup(void)172 static void cleanup(void)
173 {
174 	tst_rmdir();
175 }
176