xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fchdir/fchdir03.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) Bull S.A. 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Cyril Hrubis <[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  *	Testcase for testing that fchdir(2) sets EACCES errno
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
13*49cdfc7eSAndroid Build Coastguard Worker  *	1.	create a child process, sets its uid to ltpuser1
14*49cdfc7eSAndroid Build Coastguard Worker  *	2.	this child creates a directory with perm 400,
15*49cdfc7eSAndroid Build Coastguard Worker  *	3.	this child opens the directory and gets a file descriptor
16*49cdfc7eSAndroid Build Coastguard Worker  *	4.	this child attempts to fchdir(2) to the directory created in 2.
17*49cdfc7eSAndroid Build Coastguard Worker  *		and expects to get an EACCES.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define DIRNAME "fchdir03_dir"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static int fd;
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_fchdir(void)28*49cdfc7eSAndroid Build Coastguard Worker void verify_fchdir(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fchdir(fd));
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
33*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "fchdir() succeeded unexpectedly");
34*49cdfc7eSAndroid Build Coastguard Worker 		return;
35*49cdfc7eSAndroid Build Coastguard Worker 	}
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != EACCES) {
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "fchdir() should fail with EACCES");
39*49cdfc7eSAndroid Build Coastguard Worker 		return;
40*49cdfc7eSAndroid Build Coastguard Worker 	}
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS | TTERRNO, "fchdir() failed expectedly");
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)45*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pw->pw_uid);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DIRNAME, 0400);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(DIRNAME, O_RDONLY);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
57*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
58*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_fchdir,
59*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
60*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
61*49cdfc7eSAndroid Build Coastguard Worker };
62