blob: 15e584f6725fce2ee22e4bd7ee7bc3402926d460 [file] [log] [blame]
vapieraab2ac02006-09-10 09:59:08 +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
vapieraab2ac02006-09-10 09:59:08 +000018 *
19 * NAME
20 * linkat01.c
21 *
22 * DESCRIPTION
23 * This test case will verify basic function of linkat
24 * added by kernel 2.6.16 or up.
25 *
26 * USAGE: <for command-line>
27 * linkat01 [-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>
vapieraab2ac02006-09-10 09:59:08 +000039 *
40 * History
41 * 08/25/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>
yaberauneyab15e55e2009-11-27 07:43:12 +000051#include <unistd.h>
vapieraab2ac02006-09-10 09:59:08 +000052#include <error.h>
53#include <stdlib.h>
54#include <errno.h>
55#include <string.h>
56#include <signal.h>
yaberauneyab15e55e2009-11-27 07:43:12 +000057#include <inttypes.h>
58#include <limits.h>
vapieraab2ac02006-09-10 09:59:08 +000059#include "test.h"
60#include "usctest.h"
61#include "linux_syscall_numbers.h"
Garrett Cooper0f66fc02010-12-21 05:38:11 -080062#include "rmobj.h"
63#include "safe_macros.h"
vapieraab2ac02006-09-10 09:59:08 +000064
vapieraab2ac02006-09-10 09:59:08 +000065#ifndef AT_FDCWD
66#define AT_FDCWD -100
67#endif
subrata_modak421ddee2009-04-28 07:10:21 +000068#ifndef AT_SYMLINK_FOLLOW
69#define AT_SYMLINK_FOLLOW 0x400
vapieraab2ac02006-09-10 09:59:08 +000070#endif
71
subrata_modak421ddee2009-04-28 07:10:21 +000072struct test_struct;
73static void setup();
74static void cleanup();
75static void setup_every_copy();
Wanlong Gao354ebb42012-12-07 10:10:04 +080076static void mylinkat_test(struct test_struct *desc);
subrata_modak421ddee2009-04-28 07:10:21 +000077
78#define TEST_DIR1 "olddir"
79#define TEST_DIR2 "newdir"
80#define TEST_DIR3 "deldir"
81#define TEST_FILE1 "oldfile"
82#define TEST_FILE2 "newfile"
83#define TEST_FIFO "fifo"
84
yaberauneyab15e55e2009-11-27 07:43:12 +000085#define DPATHNAME_FMT "%s/" TEST_DIR2 "/" TEST_FILE1
86#define SPATHNAME_FMT "%s/" TEST_DIR1 "/" TEST_FILE1
87
88static char dpathname[PATH_MAX];
89static char spathname[PATH_MAX];
Wanlong Gao354ebb42012-12-07 10:10:04 +080090static int olddirfd, newdirfd = -1, cwd_fd = AT_FDCWD, stdinfd = 0, badfd =
91 -1, deldirfd;
subrata_modak421ddee2009-04-28 07:10:21 +000092
93struct test_struct {
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 int *oldfd;
95 const char *oldfn;
96 int *newfd;
97 const char *newfn;
subrata_modak421ddee2009-04-28 07:10:21 +000098 int flags;
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 const char *referencefn1;
100 const char *referencefn2;
subrata_modak421ddee2009-04-28 07:10:21 +0000101 int expected_errno;
yaberauneyab15e55e2009-11-27 07:43:12 +0000102} test_desc[] = {
103 /* 1. relative paths */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 {
105 &olddirfd, TEST_FILE1, &newdirfd, TEST_FILE1, 0,
106 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
107 /* 2. abs path at source */
108 {
109 &olddirfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
110 /* 3. abs path at dst */
111 {
112 &olddirfd, TEST_FILE1, &newdirfd, dpathname, 0,
113 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
114 /* 4. relative paths to cwd */
115 {
116 &cwd_fd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
117 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
118 /* 5. relative paths to cwd */
119 {
120 &olddirfd, TEST_FILE1, &cwd_fd, TEST_DIR2 "/" TEST_FILE1, 0,
121 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
122 /* 6. abs path at source */
123 {
124 &cwd_fd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
125 /* 7. abs path at dst */
126 {
127 &olddirfd, TEST_FILE1, &cwd_fd, dpathname, 0,
128 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
129 /* 8. relative paths to invalid */
130 {
131 &stdinfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
132 0, 0, ENOTDIR},
133 /* 9. relative paths to invalid */
134 {
135 &olddirfd, TEST_FILE1, &stdinfd, TEST_DIR2 "/" TEST_FILE1, 0,
136 0, 0, ENOTDIR},
137 /* 10. abs path at source */
138 {
139 &stdinfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
140 /* 11. abs path at dst */
141 {
142 &olddirfd, TEST_FILE1, &stdinfd, dpathname, 0,
143 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
144 /* 12. relative paths to bad */
145 {
146 &badfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
147 0, 0, EBADF},
148 /* 13. relative paths to bad */
149 {
150 &olddirfd, TEST_FILE1, &badfd, TEST_DIR2 "/" TEST_FILE1, 0,
151 0, 0, EBADF},
152 /* 14. abs path at source */
153 {
154 &badfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
155 /* 15. abs path at dst */
156 {
157 &olddirfd, TEST_FILE1, &badfd, dpathname, 0,
158 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
159 /* 16. relative paths to deleted */
160 {
161 &deldirfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
162 0, 0, ENOENT},
163 /* 17. relative paths to deleted */
164 {
165 &olddirfd, TEST_FILE1, &deldirfd, TEST_DIR2 "/" TEST_FILE1, 0,
166 0, 0, ENOENT},
167 /* 18. abs path at source */
168 {
169 &deldirfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
170 /* 19. abs path at dst */
171 {
172 &olddirfd, TEST_FILE1, &deldirfd, dpathname, 0,
173 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
174 /* 20. x-device link */
175 {
176 &cwd_fd, "/proc/cpuinfo", &newdirfd, TEST_FILE1, 0, 0, 0, EXDEV},
177 /* 21. directory link */
178 {
179 &olddirfd, ".", &newdirfd, TEST_FILE1, 0, 0, 0, EPERM},
180 /* 22. invalid flag */
181 {
182 &olddirfd, TEST_FILE1, &newdirfd, TEST_FILE1, 1, 0, 0, EINVAL},
183 /* 23. fifo link */
184 /* XXX (garrcoop): Removed because it hangs the overall test. Need to
185 * find a legitimate means to exercise this functionality, if in fact
186 * it's a valid testcase -- which it should be.
187 */
188 /* { &olddirfd, TEST_FIFO, &newdirfd, TEST_FILE1, 0,
189 TEST_DIR1"/"TEST_FIFO, TEST_DIR2"/"TEST_FILE1, 0 } */
subrata_modak421ddee2009-04-28 07:10:21 +0000190};
vapieraab2ac02006-09-10 09:59:08 +0000191
yaberauneyab15e55e2009-11-27 07:43:12 +0000192char *TCID = "linkat01";
yaberauneyab15e55e2009-11-27 07:43:12 +0000193int TST_TOTAL = sizeof(test_desc) / sizeof(*test_desc);
vapieraab2ac02006-09-10 09:59:08 +0000194
yaberauneyab15e55e2009-11-27 07:43:12 +0000195#define SUCCEED_OR_DIE(syscall, message, ...) \
196 (errno = 0, \
197 ({int ret=syscall(__VA_ARGS__); \
198 if (ret==-1) \
199 tst_brkm(TBROK | TERRNO, cleanup, \
200 message, __VA_ARGS__); \
201 ret; }))
subrata_modak421ddee2009-04-28 07:10:21 +0000202
yaberauneyab15e55e2009-11-27 07:43:12 +0000203static int mylinkat(int olddirfd, const char *oldfilename, int newdirfd,
204 const char *newfilename, int flags)
vapieraab2ac02006-09-10 09:59:08 +0000205{
Jan Stancek359980f2013-02-15 10:16:05 +0100206 return ltp_syscall(__NR_linkat, olddirfd, oldfilename, newdirfd,
vapieraab2ac02006-09-10 09:59:08 +0000207 newfilename, flags);
208}
209
210int main(int ac, char **av)
211{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200212 int lc;
213 char *msg;
vapieraab2ac02006-09-10 09:59:08 +0000214 int i;
215
subrata_modak56207ce2009-03-23 13:35:39 +0000216 if ((tst_kvercmp(2, 6, 16)) < 0) {
217 tst_resm(TWARN, "This test can only run on kernels that are ");
218 tst_resm(TWARN, "2.6.16 and higher");
219 exit(0);
220 }
mreed10807cfe52006-09-18 19:03:19 +0000221
Garrett Cooper45e285d2010-11-22 12:19:25 -0800222 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800223 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapieraab2ac02006-09-10 09:59:08 +0000224
vapieraab2ac02006-09-10 09:59:08 +0000225 setup();
226
vapieraab2ac02006-09-10 09:59:08 +0000227 for (lc = 0; TEST_LOOPING(lc); lc++) {
vapieraab2ac02006-09-10 09:59:08 +0000228
vapieraab2ac02006-09-10 09:59:08 +0000229 Tst_count = 0;
230
vapieraab2ac02006-09-10 09:59:08 +0000231 for (i = 0; i < TST_TOTAL; i++) {
subrata_modak421ddee2009-04-28 07:10:21 +0000232 setup_every_copy();
233 mylinkat_test(&test_desc[i]);
vapieraab2ac02006-09-10 09:59:08 +0000234 }
235
Garrett Cooper2c282152010-12-16 00:55:50 -0800236 }
vapieraab2ac02006-09-10 09:59:08 +0000237
vapieraab2ac02006-09-10 09:59:08 +0000238 cleanup();
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800239 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800240}
vapieraab2ac02006-09-10 09:59:08 +0000241
subrata_modak421ddee2009-04-28 07:10:21 +0000242static void setup_every_copy()
vapieraab2ac02006-09-10 09:59:08 +0000243{
subrata_modak421ddee2009-04-28 07:10:21 +0000244 close(newdirfd);
245 rmobj(TEST_DIR2, NULL);
vapieraab2ac02006-09-10 09:59:08 +0000246
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR2, 0700);
yaberauneyab15e55e2009-11-27 07:43:12 +0000248 newdirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800249 TEST_DIR2, O_DIRECTORY);
vapieraab2ac02006-09-10 09:59:08 +0000250}
251
Wanlong Gao354ebb42012-12-07 10:10:04 +0800252static void mylinkat_test(struct test_struct *desc)
vapieraab2ac02006-09-10 09:59:08 +0000253{
subrata_modak421ddee2009-04-28 07:10:21 +0000254 int fd;
vapieraab2ac02006-09-10 09:59:08 +0000255
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 TEST(mylinkat
257 (*desc->oldfd, desc->oldfn, *desc->newfd, desc->newfn,
258 desc->flags));
vapieraab2ac02006-09-10 09:59:08 +0000259
subrata_modak421ddee2009-04-28 07:10:21 +0000260 if (TEST_ERRNO == desc->expected_errno) {
vapieraab2ac02006-09-10 09:59:08 +0000261
subrata_modak421ddee2009-04-28 07:10:21 +0000262 if (STD_FUNCTIONAL_TEST) {
vapieraab2ac02006-09-10 09:59:08 +0000263
subrata_modak421ddee2009-04-28 07:10:21 +0000264 if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800265 int tnum = rand(), vnum = ~tnum;
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800266 fd = SAFE_OPEN(cleanup, desc->referencefn1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800267 O_RDWR);
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800268 SAFE_WRITE(cleanup, 1, fd, &tnum, sizeof(tnum));
269 SAFE_CLOSE(cleanup, fd);
subrata_modak421ddee2009-04-28 07:10:21 +0000270
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800271 fd = SAFE_OPEN(cleanup, desc->referencefn2,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800272 O_RDONLY);
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800273 SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum));
274 SAFE_CLOSE(cleanup, fd);
subrata_modak421ddee2009-04-28 07:10:21 +0000275
yaberauneyab15e55e2009-11-27 07:43:12 +0000276 if (tnum == vnum)
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800277 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800278 "linkat is functionality correct");
yaberauneyab15e55e2009-11-27 07:43:12 +0000279 else {
subrata_modak421ddee2009-04-28 07:10:21 +0000280 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 "The link file's content isn't "
282 "as same as the original file's "
283 "although linkat returned 0");
yaberauneyab15e55e2009-11-27 07:43:12 +0000284 }
285 } else {
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800286 if (TEST_RETURN == 0)
287 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800288 "linkat succeeded as expected");
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800289 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800290 tst_resm(TPASS | TTERRNO,
291 "linkat failed as expected");
subrata_modak421ddee2009-04-28 07:10:21 +0000292 }
subrata_modak421ddee2009-04-28 07:10:21 +0000293 } else
294 tst_resm(TPASS, "Test passed");
vapieraab2ac02006-09-10 09:59:08 +0000295 } else {
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800296 if (TEST_RETURN == 0)
297 tst_resm(TFAIL, "linkat succeeded unexpectedly");
298 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800299 tst_resm(TFAIL | TTERRNO,
300 "linkat failed unexpectedly; expected %d - %s",
301 desc->expected_errno,
302 strerror(desc->expected_errno));
vapieraab2ac02006-09-10 09:59:08 +0000303 }
304}
305
yaberauneyab15e55e2009-11-27 07:43:12 +0000306void setup()
vapieraab2ac02006-09-10 09:59:08 +0000307{
yaberauneyab15e55e2009-11-27 07:43:12 +0000308 char *cwd;
vapieraab2ac02006-09-10 09:59:08 +0000309
vapieraab2ac02006-09-10 09:59:08 +0000310 tst_sig(NOFORK, DEF_HANDLER, cleanup);
311
subrata_modak421ddee2009-04-28 07:10:21 +0000312 tst_tmpdir();
313
yaberauneyab15e55e2009-11-27 07:43:12 +0000314 cwd = get_current_dir_name();
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800315 if (cwd == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800316 tst_brkm(TFAIL | TERRNO, cleanup,
317 "Failed to get current working directory");
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800318 else {
subrata_modak421ddee2009-04-28 07:10:21 +0000319
yaberauneyab15e55e2009-11-27 07:43:12 +0000320 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR1, 0700);
321 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR3, 0700);
322 olddirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323 TEST_DIR1, O_DIRECTORY);
yaberauneyab15e55e2009-11-27 07:43:12 +0000324 deldirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800325 TEST_DIR3, O_DIRECTORY);
yaberauneyab15e55e2009-11-27 07:43:12 +0000326 SUCCEED_OR_DIE(rmdir, "rmdir(%s) failed", TEST_DIR3);
327 SUCCEED_OR_DIE(close, "close(%d) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800328 SUCCEED_OR_DIE(open, "open(%s, 0x%x, %o) "
329 "failed",
330 TEST_DIR1 "/" TEST_FILE1,
331 O_CREAT | O_EXCL, 0600));
subrata_modak421ddee2009-04-28 07:10:21 +0000332
yaberauneyab15e55e2009-11-27 07:43:12 +0000333 SUCCEED_OR_DIE(mkfifo, "mkfifo(%s, %o) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800334 TEST_DIR1 "/" TEST_FIFO, 0600);
subrata_modak421ddee2009-04-28 07:10:21 +0000335
yaberauneyab15e55e2009-11-27 07:43:12 +0000336 snprintf(dpathname, sizeof(dpathname), DPATHNAME_FMT, cwd);
337 snprintf(spathname, sizeof(spathname), SPATHNAME_FMT, cwd);
subrata_modak9c199af2009-06-09 17:57:40 +0000338
yaberauneyab15e55e2009-11-27 07:43:12 +0000339 free(cwd);
340
yaberauneyab15e55e2009-11-27 07:43:12 +0000341 TEST_PAUSE;
342
343 }
344
Garrett Cooper2c282152010-12-16 00:55:50 -0800345}
vapieraab2ac02006-09-10 09:59:08 +0000346
yaberauneyab15e55e2009-11-27 07:43:12 +0000347static void cleanup(void)
vapieraab2ac02006-09-10 09:59:08 +0000348{
subrata_modak421ddee2009-04-28 07:10:21 +0000349 tst_rmdir();
vapieraab2ac02006-09-10 09:59:08 +0000350 TEST_CLEANUP;
Garrett Cooper0f66fc02010-12-21 05:38:11 -0800351}