blob: 3bbc14fb9b184e82756e81eba1e35078c9b04a1e [file] [log] [blame]
vapier3688c122006-09-10 09:55:18 +00001/******************************************************************************
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
vapier3688c122006-09-10 09:55:18 +000018 *
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_modak4bb656a2009-02-26 12:02:09 +000038 * Yi Yang <yyangcdl@cn.ibm.com>
vapier3688c122006-09-10 09:55:18 +000039 *
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
68void setup();
69void cleanup();
70void setup_every_copy();
71
72char *TCID = "unlinkat01"; /* Test program identifier. */
73int TST_TOTAL = TEST_CASES; /* Total number of test cases. */
vapier3688c122006-09-10 09:55:18 +000074char pathname[256];
75char subpathname[256];
76char testfile[256];
77char testfile2[256];
78char testfile3[256];
79int dirfd, fd, ret;
80int fds[TEST_CASES];
81char *filenames[TEST_CASES];
82int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 };
83int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR };
84
85int myunlinkat(int dirfd, const char *filename, int flags)
86{
Jan Stancek359980f2013-02-15 10:16:05 +010087 return ltp_syscall(__NR_unlinkat, dirfd, filename, flags);
vapier3688c122006-09-10 09:55:18 +000088}
89
90int main(int ac, char **av)
91{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020092 int lc;
93 char *msg;
vapier3688c122006-09-10 09:55:18 +000094 int i;
95
subrata_modak56207ce2009-03-23 13:35:39 +000096 /* 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 }
mreed10807cfe52006-09-18 19:03:19 +0000102
vapier3688c122006-09-10 09:55:18 +0000103 /***************************************************************
104 * parse standard options
105 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800106 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier3688c122006-09-10 09:55:18 +0000108
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
vapier3688c122006-09-10 09:55:18 +0000120 Tst_count = 0;
121
subrata_modak4bb656a2009-02-26 12:02:09 +0000122 /*
123 * Call unlinkat
vapier3688c122006-09-10 09:55:18 +0000124 */
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 Cooper2c282152010-12-16 00:55:50 -0800149 }
vapier3688c122006-09-10 09:55:18 +0000150
151 /***************************************************************
152 * cleanup and exit
153 ***************************************************************/
154 cleanup();
155
156 return (0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800157}
vapier3688c122006-09-10 09:55:18 +0000158
159void 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 ***************************************************************/
223void setup()
224{
Garrett Cooper2c282152010-12-16 00:55:50 -0800225
vapier3688c122006-09-10 09:55:18 +0000226 tst_sig(NOFORK, DEF_HANDLER, cleanup);
227
vapier3688c122006-09-10 09:55:18 +0000228 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800229}
vapier3688c122006-09-10 09:55:18 +0000230
231/***************************************************************
232 * cleanup() - performs all ONE TIME cleanup for this test at
233 * completion or premature exit.
234 ***************************************************************/
235void 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 Dearmanec6edca2012-10-17 19:54:01 -0700252}