blob: c052fb9b0d849cf4652029cb3109505f1340c6bf [file] [log] [blame]
vapier460557a2006-09-10 10:00:38 +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
vapier460557a2006-09-10 10:00:38 +000018 *
19 * NAME
20 * symlinkat01.c
21 *
22 * DESCRIPTION
23 * This test case will verify basic function of symlinkat
24 * added by kernel 2.6.16 or up.
25 *
26 * USAGE: <for command-line>
27 * symlinkat01 [-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>
vapier460557a2006-09-10 10:00:38 +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>
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"
subrata_modak421ddee2009-04-28 07:10:21 +000058#include "rmobj.h"
vapier460557a2006-09-10 10:00:38 +000059#include "linux_syscall_numbers.h"
60
vapier460557a2006-09-10 10:00:38 +000061#define MYRETCODE -999
62#ifndef AT_FDCWD
63#define AT_FDCWD -100
64#endif
vapier460557a2006-09-10 10:00:38 +000065
subrata_modak421ddee2009-04-28 07:10:21 +000066struct test_struct;
67static void setup();
68static void cleanup();
69static void setup_every_copy();
Wanlong Gao354ebb42012-12-07 10:10:04 +080070static void mysymlinkat_test(struct test_struct *desc);
subrata_modak421ddee2009-04-28 07:10:21 +000071
72#define TEST_DIR1 "olddir"
73#define TEST_DIR2 "newdir"
74#define TEST_DIR3 "deldir"
75#define TEST_FILE1 "oldfile"
76#define TEST_FILE2 "newfile"
77#define TEST_FIFO "fifo"
78
Wanlong Gao354ebb42012-12-07 10:10:04 +080079static char dpathname[256] = "%s/" TEST_DIR2 "/" TEST_FILE1;
80static int olddirfd, newdirfd = -1, cwd_fd = AT_FDCWD, stdinfd = 0, crapfd =
81 -1, deldirfd;
subrata_modak421ddee2009-04-28 07:10:21 +000082
83struct test_struct {
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 const char *oldfn;
85 int *newfd;
86 const char *newfn;
87 const char *referencefn1;
88 const char *referencefn2;
subrata_modak421ddee2009-04-28 07:10:21 +000089 int expected_errno;
Wanlong Gao354ebb42012-12-07 10:10:04 +080090} test_desc[] = {
subrata_modak421ddee2009-04-28 07:10:21 +000091 /* relative paths */
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 {
93 "../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1,
94 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
95 /* abs path at dst */
96 {
97 "../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, dpathname,
98 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
99 /* relative paths to cwd */
100 {
101 "../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd,
102 TEST_DIR2 "/" TEST_FILE1, TEST_DIR1 "/" TEST_FILE1,
103 TEST_DIR2 "/" TEST_FILE1, 0},
104 /* abs path */
105 {
106 "../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd, dpathname,
107 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
108 /* relative paths to invalid */
109 {
110 "../" TEST_DIR1 "/" TEST_FILE1, &stdinfd,
111 TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOTDIR},
112 /* abs path at dst */
113 {
114 "../" TEST_DIR1 "/" TEST_FILE1, &stdinfd, dpathname,
115 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
116 /* relative paths to crap */
117 {
118 "../" TEST_DIR1 "/" TEST_FILE1, &crapfd,
119 TEST_DIR2 "/" TEST_FILE1, 0, 0, EBADF},
120 /* abs path at dst */
121 {
122 "../" TEST_DIR1 "/" TEST_FILE1, &crapfd, dpathname,
123 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
124 /* relative paths to deleted */
125 {
126 "../" TEST_DIR1 "/" TEST_FILE1, &deldirfd,
127 TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOENT},
128 /* abs path at dst */
129 {
130 "../" TEST_DIR1 "/" TEST_FILE1, &deldirfd, dpathname,
131 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
132 /* fifo link */
133 /* { TEST_FIFO, &newdirfd, TEST_FILE1, TEST_DIR1"/"TEST_FIFO, TEST_DIR2"/"TEST_FILE1, 0 }, */
subrata_modak421ddee2009-04-28 07:10:21 +0000134};
vapier460557a2006-09-10 10:00:38 +0000135
136char *TCID = "symlinkat01"; /* Test program identifier. */
subrata_modak421ddee2009-04-28 07:10:21 +0000137int TST_TOTAL = sizeof(test_desc) / sizeof(*test_desc); /* Total number of test cases. */
vapier460557a2006-09-10 10:00:38 +0000138
subrata_modak421ddee2009-04-28 07:10:21 +0000139#define SUCCEED_OR_DIE(syscall, message, ...) \
140 (errno = 0, \
141 ({int ret=syscall(__VA_ARGS__); \
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800142 if (ret==-1) \
subrata_modak421ddee2009-04-28 07:10:21 +0000143 tst_brkm(TBROK, cleanup, message, __VA_ARGS__, strerror(errno)); \
144 ret;}))
145
146static int mysymlinkat(const char *oldfilename,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 int newdirfd, const char *newfilename)
vapier460557a2006-09-10 10:00:38 +0000148{
Jan Stancek359980f2013-02-15 10:16:05 +0100149 return ltp_syscall(__NR_symlinkat, oldfilename, newdirfd, newfilename);
vapier460557a2006-09-10 10:00:38 +0000150}
151
152int main(int ac, char **av)
153{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200154 int lc;
155 char *msg;
vapier460557a2006-09-10 10:00:38 +0000156 int i;
157
subrata_modak56207ce2009-03-23 13:35:39 +0000158 /* Disable test if the version of the kernel is less than 2.6.16 */
159 if ((tst_kvercmp(2, 6, 16)) < 0) {
160 tst_resm(TWARN, "This test can only run on kernels that are ");
161 tst_resm(TWARN, "2.6.16 and higher");
162 exit(0);
163 }
mreed10807cfe52006-09-18 19:03:19 +0000164
vapier460557a2006-09-10 10:00:38 +0000165 /***************************************************************
166 * parse standard options
167 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800168 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800169 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier460557a2006-09-10 10:00:38 +0000170
171 /***************************************************************
172 * perform global setup for test
173 ***************************************************************/
174 setup();
175
176 /***************************************************************
177 * check looping state if -c option given
178 ***************************************************************/
179 for (lc = 0; TEST_LOOPING(lc); lc++) {
vapier460557a2006-09-10 10:00:38 +0000180
vapier460557a2006-09-10 10:00:38 +0000181 Tst_count = 0;
182
subrata_modak4bb656a2009-02-26 12:02:09 +0000183 /*
184 * Call symlinkat
vapier460557a2006-09-10 10:00:38 +0000185 */
186 for (i = 0; i < TST_TOTAL; i++) {
subrata_modak421ddee2009-04-28 07:10:21 +0000187 setup_every_copy();
188 mysymlinkat_test(&test_desc[i]);
vapier460557a2006-09-10 10:00:38 +0000189
vapier460557a2006-09-10 10:00:38 +0000190 }
191
Garrett Cooper2c282152010-12-16 00:55:50 -0800192 }
vapier460557a2006-09-10 10:00:38 +0000193
194 /***************************************************************
195 * cleanup and exit
196 ***************************************************************/
197 cleanup();
198
199 return (0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800200}
vapier460557a2006-09-10 10:00:38 +0000201
subrata_modak421ddee2009-04-28 07:10:21 +0000202static void setup_every_copy()
vapier460557a2006-09-10 10:00:38 +0000203{
subrata_modak421ddee2009-04-28 07:10:21 +0000204 close(newdirfd);
205 rmobj(TEST_DIR2, NULL);
vapier460557a2006-09-10 10:00:38 +0000206
subrata_modak421ddee2009-04-28 07:10:21 +0000207 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed: %s", TEST_DIR2, 0700);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 newdirfd =
209 SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed: %s", TEST_DIR2,
210 O_DIRECTORY);
vapier460557a2006-09-10 10:00:38 +0000211}
212
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213static void mysymlinkat_test(struct test_struct *desc)
vapier460557a2006-09-10 10:00:38 +0000214{
subrata_modak421ddee2009-04-28 07:10:21 +0000215 int fd;
vapier460557a2006-09-10 10:00:38 +0000216
subrata_modak421ddee2009-04-28 07:10:21 +0000217 TEST(mysymlinkat(desc->oldfn, *desc->newfd, desc->newfn));
vapier460557a2006-09-10 10:00:38 +0000218
subrata_modak421ddee2009-04-28 07:10:21 +0000219 /* check return code */
220 if (TEST_ERRNO == desc->expected_errno) {
vapier460557a2006-09-10 10:00:38 +0000221
subrata_modak421ddee2009-04-28 07:10:21 +0000222 /***************************************************************
223 * only perform functional verification if flag set (-f not given)
224 ***************************************************************/
225 if (STD_FUNCTIONAL_TEST) {
226 /* No Verification test, yet... */
vapier460557a2006-09-10 10:00:38 +0000227
subrata_modak421ddee2009-04-28 07:10:21 +0000228 if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800229 int tnum = rand(), vnum = ~tnum;
subrata_modak421ddee2009-04-28 07:10:21 +0000230 int len;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 fd = SUCCEED_OR_DIE(open,
232 "open(%s, 0x%x) failed: %s",
233 desc->referencefn1, O_RDWR);
234 if ((len =
235 write(fd, &tnum,
236 sizeof(tnum))) != sizeof(tnum))
237 tst_brkm(TBROK, cleanup,
238 "write() failed: expected %zu, returned %d; error: %s",
239 sizeof(tnum), len,
240 strerror(errno));
241 SUCCEED_OR_DIE(close, "close(%d) failed: %s",
242 fd);
subrata_modak421ddee2009-04-28 07:10:21 +0000243
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 fd = SUCCEED_OR_DIE(open,
245 "open(%s, 0x%x) failed: %s",
246 desc->referencefn2,
247 O_RDONLY);
248 if ((len =
249 read(fd, &vnum,
250 sizeof(vnum))) != sizeof(tnum))
251 tst_brkm(TBROK, cleanup,
252 "read() failed: expected %zu, returned %d; error: %s",
253 sizeof(vnum), len,
254 strerror(errno));
255 SUCCEED_OR_DIE(close, "close(%d) failed: %s",
256 fd);
subrata_modak421ddee2009-04-28 07:10:21 +0000257
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800258 if (tnum == vnum)
subrata_modak421ddee2009-04-28 07:10:21 +0000259 tst_resm(TPASS, "Test passed");
260 else
261 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 "The link file's content isn't as same as the original file's "
263 "although symlinkat returned 0");
264 } else
subrata_modak421ddee2009-04-28 07:10:21 +0000265 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800266 "symlinkat() returned the expected errno %d: %s",
267 TEST_ERRNO, strerror(TEST_ERRNO));
subrata_modak421ddee2009-04-28 07:10:21 +0000268 } else
269 tst_resm(TPASS, "Test passed");
vapier460557a2006-09-10 10:00:38 +0000270 } else {
subrata_modak421ddee2009-04-28 07:10:21 +0000271 TEST_ERROR_LOG(TEST_ERRNO);
272 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800273 TEST_RETURN ==
274 0 ? "symlinkat() surprisingly succeeded" :
275 "symlinkat() Failed, errno=%d : %s", TEST_ERRNO,
276 strerror(TEST_ERRNO));
vapier460557a2006-09-10 10:00:38 +0000277 }
278}
279
280/***************************************************************
281 * setup() - performs all ONE TIME setup for this test.
282 ***************************************************************/
subrata_modak421ddee2009-04-28 07:10:21 +0000283static void setup()
vapier460557a2006-09-10 10:00:38 +0000284{
subrata_modak421ddee2009-04-28 07:10:21 +0000285 char *tmp;
vapier460557a2006-09-10 10:00:38 +0000286
vapier460557a2006-09-10 10:00:38 +0000287 tst_sig(NOFORK, DEF_HANDLER, cleanup);
288
subrata_modak421ddee2009-04-28 07:10:21 +0000289 tst_tmpdir();
290
291 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed: %s", TEST_DIR1, 0700);
292 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed: %s", TEST_DIR3, 0700);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800293 olddirfd =
294 SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed: %s", TEST_DIR1,
295 O_DIRECTORY);
296 deldirfd =
297 SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed: %s", TEST_DIR3,
298 O_DIRECTORY);
subrata_modak421ddee2009-04-28 07:10:21 +0000299 SUCCEED_OR_DIE(rmdir, "rmdir(%s) failed: %s", TEST_DIR3);
300 SUCCEED_OR_DIE(close, "close(%d) failed: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301 SUCCEED_OR_DIE(open, "open(%s, 0x%x, %o) failed: %s",
302 TEST_DIR1 "/" TEST_FILE1,
303 O_CREAT | O_EXCL, 0600));
subrata_modak421ddee2009-04-28 07:10:21 +0000304
305 /* gratuitous memory leak here */
306 tmp = strdup(dpathname);
307 snprintf(dpathname, sizeof(dpathname), tmp, get_current_dir_name());
308
vapier460557a2006-09-10 10:00:38 +0000309 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800310}
vapier460557a2006-09-10 10:00:38 +0000311
312/***************************************************************
313 * cleanup() - performs all ONE TIME cleanup for this test at
314 * completion or premature exit.
315 ***************************************************************/
subrata_modak421ddee2009-04-28 07:10:21 +0000316static void cleanup()
vapier460557a2006-09-10 10:00:38 +0000317{
subrata_modak421ddee2009-04-28 07:10:21 +0000318 tst_rmdir();
vapier460557a2006-09-10 10:00:38 +0000319 /*
320 * print timing stats if that option was specified.
321 * print errno log if that option was specified.
322 */
323 TEST_CLEANUP;
324
Markos Chandrasf4539c62012-01-03 09:41:10 +0000325}