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