aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/containers/pidns/pidns32.c
blob: 3f7df788eef2a6551132227c348dd31b4ae1368e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) Huawei Technologies Co., Ltd., 2015
 * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
 */

/*\
 * [Description]
 *
 * Clone a process with CLONE_NEWPID flag and check for the maxium amount of
 * nested containers.
 */

#define _GNU_SOURCE

#include <sys/mman.h>
#include "tst_test.h"
#include "lapi/sched.h"

#define MAXNEST 32

static int *level;

static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
{
	pid_t cpid;
	int status;

	if (*level == MAXNEST)
		return 0;

	(*level)++;

	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
	if (cpid < 0)
		tst_brk(TBROK | TERRNO, "clone failed");

	SAFE_WAITPID(cpid, &status, 0);

	return 0;
}

static void setup(void)
{
	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
}

static void run(void)
{
	int ret, status;

	*level = 1;

	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
	if (ret < 0)
		tst_brk(TBROK | TERRNO, "clone failed");

	SAFE_WAITPID(ret, &status, 0);

	if (*level < MAXNEST) {
		tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
		return;
	}

	tst_res(TPASS, "All %d containers have been nested", MAXNEST);
}

static struct tst_test test = {
	.test_all = run,
	.needs_root = 1,
	.setup = setup,
};