* [LTP] [PATCH v1] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() @ 2024-12-19 8:19 Wei Gao via ltp 2025-02-19 15:12 ` Andrea Cervesato via ltp 2025-03-03 9:42 ` [LTP] [PATCH v2] " Wei Gao via ltp 0 siblings, 2 replies; 13+ messages in thread From: Wei Gao via ltp @ 2024-12-19 8:19 UTC (permalink / raw) To: ltp Signed-off-by: Wei Gao <wegao@suse.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/unshare/.gitignore | 1 + testcases/kernel/syscalls/unshare/unshare03.c | 91 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c diff --git a/runtest/syscalls b/runtest/syscalls index ded035ee8..10800c1a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 unshare01 unshare01 unshare02 unshare02 +unshare03 unshare03 # # These tests require an unmounted block device diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore index 855ffd055..e5b5c261d 100644 --- a/testcases/kernel/syscalls/unshare/.gitignore +++ b/testcases/kernel/syscalls/unshare/.gitignore @@ -1,2 +1,3 @@ /unshare01 /unshare02 +/unshare03 diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c new file mode 100644 index 000000000..0ff40b242 --- /dev/null +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> + * Copyright (C) 2024 Wei Gao <wegao@suse.com> + */ + +/*\ + * [Description] + * + * Test case is adapted from the kernel self test unshare_test.c. + * Test coverage for dup_fd() failure handling in unshare_fd() + */ + +#define _GNU_SOURCE + +#include <stdio.h> +#include <sys/wait.h> +#include <sys/types.h> +#include <sys/param.h> +#include <sys/syscall.h> +#include <sched.h> +#include <limits.h> +#include <unistd.h> + +#include "tst_test.h" +#include "config.h" +#include "lapi/sched.h" + +#define FS_NR_OPEN "/proc/sys/fs/nr_open" + +#ifdef HAVE_UNSHARE + +static void run(void) +{ + int nr_open; + struct rlimit rlimit; + pid_t pid; + struct clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); + + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); + + rlimit.rlim_cur = nr_open + 1024; + rlimit.rlim_max = nr_open + 1024; + + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); + + SAFE_DUP2(2, nr_open + 64); + + pid = clone3(&args, sizeof(args)); + + if (pid < 0) { + tst_res(TFAIL | TTERRNO, "clone3() failed"); + return; + } + + if (!pid) { + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); + exit(0); + } + + SAFE_WAITPID(pid, NULL, 0); +} + +static void setup(void) +{ + clone3_supported_by_kernel(); +} + +static struct tst_test test = { + .forks_child = 1, + .needs_tmpdir = 1, + .needs_root = 1, + .test_all = run, + .setup = setup, + .save_restore = (const struct tst_path_val[]) { + {FS_NR_OPEN, NULL, TST_SR_TCONF}, + {} + }, +}; + +#else +TST_TEST_TCONF("unshare is undefined."); +#endif -- 2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v1] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2024-12-19 8:19 [LTP] [PATCH v1] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() Wei Gao via ltp @ 2025-02-19 15:12 ` Andrea Cervesato via ltp 2025-03-03 9:42 ` [LTP] [PATCH v2] " Wei Gao via ltp 1 sibling, 0 replies; 13+ messages in thread From: Andrea Cervesato via ltp @ 2025-02-19 15:12 UTC (permalink / raw) To: Wei Gao, ltp Hi! On 12/19/24 09:19, Wei Gao via ltp wrote: > Signed-off-by: Wei Gao<wegao@suse.com> Missing an explanation of the commit. > --- > runtest/syscalls | 1 + > testcases/kernel/syscalls/unshare/.gitignore | 1 + > testcases/kernel/syscalls/unshare/unshare03.c | 91 +++++++++++++++++++ > 3 files changed, 93 insertions(+) > create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c > > diff --git a/runtest/syscalls b/runtest/syscalls > index ded035ee8..10800c1a3 100644 > --- a/runtest/syscalls > +++ b/runtest/syscalls > @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 > > unshare01 unshare01 > unshare02 unshare02 > +unshare03 unshare03 > > # > # These tests require an unmounted block device > diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore > index 855ffd055..e5b5c261d 100644 > --- a/testcases/kernel/syscalls/unshare/.gitignore > +++ b/testcases/kernel/syscalls/unshare/.gitignore > @@ -1,2 +1,3 @@ > /unshare01 > /unshare02 > +/unshare03 > diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c > new file mode 100644 > index 000000000..0ff40b242 > --- /dev/null > +++ b/testcases/kernel/syscalls/unshare/unshare03.c > @@ -0,0 +1,91 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2024 Al Viro<viro@zeniv.linux.org.uk> > + * Copyright (C) 2024 Wei Gao<wegao@suse.com> > + */ > + > +/*\ > + * [Description] No needed anymore. > + * > + * Test case is adapted from the kernel self test unshare_test.c. > + * Test coverage for dup_fd() failure handling in unshare_fd() The test is verifying whether unshare() raises EMFILE error when we attempt to release the file descriptor table shared with the parent process, after opening a new file descriptor in the parent and modifying the maximum number of file descriptors in the child. > + */ > + > +#define _GNU_SOURCE > + > +#include <stdio.h> > +#include <sys/wait.h> > +#include <sys/types.h> > +#include <sys/param.h> > +#include <sys/syscall.h> > +#include <sched.h> > +#include <limits.h> > +#include <unistd.h> All these imports are not needed. > + > +#include "tst_test.h" > +#include "config.h" > +#include "lapi/sched.h" > + > +#define FS_NR_OPEN "/proc/sys/fs/nr_open" > + > +#ifdef HAVE_UNSHARE > + > +static void run(void) > +{ > + int nr_open; > + struct rlimit rlimit; > + pid_t pid; > + struct clone_args args = { > + .flags = CLONE_FILES, > + .exit_signal = SIGCHLD, > + }; > + > + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); > + > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); > + > + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); I don't get the point of this call, even in the kselftests. The limits are overridden in the next 2 lines.. > + > + rlimit.rlim_cur = nr_open + 1024; > + rlimit.rlim_max = nr_open + 1024; > + > + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); > + > + SAFE_DUP2(2, nr_open + 64); We have the control over file descriptors in this process, so I don't see why adding such a big number of file descriptors, but I guess nothing happens if we are adding high numbers. Maybe rlimit.rlim_cur/rlim_max + 16 would be enough. And then here we can use nr_open + 8 ... > + > + pid = clone3(&args, sizeof(args)); > + > + if (pid < 0) { We can use SAFE_CLONE() here > + tst_res(TFAIL | TTERRNO, "clone3() failed"); > + return; > + } > + > + if (!pid) { > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > + exit(0); > + } > + > + SAFE_WAITPID(pid, NULL, 0); No need for this, tst_reap_children() is running automatically at the end of run(). > +} > + > +static void setup(void) > +{ > + clone3_supported_by_kernel(); > +} > + > +static struct tst_test test = { > + .forks_child = 1, > + .needs_tmpdir = 1, Temporary folder is not used. > + .needs_root = 1, > + .test_all = run, > + .setup = setup, > + .save_restore = (const struct tst_path_val[]) { > + {FS_NR_OPEN, NULL, TST_SR_TCONF}, > + {} > + }, > +}; > + > +#else > +TST_TEST_TCONF("unshare is undefined."); > +#endif > Kind regards, Andrea Cervesato -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* [LTP] [PATCH v2] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2024-12-19 8:19 [LTP] [PATCH v1] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() Wei Gao via ltp 2025-02-19 15:12 ` Andrea Cervesato via ltp @ 2025-03-03 9:42 ` Wei Gao via ltp 2025-03-03 10:47 ` Andrea Cervesato via ltp 2025-03-04 3:43 ` [LTP] [PATCH v3] " Wei Gao via ltp 1 sibling, 2 replies; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-03 9:42 UTC (permalink / raw) To: ltp This is new test case adapted from the kernel self test unshare_test.c. It verifies that the kernel correctly handles the EMFILE error condition during file descriptor table unsharing, specifically when the parent process modifies the file descriptor limits and the child process attempts to unshare(CLONE_FILES). Signed-off-by: Wei Gao <wegao@suse.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/unshare/.gitignore | 1 + testcases/kernel/syscalls/unshare/unshare03.c | 75 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c diff --git a/runtest/syscalls b/runtest/syscalls index ded035ee8..10800c1a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 unshare01 unshare01 unshare02 unshare02 +unshare03 unshare03 # # These tests require an unmounted block device diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore index 855ffd055..e5b5c261d 100644 --- a/testcases/kernel/syscalls/unshare/.gitignore +++ b/testcases/kernel/syscalls/unshare/.gitignore @@ -1,2 +1,3 @@ /unshare01 /unshare02 +/unshare03 diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c new file mode 100644 index 000000000..c8baecc10 --- /dev/null +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> + * Copyright (C) 2024 Wei Gao <wegao@suse.com> + */ + +/*\ + * The test is verifying whether unshare() raises EMFILE error when we + * attempt to release the file descriptor table shared with the parent + * process, after opening a new file descriptor in the parent and modifying + * the maximum number of file descriptors in the child. + */ + +#define _GNU_SOURCE + +#include "tst_test.h" +#include "config.h" +#include "lapi/sched.h" + +#define FS_NR_OPEN "/proc/sys/fs/nr_open" + +#ifdef HAVE_UNSHARE + +static void run(void) +{ + int nr_open; + struct rlimit rlimit; + pid_t pid; + struct tst_clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); + + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + + rlimit.rlim_cur = nr_open + 16; + rlimit.rlim_max = nr_open + 16; + + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); + + SAFE_DUP2(2, nr_open + 8); + + if (!SAFE_CLONE(&args)) { + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); + TST_CHECKPOINT_WAKE(0); + exit(0); + } + + TST_CHECKPOINT_WAIT(0); + tst_res(TPASS, "Verify EMFILE error pass"); +} + +static void setup(void) +{ + clone3_supported_by_kernel(); +} + +static struct tst_test test = { + .forks_child = 1, + .needs_root = 1, + .test_all = run, + .setup = setup, + .needs_checkpoints = 1, + .save_restore = (const struct tst_path_val[]) { + {FS_NR_OPEN, NULL, TST_SR_TCONF}, + {} + }, +}; + +#else +TST_TEST_TCONF("unshare is undefined."); +#endif -- 2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v2] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-03 9:42 ` [LTP] [PATCH v2] " Wei Gao via ltp @ 2025-03-03 10:47 ` Andrea Cervesato via ltp 2025-03-04 4:03 ` Wei Gao via ltp 2025-03-04 3:43 ` [LTP] [PATCH v3] " Wei Gao via ltp 1 sibling, 1 reply; 13+ messages in thread From: Andrea Cervesato via ltp @ 2025-03-03 10:47 UTC (permalink / raw) To: Wei Gao, ltp Hi! On 3/3/25 10:42, Wei Gao via ltp wrote: > This is new test case adapted from the kernel self test unshare_test.c. > It verifies that the kernel correctly handles the EMFILE error condition > during file descriptor table unsharing, specifically when the parent > process modifies the file descriptor limits and the child process attempts > to unshare(CLONE_FILES). 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). > Signed-off-by: Wei Gao <wegao@suse.com> > --- > runtest/syscalls | 1 + > testcases/kernel/syscalls/unshare/.gitignore | 1 + > testcases/kernel/syscalls/unshare/unshare03.c | 75 +++++++++++++++++++ > 3 files changed, 77 insertions(+) > create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c > > diff --git a/runtest/syscalls b/runtest/syscalls > index ded035ee8..10800c1a3 100644 > --- a/runtest/syscalls > +++ b/runtest/syscalls > @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 > > unshare01 unshare01 > unshare02 unshare02 > +unshare03 unshare03 > > # > # These tests require an unmounted block device > diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore > index 855ffd055..e5b5c261d 100644 > --- a/testcases/kernel/syscalls/unshare/.gitignore > +++ b/testcases/kernel/syscalls/unshare/.gitignore > @@ -1,2 +1,3 @@ > /unshare01 > /unshare02 > +/unshare03 > diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c > new file mode 100644 > index 000000000..c8baecc10 > --- /dev/null > +++ b/testcases/kernel/syscalls/unshare/unshare03.c > @@ -0,0 +1,75 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> > + * Copyright (C) 2024 Wei Gao <wegao@suse.com> > + */ > + > +/*\ > + * The test is verifying whether unshare() raises EMFILE error when we > + * attempt to release the file descriptor table shared with the parent > + * process, after opening a new file descriptor in the parent and modifying > + * the maximum number of file descriptors in the child. Probably we can use the commit message here :-) > + */ > + > +#define _GNU_SOURCE > + > +#include "tst_test.h" > +#include "config.h" > +#include "lapi/sched.h" > + > +#define FS_NR_OPEN "/proc/sys/fs/nr_open" > + > +#ifdef HAVE_UNSHARE > + > +static void run(void) > +{ > + int nr_open; > + struct rlimit rlimit; > + pid_t pid; > + struct tst_clone_args args = { > + .flags = CLONE_FILES, > + .exit_signal = SIGCHLD, > + }; > + > + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); Here we can print the number of open file descriptors, using tst_res(), to help debugging. > + > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); In the previous version I made a mistake in the review. The original test is using /proc/sys/fs/nr_open to set limits first, then it reads back them from the same file in order to have a starting limit. This is probably needed due to the kernel configurations. So please bring back the SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); line I asked to remove. Sorry for that. Also, if we are going to use new increments, we need to update the next increments as well according to the previous ones. So feel free to leave it as it was before. I must have been distracted that day :-) > + > + rlimit.rlim_cur = nr_open + 16; > + rlimit.rlim_max = nr_open + 16; > + > + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); Here we can print the new limits after updating it, using tst_res(), to help debugging. > + > + SAFE_DUP2(2, nr_open + 8); > + > + if (!SAFE_CLONE(&args)) { > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > + TST_CHECKPOINT_WAKE(0); > + exit(0); > + } > + > + TST_CHECKPOINT_WAIT(0); > + tst_res(TPASS, "Verify EMFILE error pass"); We don't need this since we already have TST_EXP_FAIL inside the child process. > +} > + > +static void setup(void) > +{ > + clone3_supported_by_kernel(); > +} > + > +static struct tst_test test = { > + .forks_child = 1, > + .needs_root = 1, > + .test_all = run, > + .setup = setup, > + .needs_checkpoints = 1, > + .save_restore = (const struct tst_path_val[]) { > + {FS_NR_OPEN, NULL, TST_SR_TCONF}, > + {} > + }, > +}; > + > +#else > +TST_TEST_TCONF("unshare is undefined."); unshare syscall is undefined. > +#endif The rest looks ok. Andrea -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v2] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-03 10:47 ` Andrea Cervesato via ltp @ 2025-03-04 4:03 ` Wei Gao via ltp 0 siblings, 0 replies; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-04 4:03 UTC (permalink / raw) To: Andrea Cervesato; +Cc: ltp On Mon, Mar 03, 2025 at 11:47:34AM +0100, Andrea Cervesato wrote: > Hi! > > > + > > + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); > Here we can print the number of open file descriptors, using tst_res(), to > help debugging. > > + > > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); > > In the previous version I made a mistake in the review. The original test is > using /proc/sys/fs/nr_open to set limits first, then it reads back them from > the same file in order to have a starting limit. This is probably needed due > to the kernel configurations. So please bring back the > SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); line I asked to remove. Sorry for > that. > > Also, if we are going to use new increments, we need to update the next > increments as well according to the previous ones. So feel free to leave it > as it was before. > > I must have been distracted that day :-) No problem at all :) I have sent new patch, feedback me if any misunderstanding. > > > The rest looks ok. > > Andrea > -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* [LTP] [PATCH v3] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-03 9:42 ` [LTP] [PATCH v2] " Wei Gao via ltp 2025-03-03 10:47 ` Andrea Cervesato via ltp @ 2025-03-04 3:43 ` Wei Gao via ltp 2025-03-04 4:06 ` [LTP] [PATCH v4] " Wei Gao via ltp 1 sibling, 1 reply; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-04 3:43 UTC (permalink / raw) To: ltp 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). Signed-off-by: Wei Gao <wegao@suse.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/unshare/.gitignore | 1 + testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c diff --git a/runtest/syscalls b/runtest/syscalls index ded035ee8..10800c1a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 unshare01 unshare01 unshare02 unshare02 +unshare03 unshare03 # # These tests require an unmounted block device diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore index 855ffd055..e5b5c261d 100644 --- a/testcases/kernel/syscalls/unshare/.gitignore +++ b/testcases/kernel/syscalls/unshare/.gitignore @@ -1,2 +1,3 @@ /unshare01 /unshare02 +/unshare03 diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c new file mode 100644 index 000000000..15115501a --- /dev/null +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> + * Copyright (C) 2024 Wei Gao <wegao@suse.com> + */ + +/*\ + * This 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). + */ + +#define _GNU_SOURCE + +#include "tst_test.h" +#include "config.h" +#include "lapi/sched.h" + +#define FS_NR_OPEN "/proc/sys/fs/nr_open" + +#ifdef HAVE_UNSHARE + +static void run(void) +{ + int nr_open; + struct rlimit rlimit; + struct tst_clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); + tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); + + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); + + rlimit.rlim_cur = nr_open + 1024; + rlimit.rlim_max = nr_open + 1024; + + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); + tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", + nr_open + 1024); + + SAFE_DUP2(2, nr_open + 64); + + if (!SAFE_CLONE(&args)) { + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); + TST_CHECKPOINT_WAKE(0); + exit(0); + } + + TST_CHECKPOINT_WAIT(0); +} + +static void setup(void) +{ + clone3_supported_by_kernel(); +} + +static struct tst_test test = { + .forks_child = 1, + .needs_root = 1, + .test_all = run, + .setup = setup, + .needs_checkpoints = 1, + .save_restore = (const struct tst_path_val[]) { + {FS_NR_OPEN, NULL, TST_SR_TCONF}, + {} + }, +}; + +#else +TST_TEST_TCONF("unshare is undefined."); +#endif -- 2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
* [LTP] [PATCH v4] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-04 3:43 ` [LTP] [PATCH v3] " Wei Gao via ltp @ 2025-03-04 4:06 ` Wei Gao via ltp 2025-03-04 8:31 ` Andrea Cervesato via ltp 2025-03-06 2:22 ` [LTP] [PATCH v5] " Wei Gao via ltp 0 siblings, 2 replies; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-04 4:06 UTC (permalink / raw) To: ltp 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). Signed-off-by: Wei Gao <wegao@suse.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/unshare/.gitignore | 1 + testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c diff --git a/runtest/syscalls b/runtest/syscalls index ded035ee8..10800c1a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 unshare01 unshare01 unshare02 unshare02 +unshare03 unshare03 # # These tests require an unmounted block device diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore index 855ffd055..e5b5c261d 100644 --- a/testcases/kernel/syscalls/unshare/.gitignore +++ b/testcases/kernel/syscalls/unshare/.gitignore @@ -1,2 +1,3 @@ /unshare01 /unshare02 +/unshare03 diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c new file mode 100644 index 000000000..c1f13bcd5 --- /dev/null +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> + * Copyright (C) 2024 Wei Gao <wegao@suse.com> + */ + +/*\ + * This 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). + */ + +#define _GNU_SOURCE + +#include "tst_test.h" +#include "config.h" +#include "lapi/sched.h" + +#define FS_NR_OPEN "/proc/sys/fs/nr_open" + +#ifdef HAVE_UNSHARE + +static void run(void) +{ + int nr_open; + struct rlimit rlimit; + struct tst_clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); + tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); + + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); + + rlimit.rlim_cur = nr_open + 1024; + rlimit.rlim_max = nr_open + 1024; + + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); + tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", + nr_open + 1024); + + SAFE_DUP2(2, nr_open + 64); + + if (!SAFE_CLONE(&args)) { + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); + TST_CHECKPOINT_WAKE(0); + exit(0); + } + + TST_CHECKPOINT_WAIT(0); +} + +static void setup(void) +{ + clone3_supported_by_kernel(); +} + +static struct tst_test test = { + .forks_child = 1, + .needs_root = 1, + .test_all = run, + .setup = setup, + .needs_checkpoints = 1, + .save_restore = (const struct tst_path_val[]) { + {FS_NR_OPEN, NULL, TST_SR_TCONF}, + {} + }, +}; + +#else +TST_TEST_TCONF("unshare syscall is undefined."); +#endif -- 2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-04 4:06 ` [LTP] [PATCH v4] " Wei Gao via ltp @ 2025-03-04 8:31 ` Andrea Cervesato via ltp 2025-03-05 7:04 ` Wei Gao via ltp 2025-03-06 2:22 ` [LTP] [PATCH v5] " Wei Gao via ltp 1 sibling, 1 reply; 13+ messages in thread From: Andrea Cervesato via ltp @ 2025-03-04 8:31 UTC (permalink / raw) To: Wei Gao, ltp Hi, thanks for editing the last version. A couple of comments below. On 3/4/25 05:06, Wei Gao via ltp wrote: > 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). > > Signed-off-by: Wei Gao <wegao@suse.com> > --- > runtest/syscalls | 1 + > testcases/kernel/syscalls/unshare/.gitignore | 1 + > testcases/kernel/syscalls/unshare/unshare03.c | 77 +++++++++++++++++++ > 3 files changed, 79 insertions(+) > create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c > > diff --git a/runtest/syscalls b/runtest/syscalls > index ded035ee8..10800c1a3 100644 > --- a/runtest/syscalls > +++ b/runtest/syscalls > @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 > > unshare01 unshare01 > unshare02 unshare02 > +unshare03 unshare03 > > # > # These tests require an unmounted block device > diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore > index 855ffd055..e5b5c261d 100644 > --- a/testcases/kernel/syscalls/unshare/.gitignore > +++ b/testcases/kernel/syscalls/unshare/.gitignore > @@ -1,2 +1,3 @@ > /unshare01 > /unshare02 > +/unshare03 > diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c > new file mode 100644 > index 000000000..c1f13bcd5 > --- /dev/null > +++ b/testcases/kernel/syscalls/unshare/unshare03.c > @@ -0,0 +1,77 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> > + * Copyright (C) 2024 Wei Gao <wegao@suse.com> > + */ > + > +/*\ > + * This 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). > + */ > + > +#define _GNU_SOURCE > + > +#include "tst_test.h" > +#include "config.h" > +#include "lapi/sched.h" > + > +#define FS_NR_OPEN "/proc/sys/fs/nr_open" > + > +#ifdef HAVE_UNSHARE > + > +static void run(void) > +{ > + int nr_open; > + struct rlimit rlimit; > + struct tst_clone_args args = { > + .flags = CLONE_FILES, > + .exit_signal = SIGCHLD, > + }; > + > + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); > + tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); > + > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); > + > + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); > + > + rlimit.rlim_cur = nr_open + 1024; > + rlimit.rlim_max = nr_open + 1024; > + > + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); > + tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", > + nr_open + 1024); > + > + SAFE_DUP2(2, nr_open + 64); > + > + if (!SAFE_CLONE(&args)) { > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > + TST_CHECKPOINT_WAKE(0); There's no need to use synchronization mechanisms because at the end of the test we call tst_reap_children() waiting for all the children to be completed. > + exit(0); > + } > + > + TST_CHECKPOINT_WAIT(0); > +} > + > +static void setup(void) > +{ > + clone3_supported_by_kernel(); > +} > + > +static struct tst_test test = { > + .forks_child = 1, > + .needs_root = 1, > + .test_all = run, > + .setup = setup, > + .needs_checkpoints = 1, > + .save_restore = (const struct tst_path_val[]) { > + {FS_NR_OPEN, NULL, TST_SR_TCONF}, > + {} > + }, > +}; > + > +#else > +TST_TEST_TCONF("unshare syscall is undefined."); > +#endif The rest looks good. If you want I can edit the checkpoint thing and merge this patch. Kind regards, Andrea Cervesato -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-04 8:31 ` Andrea Cervesato via ltp @ 2025-03-05 7:04 ` Wei Gao via ltp 2025-03-05 12:11 ` Andrea Cervesato via ltp 0 siblings, 1 reply; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-05 7:04 UTC (permalink / raw) To: Andrea Cervesato; +Cc: ltp On Tue, Mar 04, 2025 at 09:31:03AM +0100, Andrea Cervesato wrote: > Hi, > > thanks for editing the last version. A couple of comments below. > > On 3/4/25 05:06, Wei Gao via ltp wrote: > > + nr_open + 1024); > > + > > + SAFE_DUP2(2, nr_open + 64); > > + > > + if (!SAFE_CLONE(&args)) { > > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > > + TST_CHECKPOINT_WAKE(0); > There's no need to use synchronization mechanisms because at the end of the > test we call tst_reap_children() waiting for all the children to be > completed. There are some race condition happen and trigger failure sometimes if you do not use this synchronization(Currently no idea why this happen). Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env. > > + exit(0); > > + } > > + > > + TST_CHECKPOINT_WAIT(0); > > +} > > + > > +static void setup(void) > > +{ > > + clone3_supported_by_kernel(); > > +} > > + > > +static struct tst_test test = { > > + .forks_child = 1, > > + .needs_root = 1, > > + .test_all = run, > > + .setup = setup, > > + .needs_checkpoints = 1, > > + .save_restore = (const struct tst_path_val[]) { > > + {FS_NR_OPEN, NULL, TST_SR_TCONF}, > > + {} > > + }, > > +}; > > + > > +#else > > +TST_TEST_TCONF("unshare syscall is undefined."); > > +#endif > > The rest looks good. If you want I can edit the checkpoint thing and merge > this patch. > > Kind regards, > Andrea Cervesato > -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-05 7:04 ` Wei Gao via ltp @ 2025-03-05 12:11 ` Andrea Cervesato via ltp 2025-03-06 2:24 ` Wei Gao via ltp 0 siblings, 1 reply; 13+ messages in thread From: Andrea Cervesato via ltp @ 2025-03-05 12:11 UTC (permalink / raw) To: Wei Gao; +Cc: ltp Hi, On 3/5/25 08:04, Wei Gao wrote: >>> + if (!SAFE_CLONE(&args)) { >>> + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); >>> + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); >>> + TST_CHECKPOINT_WAKE(0); >> There's no need to use synchronization mechanisms because at the end of the >> test we call tst_reap_children() waiting for all the children to be >> completed. > There are some race condition happen and trigger failure sometimes if you do not > use this synchronization(Currently no idea why this happen). > Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env. I'm really trying to reproduce this issue, but I can't. Run the test with 100000 iterations and it didn't stuck. Can you please check again? I tried on TW. Andrea -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-05 12:11 ` Andrea Cervesato via ltp @ 2025-03-06 2:24 ` Wei Gao via ltp 0 siblings, 0 replies; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-06 2:24 UTC (permalink / raw) To: Andrea Cervesato; +Cc: ltp On Wed, Mar 05, 2025 at 01:11:20PM +0100, Andrea Cervesato wrote: > Hi, > > On 3/5/25 08:04, Wei Gao wrote: > > > > + if (!SAFE_CLONE(&args)) { > > > > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > > > > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > > > > + TST_CHECKPOINT_WAKE(0); > > > There's no need to use synchronization mechanisms because at the end of the > > > test we call tst_reap_children() waiting for all the children to be > > > completed. > > There are some race condition happen and trigger failure sometimes if you do not > > use this synchronization(Currently no idea why this happen). > > Rerun case 30 times can trigger 2 or 3 cases failed if not use synchronization in my env. > > I'm really trying to reproduce this issue, but I can't. Run the test with > 100000 iterations and it didn't stuck. Can you please check again? I tried > on TW. I have retest on my local machine with following builds, issue seems not happen: * openSUSE Leap 15.5 5.14.21-150500.53-default * Debian 6.12.0-rc4-00047-gc2ee9f594da8-dirty so i suppose we can merge my latest v5 patch without sync. > > Andrea -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 13+ messages in thread
* [LTP] [PATCH v5] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-04 4:06 ` [LTP] [PATCH v4] " Wei Gao via ltp 2025-03-04 8:31 ` Andrea Cervesato via ltp @ 2025-03-06 2:22 ` Wei Gao via ltp 2025-03-06 8:23 ` Andrea Cervesato via ltp 1 sibling, 1 reply; 13+ messages in thread From: Wei Gao via ltp @ 2025-03-06 2:22 UTC (permalink / raw) To: ltp 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). Signed-off-by: Wei Gao <wegao@suse.com> --- runtest/syscalls | 1 + testcases/kernel/syscalls/unshare/.gitignore | 1 + testcases/kernel/syscalls/unshare/unshare03.c | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c diff --git a/runtest/syscalls b/runtest/syscalls index ded035ee8..10800c1a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 unshare01 unshare01 unshare02 unshare02 +unshare03 unshare03 # # These tests require an unmounted block device diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore index 855ffd055..e5b5c261d 100644 --- a/testcases/kernel/syscalls/unshare/.gitignore +++ b/testcases/kernel/syscalls/unshare/.gitignore @@ -1,2 +1,3 @@ /unshare01 /unshare02 +/unshare03 diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c new file mode 100644 index 000000000..7298cdebe --- /dev/null +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> + * Copyright (C) 2024 Wei Gao <wegao@suse.com> + */ + +/*\ + * This 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). + */ + +#define _GNU_SOURCE + +#include "tst_test.h" +#include "config.h" +#include "lapi/sched.h" + +#define FS_NR_OPEN "/proc/sys/fs/nr_open" + +#ifdef HAVE_UNSHARE + +static void run(void) +{ + int nr_open; + struct rlimit rlimit; + struct tst_clone_args args = { + .flags = CLONE_FILES, + .exit_signal = SIGCHLD, + }; + + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); + tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); + + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); + + rlimit.rlim_cur = nr_open + 1024; + rlimit.rlim_max = nr_open + 1024; + + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); + tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", + nr_open + 1024); + + SAFE_DUP2(2, nr_open + 64); + + if (!SAFE_CLONE(&args)) { + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); + exit(0); + } + +} + +static void setup(void) +{ + clone3_supported_by_kernel(); +} + +static struct tst_test test = { + .forks_child = 1, + .needs_root = 1, + .test_all = run, + .setup = setup, + .save_restore = (const struct tst_path_val[]) { + {FS_NR_OPEN, NULL, TST_SR_TCONF}, + {} + }, +}; + +#else +TST_TEST_TCONF("unshare syscall is undefined."); +#endif -- 2.35.3 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v5] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() 2025-03-06 2:22 ` [LTP] [PATCH v5] " Wei Gao via ltp @ 2025-03-06 8:23 ` Andrea Cervesato via ltp 0 siblings, 0 replies; 13+ messages in thread From: Andrea Cervesato via ltp @ 2025-03-06 8:23 UTC (permalink / raw) To: Wei Gao, ltp Hi! Pushed with a small edit to define limits on top. Commit title was a bit too long, so I changed it. Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com> diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c index 7298cdebe..7c5e71c4e 100644 --- a/testcases/kernel/syscalls/unshare/unshare03.c +++ b/testcases/kernel/syscalls/unshare/unshare03.c @@ -17,12 +17,15 @@ #include "lapi/sched.h" #define FS_NR_OPEN "/proc/sys/fs/nr_open" +#define NR_OPEN_LIMIT 1024 +#define NR_OPEN_DUP 64 #ifdef HAVE_UNSHARE static void run(void) { int nr_open; + int nr_limit; struct rlimit rlimit; struct tst_clone_args args = { .flags = CLONE_FILES, @@ -32,18 +35,19 @@ static void run(void) SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); - SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); + nr_limit = nr_open + NR_OPEN_LIMIT; + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_limit); SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); - rlimit.rlim_cur = nr_open + 1024; - rlimit.rlim_max = nr_open + 1024; + rlimit.rlim_cur = nr_limit; + rlimit.rlim_max = nr_limit; SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", - nr_open + 1024); + nr_limit); - SAFE_DUP2(2, nr_open + 64); + SAFE_DUP2(2, nr_open + NR_OPEN_DUP); if (!SAFE_CLONE(&args)) { SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); Andrea On 3/6/25 03:22, Wei Gao via ltp wrote: > 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). > > Signed-off-by: Wei Gao <wegao@suse.com> > --- > runtest/syscalls | 1 + > testcases/kernel/syscalls/unshare/.gitignore | 1 + > testcases/kernel/syscalls/unshare/unshare03.c | 74 +++++++++++++++++++ > 3 files changed, 76 insertions(+) > create mode 100644 testcases/kernel/syscalls/unshare/unshare03.c > > diff --git a/runtest/syscalls b/runtest/syscalls > index ded035ee8..10800c1a3 100644 > --- a/runtest/syscalls > +++ b/runtest/syscalls > @@ -1715,6 +1715,7 @@ unlinkat01 unlinkat01 > > unshare01 unshare01 > unshare02 unshare02 > +unshare03 unshare03 > > # > # These tests require an unmounted block device > diff --git a/testcases/kernel/syscalls/unshare/.gitignore b/testcases/kernel/syscalls/unshare/.gitignore > index 855ffd055..e5b5c261d 100644 > --- a/testcases/kernel/syscalls/unshare/.gitignore > +++ b/testcases/kernel/syscalls/unshare/.gitignore > @@ -1,2 +1,3 @@ > /unshare01 > /unshare02 > +/unshare03 > diff --git a/testcases/kernel/syscalls/unshare/unshare03.c b/testcases/kernel/syscalls/unshare/unshare03.c > new file mode 100644 > index 000000000..7298cdebe > --- /dev/null > +++ b/testcases/kernel/syscalls/unshare/unshare03.c > @@ -0,0 +1,74 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2024 Al Viro <viro@zeniv.linux.org.uk> > + * Copyright (C) 2024 Wei Gao <wegao@suse.com> > + */ > + > +/*\ > + * This 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). > + */ > + > +#define _GNU_SOURCE > + > +#include "tst_test.h" > +#include "config.h" > +#include "lapi/sched.h" > + > +#define FS_NR_OPEN "/proc/sys/fs/nr_open" > + > +#ifdef HAVE_UNSHARE > + > +static void run(void) > +{ > + int nr_open; > + struct rlimit rlimit; > + struct tst_clone_args args = { > + .flags = CLONE_FILES, > + .exit_signal = SIGCHLD, > + }; > + > + SAFE_FILE_SCANF(FS_NR_OPEN, "%d", &nr_open); > + tst_res(TDEBUG, "Maximum number of file descriptors: %d", nr_open); > + > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open + 1024); > + > + SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlimit); > + > + rlimit.rlim_cur = nr_open + 1024; > + rlimit.rlim_max = nr_open + 1024; > + > + SAFE_SETRLIMIT(RLIMIT_NOFILE, &rlimit); > + tst_res(TDEBUG, "Set new maximum number of file descriptors to : %d", > + nr_open + 1024); > + > + SAFE_DUP2(2, nr_open + 64); > + > + if (!SAFE_CLONE(&args)) { > + SAFE_FILE_PRINTF(FS_NR_OPEN, "%d", nr_open); > + TST_EXP_FAIL(unshare(CLONE_FILES), EMFILE); > + exit(0); > + } > + > +} > + > +static void setup(void) > +{ > + clone3_supported_by_kernel(); > +} > + > +static struct tst_test test = { > + .forks_child = 1, > + .needs_root = 1, > + .test_all = run, > + .setup = setup, > + .save_restore = (const struct tst_path_val[]) { > + {FS_NR_OPEN, NULL, TST_SR_TCONF}, > + {} > + }, > +}; > + > +#else > +TST_TEST_TCONF("unshare syscall is undefined."); > +#endif -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 13+ messages in thread
end of thread, other threads:[~2025-03-06 8:23 UTC | newest] Thread overview: 13+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-12-19 8:19 [LTP] [PATCH v1] unshare03.c: Add test coverage for dup_fd() failure handling in unshare_fd() Wei Gao via ltp 2025-02-19 15:12 ` Andrea Cervesato via ltp 2025-03-03 9:42 ` [LTP] [PATCH v2] " Wei Gao via ltp 2025-03-03 10:47 ` Andrea Cervesato via ltp 2025-03-04 4:03 ` Wei Gao via ltp 2025-03-04 3:43 ` [LTP] [PATCH v3] " Wei Gao via ltp 2025-03-04 4:06 ` [LTP] [PATCH v4] " Wei Gao via ltp 2025-03-04 8:31 ` Andrea Cervesato via ltp 2025-03-05 7:04 ` Wei Gao via ltp 2025-03-05 12:11 ` Andrea Cervesato via ltp 2025-03-06 2:24 ` Wei Gao via ltp 2025-03-06 2:22 ` [LTP] [PATCH v5] " Wei Gao via ltp 2025-03-06 8:23 ` Andrea Cervesato via ltp
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox