vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 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> |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 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. |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 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. |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 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 |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 20 | * |
| 21 | * DESCRIPTION |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 22 | * This test case will verify basic function of mknodat |
| 23 | * added by kernel 2.6.16 or up. |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 24 | * |
| 25 | *****************************************************************************/ |
| 26 | |
| 27 | #define _GNU_SOURCE |
| 28 | |
| 29 | #include <sys/types.h> |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
yaberauneya | 13ace44 | 2009-11-20 07:07:04 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 32 | #include <error.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <errno.h> |
| 35 | #include <string.h> |
| 36 | #include <signal.h> |
| 37 | #include "test.h" |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 38 | #include "safe_macros.h" |
Xiaoguang Wang | 7525e62 | 2014-03-04 14:32:00 +0800 | [diff] [blame] | 39 | #include "lapi/fcntl.h" |
| 40 | #include "mknodat.h" |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 42 | #define PATHNAME "mknodattestdir" |
Xiaoguang Wang | 7525e62 | 2014-03-04 14:32:00 +0800 | [diff] [blame] | 43 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 44 | static void setup(void); |
| 45 | static void cleanup(void); |
| 46 | static void clean(void); |
| 47 | |
| 48 | static char testfilepath[256]; |
| 49 | static char testfile[256]; |
| 50 | static char testfile2[256]; |
| 51 | static char testfile3[256]; |
| 52 | |
| 53 | static int dirfd, fd; |
| 54 | static int fd_invalid = 100; |
| 55 | static int fd_atcwd = AT_FDCWD; |
| 56 | |
| 57 | static 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 | }; |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 69 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 70 | char *TCID = "mknodat01"; |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 71 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 72 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 73 | static dev_t dev; |
| 74 | |
| 75 | static 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 | } |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 95 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 96 | int main(int ac, char **av) |
| 97 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 98 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 99 | const char *msg; |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 100 | int i; |
| 101 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 102 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 103 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 104 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 107 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | tst_count = 0; |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 110 | for (i = 0; i < TST_TOTAL; i++) |
| 111 | verify_mknodat(test_cases + i); |
yaberauneya | 13ace44 | 2009-11-20 07:07:04 +0000 | [diff] [blame] | 112 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 113 | /* clean created nodes before next run */ |
| 114 | clean(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 115 | } |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 116 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 117 | cleanup(); |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 118 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | } |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 121 | static void setup(void) |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 122 | { |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 123 | char *tmpdir; |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 124 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 125 | if (tst_kvercmp(2, 6, 16) < 0) |
| 126 | tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer"); |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 127 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 128 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 129 | |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 130 | TEST_PAUSE; |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 131 | |
| 132 | tst_tmpdir(); |
| 133 | |
| 134 | /* Initialize test dir and file names */ |
Cyril Hrubis | 9c31ad2 | 2014-05-14 17:15:39 +0200 | [diff] [blame] | 135 | tmpdir = tst_get_tmpdir(); |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 136 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 147 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 148 | static void clean(void) |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 149 | { |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 150 | SAFE_UNLINK(cleanup, testfilepath); |
| 151 | SAFE_UNLINK(cleanup, testfile3); |
| 152 | SAFE_UNLINK(cleanup, testfile); |
| 153 | } |
vapier | d780ff6 | 2006-08-24 00:23:08 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | bc63a8f | 2014-03-20 19:36:47 +0100 | [diff] [blame] | 155 | static 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 Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 164 | } |