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