xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/open/open13.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Guangwen Feng <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker  * with this program.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker /*
18*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
19*49cdfc7eSAndroid Build Coastguard Worker  *  Basic test for O_PATH flag of open(2).
20*49cdfc7eSAndroid Build Coastguard Worker  *  "Obtain a file descriptor that can be used to perform operations
21*49cdfc7eSAndroid Build Coastguard Worker  *   that act purely at the file descriptor level, the file itself is
22*49cdfc7eSAndroid Build Coastguard Worker  *   not opened, the operations read(2), write(2), fchmod(2), fchown(2)
23*49cdfc7eSAndroid Build Coastguard Worker  *   and fgetxattr(2) fail with the error EBADF."
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  *  The operations include but are not limited to the syscalls above.
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/xattr.h>
36*49cdfc7eSAndroid Build Coastguard Worker #endif
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
43*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	(S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
46*49cdfc7eSAndroid Build Coastguard Worker static void verify_read(void);
47*49cdfc7eSAndroid Build Coastguard Worker static void verify_write(void);
48*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void);
49*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchown(void);
50*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
51*49cdfc7eSAndroid Build Coastguard Worker static void verify_fgetxattr(void);
52*49cdfc7eSAndroid Build Coastguard Worker #endif
53*49cdfc7eSAndroid Build Coastguard Worker static void check_result(const char *call_name);
54*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static int fd;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static void (*test_func[])(void) = {
59*49cdfc7eSAndroid Build Coastguard Worker 	verify_read,
60*49cdfc7eSAndroid Build Coastguard Worker 	verify_write,
61*49cdfc7eSAndroid Build Coastguard Worker 	verify_fchmod,
62*49cdfc7eSAndroid Build Coastguard Worker 	verify_fchown,
63*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
64*49cdfc7eSAndroid Build Coastguard Worker 	verify_fgetxattr
65*49cdfc7eSAndroid Build Coastguard Worker #endif
66*49cdfc7eSAndroid Build Coastguard Worker };
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "open13";
69*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(test_func);
70*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)71*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
74*49cdfc7eSAndroid Build Coastguard Worker 	int tc;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	setup();
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_PATH);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 		for (tc = 0; tc < TST_TOTAL; tc++)
86*49cdfc7eSAndroid Build Coastguard Worker 			(*test_func[tc])();
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(cleanup, fd);
89*49cdfc7eSAndroid Build Coastguard Worker 		fd = 0;
90*49cdfc7eSAndroid Build Coastguard Worker 	}
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
verify_read(void)107*49cdfc7eSAndroid Build Coastguard Worker static void verify_read(void)
108*49cdfc7eSAndroid Build Coastguard Worker {
109*49cdfc7eSAndroid Build Coastguard Worker 	char buf[255];
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(fd, buf, sizeof(buf)));
112*49cdfc7eSAndroid Build Coastguard Worker 	check_result("read(2)");
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker 
verify_write(void)115*49cdfc7eSAndroid Build Coastguard Worker static void verify_write(void)
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker 	TEST(write(fd, "w", 1));
118*49cdfc7eSAndroid Build Coastguard Worker 	check_result("write(2)");
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
verify_fchmod(void)121*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void)
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fchmod(fd, 0666));
124*49cdfc7eSAndroid Build Coastguard Worker 	check_result("fchmod(2)");
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker 
verify_fchown(void)127*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchown(void)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fchown(fd, 1000, 1000));
130*49cdfc7eSAndroid Build Coastguard Worker 	check_result("fchown(2)");
131*49cdfc7eSAndroid Build Coastguard Worker }
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
verify_fgetxattr(void)134*49cdfc7eSAndroid Build Coastguard Worker static void verify_fgetxattr(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fgetxattr(fd, "tkey", NULL, 1));
137*49cdfc7eSAndroid Build Coastguard Worker 	check_result("fgetxattr(2)");
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker #endif
140*49cdfc7eSAndroid Build Coastguard Worker 
check_result(const char * call_name)141*49cdfc7eSAndroid Build Coastguard Worker static void check_result(const char *call_name)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == 0) {
144*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "%s succeeded unexpectedly", call_name);
145*49cdfc7eSAndroid Build Coastguard Worker 		return;
146*49cdfc7eSAndroid Build Coastguard Worker 	}
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_ERRNO != EBADF) {
149*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TTERRNO, "%s failed unexpectedly, "
150*49cdfc7eSAndroid Build Coastguard Worker 			"expected EBADF", call_name);
151*49cdfc7eSAndroid Build Coastguard Worker 		return;
152*49cdfc7eSAndroid Build Coastguard Worker 	}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "%s failed with EBADF", call_name);
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)157*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
158*49cdfc7eSAndroid Build Coastguard Worker {
159*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0 && close(fd))
160*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "failed to close file");
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
163*49cdfc7eSAndroid Build Coastguard Worker }
164