xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/readlink/readlink03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test Description :
9*49cdfc7eSAndroid Build Coastguard Worker  *   1) readlink(2) returns -1 and sets errno to EACCES if search/write
10*49cdfc7eSAndroid Build Coastguard Worker  *	permission is denied in the directory where the symbolic link
11*49cdfc7eSAndroid Build Coastguard Worker  *	resides.
12*49cdfc7eSAndroid Build Coastguard Worker  *   2) readlink(2) returns -1 and sets errno to EINVAL if the buffer size
13*49cdfc7eSAndroid Build Coastguard Worker  *	is not positive.
14*49cdfc7eSAndroid Build Coastguard Worker  *   3) readlink(2) returns -1 and sets errno to EINVAL if the specified
15*49cdfc7eSAndroid Build Coastguard Worker  *	file is not a symbolic link file.
16*49cdfc7eSAndroid Build Coastguard Worker  *   4) readlink(2) returns -1 and sets errno to ENAMETOOLONG if the
17*49cdfc7eSAndroid Build Coastguard Worker  *	pathname component of symbolic link is too long (ie, > PATH_MAX).
18*49cdfc7eSAndroid Build Coastguard Worker  *   5) readlink(2) returns -1 and sets errno to ENOENT if the component of
19*49cdfc7eSAndroid Build Coastguard Worker  *	symbolic link points to an empty string.
20*49cdfc7eSAndroid Build Coastguard Worker  *   6) readlink(2) returns -1 and sets errno to ENOTDIR if a component of
21*49cdfc7eSAndroid Build Coastguard Worker  *	the path prefix is not a directory.
22*49cdfc7eSAndroid Build Coastguard Worker  *   7) readlink(2) returns -1 and sets errno to ELOOP if too many symbolic
23*49cdfc7eSAndroid Build Coastguard Worker  *	links were encountered in translating the pathname.
24*49cdfc7eSAndroid Build Coastguard Worker  *   8) readlink(2) returns -1 and sets errno to EFAULT if buf outside the
25*49cdfc7eSAndroid Build Coastguard Worker  *	process allocated address space.
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker #define DIR_TEMP	"test_dir_1"
35*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE1	"test_dir_1/test_file_1"
36*49cdfc7eSAndroid Build Coastguard Worker #define SYM_FILE1	"test_dir_1/slink_file_1"
37*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE2	"test_file_2"
38*49cdfc7eSAndroid Build Coastguard Worker #define SYM_FILE2	"slink_file_2"
39*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE3	"test_file_3"
40*49cdfc7eSAndroid Build Coastguard Worker #define SYM_FILE3	"test_file_3/slink_file_3"
41*49cdfc7eSAndroid Build Coastguard Worker #define ELOOPFILE	"/test_eloop"
42*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"test_file"
43*49cdfc7eSAndroid Build Coastguard Worker #define SYMFILE		"slink_file"
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker static char longpathname[PATH_MAX + 2];
46*49cdfc7eSAndroid Build Coastguard Worker static char elooppathname[sizeof(ELOOPFILE) * 43] = ".";
47*49cdfc7eSAndroid Build Coastguard Worker static char buffer[256];
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
50*49cdfc7eSAndroid Build Coastguard Worker 	char *link;
51*49cdfc7eSAndroid Build Coastguard Worker 	char *buf;
52*49cdfc7eSAndroid Build Coastguard Worker 	size_t buf_size;
53*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
54*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
55*49cdfc7eSAndroid Build Coastguard Worker 	{SYM_FILE1, buffer, sizeof(buffer), EACCES},
56*49cdfc7eSAndroid Build Coastguard Worker 	{SYM_FILE2, buffer, 0, EINVAL},
57*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_FILE2, buffer, sizeof(buffer), EINVAL},
58*49cdfc7eSAndroid Build Coastguard Worker 	{longpathname, buffer, sizeof(buffer), ENAMETOOLONG},
59*49cdfc7eSAndroid Build Coastguard Worker 	{"", buffer, sizeof(buffer), ENOENT},
60*49cdfc7eSAndroid Build Coastguard Worker 	{SYM_FILE3, buffer, sizeof(buffer), ENOTDIR},
61*49cdfc7eSAndroid Build Coastguard Worker 	{elooppathname, buffer, sizeof(buffer), ELOOP},
62*49cdfc7eSAndroid Build Coastguard Worker 	{SYMFILE, (char *)-1, sizeof(buffer), EFAULT},
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker 
verify_readlink(unsigned int n)65*49cdfc7eSAndroid Build Coastguard Worker static void verify_readlink(unsigned int n)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	TEST(readlink(tc->link, tc->buf, tc->buf_size));
70*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "readlink() sueeeeded unexpectedly");
72*49cdfc7eSAndroid Build Coastguard Worker 		return;
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != tc->exp_errno) {
76*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
77*49cdfc7eSAndroid Build Coastguard Worker 			"readlink() failed unexpectedly; expected: %d - %s, got",
78*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errno, tst_strerrno(tc->exp_errno));
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->exp_errno == ENOENT && TST_ERR == EINVAL) {
81*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TWARN | TTERRNO,
82*49cdfc7eSAndroid Build Coastguard Worker 				"It may be a Kernel Bug, see the patch:"
83*49cdfc7eSAndroid Build Coastguard Worker 				"http://git.kernel.org/linus/1fa1e7f6");
84*49cdfc7eSAndroid Build Coastguard Worker 		}
85*49cdfc7eSAndroid Build Coastguard Worker 	} else {
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "readlink() failed as expected");
87*49cdfc7eSAndroid Build Coastguard Worker 	}
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)90*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker 	int i;
93*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pwent;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	pwent = SAFE_GETPWNAM("nobody");
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pwent->pw_uid);
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DIR_TEMP, 0777);
99*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE1, 0666, NULL);
100*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST_FILE1, SYM_FILE1);
101*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(DIR_TEMP, 0444);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE2, 0666, NULL);
104*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST_FILE2, SYM_FILE2);
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	memset(longpathname, 'a', PATH_MAX + 1);
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE3, 0666, NULL);
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR("test_eloop", 0777);
111*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 43; i++)
114*49cdfc7eSAndroid Build Coastguard Worker 		strcat(elooppathname, ELOOPFILE);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TESTFILE, 0666, NULL);
117*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TESTFILE, SYMFILE);
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
121*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
122*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_readlink,
123*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
124*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
125*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
126*49cdfc7eSAndroid Build Coastguard Worker };
127