1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
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:
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * - child session ID must be 1
13f08c3bdfSopenharmony_ci * - parent process group ID must be 1
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "lapi/sched.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void child_func(void)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(getsid(0), 0);
22f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(getpgid(0), 0);
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	tst_res(TINFO, "setsid()");
25f08c3bdfSopenharmony_ci	SAFE_SETSID();
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(getsid(0), 1);
28f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(getpgid(0), 1);
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