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