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