vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 18 | * |
| 19 | * NAME |
| 20 | * unlinkat01.c |
| 21 | * |
| 22 | * DESCRIPTION |
| 23 | * This test case will verify basic function of unlinkat |
| 24 | * added by kernel 2.6.16 or up. |
| 25 | * |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 26 | * Author |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * Yi Yang <yyangcdl@cn.ibm.com> |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 28 | * |
| 29 | * History |
| 30 | * 08/24/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com> |
| 31 | * |
| 32 | *****************************************************************************/ |
| 33 | |
| 34 | #define _GNU_SOURCE |
| 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <error.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <errno.h> |
| 43 | #include <string.h> |
| 44 | #include <signal.h> |
| 45 | #include "test.h" |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 46 | #include "linux_syscall_numbers.h" |
| 47 | |
| 48 | #define TEST_CASES 7 |
| 49 | #ifndef AT_FDCWD |
| 50 | #define AT_FDCWD -100 |
| 51 | #endif |
| 52 | #ifndef AT_REMOVEDIR |
| 53 | #define AT_REMOVEDIR 0x200 |
| 54 | #endif |
| 55 | |
| 56 | void setup(); |
| 57 | void cleanup(); |
| 58 | void setup_every_copy(); |
| 59 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 60 | char *TCID = "unlinkat01"; |
| 61 | int TST_TOTAL = TEST_CASES; |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 62 | char pathname[256]; |
| 63 | char subpathname[256]; |
| 64 | char testfile[256]; |
| 65 | char testfile2[256]; |
| 66 | char testfile3[256]; |
| 67 | int dirfd, fd, ret; |
| 68 | int fds[TEST_CASES]; |
| 69 | char *filenames[TEST_CASES]; |
| 70 | int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 }; |
| 71 | int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR }; |
| 72 | |
| 73 | int myunlinkat(int dirfd, const char *filename, int flags) |
| 74 | { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 75 | return ltp_syscall(__NR_unlinkat, dirfd, filename, flags); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int main(int ac, char **av) |
| 79 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 82 | int i; |
| 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | /* Disable test if the version of the kernel is less than 2.6.16 */ |
| 85 | if ((tst_kvercmp(2, 6, 16)) < 0) { |
| 86 | tst_resm(TWARN, "This test can only run on kernels that are "); |
| 87 | tst_resm(TWARN, "2.6.16 and higher"); |
| 88 | exit(0); |
| 89 | } |
mreed10 | 807cfe5 | 2006-09-18 19:03:19 +0000 | [diff] [blame] | 90 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 91 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 92 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 93 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 94 | setup(); |
| 95 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 96 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 97 | setup_every_copy(); |
| 98 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 99 | tst_count = 0; |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 100 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 101 | for (i = 0; i < TST_TOTAL; i++) { |
| 102 | TEST(myunlinkat(fds[i], filenames[i], flags[i])); |
| 103 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 104 | if (TEST_ERRNO == expected_errno[i]) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 105 | tst_resm(TPASS, |
| 106 | "unlinkat() returned the expected errno %d: %s", |
| 107 | TEST_ERRNO, strerror(TEST_ERRNO)); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 108 | } else { |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 109 | tst_resm(TFAIL, |
| 110 | "unlinkat() Failed, errno=%d : %s", |
| 111 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 112 | } |
| 113 | } |
| 114 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 115 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 116 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 117 | cleanup(); |
Cyril Hrubis | 4dff854 | 2014-09-23 12:11:59 +0200 | [diff] [blame] | 118 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 120 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 121 | void setup_every_copy(void) |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 122 | { |
| 123 | /* Initialize test dir and file names */ |
| 124 | char tmppathname[256] = ""; |
| 125 | |
| 126 | sprintf(pathname, "unlinkattestdir%d", getpid()); |
| 127 | sprintf(subpathname, "unlinkatsubtestdir%d", getpid()); |
| 128 | sprintf(testfile, "unlinkattestfile%d.txt", getpid()); |
| 129 | sprintf(testfile2, "unlinkattestdir%d/unlinkattestfile%d.txt", getpid(), |
| 130 | getpid()); |
| 131 | sprintf(testfile3, "/tmp/unlinkattestfile%d.txt", getpid()); |
| 132 | |
| 133 | ret = mkdir(pathname, 0700); |
| 134 | if (ret < 0) { |
| 135 | perror("mkdir: "); |
| 136 | exit(-1); |
| 137 | } |
| 138 | |
| 139 | strcat(strcat(strcat(tmppathname, pathname), "/"), subpathname); |
| 140 | |
| 141 | ret = mkdir(tmppathname, 0700); |
| 142 | if (ret < 0) { |
| 143 | perror("mkdir: "); |
| 144 | exit(-1); |
| 145 | } |
| 146 | |
| 147 | dirfd = open(pathname, O_DIRECTORY); |
| 148 | if (dirfd < 0) { |
| 149 | perror("open: "); |
| 150 | exit(-1); |
| 151 | } |
| 152 | |
| 153 | fd = open(testfile, O_CREAT | O_RDWR, 0600); |
| 154 | if (fd < 0) { |
| 155 | perror("open: "); |
| 156 | exit(-1); |
| 157 | } |
| 158 | |
| 159 | fd = open(testfile2, O_CREAT | O_RDWR, 0600); |
| 160 | if (fd < 0) { |
| 161 | perror("open: "); |
| 162 | exit(-1); |
| 163 | } |
| 164 | |
| 165 | fd = open(testfile3, O_CREAT | O_RDWR, 0600); |
| 166 | if (fd < 0) { |
| 167 | perror("open: "); |
| 168 | exit(-1); |
| 169 | } |
| 170 | |
| 171 | fds[0] = fds[1] = fds[4] = fds[6] = dirfd; |
| 172 | fds[2] = fd; |
| 173 | fds[3] = 100; |
| 174 | fds[5] = AT_FDCWD; |
| 175 | |
| 176 | filenames[0] = filenames[2] = filenames[3] = filenames[4] = |
| 177 | filenames[5] = testfile; |
| 178 | filenames[1] = testfile3; |
| 179 | filenames[6] = subpathname; |
| 180 | } |
| 181 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 182 | void setup(void) |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 183 | { |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 184 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 185 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 187 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 188 | void cleanup(void) |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 189 | { |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 190 | char tmppathname[256] = ""; |
| 191 | strcat(strcat(strcat(tmppathname, pathname), "/"), subpathname); |
| 192 | rmdir(tmppathname); |
| 193 | unlink(testfile2); |
| 194 | unlink(testfile3); |
| 195 | unlink(testfile); |
| 196 | rmdir(pathname); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 197 | } |