xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/pathconf/pathconf02.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) 2023 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that,
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with ENOTDIR if a component used as a directory
13*49cdfc7eSAndroid Build Coastguard Worker  *   in path is not in fact a directory.
14*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with ENOENT if path is an empty string.
15*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with ENAMETOOLONG if path is too long.
16*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with EINVA if name is invalid.
17*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with EACCES if search permission is denied for
18*49cdfc7eSAndroid Build Coastguard Worker  *   one of the directories in the path prefix of path.
19*49cdfc7eSAndroid Build Coastguard Worker  * - pathconf() fails with ELOOP if too many symbolic links were
20*49cdfc7eSAndroid Build Coastguard Worker  *   encountered while resolving path.
21*49cdfc7eSAndroid Build Coastguard Worker  */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define FILEPATH "testfile/testfile_1"
24*49cdfc7eSAndroid Build Coastguard Worker #define TESTELOOP "test_eloop1"
25*49cdfc7eSAndroid Build Coastguard Worker #define PATH_LEN (PATH_MAX + 2)
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static char *fpath;
32*49cdfc7eSAndroid Build Coastguard Worker static char *emptypath;
33*49cdfc7eSAndroid Build Coastguard Worker static char path[PATH_LEN];
34*49cdfc7eSAndroid Build Coastguard Worker static char *long_path = path;
35*49cdfc7eSAndroid Build Coastguard Worker static char *abs_path;
36*49cdfc7eSAndroid Build Coastguard Worker static char *testeloop;
37*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *user;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
40*49cdfc7eSAndroid Build Coastguard Worker 	char **path;
41*49cdfc7eSAndroid Build Coastguard Worker 	int name;
42*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
43*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
44*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
45*49cdfc7eSAndroid Build Coastguard Worker 	{&fpath, 0, ENOTDIR, "path prefix is not a directory"},
46*49cdfc7eSAndroid Build Coastguard Worker 	{&emptypath, 0, ENOENT, "path is an empty string"},
47*49cdfc7eSAndroid Build Coastguard Worker 	{&long_path, 0, ENAMETOOLONG, "path is too long"},
48*49cdfc7eSAndroid Build Coastguard Worker 	{&abs_path, -1, EINVAL, "name is invalid"},
49*49cdfc7eSAndroid Build Coastguard Worker 	{&abs_path, 0, EACCES, "without full permissions of the path prefix"},
50*49cdfc7eSAndroid Build Coastguard Worker 	{&testeloop, 0, ELOOP, "too many symbolic links"},
51*49cdfc7eSAndroid Build Coastguard Worker };
52*49cdfc7eSAndroid Build Coastguard Worker 
verify_fpathconf(unsigned int i)53*49cdfc7eSAndroid Build Coastguard Worker static void verify_fpathconf(unsigned int i)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == EACCES)
58*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(user->pw_uid);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(pathconf(*tc->path, tc->name), tc->exp_errno,
61*49cdfc7eSAndroid Build Coastguard Worker 		     "pathconf() fail with %s", tc->desc);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == EACCES)
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(0);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)67*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	user = SAFE_GETPWNAM("nobody");
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH("testfile", 0777, NULL);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	char *tmpdir =  tst_get_tmpdir();
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	abs_path = tst_aprintf("%s/%s", tmpdir, FILEPATH);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(tmpdir, 0);
78*49cdfc7eSAndroid Build Coastguard Worker 	free(tmpdir);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	memset(path, 'a', PATH_LEN);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("test_eloop1", "test_eloop2");
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("test_eloop2", "test_eloop1");
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
88*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_fpathconf,
89*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
91*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
92*49cdfc7eSAndroid Build Coastguard Worker 		{&fpath, .str = FILEPATH},
93*49cdfc7eSAndroid Build Coastguard Worker 		{&emptypath, .str = ""},
94*49cdfc7eSAndroid Build Coastguard Worker 		{&testeloop, .str = TESTELOOP},
95*49cdfc7eSAndroid Build Coastguard Worker 		{},
96*49cdfc7eSAndroid Build Coastguard Worker 	},
97*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
98*49cdfc7eSAndroid Build Coastguard Worker };
99