1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Veerendra C <[email protected]>, 2008
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Clone a process with CLONE_NEWPID flag and create many levels of child
11*49cdfc7eSAndroid Build Coastguard Worker * containers. Then kill container init process from parent and check if all
12*49cdfc7eSAndroid Build Coastguard Worker * containers have been killed.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #define MAX_DEPTH 5
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker static struct tst_clone_args clone_args = {
22*49cdfc7eSAndroid Build Coastguard Worker .flags = CLONE_NEWPID,
23*49cdfc7eSAndroid Build Coastguard Worker .exit_signal = SIGCHLD
24*49cdfc7eSAndroid Build Coastguard Worker };
25*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid_max;
26*49cdfc7eSAndroid Build Coastguard Worker
child_func(const int level)27*49cdfc7eSAndroid Build Coastguard Worker static void child_func(const int level)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker pid_t cpid, ppid;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker cpid = tst_getpid();
32*49cdfc7eSAndroid Build Coastguard Worker ppid = getppid();
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(cpid, 1);
35*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(ppid, 0);
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if (level >= MAX_DEPTH - 1) {
38*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
39*49cdfc7eSAndroid Build Coastguard Worker return;
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_CLONE(&clone_args)) {
43*49cdfc7eSAndroid Build Coastguard Worker child_func(level + 1);
44*49cdfc7eSAndroid Build Coastguard Worker return;
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker pause();
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker
find_cinit_pids(pid_t * pids)50*49cdfc7eSAndroid Build Coastguard Worker static int find_cinit_pids(pid_t *pids)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker int pid;
53*49cdfc7eSAndroid Build Coastguard Worker int next = 0;
54*49cdfc7eSAndroid Build Coastguard Worker pid_t parentpid, pgid, pgid2;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker parentpid = tst_getpid();
57*49cdfc7eSAndroid Build Coastguard Worker pgid = SAFE_GETPGID(parentpid);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker for (pid = 2; pid < pid_max; pid++) {
60*49cdfc7eSAndroid Build Coastguard Worker if (pid == parentpid)
61*49cdfc7eSAndroid Build Coastguard Worker continue;
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker pgid2 = getpgid(pid);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker if (pgid2 == pgid) {
66*49cdfc7eSAndroid Build Coastguard Worker pids[next] = pid;
67*49cdfc7eSAndroid Build Coastguard Worker next++;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker return next;
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
setup(void)74*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF("/proc/sys/kernel/pid_max", "%d\n", &pid_max);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
run(void)79*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker int i, status, children;
82*49cdfc7eSAndroid Build Coastguard Worker pid_t pids_new[MAX_DEPTH];
83*49cdfc7eSAndroid Build Coastguard Worker pid_t pids[MAX_DEPTH];
84*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_CLONE(&clone_args);
87*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
88*49cdfc7eSAndroid Build Coastguard Worker child_func(0);
89*49cdfc7eSAndroid Build Coastguard Worker return;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_POSITIVE(find_cinit_pids(pids));
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker SAFE_KILL(pid, SIGKILL);
97*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(0, &status, 0);
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker children = find_cinit_pids(pids_new);
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker if (children > 0) {
102*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%d children left after sending SIGKILL", children);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < MAX_DEPTH; i++) {
105*49cdfc7eSAndroid Build Coastguard Worker kill(pids[i], SIGKILL);
106*49cdfc7eSAndroid Build Coastguard Worker waitpid(pids[i], &status, 0);
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker return;
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No children left after sending SIGKILL to the first child");
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
116*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
117*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
118*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
119*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
120*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
121*49cdfc7eSAndroid Build Coastguard Worker };
122