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