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