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