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 * 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 * Author
27 * Yi Yang <yyangcdl@cn.ibm.com>
28 *
29 * History
30 * 08/25/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 <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <signal.h>
44 #include "test.h"
45 #include "safe_macros.h"
46 #include "lapi/syscalls.h"
47
48 #define MYRETCODE -999
49 #ifndef AT_FDCWD
50 #define AT_FDCWD -100
51 #endif
52
53 struct test_struct;
54 static void setup();
55 static void cleanup();
56 static void setup_every_copy();
57 static void mysymlinkat_test(struct test_struct *desc);
58
59 #define TEST_DIR1 "olddir"
60 #define TEST_DIR2 "newdir"
61 #define TEST_DIR3 "deldir"
62 #define TEST_FILE1 "oldfile"
63 #define TEST_FILE2 "newfile"
64 #define TEST_FIFO "fifo"
65
66 static char dpathname[256] = "%s/" TEST_DIR2 "/" TEST_FILE1;
67 static int olddirfd, newdirfd = -1, cwd_fd = AT_FDCWD, stdinfd = 0, crapfd =
68 -1, deldirfd;
69
70 struct test_struct {
71 const char *oldfn;
72 int *newfd;
73 const char *newfn;
74 const char *referencefn1;
75 const char *referencefn2;
76 int expected_errno;
77 } test_desc[] = {
78 /* relative paths */
79 {
80 "../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1,
81 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
82 /* abs path at dst */
83 {
84 "../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, dpathname,
85 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
86 /* relative paths to cwd */
87 {
88 "../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd,
89 TEST_DIR2 "/" TEST_FILE1, TEST_DIR1 "/" TEST_FILE1,
90 TEST_DIR2 "/" TEST_FILE1, 0},
91 /* abs path */
92 {
93 "../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd, dpathname,
94 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
95 /* relative paths to invalid */
96 {
97 "../" TEST_DIR1 "/" TEST_FILE1, &stdinfd,
98 TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOTDIR},
99 /* abs path at dst */
100 {
101 "../" TEST_DIR1 "/" TEST_FILE1, &stdinfd, dpathname,
102 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
103 /* relative paths to crap */
104 {
105 "../" TEST_DIR1 "/" TEST_FILE1, &crapfd,
106 TEST_DIR2 "/" TEST_FILE1, 0, 0, EBADF},
107 /* abs path at dst */
108 {
109 "../" TEST_DIR1 "/" TEST_FILE1, &crapfd, dpathname,
110 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
111 /* relative paths to deleted */
112 {
113 "../" TEST_DIR1 "/" TEST_FILE1, &deldirfd,
114 TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOENT},
115 /* abs path at dst */
116 {
117 "../" TEST_DIR1 "/" TEST_FILE1, &deldirfd, dpathname,
118 TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
119 /* fifo link */
120 /* { TEST_FIFO, &newdirfd, TEST_FILE1, TEST_DIR1"/"TEST_FIFO, TEST_DIR2"/"TEST_FILE1, 0 }, */
121 };
122
123 char *TCID = "symlinkat01";
124 int TST_TOTAL = sizeof(test_desc) / sizeof(*test_desc);
125
mysymlinkat(const char * oldfilename,int newdirfd,const char * newfilename)126 static int mysymlinkat(const char *oldfilename,
127 int newdirfd, const char *newfilename)
128 {
129 return ltp_syscall(__NR_symlinkat, oldfilename, newdirfd, newfilename);
130 }
131
main(int ac,char ** av)132 int main(int ac, char **av)
133 {
134 int lc;
135 int i;
136
137 /* Disable test if the version of the kernel is less than 2.6.16 */
138 if ((tst_kvercmp(2, 6, 16)) < 0) {
139 tst_resm(TWARN, "This test can only run on kernels that are ");
140 tst_resm(TWARN, "2.6.16 and higher");
141 exit(0);
142 }
143
144 tst_parse_opts(ac, av, NULL, NULL);
145
146 setup();
147
148 for (lc = 0; TEST_LOOPING(lc); lc++) {
149
150 tst_count = 0;
151
152 for (i = 0; i < TST_TOTAL; i++) {
153 setup_every_copy();
154 mysymlinkat_test(&test_desc[i]);
155
156 }
157
158 }
159
160 cleanup();
161 tst_exit();
162 }
163
setup_every_copy(void)164 static void setup_every_copy(void)
165 {
166 close(newdirfd);
167 unlink(dpathname);
168 rmdir(TEST_DIR2);
169
170 SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
171 newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
172 }
173
mysymlinkat_test(struct test_struct * desc)174 static void mysymlinkat_test(struct test_struct *desc)
175 {
176 int fd;
177
178 TEST(mysymlinkat(desc->oldfn, *desc->newfd, desc->newfn));
179
180 /* check return code */
181 if (TEST_ERRNO == desc->expected_errno) {
182 if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
183 int tnum = rand(), vnum = ~tnum;
184
185 fd = SAFE_OPEN(cleanup, desc->referencefn1, O_RDWR);
186 SAFE_WRITE(cleanup, 1, fd, &tnum, sizeof(tnum));
187 SAFE_CLOSE(cleanup, fd);
188
189 fd = SAFE_OPEN(cleanup, desc->referencefn2, O_RDONLY);
190 SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum));
191 SAFE_CLOSE(cleanup, fd);
192
193 if (tnum == vnum)
194 tst_resm(TPASS, "Test passed");
195 else
196 tst_resm(TFAIL,
197 "The link file's content isn't as same as the original file's "
198 "although symlinkat returned 0");
199 } else {
200 tst_resm(TPASS,
201 "symlinkat() returned the expected errno %d: %s",
202 TEST_ERRNO, strerror(TEST_ERRNO));
203 }
204 } else {
205 tst_resm(TFAIL,
206 TEST_RETURN ==
207 0 ? "symlinkat() surprisingly succeeded" :
208 "symlinkat() Failed, errno=%d : %s", TEST_ERRNO,
209 strerror(TEST_ERRNO));
210 }
211 }
212
setup(void)213 static void setup(void)
214 {
215 char *tmp;
216 int fd;
217
218 tst_sig(NOFORK, DEF_HANDLER, cleanup);
219
220 tst_tmpdir();
221
222 SAFE_MKDIR(cleanup, TEST_DIR1, 0700);
223 SAFE_MKDIR(cleanup, TEST_DIR3, 0700);
224 olddirfd = SAFE_OPEN(cleanup, TEST_DIR1, O_DIRECTORY);
225 deldirfd = SAFE_OPEN(cleanup, TEST_DIR3, O_DIRECTORY);
226 SAFE_RMDIR(cleanup, TEST_DIR3);
227 fd = SAFE_OPEN(cleanup, TEST_DIR1 "/" TEST_FILE1, O_CREAT | O_EXCL, 0600);
228 SAFE_CLOSE(cleanup, fd);
229
230 /* gratuitous memory leak here */
231 tmp = strdup(dpathname);
232 snprintf(dpathname, sizeof(dpathname), tmp, get_current_dir_name());
233
234 TEST_PAUSE;
235 }
236
cleanup(void)237 static void cleanup(void)
238 {
239 tst_rmdir();
240 }
241