xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/open/open11.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) 2013 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2013-2022
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Basic tests for open(2) and make sure open(2) works and handles error
11*49cdfc7eSAndroid Build Coastguard Worker  * conditions correctly.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * There are 28 test cases:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * 1. Open regular file O_RDONLY
16*49cdfc7eSAndroid Build Coastguard Worker  * 2. Open regular file O_WRONLY
17*49cdfc7eSAndroid Build Coastguard Worker  * 3. Open regular file O_RDWR
18*49cdfc7eSAndroid Build Coastguard Worker  * 4. Open regular file O_RDWR | O_SYNC
19*49cdfc7eSAndroid Build Coastguard Worker  * 5. Open regular file O_RDWR | O_TRUNC
20*49cdfc7eSAndroid Build Coastguard Worker  * 6. Open directory O_RDONLY
21*49cdfc7eSAndroid Build Coastguard Worker  * 7. Open directory O_RDWR, expect EISDIR
22*49cdfc7eSAndroid Build Coastguard Worker  * 8. Open regular file O_DIRECTORY, expect ENOTDIR
23*49cdfc7eSAndroid Build Coastguard Worker  * 9. Open hard link file O_RDONLY
24*49cdfc7eSAndroid Build Coastguard Worker  * 10. Open hard link file O_WRONLY
25*49cdfc7eSAndroid Build Coastguard Worker  * 11. Open hard link file O_RDWR
26*49cdfc7eSAndroid Build Coastguard Worker  * 12. Open symlink file O_RDONLY
27*49cdfc7eSAndroid Build Coastguard Worker  * 13. Open symlink file O_WRONLY
28*49cdfc7eSAndroid Build Coastguard Worker  * 14. Open symlink file O_RDWR
29*49cdfc7eSAndroid Build Coastguard Worker  * 15. Open symlink directory O_RDONLY
30*49cdfc7eSAndroid Build Coastguard Worker  * 16. Open symlink directory O_WRONLY, expect EISDIR
31*49cdfc7eSAndroid Build Coastguard Worker  * 17. Open symlink directory O_RDWR, expect EISDIR
32*49cdfc7eSAndroid Build Coastguard Worker  * 18. Open device special file O_RDONLY
33*49cdfc7eSAndroid Build Coastguard Worker  * 19. Open device special file O_WRONLY
34*49cdfc7eSAndroid Build Coastguard Worker  * 20. Open device special file O_RDWR
35*49cdfc7eSAndroid Build Coastguard Worker  * 21. Open non-existing regular file in existing dir
36*49cdfc7eSAndroid Build Coastguard Worker  * 22. Open link file O_RDONLY | O_CREAT
37*49cdfc7eSAndroid Build Coastguard Worker  * 23. Open symlink file O_RDONLY | O_CREAT
38*49cdfc7eSAndroid Build Coastguard Worker  * 24. Open regular file O_RDONLY | O_CREAT
39*49cdfc7eSAndroid Build Coastguard Worker  * 25. Open symlink directory O_RDONLY | O_CREAT, expect EISDIR
40*49cdfc7eSAndroid Build Coastguard Worker  * 26. Open directory O_RDONLY | O_CREAT, expect EISDIR
41*49cdfc7eSAndroid Build Coastguard Worker  * 27. Open regular file O_RDONLY | O_TRUNC, behaviour is undefined but should
42*49cdfc7eSAndroid Build Coastguard Worker  *     not oops or hang
43*49cdfc7eSAndroid Build Coastguard Worker  * 28. Open regular file(non-empty) O_RDONLY | O_TRUNC, behaviour is undefined
44*49cdfc7eSAndroid Build Coastguard Worker  *     but should not oops or hang
45*49cdfc7eSAndroid Build Coastguard Worker  */
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
48*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sysmacros.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
58*49cdfc7eSAndroid Build Coastguard Worker #define T_REG "t_reg"			/* regular file with content */
59*49cdfc7eSAndroid Build Coastguard Worker #define T_REG_EMPTY "t_reg_empty"	/* empty regular file */
60*49cdfc7eSAndroid Build Coastguard Worker #define T_LINK_REG "t_link_reg"		/* hard link to T_REG */
61*49cdfc7eSAndroid Build Coastguard Worker #define T_NEW_REG "t_new_reg"		/* new regular file to be created */
62*49cdfc7eSAndroid Build Coastguard Worker #define T_SYMLINK_REG "t_symlink_reg"	/* symlink to T_REG */
63*49cdfc7eSAndroid Build Coastguard Worker #define T_DIR "t_dir"			/* test directory */
64*49cdfc7eSAndroid Build Coastguard Worker #define T_SYMLINK_DIR "t_symlink_dir"	/* symlink to T_DIR */
65*49cdfc7eSAndroid Build Coastguard Worker #define T_DEV MNTPOINT"/t_dev"		/* test device special file */
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker #define T_MSG "this is a test string"
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
70*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
71*49cdfc7eSAndroid Build Coastguard Worker 	char *path;
72*49cdfc7eSAndroid Build Coastguard Worker 	int flags;
73*49cdfc7eSAndroid Build Coastguard Worker 	mode_t mode;
74*49cdfc7eSAndroid Build Coastguard Worker 	int err;
75*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
76*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) regular file */
77*49cdfc7eSAndroid Build Coastguard Worker 	{
78*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDONLY",
79*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
80*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
81*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
82*49cdfc7eSAndroid Build Coastguard Worker 	},
83*49cdfc7eSAndroid Build Coastguard Worker 	{
84*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_WRONLY",
85*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
86*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_WRONLY,
87*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
88*49cdfc7eSAndroid Build Coastguard Worker 	},
89*49cdfc7eSAndroid Build Coastguard Worker 	{
90*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDWR",
91*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
92*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
93*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
94*49cdfc7eSAndroid Build Coastguard Worker 	},
95*49cdfc7eSAndroid Build Coastguard Worker 	{
96*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDWR | O_SYNC",
97*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
98*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR | O_SYNC,
99*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
100*49cdfc7eSAndroid Build Coastguard Worker 	},
101*49cdfc7eSAndroid Build Coastguard Worker 	{
102*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDWR | O_TRUNC",
103*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
104*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR | O_TRUNC,
105*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
106*49cdfc7eSAndroid Build Coastguard Worker 	},
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) directory */
109*49cdfc7eSAndroid Build Coastguard Worker 	{
110*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open directory O_RDONLY",
111*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DIR,
112*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
113*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0755,
114*49cdfc7eSAndroid Build Coastguard Worker 	},
115*49cdfc7eSAndroid Build Coastguard Worker 	{
116*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open directory O_RDWR",
117*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DIR,
118*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
119*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0755,
120*49cdfc7eSAndroid Build Coastguard Worker 		.err = EISDIR,
121*49cdfc7eSAndroid Build Coastguard Worker 	},
122*49cdfc7eSAndroid Build Coastguard Worker 	{
123*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_DIRECTORY",
124*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
125*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_DIRECTORY,
126*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
127*49cdfc7eSAndroid Build Coastguard Worker 		.err = ENOTDIR,
128*49cdfc7eSAndroid Build Coastguard Worker 	},
129*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) hard link */
130*49cdfc7eSAndroid Build Coastguard Worker 	{
131*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open hard link file O_RDONLY",
132*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_LINK_REG,
133*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
134*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
135*49cdfc7eSAndroid Build Coastguard Worker 	},
136*49cdfc7eSAndroid Build Coastguard Worker 	{
137*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open hard link file O_WRONLY",
138*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_LINK_REG,
139*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_WRONLY,
140*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
141*49cdfc7eSAndroid Build Coastguard Worker 	},
142*49cdfc7eSAndroid Build Coastguard Worker 	{
143*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open hard link file O_RDWR",
144*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_LINK_REG,
145*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
146*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
147*49cdfc7eSAndroid Build Coastguard Worker 	},
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) symlink */
150*49cdfc7eSAndroid Build Coastguard Worker 	{
151*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink file O_RDONLY",
152*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_REG,
153*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
154*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
155*49cdfc7eSAndroid Build Coastguard Worker 	},
156*49cdfc7eSAndroid Build Coastguard Worker 	{
157*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink file O_WRONLY",
158*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_REG,
159*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_WRONLY,
160*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
161*49cdfc7eSAndroid Build Coastguard Worker 	},
162*49cdfc7eSAndroid Build Coastguard Worker 	{
163*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink file O_RDWR",
164*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_REG,
165*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
166*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
167*49cdfc7eSAndroid Build Coastguard Worker 	},
168*49cdfc7eSAndroid Build Coastguard Worker 	{
169*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink directory O_RDONLY",
170*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_DIR,
171*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
172*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
173*49cdfc7eSAndroid Build Coastguard Worker 	},
174*49cdfc7eSAndroid Build Coastguard Worker 	{
175*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink directory O_WRONLY",
176*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_DIR,
177*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_WRONLY,
178*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
179*49cdfc7eSAndroid Build Coastguard Worker 		.err = EISDIR,
180*49cdfc7eSAndroid Build Coastguard Worker 	},
181*49cdfc7eSAndroid Build Coastguard Worker 	{
182*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink directory O_RDWR",
183*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_DIR,
184*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
185*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
186*49cdfc7eSAndroid Build Coastguard Worker 		.err = EISDIR,
187*49cdfc7eSAndroid Build Coastguard Worker 	},
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) device special */
190*49cdfc7eSAndroid Build Coastguard Worker 	{
191*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open device special file O_RDONLY",
192*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DEV,
193*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY,
194*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
195*49cdfc7eSAndroid Build Coastguard Worker 	},
196*49cdfc7eSAndroid Build Coastguard Worker 	{
197*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open device special file O_WRONLY",
198*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DEV,
199*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_WRONLY,
200*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
201*49cdfc7eSAndroid Build Coastguard Worker 	},
202*49cdfc7eSAndroid Build Coastguard Worker 	{
203*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open device special file O_RDWR",
204*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DEV,
205*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR,
206*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
207*49cdfc7eSAndroid Build Coastguard Worker 	},
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	/* Test open(2) non-existing file */
210*49cdfc7eSAndroid Build Coastguard Worker 	{
211*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open non-existing regular file in existing dir",
212*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DIR"/"T_NEW_REG,
213*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDWR | O_CREAT,
214*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
215*49cdfc7eSAndroid Build Coastguard Worker 	},
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	/* test open(2) with O_CREAT */
218*49cdfc7eSAndroid Build Coastguard Worker 	{
219*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open link file O_RDONLY | O_CREAT",
220*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_LINK_REG,
221*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_CREAT,
222*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
223*49cdfc7eSAndroid Build Coastguard Worker 	},
224*49cdfc7eSAndroid Build Coastguard Worker 	{
225*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink file O_RDONLY | O_CREAT",
226*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_REG,
227*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_CREAT,
228*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
229*49cdfc7eSAndroid Build Coastguard Worker 	},
230*49cdfc7eSAndroid Build Coastguard Worker 	{
231*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDONLY | O_CREAT",
232*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
233*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_CREAT,
234*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
235*49cdfc7eSAndroid Build Coastguard Worker 	},
236*49cdfc7eSAndroid Build Coastguard Worker 	{
237*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open symlink directory O_RDONLY | O_CREAT",
238*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_SYMLINK_DIR,
239*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_CREAT,
240*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
241*49cdfc7eSAndroid Build Coastguard Worker 		.err = EISDIR,
242*49cdfc7eSAndroid Build Coastguard Worker 	},
243*49cdfc7eSAndroid Build Coastguard Worker 	{
244*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open directory O_RDONLY | O_CREAT",
245*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_DIR,
246*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_CREAT,
247*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
248*49cdfc7eSAndroid Build Coastguard Worker 		.err = EISDIR,
249*49cdfc7eSAndroid Build Coastguard Worker 	},
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 	/* Other random open(2) tests */
252*49cdfc7eSAndroid Build Coastguard Worker 	{
253*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file O_RDONLY | O_TRUNC, "
254*49cdfc7eSAndroid Build Coastguard Worker 			"behaviour is undefined but should not oops or hang",
255*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG_EMPTY,
256*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_TRUNC,
257*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
258*49cdfc7eSAndroid Build Coastguard Worker 		.err = -1,
259*49cdfc7eSAndroid Build Coastguard Worker 	},
260*49cdfc7eSAndroid Build Coastguard Worker 	{
261*49cdfc7eSAndroid Build Coastguard Worker 		.desc = "open regular file(non-empty) O_RDONLY | O_TRUNC, "
262*49cdfc7eSAndroid Build Coastguard Worker 			"behaviour is undefined but should not oops or hang",
263*49cdfc7eSAndroid Build Coastguard Worker 		.path = T_REG,
264*49cdfc7eSAndroid Build Coastguard Worker 		.flags = O_RDONLY | O_TRUNC,
265*49cdfc7eSAndroid Build Coastguard Worker 		.mode = 0644,
266*49cdfc7eSAndroid Build Coastguard Worker 		.err = -1,
267*49cdfc7eSAndroid Build Coastguard Worker 	},
268*49cdfc7eSAndroid Build Coastguard Worker };
269*49cdfc7eSAndroid Build Coastguard Worker 
verify_open(unsigned int n)270*49cdfc7eSAndroid Build Coastguard Worker static void verify_open(unsigned int n)
271*49cdfc7eSAndroid Build Coastguard Worker {
272*49cdfc7eSAndroid Build Coastguard Worker 	if (tc[n].err > 0) {
273*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL2(open(tc[n].path, tc[n].flags, tc[n].mode),
274*49cdfc7eSAndroid Build Coastguard Worker 		             tc[n].err, "%s", tc[n].desc);
275*49cdfc7eSAndroid Build Coastguard Worker 	} else if (tc[n].err == 0) {
276*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FD(open(tc[n].path, tc[n].flags, tc[n].mode),
277*49cdfc7eSAndroid Build Coastguard Worker 		           "%s", tc[n].desc);
278*49cdfc7eSAndroid Build Coastguard Worker 	} else {
279*49cdfc7eSAndroid Build Coastguard Worker 		TEST(open(tc[n].path, tc[n].flags, tc[n].mode));
280*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "%s", tc[n].desc);
281*49cdfc7eSAndroid Build Coastguard Worker 	}
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET > 0)
284*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(TST_RET);
285*49cdfc7eSAndroid Build Coastguard Worker }
286*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)287*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
288*49cdfc7eSAndroid Build Coastguard Worker {
289*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
290*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
291*49cdfc7eSAndroid Build Coastguard Worker 
292*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(T_REG, O_WRONLY | O_CREAT, 0644);
293*49cdfc7eSAndroid Build Coastguard Worker 	ret = write(fd, T_MSG, sizeof(T_MSG));
294*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1) {
295*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
296*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "Write %s failed", T_REG);
297*49cdfc7eSAndroid Build Coastguard Worker 	}
298*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
299*49cdfc7eSAndroid Build Coastguard Worker 
300*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(T_REG_EMPTY, 0644, NULL);
301*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LINK(T_REG, T_LINK_REG);
302*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(T_REG, T_SYMLINK_REG);
303*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(T_DIR, 0755);
304*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(T_DIR, T_SYMLINK_DIR);
305*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKNOD(T_DEV, S_IFCHR, makedev(1, 5));
306*49cdfc7eSAndroid Build Coastguard Worker }
307*49cdfc7eSAndroid Build Coastguard Worker 
308*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
309*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
310*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
311*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_open,
312*49cdfc7eSAndroid Build Coastguard Worker 	.needs_devfs = 1,
313*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
314*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
315*49cdfc7eSAndroid Build Coastguard Worker };
316