File: fork--pidns-translation.c

package info (click to toggle)
strace 5.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 49,332 kB
  • sloc: ansic: 113,177; sh: 8,831; makefile: 3,108; awk: 364; perl: 267; sed: 9
file content (78 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download | duplicates (3)
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
73
74
75
76
77
78
/*
 * Test PID namespace translation
 *
 * Copyright (c) 2020 Ákos Uzonyi <uzonyi.akos@gmail.com>
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "tests.h"
#include "scno.h"
#include "pidns.h"

#ifdef __NR_fork

# include <errno.h>
# include <limits.h>
# include <sched.h>
# include <signal.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/wait.h>
# include <unistd.h>
# include <linux/sched.h>
# include "nsfs.h"

# ifndef CLONE_NEWUSER
#  define CLONE_NEWUSER 0x10000000
# endif

# ifndef CLONE_NEWPID
#  define CLONE_NEWPID 0x20000000
# endif

static int
fork_chain(int depth)
{
	if (!depth)
		return 0;

	int pid = syscall(__NR_fork);
	if (pid < 0)
		return errno;

	if (!pid)
		_exit(fork_chain(depth - 1));

	int status;
	if (wait(&status) < 0)
		return errno;

	if (!WIFEXITED(status))
		return -1;

	return WEXITSTATUS(status);
}

int main(void)
{
	check_ns_ioctl();

	if (unshare(CLONE_NEWPID | CLONE_NEWUSER) < 0) {
		if (errno == EPERM)
			perror_msg_and_skip("unshare");

		perror_msg_and_fail("unshare");
	}

	errno = fork_chain(2);
	if (errno)
		perror("fork_chain");
}

#else

SKIP_MAIN_UNDEFINED("__NR_fork")

#endif