1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
4f08c3bdfSopenharmony_ci *               13/11/08  Gowrishankar M <gowrishankar.m@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 spawn many children inside the
12f08c3bdfSopenharmony_ci * container. Then terminate all children and check if they were signaled.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <sys/wait.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "lapi/sched.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define CHILDREN_NUM 10
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void child_func(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	int children[CHILDREN_NUM], status;
24f08c3bdfSopenharmony_ci	unsigned int i;
25f08c3bdfSopenharmony_ci	pid_t cpid, ppid;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	cpid = tst_getpid();
28f08c3bdfSopenharmony_ci	ppid = getppid();
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(cpid, 1);
31f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(ppid, 0);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	tst_res(TINFO, "Spawning %d children", CHILDREN_NUM);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	for (i = 0; i < CHILDREN_NUM; i++) {
36f08c3bdfSopenharmony_ci		children[i] = SAFE_FORK();
37f08c3bdfSopenharmony_ci		if (!children[i]) {
38f08c3bdfSopenharmony_ci			pause();
39f08c3bdfSopenharmony_ci			return;
40f08c3bdfSopenharmony_ci		}
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	tst_res(TINFO, "Terminate children with SIGUSR1");
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	SAFE_KILL(-1, SIGUSR1);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	for (i = 0; i < CHILDREN_NUM; i++) {
48f08c3bdfSopenharmony_ci		SAFE_WAITPID(children[i], &status, 0);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci		TST_EXP_EQ_LI(WIFSIGNALED(status), 1);
51f08c3bdfSopenharmony_ci		TST_EXP_EQ_LI(WTERMSIG(status), SIGUSR1);
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void run(void)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	const struct tst_clone_args args = {
58f08c3bdfSopenharmony_ci		.flags = CLONE_NEWPID,
59f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
60f08c3bdfSopenharmony_ci	};
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	if (!SAFE_CLONE(&args)) {
63f08c3bdfSopenharmony_ci		child_func();
64f08c3bdfSopenharmony_ci		return;
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_cistatic struct tst_test test = {
69f08c3bdfSopenharmony_ci	.test_all = run,
70f08c3bdfSopenharmony_ci	.needs_root = 1,
71f08c3bdfSopenharmony_ci	.forks_child = 1,
72f08c3bdfSopenharmony_ci};
73