xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mknodat/mknodat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /******************************************************************************
2  *
3  * Copyright (c) International Business Machines  Corp., 2006
4  *  Author: Yi Yang <[email protected]>
5  * Copyright (c) Cyril Hrubis 2014 <[email protected]>
6  *
7  * This program is free software;  you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15  * the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program;  if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * DESCRIPTION
22  *  This test case will verify basic function of mknodat
23  *  added by kernel 2.6.16 or up.
24  *
25  *****************************************************************************/
26 
27 #define _GNU_SOURCE
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <signal.h>
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "lapi/fcntl.h"
38 
39 #define PATHNAME "mknodattestdir"
40 
41 static void setup(void);
42 static void cleanup(void);
43 static void clean(void);
44 
45 static char testfilepath[256];
46 static char testfile[256];
47 static char testfile2[256];
48 static char testfile3[256];
49 
50 static int dir_fd, fd;
51 static int fd_invalid = 100;
52 static int fd_atcwd = AT_FDCWD;
53 
54 static struct test_case {
55 	int *dir_fd;
56 	const char *name;
57 	int exp_ret;
58 	int exp_errno;
59 } test_cases[] = {
60 	{&dir_fd, testfile, 0, 0},
61 	{&dir_fd, testfile3, 0, 0},
62 	{&fd, testfile2, -1, ENOTDIR},
63 	{&fd_invalid, testfile, -1, EBADF},
64 	{&fd_atcwd, testfile, 0, 0}
65 };
66 
67 char *TCID = "mknodat01";
68 int TST_TOTAL = ARRAY_SIZE(test_cases);
69 
70 static dev_t dev;
71 
verify_mknodat(struct test_case * test)72 static void verify_mknodat(struct test_case *test)
73 {
74 	TEST(mknodat(*test->dir_fd, test->name, S_IFREG, dev));
75 
76 	if (TEST_RETURN != test->exp_ret) {
77 		tst_resm(TFAIL | TTERRNO,
78 		         "mknodat() returned %ld, expected %d",
79 			 TEST_RETURN, test->exp_ret);
80 		return;
81 	}
82 
83 	if (TEST_ERRNO != test->exp_errno) {
84 		tst_resm(TFAIL | TTERRNO,
85 		         "mknodat() returned wrong errno, expected %d",
86 			 test->exp_errno);
87 		return;
88 	}
89 
90 	tst_resm(TPASS | TTERRNO, "mknodat() returned %ld", TEST_RETURN);
91 }
92 
main(int ac,char ** av)93 int main(int ac, char **av)
94 {
95 	int lc;
96 	int i;
97 
98 	tst_parse_opts(ac, av, NULL, NULL);
99 
100 	setup();
101 
102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
103 		tst_count = 0;
104 
105 		for (i = 0; i < TST_TOTAL; i++)
106 			verify_mknodat(test_cases + i);
107 
108 		/* clean created nodes before next run */
109 		clean();
110 	}
111 
112 	cleanup();
113 	tst_exit();
114 }
115 
setup(void)116 static void setup(void)
117 {
118 	char *tmpdir;
119 
120 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
121 
122 	TEST_PAUSE;
123 
124 	tst_tmpdir();
125 
126 	/* Initialize test dir and file names */
127 	tmpdir = tst_get_tmpdir();
128 	sprintf(testfilepath, PATHNAME"/mknodattestfile%d", getpid());
129 	sprintf(testfile, "mknodattestfile%d", getpid());
130 	sprintf(testfile2, "mknodattestfile2%d", getpid());
131 	sprintf(testfile3, "%s/mknodattestfile3%d", tmpdir, getpid());
132 	free(tmpdir);
133 
134 	SAFE_MKDIR(cleanup, PATHNAME, 0700);
135 
136 	dir_fd = SAFE_OPEN(cleanup, PATHNAME, O_DIRECTORY);
137 	fd = SAFE_OPEN(cleanup, testfile2, O_CREAT | O_RDWR, 0600);
138 }
139 
clean(void)140 static void clean(void)
141 {
142 	SAFE_UNLINK(cleanup, testfilepath);
143 	SAFE_UNLINK(cleanup, testfile3);
144 	SAFE_UNLINK(cleanup, testfile);
145 }
146 
cleanup(void)147 static void cleanup(void)
148 {
149 	if (dir_fd > 0 && close(dir_fd))
150 		tst_resm(TWARN | TERRNO, "Failed to close(dir_fd)");
151 
152 	if (fd > 0 && close(fd))
153 		tst_resm(TWARN | TERRNO, "Failed to close(fd)");
154 
155 	tst_rmdir();
156 }
157