1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
4f08c3bdfSopenharmony_ci *               27/12/07  Rishikesh K Rajak <risrajak@in.ibm.com>
5f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Clone a process with CLONE_NEWPID flag and check:
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - child process ID must be 1
14f08c3bdfSopenharmony_ci * - parent process ID must be 0
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "lapi/sched.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic void child_func(void)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	pid_t cpid, ppid;
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	cpid = tst_getpid();
25f08c3bdfSopenharmony_ci	ppid = getppid();
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(cpid, 1);
28f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(ppid, 0);
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void run(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	const struct tst_clone_args args = {
34f08c3bdfSopenharmony_ci		.flags = CLONE_NEWPID,
35f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
36f08c3bdfSopenharmony_ci	};
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	if (!SAFE_CLONE(&args)) {
39f08c3bdfSopenharmony_ci		child_func();
40f08c3bdfSopenharmony_ci		return;
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic struct tst_test test = {
45f08c3bdfSopenharmony_ci	.test_all = run,
46f08c3bdfSopenharmony_ci	.needs_root = 1,
47f08c3bdfSopenharmony_ci	.forks_child = 1,
48f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
49f08c3bdfSopenharmony_ci		"CONFIG_PID_NS",
50f08c3bdfSopenharmony_ci		NULL,
51f08c3bdfSopenharmony_ci	},
52f08c3bdfSopenharmony_ci};
53