Skip to content

Commit ca523e8

Browse files
Wei Gao via ltpacerv
authored andcommitted
Add unshare03 test
Add a test case based on kernel self-test unshare_test.c to check that the kernel handles the EMFILE error when a parent process changes file descriptor limits and the child process tries to unshare (CLONE_FILES). Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com> Signed-off-by: Wei Gao <wegao@suse.com>
1 parent 4855075 commit ca523e8

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ unlinkat01 unlinkat01
17181718

17191719
unshare01 unshare01
17201720
unshare02 unshare02
1721+
unshare03 unshare03
17211722

17221723
#
17231724
# These tests require an unmounted block device
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/unshare01
22
/unshare02
3+
/unshare03
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk>
4+
* Copyright (C) 2024 Wei Gao <wegao@suse.com>
5+
*/
6+
7+
/*\
8+
* This test case based on kernel self-test unshare_test.c to check that
9+
* the kernel handles the EMFILE error when a parent process changes file
10+
* descriptor limits and the child process tries to unshare (CLONE_FILES).
11+
*/
12+
13+
#define _GNU_SOURCE
14+
15+
#include "tst_test.h"
16+
#include "config.h"
17+
#include "lapi/sched.h"
18+
19+
#define FS_NR_OPEN "/proc/sys/fs/nr_open"
20+
#define NR_OPEN_LIMIT 1024
21+
#define NR_OPEN_DUP 64
22+
23+
#ifdef HAVE_UNSHARE
24+
25+
static void run(void)
26+
{
27+
int nr_open;
28+
int nr_limit;
29+
struct rlimit rlimit;
30+
struct tst_clone_args args = {
31+
.flags = CLONE_FILES,
32+
.exit_signal = SIGCHLD,
33+
};
34+
35+
SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open);
36+
tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open);
37+
38+
nr_limit = nr_open + NR_OPEN_LIMIT;
39+
SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit);
40+
41+
SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit);
42+
43+
rlimit.rlim_cur = nr_limit;
44+
rlimit.rlim_max = nr_limit;
45+
46+
SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit);
47+
tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d",
48+
nr_limit);
49+
50+
SAFE_DUP2(2, nr_open + NR_OPEN_DUP);
51+
52+
if (!SAFE_CLONE(&args)) {
53+
SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open);
54+
TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE);
55+
exit(0);
56+
}
57+
58+
}
59+
60+
static void setup(void)
61+
{
62+
clone3_supported_by_kernel();
63+
}
64+
65+
static struct tst_test test = {
66+
.forks_child = 1,
67+
.needs_root = 1,
68+
.test_all = run,
69+
.setup = setup,
70+
.save_restore = (const struct tst_path_val[]) {
71+
{FS_NR_OPEN, NULL, TST_SR_TCONF},
72+
{}
73+
},
74+
};
75+
76+
#else
77+
TST_TEST_TCONF("unshare syscall is undefined.");
78+
#endif

0 commit comments

Comments
 (0)