blob: ab255aa2c757616024abc3bae3b26f54716be177 [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 *
vapier3688c122006-09-10 09:55:18 +000026 * Author
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * Yi Yang <yyangcdl@cn.ibm.com>
vapier3688c122006-09-10 09:55:18 +000028 *
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"
vapier3688c122006-09-10 09:55:18 +000046#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
56void setup();
57void cleanup();
58void setup_every_copy();
59
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020060char *TCID = "unlinkat01";
61int TST_TOTAL = TEST_CASES;
vapier3688c122006-09-10 09:55:18 +000062char pathname[256];
63char subpathname[256];
64char testfile[256];
65char testfile2[256];
66char testfile3[256];
67int dirfd, fd, ret;
68int fds[TEST_CASES];
69char *filenames[TEST_CASES];
70int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0, 0 };
71int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0, AT_REMOVEDIR };
72
73int myunlinkat(int dirfd, const char *filename, int flags)
74{
Jan Stancek359980f2013-02-15 10:16:05 +010075 return ltp_syscall(__NR_unlinkat, dirfd, filename, flags);
vapier3688c122006-09-10 09:55:18 +000076}
77
78int main(int ac, char **av)
79{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020080 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020081 const char *msg;
vapier3688c122006-09-10 09:55:18 +000082 int i;
83
subrata_modak56207ce2009-03-23 13:35:39 +000084 /* 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 }
mreed10807cfe52006-09-18 19:03:19 +000090
Garrett Cooper45e285d2010-11-22 12:19:25 -080091 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080092 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier3688c122006-09-10 09:55:18 +000093
vapier3688c122006-09-10 09:55:18 +000094 setup();
95
vapier3688c122006-09-10 09:55:18 +000096 for (lc = 0; TEST_LOOPING(lc); lc++) {
97 setup_every_copy();
98
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 tst_count = 0;
vapier3688c122006-09-10 09:55:18 +0000100
vapier3688c122006-09-10 09:55:18 +0000101 for (i = 0; i < TST_TOTAL; i++) {
102 TEST(myunlinkat(fds[i], filenames[i], flags[i]));
103
vapier3688c122006-09-10 09:55:18 +0000104 if (TEST_ERRNO == expected_errno[i]) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200105 tst_resm(TPASS,
106 "unlinkat() returned the expected errno %d: %s",
107 TEST_ERRNO, strerror(TEST_ERRNO));
vapier3688c122006-09-10 09:55:18 +0000108 } else {
vapier3688c122006-09-10 09:55:18 +0000109 tst_resm(TFAIL,
110 "unlinkat() Failed, errno=%d : %s",
111 TEST_ERRNO, strerror(TEST_ERRNO));
112 }
113 }
114
Garrett Cooper2c282152010-12-16 00:55:50 -0800115 }
vapier3688c122006-09-10 09:55:18 +0000116
vapier3688c122006-09-10 09:55:18 +0000117 cleanup();
Cyril Hrubis4dff8542014-09-23 12:11:59 +0200118 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800119}
vapier3688c122006-09-10 09:55:18 +0000120
Mike Frysingerc57fba52014-04-09 18:56:30 -0400121void setup_every_copy(void)
vapier3688c122006-09-10 09:55:18 +0000122{
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 Frysingerc57fba52014-04-09 18:56:30 -0400182void setup(void)
vapier3688c122006-09-10 09:55:18 +0000183{
vapier3688c122006-09-10 09:55:18 +0000184 tst_sig(NOFORK, DEF_HANDLER, cleanup);
vapier3688c122006-09-10 09:55:18 +0000185 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800186}
vapier3688c122006-09-10 09:55:18 +0000187
Mike Frysingerc57fba52014-04-09 18:56:30 -0400188void cleanup(void)
vapier3688c122006-09-10 09:55:18 +0000189{
vapier3688c122006-09-10 09:55:18 +0000190 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 Dearmanec6edca2012-10-17 19:54:01 -0700197}