From: lufei <lufei@uniontech.com>
To: linux-kselftest@vger.kernel.org
Cc: skhan@linuxfoundation.org, pvorel@suse.cz,
viro@zeniv.linux.org.uk, lufei <lufei@uniontech.com>
Subject: [PATCH v3] selftests:core: unshare_test: using bits_per_long instead of nr_open+64.
Date: Mon, 14 Apr 2025 10:17:01 +0800 [thread overview]
Message-ID: <20250414021701.136291-1-lufei@uniontech.com> (raw)
In-Reply-To: <20250401015557.113872-1-lufei@uniontech.com>
dup2(2, nr_open+64) may cause ENOMEM error, change to dup2(2,
bits_per_long +1) and set nr_open to bits_per_long to avoid ENOMEM.
Signed-off-by: lufei <lufei@uniontech.com>
---
tools/testing/selftests/core/unshare_test.c | 32 +++++++--------------
1 file changed, 11 insertions(+), 21 deletions(-)
diff --git a/tools/testing/selftests/core/unshare_test.c b/tools/testing/selftests/core/unshare_test.c
index 7fec9dfb1b0e..ea4caa7ec28d 100644
--- a/tools/testing/selftests/core/unshare_test.c
+++ b/tools/testing/selftests/core/unshare_test.c
@@ -28,8 +28,8 @@ TEST(unshare_EMFILE)
int fd;
ssize_t n, n2;
static char buf[512], buf2[512];
- struct rlimit rlimit;
int nr_open;
+ int bits_per_long = sizeof(long) * 8;
fd = open("/proc/sys/fs/nr_open", O_RDWR);
ASSERT_GE(fd, 0);
@@ -40,24 +40,8 @@ TEST(unshare_EMFILE)
ASSERT_EQ(sscanf(buf, "%d", &nr_open), 1);
- ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlimit));
-
- /* bump fs.nr_open */
- n2 = sprintf(buf2, "%d\n", nr_open + 1024);
- lseek(fd, 0, SEEK_SET);
- write(fd, buf2, n2);
-
- /* bump ulimit -n */
- rlimit.rlim_cur = nr_open + 1024;
- rlimit.rlim_max = nr_open + 1024;
- EXPECT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlimit)) {
- lseek(fd, 0, SEEK_SET);
- write(fd, buf, n);
- exit(EXIT_FAILURE);
- }
-
- /* get a descriptor past the old fs.nr_open */
- EXPECT_GE(dup2(2, nr_open + 64), 0) {
+ /* get a descriptor >= bits_per_long */
+ EXPECT_GE(dup2(2, bits_per_long+1), 0) {
lseek(fd, 0, SEEK_SET);
write(fd, buf, n);
exit(EXIT_FAILURE);
@@ -74,15 +58,21 @@ TEST(unshare_EMFILE)
if (pid == 0) {
int err;
- /* restore fs.nr_open */
+ /* set nr_open == bits_per_long */
+ n2 = sprintf(buf2, "%d\n", bits_per_long);
lseek(fd, 0, SEEK_SET);
- write(fd, buf, n);
+ write(fd, buf2, n2);
+
/* ... and now unshare(CLONE_FILES) must fail with EMFILE */
err = unshare(CLONE_FILES);
EXPECT_EQ(err, -1)
exit(EXIT_FAILURE);
EXPECT_EQ(errno, EMFILE)
exit(EXIT_FAILURE);
+
+ /* restore fs.nr_open */
+ lseek(fd, 0, SEEK_SET);
+ write(fd, buf, n);
exit(EXIT_SUCCESS);
}
--
2.39.3
prev parent reply other threads:[~2025-04-14 2:17 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-01 1:55 [PATCH v2] unshare_test: set nr_open using soft limit lufei
2025-04-08 23:42 ` Shuah Khan
2025-04-09 5:56 ` Petr Vorel
2025-04-09 21:30 ` Shuah Khan
2025-04-10 2:05 ` Lu Fei
2025-04-14 2:17 ` lufei [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250414021701.136291-1-lufei@uniontech.com \
--to=lufei@uniontech.com \
--cc=linux-kselftest@vger.kernel.org \
--cc=pvorel@suse.cz \
--cc=skhan@linuxfoundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox