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
22 /*
23 * DESCRIPTION
24 * This test case will verify basic function of mkdirat
25 * added by kernel 2.6.16 or up.
26 */
27
28 #define _GNU_SOURCE
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <signal.h>
37 #include "test.h"
38 #include "safe_macros.h"
39
40 static void setup(void);
41 static void cleanup(void);
42
43 static char relpath[256];
44 static char abspath[1024];
45 static int dir_fd, fd;
46 static int fd_invalid = 100;
47 static int fd_atcwd = AT_FDCWD;
48
49 static struct test_case {
50 int *dir_fd;
51 const char *name;
52 int exp_ret;
53 int exp_errno;
54 } test_cases[] = {
55 {&dir_fd, relpath, 0, 0},
56 {&dir_fd, abspath, 0, 0},
57 {&fd_atcwd, relpath, 0, 0},
58 {&fd, relpath, -1, ENOTDIR},
59 {&fd_invalid, relpath, -1, EBADF},
60 };
61
62 char *TCID = "mkdirat01";
63 int TST_TOTAL = ARRAY_SIZE(test_cases);
64
verify_mkdirat(struct test_case * test)65 static void verify_mkdirat(struct test_case *test)
66 {
67 TEST(mkdirat(*test->dir_fd, test->name, 0600));
68
69 if (TEST_RETURN != test->exp_ret) {
70 tst_resm(TFAIL | TTERRNO,
71 "mkdirat() returned %ld, expected %d",
72 TEST_RETURN, test->exp_ret);
73 return;
74 }
75
76 if (TEST_ERRNO != test->exp_errno) {
77 tst_resm(TFAIL | TTERRNO,
78 "mkdirat() returned wrong errno, expected %d",
79 test->exp_errno);
80 return;
81 }
82
83 tst_resm(TPASS | TTERRNO, "mkdirat() returned %ld", TEST_RETURN);
84 }
85
setup_iteration(int i)86 static void setup_iteration(int i)
87 {
88 static char testdir[256];
89 char *tmpdir = tst_get_tmpdir();
90
91 /* Initialize test dir and file names */
92 sprintf(testdir, "mkdirattestdir%d_%d", getpid(), i);
93 sprintf(relpath, "mkdiratrelpath%d_%d", getpid(), i);
94 sprintf(abspath, "%s/mkdiratrelpath%d_%d_2", tmpdir, getpid(), i);
95
96 free(tmpdir);
97
98 SAFE_MKDIR(cleanup, testdir, 0700);
99 dir_fd = SAFE_OPEN(cleanup, testdir, O_DIRECTORY);
100 }
101
cleanup_iteration(void)102 static void cleanup_iteration(void)
103 {
104 SAFE_CLOSE(cleanup, dir_fd);
105 }
106
main(int ac,char ** av)107 int main(int ac, char **av)
108 {
109 int lc;
110 int i;
111
112 tst_parse_opts(ac, av, NULL, NULL);
113
114 setup();
115
116 for (lc = 0; TEST_LOOPING(lc); lc++) {
117 tst_count = 0;
118
119 setup_iteration(lc);
120
121 for (i = 0; i < TST_TOTAL; i++)
122 verify_mkdirat(test_cases + i);
123
124 cleanup_iteration();
125 }
126
127 cleanup();
128 tst_exit();
129 }
130
setup(void)131 static void setup(void)
132 {
133 TEST_PAUSE;
134 tst_tmpdir();
135
136 fd = SAFE_OPEN(cleanup, "mkdirattestfile", O_CREAT | O_RDWR, 0600);
137 }
138
cleanup(void)139 static void cleanup(void)
140 {
141 if (fd > 0)
142 close(fd);
143
144 tst_rmdir();
145 }
146