xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/chroot/chroot02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   07/2001 Ported by Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker  *	 04/2003 Modified by Manoj Iyer - [email protected]
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Basic chroot() functionality test.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - Create a file in the temporary directory
15*49cdfc7eSAndroid Build Coastguard Worker  * - Change the root to this temporary directory
16*49cdfc7eSAndroid Build Coastguard Worker  * - Check whether this file can be accessed in the new root directory
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define TMP_FILENAME	"chroot02_testfile"
23*49cdfc7eSAndroid Build Coastguard Worker static char *path;
24*49cdfc7eSAndroid Build Coastguard Worker 
verify_chroot(void)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_chroot(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
30*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_PASS(chroot(path), "chroot(%s)", path);
31*49cdfc7eSAndroid Build Coastguard Worker 		if (!TST_PASS)
32*49cdfc7eSAndroid Build Coastguard Worker 			return;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_PASS(stat("/" TMP_FILENAME, &buf), "stat(/%s)", TMP_FILENAME);
35*49cdfc7eSAndroid Build Coastguard Worker 	}
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)38*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	path = tst_get_tmpdir();
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TMP_FILENAME, 0666, NULL);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	free(path);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
51*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
52*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_chroot,
53*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
54*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
55*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
56*49cdfc7eSAndroid Build Coastguard Worker };
57*49cdfc7eSAndroid Build Coastguard Worker 
58