1 #define _GNU_SOURCE
2 #include <assert.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <linux/types.h>
6 #include <sched.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <syscall.h>
12 #include <sys/wait.h>
13 
14 #include "../kselftest_harness.h"
15 #include "../pidfd/pidfd.h"
16 
17 /*
18  * Regression test for:
19  * 35f71bc0a09a ("fork: report pid reservation failure properly")
20  * b26ebfe12f34 ("pid: Fix error return value in some cases")
21  */
TEST(regression_enomem)22 TEST(regression_enomem)
23 {
24 	pid_t pid;
25 
26 	if (geteuid())
27 		EXPECT_EQ(0, unshare(CLONE_NEWUSER));
28 
29 	EXPECT_EQ(0, unshare(CLONE_NEWPID));
30 
31 	pid = fork();
32 	ASSERT_GE(pid, 0);
33 
34 	if (pid == 0)
35 		exit(EXIT_SUCCESS);
36 
37 	EXPECT_EQ(0, wait_for_pid(pid));
38 
39 	pid = fork();
40 	ASSERT_LT(pid, 0);
41 	ASSERT_EQ(errno, ENOMEM);
42 }
43 
44 TEST_HARNESS_MAIN
45