xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/readlinkat/readlinkat02.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) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2003-2023
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with EINVAL if the bufsiz is 0.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with EINVAL if the named file is not a symbolic link.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with ENOTDIR if the component of the path prefix is
16*49cdfc7eSAndroid Build Coastguard Worker  *   not a directory.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with ENOTDIR if the pathname is relative and
19*49cdfc7eSAndroid Build Coastguard Worker  *   dirfd is a file descriptor referring to a file other than a directory.
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with EBADF if the file descriptor is invalid.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * - readlinkat() fails with ENOENT when the pathname does not exists.
24*49cdfc7eSAndroid Build Coastguard Worker  */
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE	"test_file"
29*49cdfc7eSAndroid Build Coastguard Worker #define SYMLINK_FILE	"symlink_file"
30*49cdfc7eSAndroid Build Coastguard Worker #define BUFF_SIZE	256
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static int file_fd, dir_fd;
33*49cdfc7eSAndroid Build Coastguard Worker static int fd_invalid = -1;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
36*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
37*49cdfc7eSAndroid Build Coastguard Worker 	const char *pathname;
38*49cdfc7eSAndroid Build Coastguard Worker 	size_t bufsiz;
39*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
40*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
41*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, SYMLINK_FILE, 0, EINVAL},
42*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, TEST_FILE, BUFF_SIZE, EINVAL},
43*49cdfc7eSAndroid Build Coastguard Worker 	{&file_fd, SYMLINK_FILE, BUFF_SIZE, ENOTDIR},
44*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, "test_file/test_file", BUFF_SIZE, ENOTDIR},
45*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_invalid, SYMLINK_FILE, BUFF_SIZE, EBADF},
46*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, "does_not_exists", BUFF_SIZE, ENOENT},
47*49cdfc7eSAndroid Build Coastguard Worker };
48*49cdfc7eSAndroid Build Coastguard Worker 
verify_readlinkat(unsigned int i)49*49cdfc7eSAndroid Build Coastguard Worker static void verify_readlinkat(unsigned int i)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	char buf[BUFF_SIZE];
52*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, 0, sizeof(buf));
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(readlinkat(*tc->fd, tc->pathname, buf, tc->bufsiz),
57*49cdfc7eSAndroid Build Coastguard Worker 		     tc->exp_errno, "readlinkat(%d, %s, NULL, %ld)",
58*49cdfc7eSAndroid Build Coastguard Worker 		     *tc->fd, tc->pathname, tc->bufsiz);
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)61*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(".", O_RDONLY);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	file_fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0644);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST_FILE, SYMLINK_FILE);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	if (file_fd > -1)
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(file_fd);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	if (dir_fd > -1)
76*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dir_fd);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
80*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_readlinkat,
81*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
82*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
83*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
84*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
85*49cdfc7eSAndroid Build Coastguard Worker };
86