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 | * |
| 26 | * USAGE: <for command-line> |
| 27 | * unlinkat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p] |
| 28 | * where: |
| 29 | * -c n : Run n copies simultaneously. |
| 30 | * -e : Turn on errno logging. |
| 31 | * -i n : Execute test n times. |
| 32 | * -I x : Execute test for x seconds. |
| 33 | * -p : Pause for SIGUSR1 before starting |
| 34 | * -P x : Pause for x seconds between iterations. |
| 35 | * -t : Turn on syscall timing. |
| 36 | * |
| 37 | * Author |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 38 | * Yi Yang <yyangcdl@cn.ibm.com> |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 39 | * |
| 40 | * History |
| 41 | * 08/24/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com> |
| 42 | * |
| 43 | *****************************************************************************/ |
| 44 | |
| 45 | #define _GNU_SOURCE |
| 46 | |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <sys/time.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <error.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <errno.h> |
| 54 | #include <string.h> |
| 55 | #include <signal.h> |
| 56 | #include "test.h" |
| 57 | #include "usctest.h" |
| 58 | #include "linux_syscall_numbers.h" |
| 59 | |
| 60 | #define TEST_CASES 7 |
| 61 | #ifndef AT_FDCWD |
| 62 | #define AT_FDCWD -100 |
| 63 | #endif |
| 64 | #ifndef AT_REMOVEDIR |
| 65 | #define AT_REMOVEDIR 0x200 |
| 66 | #endif |
| 67 | |
| 68 | void setup(); |
| 69 | void cleanup(); |
| 70 | void setup_every_copy(); |
| 71 | |
| 72 | char *TCID = "unlinkat01"; /* Test program identifier. */ |
| 73 | int TST_TOTAL = TEST_CASES; /* Total number of test cases. */ |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 74 | char pathname[256]; |
| 75 | char subpathname[256]; |
| 76 | char testfile[256]; |
| 77 | char testfile2[256]; |
| 78 | char testfile3[256]; |
| 79 | int dirfd, fd, ret; |
| 80 | int fds[TEST_CASES]; |
| 81 | char *filenames[TEST_CASES]; |
| 82 | int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 }; |
| 83 | int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR }; |
| 84 | |
| 85 | int myunlinkat(int dirfd, const char *filename, int flags) |
| 86 | { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 87 | return ltp_syscall(__NR_unlinkat, dirfd, filename, flags); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int main(int ac, char **av) |
| 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
| 93 | char *msg; |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 94 | int i; |
| 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | /* Disable test if the version of the kernel is less than 2.6.16 */ |
| 97 | if ((tst_kvercmp(2, 6, 16)) < 0) { |
| 98 | tst_resm(TWARN, "This test can only run on kernels that are "); |
| 99 | tst_resm(TWARN, "2.6.16 and higher"); |
| 100 | exit(0); |
| 101 | } |
mreed10 | 807cfe5 | 2006-09-18 19:03:19 +0000 | [diff] [blame] | 102 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 103 | /*************************************************************** |
| 104 | * parse standard options |
| 105 | ***************************************************************/ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 106 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 107 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 108 | |
| 109 | /*************************************************************** |
| 110 | * perform global setup for test |
| 111 | ***************************************************************/ |
| 112 | setup(); |
| 113 | |
| 114 | /*************************************************************** |
| 115 | * check looping state if -c option given |
| 116 | ***************************************************************/ |
| 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 118 | setup_every_copy(); |
| 119 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 120 | Tst_count = 0; |
| 121 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Call unlinkat |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 124 | */ |
| 125 | for (i = 0; i < TST_TOTAL; i++) { |
| 126 | TEST(myunlinkat(fds[i], filenames[i], flags[i])); |
| 127 | |
| 128 | /* check return code */ |
| 129 | if (TEST_ERRNO == expected_errno[i]) { |
| 130 | |
| 131 | /*************************************************************** |
| 132 | * only perform functional verification if flag set (-f not given) |
| 133 | ***************************************************************/ |
| 134 | if (STD_FUNCTIONAL_TEST) { |
| 135 | /* No Verification test, yet... */ |
| 136 | tst_resm(TPASS, |
| 137 | "unlinkat() returned the expected errno %d: %s", |
| 138 | TEST_ERRNO, |
| 139 | strerror(TEST_ERRNO)); |
| 140 | } |
| 141 | } else { |
| 142 | TEST_ERROR_LOG(TEST_ERRNO); |
| 143 | tst_resm(TFAIL, |
| 144 | "unlinkat() Failed, errno=%d : %s", |
| 145 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 146 | } |
| 147 | } |
| 148 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 150 | |
| 151 | /*************************************************************** |
| 152 | * cleanup and exit |
| 153 | ***************************************************************/ |
| 154 | cleanup(); |
| 155 | |
| 156 | return (0); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 158 | |
| 159 | void setup_every_copy() |
| 160 | { |
| 161 | /* Initialize test dir and file names */ |
| 162 | char tmppathname[256] = ""; |
| 163 | |
| 164 | sprintf(pathname, "unlinkattestdir%d", getpid()); |
| 165 | sprintf(subpathname, "unlinkatsubtestdir%d", getpid()); |
| 166 | sprintf(testfile, "unlinkattestfile%d.txt", getpid()); |
| 167 | sprintf(testfile2, "unlinkattestdir%d/unlinkattestfile%d.txt", getpid(), |
| 168 | getpid()); |
| 169 | sprintf(testfile3, "/tmp/unlinkattestfile%d.txt", getpid()); |
| 170 | |
| 171 | ret = mkdir(pathname, 0700); |
| 172 | if (ret < 0) { |
| 173 | perror("mkdir: "); |
| 174 | exit(-1); |
| 175 | } |
| 176 | |
| 177 | strcat(strcat(strcat(tmppathname, pathname), "/"), subpathname); |
| 178 | |
| 179 | ret = mkdir(tmppathname, 0700); |
| 180 | if (ret < 0) { |
| 181 | perror("mkdir: "); |
| 182 | exit(-1); |
| 183 | } |
| 184 | |
| 185 | dirfd = open(pathname, O_DIRECTORY); |
| 186 | if (dirfd < 0) { |
| 187 | perror("open: "); |
| 188 | exit(-1); |
| 189 | } |
| 190 | |
| 191 | fd = open(testfile, O_CREAT | O_RDWR, 0600); |
| 192 | if (fd < 0) { |
| 193 | perror("open: "); |
| 194 | exit(-1); |
| 195 | } |
| 196 | |
| 197 | fd = open(testfile2, O_CREAT | O_RDWR, 0600); |
| 198 | if (fd < 0) { |
| 199 | perror("open: "); |
| 200 | exit(-1); |
| 201 | } |
| 202 | |
| 203 | fd = open(testfile3, O_CREAT | O_RDWR, 0600); |
| 204 | if (fd < 0) { |
| 205 | perror("open: "); |
| 206 | exit(-1); |
| 207 | } |
| 208 | |
| 209 | fds[0] = fds[1] = fds[4] = fds[6] = dirfd; |
| 210 | fds[2] = fd; |
| 211 | fds[3] = 100; |
| 212 | fds[5] = AT_FDCWD; |
| 213 | |
| 214 | filenames[0] = filenames[2] = filenames[3] = filenames[4] = |
| 215 | filenames[5] = testfile; |
| 216 | filenames[1] = testfile3; |
| 217 | filenames[6] = subpathname; |
| 218 | } |
| 219 | |
| 220 | /*************************************************************** |
| 221 | * setup() - performs all ONE TIME setup for this test. |
| 222 | ***************************************************************/ |
| 223 | void setup() |
| 224 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 225 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 226 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 227 | |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 228 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 229 | } |
vapier | 3688c12 | 2006-09-10 09:55:18 +0000 | [diff] [blame] | 230 | |
| 231 | /*************************************************************** |
| 232 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 233 | * completion or premature exit. |
| 234 | ***************************************************************/ |
| 235 | void cleanup() |
| 236 | { |
| 237 | /* Remove them */ |
| 238 | char tmppathname[256] = ""; |
| 239 | strcat(strcat(strcat(tmppathname, pathname), "/"), subpathname); |
| 240 | rmdir(tmppathname); |
| 241 | unlink(testfile2); |
| 242 | unlink(testfile3); |
| 243 | unlink(testfile); |
| 244 | rmdir(pathname); |
| 245 | |
| 246 | /* |
| 247 | * print timing stats if that option was specified. |
| 248 | * print errno log if that option was specified. |
| 249 | */ |
| 250 | TEST_CLEANUP; |
| 251 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 252 | } |