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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
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
38 * Yi Yang <yyangcdl@cn.ibm.com>
39 *
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 <unistd.h>
52 #include <error.h>
53 #include <stdlib.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <signal.h>
57 #include <inttypes.h>
58 #include <limits.h>
59 #include "test.h"
60 #include "linux_syscall_numbers.h"
61 #include "rmobj.h"
62 #include "safe_macros.h"
63
64 #ifndef AT_FDCWD
65 #define AT_FDCWD -100
66 #endif
67 #ifndef AT_SYMLINK_FOLLOW
68 #define AT_SYMLINK_FOLLOW 0x400
69 #endif
70
71 struct test_struct;
72 static void setup();
73 static void cleanup();
74 static void setup_every_copy();
75 static void mylinkat_test(struct test_struct *desc);
76
77 #define TEST_DIR1 "olddir"
78 #define TEST_DIR2 "newdir"
79 #define TEST_DIR3 "deldir"
80 #define TEST_FILE1 "oldfile"
81 #define TEST_FILE2 "newfile"
82 #define TEST_FIFO "fifo"
83
84 #define DPATHNAME_FMT "%s/" TEST_DIR2 "/" TEST_FILE1
85 #define SPATHNAME_FMT "%s/" TEST_DIR1 "/" TEST_FILE1
86
87 static char dpathname[PATH_MAX];
88 static char spathname[PATH_MAX];
89 static int olddirfd, newdirfd = -1, cwd_fd = AT_FDCWD, stdinfd = 0, badfd =
90 -1, deldirfd;
91
92 struct test_struct {
93 int *oldfd;
94 const char *oldfn;
95 int *newfd;
96 const char *newfn;
97 int flags;
98 const char *referencefn1;
99 const char *referencefn2;
100 int expected_errno;
101 } test_desc[] = {
102 /* 1. relative paths */
103 {
104 &olddirfd, TEST_FILE1, &newdirfd, TEST_FILE1, 0,
105 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
106 /* 2. abs path at source */
107 {
108 &olddirfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
109 /* 3. abs path at dst */
110 {
111 &olddirfd, TEST_FILE1, &newdirfd, dpathname, 0,
112 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
113 /* 4. relative paths to cwd */
114 {
115 &cwd_fd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
116 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
117 /* 5. relative paths to cwd */
118 {
119 &olddirfd, TEST_FILE1, &cwd_fd, TEST_DIR2 "/" TEST_FILE1, 0,
120 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
121 /* 6. abs path at source */
122 {
123 &cwd_fd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
124 /* 7. abs path at dst */
125 {
126 &olddirfd, TEST_FILE1, &cwd_fd, dpathname, 0,
127 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
128 /* 8. relative paths to invalid */
129 {
130 &stdinfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
131 0, 0, ENOTDIR},
132 /* 9. relative paths to invalid */
133 {
134 &olddirfd, TEST_FILE1, &stdinfd, TEST_DIR2 "/" TEST_FILE1, 0,
135 0, 0, ENOTDIR},
136 /* 10. abs path at source */
137 {
138 &stdinfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
139 /* 11. abs path at dst */
140 {
141 &olddirfd, TEST_FILE1, &stdinfd, dpathname, 0,
142 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
143 /* 12. relative paths to bad */
144 {
145 &badfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
146 0, 0, EBADF},
147 /* 13. relative paths to bad */
148 {
149 &olddirfd, TEST_FILE1, &badfd, TEST_DIR2 "/" TEST_FILE1, 0,
150 0, 0, EBADF},
151 /* 14. abs path at source */
152 {
153 &badfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
154 /* 15. abs path at dst */
155 {
156 &olddirfd, TEST_FILE1, &badfd, dpathname, 0,
157 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
158 /* 16. relative paths to deleted */
159 {
160 &deldirfd, TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1, 0,
161 0, 0, ENOENT},
162 /* 17. relative paths to deleted */
163 {
164 &olddirfd, TEST_FILE1, &deldirfd, TEST_DIR2 "/" TEST_FILE1, 0,
165 0, 0, ENOENT},
166 /* 18. abs path at source */
167 {
168 &deldirfd, spathname, &newdirfd, TEST_FILE1, 0, 0, 0, 0},
169 /* 19. abs path at dst */
170 {
171 &olddirfd, TEST_FILE1, &deldirfd, dpathname, 0,
172 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
173 /* 20. x-device link */
174 {
175 &cwd_fd, "/proc/cpuinfo", &newdirfd, TEST_FILE1, 0, 0, 0, EXDEV},
176 /* 21. directory link */
177 {
178 &olddirfd, ".", &newdirfd, TEST_FILE1, 0, 0, 0, EPERM},
179 /* 22. invalid flag */
180 {
181 &olddirfd, TEST_FILE1, &newdirfd, TEST_FILE1, 1, 0, 0, EINVAL},
182 /* 23. fifo link */
183 /* XXX (garrcoop): Removed because it hangs the overall test. Need to
184 * find a legitimate means to exercise this functionality, if in fact
185 * it's a valid testcase -- which it should be.
186 */
187 /* { &olddirfd, TEST_FIFO, &newdirfd, TEST_FILE1, 0,
188 TEST_DIR1"/"TEST_FIFO, TEST_DIR2"/"TEST_FILE1, 0 } */
189 };
190
191 char *TCID = "linkat01";
192 int TST_TOTAL = sizeof(test_desc) / sizeof(*test_desc);
193
194 #define SUCCEED_OR_DIE(syscall, message, ...) \
195 (errno = 0, \
196 ({int ret=syscall(__VA_ARGS__); \
197 if (ret==-1) \
198 tst_brkm(TBROK | TERRNO, cleanup, \
199 message, __VA_ARGS__); \
200 ret; }))
201
mylinkat(int olddirfd,const char * oldfilename,int newdirfd,const char * newfilename,int flags)202 static int mylinkat(int olddirfd, const char *oldfilename, int newdirfd,
203 const char *newfilename, int flags)
204 {
205 return ltp_syscall(__NR_linkat, olddirfd, oldfilename, newdirfd,
206 newfilename, flags);
207 }
208
main(int ac,char ** av)209 int main(int ac, char **av)
210 {
211 int lc;
212 int i;
213
214 if ((tst_kvercmp(2, 6, 16)) < 0) {
215 tst_resm(TWARN, "This test can only run on kernels that are ");
216 tst_resm(TWARN, "2.6.16 and higher");
217 exit(0);
218 }
219
220 tst_parse_opts(ac, av, NULL, NULL);
221
222 setup();
223
224 for (lc = 0; TEST_LOOPING(lc); lc++) {
225
226 tst_count = 0;
227
228 for (i = 0; i < TST_TOTAL; i++) {
229 setup_every_copy();
230 mylinkat_test(&test_desc[i]);
231 }
232
233 }
234
235 cleanup();
236 tst_exit();
237 }
238
setup_every_copy(void)239 static void setup_every_copy(void)
240 {
241 close(newdirfd);
242 rmobj(TEST_DIR2, NULL);
243
244 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR2, 0700);
245 newdirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
246 TEST_DIR2, O_DIRECTORY);
247 }
248
mylinkat_test(struct test_struct * desc)249 static void mylinkat_test(struct test_struct *desc)
250 {
251 int fd;
252
253 TEST(mylinkat
254 (*desc->oldfd, desc->oldfn, *desc->newfd, desc->newfn,
255 desc->flags));
256
257 if (TEST_ERRNO == desc->expected_errno) {
258 if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
259 int tnum = rand(), vnum = ~tnum;
260 fd = SAFE_OPEN(cleanup, desc->referencefn1,
261 O_RDWR);
262 SAFE_WRITE(cleanup, 1, fd, &tnum, sizeof(tnum));
263 SAFE_CLOSE(cleanup, fd);
264
265 fd = SAFE_OPEN(cleanup, desc->referencefn2,
266 O_RDONLY);
267 SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum));
268 SAFE_CLOSE(cleanup, fd);
269
270 if (tnum == vnum)
271 tst_resm(TPASS,
272 "linkat is functionality correct");
273 else {
274 tst_resm(TFAIL,
275 "The link file's content isn't "
276 "as same as the original file's "
277 "although linkat returned 0");
278 }
279 } else {
280 if (TEST_RETURN == 0)
281 tst_resm(TPASS,
282 "linkat succeeded as expected");
283 else
284 tst_resm(TPASS | TTERRNO,
285 "linkat failed as expected");
286 }
287 } else {
288 if (TEST_RETURN == 0)
289 tst_resm(TFAIL, "linkat succeeded unexpectedly");
290 else
291 tst_resm(TFAIL | TTERRNO,
292 "linkat failed unexpectedly; expected %d - %s",
293 desc->expected_errno,
294 strerror(desc->expected_errno));
295 }
296 }
297
setup(void)298 void setup(void)
299 {
300 char *cwd;
301
302 tst_sig(NOFORK, DEF_HANDLER, cleanup);
303
304 tst_tmpdir();
305
306 cwd = get_current_dir_name();
307 if (cwd == NULL)
308 tst_brkm(TFAIL | TERRNO, cleanup,
309 "Failed to get current working directory");
310 else {
311
312 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR1, 0700);
313 SUCCEED_OR_DIE(mkdir, "mkdir(%s, %o) failed", TEST_DIR3, 0700);
314 olddirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
315 TEST_DIR1, O_DIRECTORY);
316 deldirfd = SUCCEED_OR_DIE(open, "open(%s, 0x%x) failed",
317 TEST_DIR3, O_DIRECTORY);
318 SUCCEED_OR_DIE(rmdir, "rmdir(%s) failed", TEST_DIR3);
319 SUCCEED_OR_DIE(close, "close(%d) failed",
320 SUCCEED_OR_DIE(open, "open(%s, 0x%x, %o) "
321 "failed",
322 TEST_DIR1 "/" TEST_FILE1,
323 O_CREAT | O_EXCL, 0600));
324
325 SUCCEED_OR_DIE(mkfifo, "mkfifo(%s, %o) failed",
326 TEST_DIR1 "/" TEST_FIFO, 0600);
327
328 snprintf(dpathname, sizeof(dpathname), DPATHNAME_FMT, cwd);
329 snprintf(spathname, sizeof(spathname), SPATHNAME_FMT, cwd);
330
331 free(cwd);
332
333 TEST_PAUSE;
334
335 }
336
337 }
338
cleanup(void)339 static void cleanup(void)
340 {
341 tst_rmdir();
342 }
343