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