blob: 19f7e9afcdcbda90c47a194bf492bc30d57d5ef5 [file] [log] [blame]
vapierd780ff62006-08-24 00:23:08 +00001/******************************************************************************
2 *
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +01003 * Copyright (c) International Business Machines Corp., 2006
4 * Author: Yi Yang <yyangcdl@cn.ibm.com>
5 * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
vapierd780ff62006-08-24 00:23:08 +00006 *
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +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.
vapierd780ff62006-08-24 00:23:08 +000011 *
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +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.
vapierd780ff62006-08-24 00:23:08 +000016 *
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +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
vapierd780ff62006-08-24 00:23:08 +000020 *
21 * DESCRIPTION
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010022 * This test case will verify basic function of mknodat
23 * added by kernel 2.6.16 or up.
vapierd780ff62006-08-24 00:23:08 +000024 *
25 *****************************************************************************/
26
27#define _GNU_SOURCE
28
29#include <sys/types.h>
vapierd780ff62006-08-24 00:23:08 +000030#include <fcntl.h>
yaberauneya13ace442009-11-20 07:07:04 +000031#include <sys/stat.h>
vapierd780ff62006-08-24 00:23:08 +000032#include <error.h>
33#include <stdlib.h>
34#include <errno.h>
35#include <string.h>
36#include <signal.h>
37#include "test.h"
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010038#include "safe_macros.h"
Xiaoguang Wang7525e622014-03-04 14:32:00 +080039#include "lapi/fcntl.h"
40#include "mknodat.h"
vapierd780ff62006-08-24 00:23:08 +000041
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010042#define PATHNAME "mknodattestdir"
Xiaoguang Wang7525e622014-03-04 14:32:00 +080043
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010044static void setup(void);
45static void cleanup(void);
46static void clean(void);
47
48static char testfilepath[256];
49static char testfile[256];
50static char testfile2[256];
51static char testfile3[256];
52
53static int dirfd, fd;
54static int fd_invalid = 100;
55static int fd_atcwd = AT_FDCWD;
56
57static struct test_case {
58 int *dirfd;
59 const char *name;
60 int exp_ret;
61 int exp_errno;
62} test_cases[] = {
63 {&dirfd, testfile, 0, 0},
64 {&dirfd, testfile3, 0, 0},
65 {&fd, testfile2, -1, ENOTDIR},
66 {&fd_invalid, testfile, -1, EBADF},
67 {&fd_atcwd, testfile, 0, 0}
68};
vapierd780ff62006-08-24 00:23:08 +000069
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020070char *TCID = "mknodat01";
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010071int TST_TOTAL = ARRAY_SIZE(test_cases);
Wanlong Gao354ebb42012-12-07 10:10:04 +080072
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +010073static dev_t dev;
74
75static void verify_mknodat(struct test_case *test)
76{
77 TEST(mknodat(*test->dirfd, test->name, S_IFREG, dev));
78
79 if (TEST_RETURN != test->exp_ret) {
80 tst_resm(TFAIL | TTERRNO,
81 "mknodat() returned %ld, expected %d",
82 TEST_RETURN, test->exp_ret);
83 return;
84 }
85
86 if (TEST_ERRNO != test->exp_errno) {
87 tst_resm(TFAIL | TTERRNO,
88 "mknodat() returned wrong errno, expected %d",
89 test->exp_errno);
90 return;
91 }
92
93 tst_resm(TPASS | TTERRNO, "mknodat() returned %ld", TEST_RETURN);
94}
vapierd780ff62006-08-24 00:23:08 +000095
vapierd780ff62006-08-24 00:23:08 +000096int main(int ac, char **av)
97{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020098 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020099 const char *msg;
vapierd780ff62006-08-24 00:23:08 +0000100 int i;
101
Garrett Cooper45e285d2010-11-22 12:19:25 -0800102 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800103 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd780ff62006-08-24 00:23:08 +0000104
vapierd780ff62006-08-24 00:23:08 +0000105 setup();
106
vapierd780ff62006-08-24 00:23:08 +0000107 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800108 tst_count = 0;
vapierd780ff62006-08-24 00:23:08 +0000109
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100110 for (i = 0; i < TST_TOTAL; i++)
111 verify_mknodat(test_cases + i);
yaberauneya13ace442009-11-20 07:07:04 +0000112
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100113 /* clean created nodes before next run */
114 clean();
Garrett Cooper2c282152010-12-16 00:55:50 -0800115 }
vapierd780ff62006-08-24 00:23:08 +0000116
vapierd780ff62006-08-24 00:23:08 +0000117 cleanup();
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100118 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800119}
vapierd780ff62006-08-24 00:23:08 +0000120
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100121static void setup(void)
vapierd780ff62006-08-24 00:23:08 +0000122{
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100123 char *tmpdir;
vapierd780ff62006-08-24 00:23:08 +0000124
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100125 if (tst_kvercmp(2, 6, 16) < 0)
126 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
vapierd780ff62006-08-24 00:23:08 +0000127
vapierd780ff62006-08-24 00:23:08 +0000128 tst_sig(NOFORK, DEF_HANDLER, cleanup);
129
vapierd780ff62006-08-24 00:23:08 +0000130 TEST_PAUSE;
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100131
132 tst_tmpdir();
133
134 /* Initialize test dir and file names */
Cyril Hrubis9c31ad22014-05-14 17:15:39 +0200135 tmpdir = tst_get_tmpdir();
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100136 sprintf(testfilepath, PATHNAME"/mknodattestfile%d", getpid());
137 sprintf(testfile, "mknodattestfile%d", getpid());
138 sprintf(testfile2, "mknodattestfile2%d", getpid());
139 sprintf(testfile3, "%s/mknodattestfile3%d", tmpdir, getpid());
140 free(tmpdir);
141
142 SAFE_MKDIR(cleanup, PATHNAME, 0700);
143
144 dirfd = SAFE_OPEN(cleanup, PATHNAME, O_DIRECTORY);
145 fd = SAFE_OPEN(cleanup, testfile2, O_CREAT | O_RDWR, 0600);
Garrett Cooper2c282152010-12-16 00:55:50 -0800146}
vapierd780ff62006-08-24 00:23:08 +0000147
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100148static void clean(void)
vapierd780ff62006-08-24 00:23:08 +0000149{
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100150 SAFE_UNLINK(cleanup, testfilepath);
151 SAFE_UNLINK(cleanup, testfile3);
152 SAFE_UNLINK(cleanup, testfile);
153}
vapierd780ff62006-08-24 00:23:08 +0000154
Cyril Hrubisbc63a8f2014-03-20 19:36:47 +0100155static void cleanup(void)
156{
157 if (dirfd > 0 && close(dirfd))
158 tst_resm(TWARN | TERRNO, "Failed to close(dirfd)");
159
160 if (fd > 0 && close(fd))
161 tst_resm(TWARN | TERRNO, "Failed to close(fd)");
162
163 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700164}