blob: b0f225d0a6fb7cf10094788944f2a4de4202b09b [file] [log] [blame]
vapierdfa737c2006-08-24 00:20:29 +00001/******************************************************************************
2 *
Cyril Hrubis33e1db82014-03-05 13:54:53 +01003 * Copyright (c) International Business Machines Corp., 2006
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +01004 * Author: Yi Yang <yyangcdl@cn.ibm.com>
Cyril Hrubis33e1db82014-03-05 13:54:53 +01005 * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
vapierdfa737c2006-08-24 00:20:29 +00006 *
Cyril Hrubis33e1db82014-03-05 13:54:53 +01007 * 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.
vapierdfa737c2006-08-24 00:20:29 +000011 *
Cyril Hrubis33e1db82014-03-05 13:54:53 +010012 * 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.
vapierdfa737c2006-08-24 00:20:29 +000016 *
Cyril Hrubis33e1db82014-03-05 13:54:53 +010017 * 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/*
vapierdfa737c2006-08-24 00:20:29 +000023 * DESCRIPTION
24 * This test case will verify basic function of mkdirat
25 * added by kernel 2.6.16 or up.
Cyril Hrubis33e1db82014-03-05 13:54:53 +010026 */
vapierdfa737c2006-08-24 00:20:29 +000027
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"
Cyril Hrubis33e1db82014-03-05 13:54:53 +010039#include "lapi/mkdirat.h"
40#include "safe_macros.h"
vapierdfa737c2006-08-24 00:20:29 +000041
Cyril Hrubis33e1db82014-03-05 13:54:53 +010042static void setup(void);
43static void cleanup(void);
44
45static char relpath[256];
46static char abspath[1024];
47static int dirfd, fd;
48static int fd_invalid = 100;
49static int fd_atcwd = AT_FDCWD;
50
51static struct test_case {
52 int *dirfd;
53 const char *name;
54 int exp_ret;
55 int exp_errno;
56} test_cases[] = {
57 {&dirfd, relpath, 0, 0},
58 {&dirfd, abspath, 0, 0},
59 {&fd_atcwd, relpath, 0, 0},
60 {&fd, relpath, -1, ENOTDIR},
61 {&fd_invalid, relpath, -1, EBADF},
62};
vapierdfa737c2006-08-24 00:20:29 +000063
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020064char *TCID = "mkdirat01";
Cyril Hrubis33e1db82014-03-05 13:54:53 +010065int TST_TOTAL = ARRAY_SIZE(test_cases);
vapierdfa737c2006-08-24 00:20:29 +000066
Cyril Hrubis33e1db82014-03-05 13:54:53 +010067static void verify_mkdirat(struct test_case *test)
vapierdfa737c2006-08-24 00:20:29 +000068{
Cyril Hrubis33e1db82014-03-05 13:54:53 +010069 TEST(mkdirat(*test->dirfd, 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
88static void setup_iteration(int i)
89{
90 static char testdir[256];
Cyril Hrubis9c31ad22014-05-14 17:15:39 +020091 char *tmpdir = tst_get_tmpdir();
Cyril Hrubis33e1db82014-03-05 13:54:53 +010092
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 dirfd = SAFE_OPEN(cleanup, testdir, O_DIRECTORY);
102}
103
104static void cleanup_iteration(void)
105{
106 SAFE_CLOSE(cleanup, dirfd);
vapierdfa737c2006-08-24 00:20:29 +0000107}
108
109int main(int ac, char **av)
110{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200111 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200112 const char *msg;
vapierdfa737c2006-08-24 00:20:29 +0000113 int i;
114
Garrett Cooper45e285d2010-11-22 12:19:25 -0800115 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800116 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierdfa737c2006-08-24 00:20:29 +0000117
vapierdfa737c2006-08-24 00:20:29 +0000118 setup();
119
vapierdfa737c2006-08-24 00:20:29 +0000120 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800121 tst_count = 0;
vapierdfa737c2006-08-24 00:20:29 +0000122
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100123 setup_iteration(lc);
vapierdfa737c2006-08-24 00:20:29 +0000124
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100125 for (i = 0; i < TST_TOTAL; i++)
126 verify_mkdirat(test_cases + i);
vapierdfa737c2006-08-24 00:20:29 +0000127
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100128 cleanup_iteration();
Garrett Cooper2c282152010-12-16 00:55:50 -0800129 }
vapierdfa737c2006-08-24 00:20:29 +0000130
vapierdfa737c2006-08-24 00:20:29 +0000131 cleanup();
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100132 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800133}
vapierdfa737c2006-08-24 00:20:29 +0000134
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100135static void setup(void)
vapierdfa737c2006-08-24 00:20:29 +0000136{
vapierdfa737c2006-08-24 00:20:29 +0000137 TEST_PAUSE;
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100138 tst_tmpdir();
139
140 fd = SAFE_OPEN(cleanup, "mkdirattestfile", O_CREAT | O_RDWR, 0600);
Garrett Cooper2c282152010-12-16 00:55:50 -0800141}
vapierdfa737c2006-08-24 00:20:29 +0000142
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100143static void cleanup(void)
vapierdfa737c2006-08-24 00:20:29 +0000144{
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100145 if (fd > 0)
146 close(fd);
vapierdfa737c2006-08-24 00:20:29 +0000147
Cyril Hrubis33e1db82014-03-05 13:54:53 +0100148 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700149}