xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fchown/fchown05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * 07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2021 Xie Ziyao <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that, fchown() succeeds to change the owner and group of a file
12*49cdfc7eSAndroid Build Coastguard Worker  * specified by file descriptor to any numeric owner(uid)/group(gid) values
13*49cdfc7eSAndroid Build Coastguard Worker  * when invoked by super-user.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
21*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "testfile"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static int fd;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
26*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
27*49cdfc7eSAndroid Build Coastguard Worker 	uid_t uid;
28*49cdfc7eSAndroid Build Coastguard Worker 	gid_t gid;
29*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
30*49cdfc7eSAndroid Build Coastguard Worker 	{"change owner/group ids", 700, 701},
31*49cdfc7eSAndroid Build Coastguard Worker 	{"change owner id only", 702, -1},
32*49cdfc7eSAndroid Build Coastguard Worker 	{"change owner id only", 703, 701},
33*49cdfc7eSAndroid Build Coastguard Worker 	{"change group id only", -1, 704},
34*49cdfc7eSAndroid Build Coastguard Worker 	{"change group id only", 703, 705},
35*49cdfc7eSAndroid Build Coastguard Worker 	{"no change", -1, -1}
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)38*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
41*49cdfc7eSAndroid Build Coastguard Worker 	uid_t expect_uid = tc[i].uid == (uid_t)-1 ? tc[i - 1].uid : tc[i].uid;
42*49cdfc7eSAndroid Build Coastguard Worker 	gid_t expect_gid = tc[i].gid == (uid_t)-1 ? tc[i - 1].gid : tc[i].gid;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(FCHOWN(fd, tc[i].uid, tc[i].gid), "fchown(%i, %i, %i), %s",
45*49cdfc7eSAndroid Build Coastguard Worker 		     fd, tc[i].uid, tc[i].gid, tc[i].desc);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &stat_buf);
48*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_uid != expect_uid || stat_buf.st_gid != expect_gid) {
49*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: incorrect ownership set, expected %d %d",
50*49cdfc7eSAndroid Build Coastguard Worker 			TESTFILE, expect_uid, expect_gid);
51*49cdfc7eSAndroid Build Coastguard Worker 	}
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)59*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
62*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
67*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
68*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
69*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
70*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
71*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
72*49cdfc7eSAndroid Build Coastguard Worker };
73