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