1 /******************************************************************************
2 *
3 * Copyright (c) International Business Machines Corp., 2006
4 * Author: Yi Yang <yyangcdl@cn.ibm.com>
5 * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
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 #include "mknodat.h"
39
40 #define PATHNAME "mknodattestdir"
41
42 static void setup(void);
43 static void cleanup(void);
44 static void clean(void);
45
46 static char testfilepath[256];
47 static char testfile[256];
48 static char testfile2[256];
49 static char testfile3[256];
50
51 static int dir_fd, fd;
52 static int fd_invalid = 100;
53 static int fd_atcwd = AT_FDCWD;
54
55 static struct test_case {
56 int *dir_fd;
57 const char *name;
58 int exp_ret;
59 int exp_errno;
60 } test_cases[] = {
61 {&dir_fd, testfile, 0, 0},
62 {&dir_fd, testfile3, 0, 0},
63 {&fd, testfile2, -1, ENOTDIR},
64 {&fd_invalid, testfile, -1, EBADF},
65 {&fd_atcwd, testfile, 0, 0}
66 };
67
68 char *TCID = "mknodat01";
69 int TST_TOTAL = ARRAY_SIZE(test_cases);
70
71 static dev_t dev;
72
verify_mknodat(struct test_case * test)73 static void verify_mknodat(struct test_case *test)
74 {
75 TEST(mknodat(*test->dir_fd, test->name, S_IFREG, dev));
76
77 if (TEST_RETURN != test->exp_ret) {
78 tst_resm(TFAIL | TTERRNO,
79 "mknodat() returned %ld, expected %d",
80 TEST_RETURN, test->exp_ret);
81 return;
82 }
83
84 if (TEST_ERRNO != test->exp_errno) {
85 tst_resm(TFAIL | TTERRNO,
86 "mknodat() returned wrong errno, expected %d",
87 test->exp_errno);
88 return;
89 }
90
91 tst_resm(TPASS | TTERRNO, "mknodat() returned %ld", TEST_RETURN);
92 }
93
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 int lc;
97 int i;
98
99 tst_parse_opts(ac, av, NULL, NULL);
100
101 setup();
102
103 for (lc = 0; TEST_LOOPING(lc); lc++) {
104 tst_count = 0;
105
106 for (i = 0; i < TST_TOTAL; i++)
107 verify_mknodat(test_cases + i);
108
109 /* clean created nodes before next run */
110 clean();
111 }
112
113 cleanup();
114 tst_exit();
115 }
116
setup(void)117 static void setup(void)
118 {
119 char *tmpdir;
120
121 if (tst_kvercmp(2, 6, 16) < 0)
122 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
123
124 tst_sig(NOFORK, DEF_HANDLER, cleanup);
125
126 TEST_PAUSE;
127
128 tst_tmpdir();
129
130 /* Initialize test dir and file names */
131 tmpdir = tst_get_tmpdir();
132 sprintf(testfilepath, PATHNAME"/mknodattestfile%d", getpid());
133 sprintf(testfile, "mknodattestfile%d", getpid());
134 sprintf(testfile2, "mknodattestfile2%d", getpid());
135 sprintf(testfile3, "%s/mknodattestfile3%d", tmpdir, getpid());
136 free(tmpdir);
137
138 SAFE_MKDIR(cleanup, PATHNAME, 0700);
139
140 dir_fd = SAFE_OPEN(cleanup, PATHNAME, O_DIRECTORY);
141 fd = SAFE_OPEN(cleanup, testfile2, O_CREAT | O_RDWR, 0600);
142 }
143
clean(void)144 static void clean(void)
145 {
146 SAFE_UNLINK(cleanup, testfilepath);
147 SAFE_UNLINK(cleanup, testfile3);
148 SAFE_UNLINK(cleanup, testfile);
149 }
150
cleanup(void)151 static void cleanup(void)
152 {
153 if (dir_fd > 0 && close(dir_fd))
154 tst_resm(TWARN | TERRNO, "Failed to close(dir_fd)");
155
156 if (fd > 0 && close(fd))
157 tst_resm(TWARN | TERRNO, "Failed to close(fd)");
158
159 tst_rmdir();
160 }
161