1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Red Hat, Inc. All rights reserved.
4f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Clone a process with CLONE_NEWPID flag and check if procfs mounted folder
11f08c3bdfSopenharmony_ci * belongs to the new pid namespace by looking at /proc/self .
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <sys/mount.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci#include "lapi/sched.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define PROCDIR "proc"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic void child_func(void)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	char proc_self[10];
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	SAFE_MOUNT("none", PROCDIR, "proc", MS_RDONLY, NULL);
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	SAFE_READLINK(PROCDIR"/self", proc_self, sizeof(proc_self) - 1);
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	SAFE_UMOUNT(PROCDIR);
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	TST_EXP_PASS(strcmp(proc_self, "1"), PROCDIR"/self contains 1:");
31f08c3bdfSopenharmony_ci}
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void setup(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	SAFE_MKDIR(PROCDIR, 0555);
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void cleanup(void)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	if (tst_is_mounted_at_tmpdir(PROCDIR))
41f08c3bdfSopenharmony_ci		SAFE_UMOUNT(PROCDIR);
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void run(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	const struct tst_clone_args args = {
47f08c3bdfSopenharmony_ci		.flags = CLONE_NEWPID,
48f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
49f08c3bdfSopenharmony_ci	};
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	if (!SAFE_CLONE(&args)) {
52f08c3bdfSopenharmony_ci		child_func();
53f08c3bdfSopenharmony_ci		return;
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic struct tst_test test = {
58f08c3bdfSopenharmony_ci	.test_all = run,
59f08c3bdfSopenharmony_ci	.setup = setup,
60f08c3bdfSopenharmony_ci	.cleanup = cleanup,
61f08c3bdfSopenharmony_ci	.needs_root = 1,
62f08c3bdfSopenharmony_ci	.forks_child = 1,
63f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
64f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
65f08c3bdfSopenharmony_ci		"CONFIG_PID_NS",
66f08c3bdfSopenharmony_ci		NULL,
67f08c3bdfSopenharmony_ci	},
68f08c3bdfSopenharmony_ci};
69