1 /*
2  * Copyright (C) Bull S.A. 2001
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * DESCRIPTION
23  *	Testcase for testing that fchdir(2) sets EACCES errno
24  *
25  * ALGORITHM
26  *	1.	create a child process, sets its uid to ltpuser1
27  *	2.	this child creates a directory with perm 400,
28  *	3.	this child opens the directory and gets a file descriptor
29  *	4.	this child attempts to fchdir(2) to the directory created in 2.
30  *		and expects to get an EACCES.
31  */
32 
33 #include <errno.h>
34 #include <pwd.h>
35 #include "tst_test.h"
36 
37 #define DIRNAME "fchdir03_dir"
38 
39 static int fd;
40 
41 void verify_fchdir(void)
42 {
43 	TEST(fchdir(fd));
44 
45 	if (TEST_RETURN != -1) {
46 		tst_res(TFAIL, "fchdir() succeeded unexpectedly");
47 		return;
48 	}
49 
50 	if (TEST_ERRNO != EACCES) {
51 		tst_res(TFAIL | TTERRNO, "fchdir() should fail with EACCES");
52 		return;
53 	}
54 
55 	tst_res(TPASS | TTERRNO, "fchdir() failed expectedly");
56 }
57 
58 void setup(void)
59 {
60 	struct passwd *pw;
61 
62 	pw = SAFE_GETPWNAM("nobody");
63 	SAFE_SETEUID(pw->pw_uid);
64 	SAFE_MKDIR(DIRNAME, 0400);
65 
66 	fd = SAFE_OPEN(DIRNAME, O_RDONLY);
67 }
68 
69 static struct tst_test test = {
70 	.setup = setup,
71 	.test_all = verify_fchdir,
72 	.needs_tmpdir = 1,
73 	.needs_root = 1,
74 };
75