1f08c3bdfSopenharmony_ci/******************************************************************************
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2006
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * NAME
20f08c3bdfSopenharmony_ci *      symlinkat01.c
21f08c3bdfSopenharmony_ci *
22f08c3bdfSopenharmony_ci * DESCRIPTION
23f08c3bdfSopenharmony_ci *	This test case will verify basic function of symlinkat
24f08c3bdfSopenharmony_ci *	added by kernel 2.6.16 or up.
25f08c3bdfSopenharmony_ci *
26f08c3bdfSopenharmony_ci * Author
27f08c3bdfSopenharmony_ci *	Yi Yang <yyangcdl@cn.ibm.com>
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * History
30f08c3bdfSopenharmony_ci *      08/25/2006      Created first by Yi Yang <yyangcdl@cn.ibm.com>
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci *****************************************************************************/
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#define _GNU_SOURCE
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#include <sys/types.h>
37f08c3bdfSopenharmony_ci#include <sys/stat.h>
38f08c3bdfSopenharmony_ci#include <sys/time.h>
39f08c3bdfSopenharmony_ci#include <fcntl.h>
40f08c3bdfSopenharmony_ci#include <stdlib.h>
41f08c3bdfSopenharmony_ci#include <errno.h>
42f08c3bdfSopenharmony_ci#include <string.h>
43f08c3bdfSopenharmony_ci#include <signal.h>
44f08c3bdfSopenharmony_ci#include "test.h"
45f08c3bdfSopenharmony_ci#include "safe_macros.h"
46f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci#define MYRETCODE -999
49f08c3bdfSopenharmony_ci#ifndef AT_FDCWD
50f08c3bdfSopenharmony_ci#define AT_FDCWD -100
51f08c3bdfSopenharmony_ci#endif
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistruct test_struct;
54f08c3bdfSopenharmony_cistatic void setup();
55f08c3bdfSopenharmony_cistatic void cleanup();
56f08c3bdfSopenharmony_cistatic void setup_every_copy();
57f08c3bdfSopenharmony_cistatic void mysymlinkat_test(struct test_struct *desc);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#define TEST_DIR1 "olddir"
60f08c3bdfSopenharmony_ci#define TEST_DIR2 "newdir"
61f08c3bdfSopenharmony_ci#define TEST_DIR3 "deldir"
62f08c3bdfSopenharmony_ci#define TEST_FILE1 "oldfile"
63f08c3bdfSopenharmony_ci#define TEST_FILE2 "newfile"
64f08c3bdfSopenharmony_ci#define TEST_FIFO "fifo"
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic char dpathname[256] = "%s/" TEST_DIR2 "/" TEST_FILE1;
67f08c3bdfSopenharmony_cistatic int olddirfd, newdirfd = -1, cwd_fd = AT_FDCWD, stdinfd = 0, crapfd =
68f08c3bdfSopenharmony_ci    -1, deldirfd;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistruct test_struct {
71f08c3bdfSopenharmony_ci	const char *oldfn;
72f08c3bdfSopenharmony_ci	int *newfd;
73f08c3bdfSopenharmony_ci	const char *newfn;
74f08c3bdfSopenharmony_ci	const char *referencefn1;
75f08c3bdfSopenharmony_ci	const char *referencefn2;
76f08c3bdfSopenharmony_ci	int expected_errno;
77f08c3bdfSopenharmony_ci} test_desc[] = {
78f08c3bdfSopenharmony_ci	/* relative paths */
79f08c3bdfSopenharmony_ci	{
80f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, TEST_FILE1,
81f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
82f08c3bdfSopenharmony_ci	    /* abs path at dst */
83f08c3bdfSopenharmony_ci	{
84f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &newdirfd, dpathname,
85f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
86f08c3bdfSopenharmony_ci	    /* relative paths to cwd */
87f08c3bdfSopenharmony_ci	{
88f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd,
89f08c3bdfSopenharmony_ci		    TEST_DIR2 "/" TEST_FILE1, TEST_DIR1 "/" TEST_FILE1,
90f08c3bdfSopenharmony_ci		    TEST_DIR2 "/" TEST_FILE1, 0},
91f08c3bdfSopenharmony_ci	    /* abs path */
92f08c3bdfSopenharmony_ci	{
93f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &cwd_fd, dpathname,
94f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
95f08c3bdfSopenharmony_ci	    /* relative paths to invalid */
96f08c3bdfSopenharmony_ci	{
97f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &stdinfd,
98f08c3bdfSopenharmony_ci		    TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOTDIR},
99f08c3bdfSopenharmony_ci	    /* abs path at dst */
100f08c3bdfSopenharmony_ci	{
101f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &stdinfd, dpathname,
102f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
103f08c3bdfSopenharmony_ci	    /* relative paths to crap */
104f08c3bdfSopenharmony_ci	{
105f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &crapfd,
106f08c3bdfSopenharmony_ci		    TEST_DIR2 "/" TEST_FILE1, 0, 0, EBADF},
107f08c3bdfSopenharmony_ci	    /* abs path at dst */
108f08c3bdfSopenharmony_ci	{
109f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &crapfd, dpathname,
110f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
111f08c3bdfSopenharmony_ci	    /* relative paths to deleted */
112f08c3bdfSopenharmony_ci	{
113f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &deldirfd,
114f08c3bdfSopenharmony_ci		    TEST_DIR2 "/" TEST_FILE1, 0, 0, ENOENT},
115f08c3bdfSopenharmony_ci	    /* abs path at dst */
116f08c3bdfSopenharmony_ci	{
117f08c3bdfSopenharmony_ci	"../" TEST_DIR1 "/" TEST_FILE1, &deldirfd, dpathname,
118f08c3bdfSopenharmony_ci		    TEST_DIR1 "/" TEST_FILE1, TEST_DIR2 "/" TEST_FILE1, 0},
119f08c3bdfSopenharmony_ci	    /* fifo link */
120f08c3bdfSopenharmony_ci	    /*      { TEST_FIFO, &newdirfd, TEST_FILE1, TEST_DIR1"/"TEST_FIFO, TEST_DIR2"/"TEST_FILE1, 0 }, */
121f08c3bdfSopenharmony_ci};
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_cichar *TCID = "symlinkat01";
124f08c3bdfSopenharmony_ciint TST_TOTAL = sizeof(test_desc) / sizeof(*test_desc);
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_cistatic int mysymlinkat(const char *oldfilename,
127f08c3bdfSopenharmony_ci		       int newdirfd, const char *newfilename)
128f08c3bdfSopenharmony_ci{
129f08c3bdfSopenharmony_ci	return tst_syscall(__NR_symlinkat, oldfilename, newdirfd, newfilename);
130f08c3bdfSopenharmony_ci}
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ciint main(int ac, char **av)
133f08c3bdfSopenharmony_ci{
134f08c3bdfSopenharmony_ci	int lc;
135f08c3bdfSopenharmony_ci	int i;
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	setup();
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci		tst_count = 0;
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++) {
146f08c3bdfSopenharmony_ci			setup_every_copy();
147f08c3bdfSopenharmony_ci			mysymlinkat_test(&test_desc[i]);
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci		}
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	cleanup();
154f08c3bdfSopenharmony_ci	tst_exit();
155f08c3bdfSopenharmony_ci}
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_cistatic void setup_every_copy(void)
158f08c3bdfSopenharmony_ci{
159f08c3bdfSopenharmony_ci	close(newdirfd);
160f08c3bdfSopenharmony_ci	unlink(dpathname);
161f08c3bdfSopenharmony_ci	rmdir(TEST_DIR2);
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
164f08c3bdfSopenharmony_ci	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
165f08c3bdfSopenharmony_ci}
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_cistatic void mysymlinkat_test(struct test_struct *desc)
168f08c3bdfSopenharmony_ci{
169f08c3bdfSopenharmony_ci	int fd;
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	TEST(mysymlinkat(desc->oldfn, *desc->newfd, desc->newfn));
172f08c3bdfSopenharmony_ci
173f08c3bdfSopenharmony_ci	/* check return code */
174f08c3bdfSopenharmony_ci	if (TEST_ERRNO == desc->expected_errno) {
175f08c3bdfSopenharmony_ci		if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
176f08c3bdfSopenharmony_ci			int tnum = rand(), vnum = ~tnum;
177f08c3bdfSopenharmony_ci
178f08c3bdfSopenharmony_ci			fd = SAFE_OPEN(cleanup, desc->referencefn1, O_RDWR);
179f08c3bdfSopenharmony_ci			SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, &tnum,
180f08c3bdfSopenharmony_ci				sizeof(tnum));
181f08c3bdfSopenharmony_ci			SAFE_CLOSE(cleanup, fd);
182f08c3bdfSopenharmony_ci
183f08c3bdfSopenharmony_ci			fd = SAFE_OPEN(cleanup, desc->referencefn2, O_RDONLY);
184f08c3bdfSopenharmony_ci			SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum));
185f08c3bdfSopenharmony_ci			SAFE_CLOSE(cleanup, fd);
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_ci			if (tnum == vnum)
188f08c3bdfSopenharmony_ci				tst_resm(TPASS, "Test passed");
189f08c3bdfSopenharmony_ci			else
190f08c3bdfSopenharmony_ci				tst_resm(TFAIL,
191f08c3bdfSopenharmony_ci					 "The link file's content isn't as same as the original file's "
192f08c3bdfSopenharmony_ci					 "although symlinkat returned 0");
193f08c3bdfSopenharmony_ci		} else {
194f08c3bdfSopenharmony_ci			tst_resm(TPASS,
195f08c3bdfSopenharmony_ci				 "symlinkat() returned the expected  errno %d: %s",
196f08c3bdfSopenharmony_ci				 TEST_ERRNO, strerror(TEST_ERRNO));
197f08c3bdfSopenharmony_ci		}
198f08c3bdfSopenharmony_ci	} else {
199f08c3bdfSopenharmony_ci		tst_resm(TFAIL,
200f08c3bdfSopenharmony_ci			 TEST_RETURN ==
201f08c3bdfSopenharmony_ci			 0 ? "symlinkat() surprisingly succeeded" :
202f08c3bdfSopenharmony_ci			 "symlinkat() Failed, errno=%d : %s", TEST_ERRNO,
203f08c3bdfSopenharmony_ci			 strerror(TEST_ERRNO));
204f08c3bdfSopenharmony_ci	}
205f08c3bdfSopenharmony_ci}
206f08c3bdfSopenharmony_ci
207f08c3bdfSopenharmony_cistatic void setup(void)
208f08c3bdfSopenharmony_ci{
209f08c3bdfSopenharmony_ci	char *tmp;
210f08c3bdfSopenharmony_ci	int fd;
211f08c3bdfSopenharmony_ci
212f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ci	tst_tmpdir();
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR1, 0700);
217f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR3, 0700);
218f08c3bdfSopenharmony_ci	olddirfd = SAFE_OPEN(cleanup, TEST_DIR1, O_DIRECTORY);
219f08c3bdfSopenharmony_ci	deldirfd = SAFE_OPEN(cleanup, TEST_DIR3, O_DIRECTORY);
220f08c3bdfSopenharmony_ci	SAFE_RMDIR(cleanup, TEST_DIR3);
221f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(cleanup, TEST_DIR1 "/" TEST_FILE1, O_CREAT | O_EXCL, 0600);
222f08c3bdfSopenharmony_ci	SAFE_CLOSE(cleanup, fd);
223f08c3bdfSopenharmony_ci
224f08c3bdfSopenharmony_ci	/* gratuitous memory leak here */
225f08c3bdfSopenharmony_ci	tmp = strdup(dpathname);
226f08c3bdfSopenharmony_ci	snprintf(dpathname, sizeof(dpathname), tmp, get_current_dir_name());
227f08c3bdfSopenharmony_ci
228f08c3bdfSopenharmony_ci	TEST_PAUSE;
229f08c3bdfSopenharmony_ci}
230f08c3bdfSopenharmony_ci
231f08c3bdfSopenharmony_cistatic void cleanup(void)
232f08c3bdfSopenharmony_ci{
233f08c3bdfSopenharmony_ci	tst_rmdir();
234f08c3bdfSopenharmony_ci}
235