xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fchownat/fchownat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2006
3*49cdfc7eSAndroid Build Coastguard Worker  *   AUTHOR: Yi Yang <[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 Foundation,
17*49cdfc7eSAndroid Build Coastguard Worker  *   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  * DESCRIPTION
21*49cdfc7eSAndroid Build Coastguard Worker  *	This test case will verify basic function of fchownat
22*49cdfc7eSAndroid Build Coastguard Worker  *	added by kernel 2.6.16 or up.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
36*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
42*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd;
45*49cdfc7eSAndroid Build Coastguard Worker static int fd;
46*49cdfc7eSAndroid Build Coastguard Worker static int no_fd = -1;
47*49cdfc7eSAndroid Build Coastguard Worker static int cu_fd = AT_FDCWD;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
50*49cdfc7eSAndroid Build Coastguard Worker 	int exp_ret;
51*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
52*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
53*49cdfc7eSAndroid Build Coastguard Worker 	int *fds;
54*49cdfc7eSAndroid Build Coastguard Worker 	char *filenames;
55*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
56*49cdfc7eSAndroid Build Coastguard Worker 	{0, 0, 0, &dir_fd, TESTFILE},
57*49cdfc7eSAndroid Build Coastguard Worker 	{-1, ENOTDIR, 0, &fd, TESTFILE},
58*49cdfc7eSAndroid Build Coastguard Worker 	{-1, EBADF, 0, &no_fd, TESTFILE},
59*49cdfc7eSAndroid Build Coastguard Worker 	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
60*49cdfc7eSAndroid Build Coastguard Worker 	{0, 0, 0, &cu_fd, TESTFILE},
61*49cdfc7eSAndroid Build Coastguard Worker };
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fchownat01";
64*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_cases);
65*49cdfc7eSAndroid Build Coastguard Worker static void fchownat_verify(const struct test_case_t *);
66*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)67*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
70*49cdfc7eSAndroid Build Coastguard Worker 	int i;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	setup();
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
78*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++)
79*49cdfc7eSAndroid Build Coastguard Worker 			fchownat_verify(&test_cases[i]);
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
83*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)86*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker 
fchownat_verify(const struct test_case_t * test)101*49cdfc7eSAndroid Build Coastguard Worker static void fchownat_verify(const struct test_case_t *test)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
104*49cdfc7eSAndroid Build Coastguard Worker 		      getegid(), test->flag));
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN != test->exp_ret) {
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TTERRNO,
108*49cdfc7eSAndroid Build Coastguard Worker 			 "fchownat() returned %ld, expected %d, errno=%d",
109*49cdfc7eSAndroid Build Coastguard Worker 			 TEST_RETURN, test->exp_ret, test->exp_errno);
110*49cdfc7eSAndroid Build Coastguard Worker 		return;
111*49cdfc7eSAndroid Build Coastguard Worker 	}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_ERRNO == test->exp_errno) {
114*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS | TTERRNO,
115*49cdfc7eSAndroid Build Coastguard Worker 			 "fchownat() returned the expected errno %d: %s",
116*49cdfc7eSAndroid Build Coastguard Worker 			 test->exp_ret, strerror(test->exp_errno));
117*49cdfc7eSAndroid Build Coastguard Worker 	} else {
118*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TTERRNO,
119*49cdfc7eSAndroid Build Coastguard Worker 			 "fchownat() failed unexpectedly; expected: %d - %s",
120*49cdfc7eSAndroid Build Coastguard Worker 			 test->exp_errno, strerror(test->exp_errno));
121*49cdfc7eSAndroid Build Coastguard Worker 	}
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)124*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
127*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 	if (dir_fd > 0)
130*49cdfc7eSAndroid Build Coastguard Worker 		close(dir_fd);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
133*49cdfc7eSAndroid Build Coastguard Worker }
134