xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/creat/creat06.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  * Ported to LTP: Wayne Boyer
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  *	Testcase to check creat(2) sets the following errnos correctly:
10*49cdfc7eSAndroid Build Coastguard Worker  *	1.	EISDIR
11*49cdfc7eSAndroid Build Coastguard Worker  *	2.	ENAMETOOLONG
12*49cdfc7eSAndroid Build Coastguard Worker  *	3.	ENOENT
13*49cdfc7eSAndroid Build Coastguard Worker  *	4.	ENOTDIR
14*49cdfc7eSAndroid Build Coastguard Worker  *	5.	EFAULT
15*49cdfc7eSAndroid Build Coastguard Worker  *	6.	EACCES
16*49cdfc7eSAndroid Build Coastguard Worker  *	7.	ELOOP
17*49cdfc7eSAndroid Build Coastguard Worker  *	8.	EROFS
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *
20*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
21*49cdfc7eSAndroid Build Coastguard Worker  *	1.	Attempt to creat(2) an existing directory, and test for
22*49cdfc7eSAndroid Build Coastguard Worker  *		EISDIR
23*49cdfc7eSAndroid Build Coastguard Worker  *	2.	Attempt to creat(2) a file whose name is more than
24*49cdfc7eSAndroid Build Coastguard Worker  *		VFS_MAXNAMLEN and test for ENAMETOOLONG.
25*49cdfc7eSAndroid Build Coastguard Worker  *	3.	Attempt to creat(2) a file inside a directory which doesn't
26*49cdfc7eSAndroid Build Coastguard Worker  *		exist, and test for ENOENT
27*49cdfc7eSAndroid Build Coastguard Worker  *	4.	Attempt to creat(2) a file, the pathname of which comprises
28*49cdfc7eSAndroid Build Coastguard Worker  *		a component which is a file, test for ENOTDIR.
29*49cdfc7eSAndroid Build Coastguard Worker  *	5.	Attempt to creat(2) a file with a bad address
30*49cdfc7eSAndroid Build Coastguard Worker  *		and test for EFAULT
31*49cdfc7eSAndroid Build Coastguard Worker  *	6.	Attempt to creat(2) a file in a directory with no
32*49cdfc7eSAndroid Build Coastguard Worker  *		execute permission and test for EACCES
33*49cdfc7eSAndroid Build Coastguard Worker  *	7.	Attempt to creat(2) a file which links the other file that
34*49cdfc7eSAndroid Build Coastguard Worker  *		links the former and test for ELOOP
35*49cdfc7eSAndroid Build Coastguard Worker  *	8.	Attempt to creat(2) a file in a Read-only file system
36*49cdfc7eSAndroid Build Coastguard Worker  *		and test for EROFS
37*49cdfc7eSAndroid Build Coastguard Worker  */
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker #define	TEST_FILE	"test_dir"
51*49cdfc7eSAndroid Build Coastguard Worker #define	NO_DIR		"testfile/testdir"
52*49cdfc7eSAndroid Build Coastguard Worker #define	NOT_DIR		"file1/testdir"
53*49cdfc7eSAndroid Build Coastguard Worker #define	TEST6_FILE	"dir6/file6"
54*49cdfc7eSAndroid Build Coastguard Worker #define	TEST7_FILE	"file7"
55*49cdfc7eSAndroid Build Coastguard Worker #define	TEST8_FILE	"mntpoint/tmp"
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker #define	MODE1		0444
58*49cdfc7eSAndroid Build Coastguard Worker #define	MODE2		0666
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
61*49cdfc7eSAndroid Build Coastguard Worker static void test6_setup(void);
62*49cdfc7eSAndroid Build Coastguard Worker static void test6_cleanup(void);
63*49cdfc7eSAndroid Build Coastguard Worker static void bad_addr_setup(int);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
66*49cdfc7eSAndroid Build Coastguard Worker static char long_name[PATH_MAX+2];
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
69*49cdfc7eSAndroid Build Coastguard Worker 	char *fname;
70*49cdfc7eSAndroid Build Coastguard Worker 	int mode;
71*49cdfc7eSAndroid Build Coastguard Worker 	int error;
72*49cdfc7eSAndroid Build Coastguard Worker 	void (*setup)();
73*49cdfc7eSAndroid Build Coastguard Worker 	void (*cleanup)(void);
74*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
75*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_FILE, MODE1, EISDIR, NULL, NULL},
76*49cdfc7eSAndroid Build Coastguard Worker 	{long_name, MODE1, ENAMETOOLONG, NULL, NULL},
77*49cdfc7eSAndroid Build Coastguard Worker 	{NO_DIR, MODE1, ENOENT, NULL, NULL},
78*49cdfc7eSAndroid Build Coastguard Worker 	{NOT_DIR, MODE1, ENOTDIR, NULL, NULL},
79*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, MODE1, EFAULT, bad_addr_setup, NULL},
80*49cdfc7eSAndroid Build Coastguard Worker 	{TEST6_FILE, MODE1, EACCES, test6_setup, test6_cleanup},
81*49cdfc7eSAndroid Build Coastguard Worker 	{TEST7_FILE, MODE1, ELOOP, NULL, NULL},
82*49cdfc7eSAndroid Build Coastguard Worker 	{TEST8_FILE, MODE1, EROFS, NULL, NULL},
83*49cdfc7eSAndroid Build Coastguard Worker };
84*49cdfc7eSAndroid Build Coastguard Worker 
verify_creat(unsigned int i)85*49cdfc7eSAndroid Build Coastguard Worker static void verify_creat(unsigned int i)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker 	if (tcases[i].setup != NULL)
88*49cdfc7eSAndroid Build Coastguard Worker 		tcases[i].setup(i);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	TEST(creat(tcases[i].fname, tcases[i].mode));
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	if (tcases[i].cleanup != NULL)
93*49cdfc7eSAndroid Build Coastguard Worker 		tcases[i].cleanup();
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
96*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "call succeeded unexpectedly");
97*49cdfc7eSAndroid Build Coastguard Worker 		return;
98*49cdfc7eSAndroid Build Coastguard Worker 	}
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == tcases[i].error) {
101*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "got expected failure");
102*49cdfc7eSAndroid Build Coastguard Worker 		return;
103*49cdfc7eSAndroid Build Coastguard Worker 	}
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL | TTERRNO, "expected %s",
106*49cdfc7eSAndroid Build Coastguard Worker 	         tst_strerrno(tcases[i].error));
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)110*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_FILE, MODE2);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	memset(long_name, 'a', PATH_MAX+1);
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH("file1", MODE1, NULL);
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR("dir6", MODE2);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST7_FILE, "test_file_eloop2");
123*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("test_file_eloop2", TEST7_FILE);
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker 
bad_addr_setup(int i)126*49cdfc7eSAndroid Build Coastguard Worker static void bad_addr_setup(int i)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	if (tcases[i].fname)
129*49cdfc7eSAndroid Build Coastguard Worker 		return;
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	tcases[i].fname = SAFE_MMAP(0, 1, PROT_NONE,
132*49cdfc7eSAndroid Build Coastguard Worker 	                            MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker 
test6_setup(void)135*49cdfc7eSAndroid Build Coastguard Worker static void test6_setup(void)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker 
test6_cleanup(void)140*49cdfc7eSAndroid Build Coastguard Worker static void test6_cleanup(void)
141*49cdfc7eSAndroid Build Coastguard Worker {
142*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(0);
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
146*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
147*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_creat,
148*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
149*49cdfc7eSAndroid Build Coastguard Worker 	.needs_rofs = 1,
150*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = "mntpoint",
151*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
152*49cdfc7eSAndroid Build Coastguard Worker };
153