From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by sourceware.org (Postfix) with ESMTPS id 4E1333858413 for ; Tue, 21 Sep 2021 10:25:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4E1333858413 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 1AB6E5C015A; Tue, 21 Sep 2021 06:25:39 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 21 Sep 2021 06:25:39 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudeigedgvdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgr vghlucfjuhgushhonhdqffhohihlvgcuoehmihgthhgrvghlrdhhuhgushhonhestggrnh honhhitggrlhdrtghomheqnecuggftrfgrthhtvghrnhepueffgedtjeegfefgveehgfdu kefhueffteehhfetudduvdffjeehleeguedttdegnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomhepmhifhhhuughsohhnsehfrghsthhmrghilhdr fhhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 21 Sep 2021 06:25:38 -0400 (EDT) From: Michael Hudson-Doyle To: libc-stable@sourceware.org Subject: [COMMITTED 2.34 2/2] Use support_open_dev_null_range io/tst-closefrom, misc/tst-close_range, and posix/tst-spawn5 (BZ #28260) Date: Tue, 21 Sep 2021 22:25:24 +1200 Message-Id: <20210921102524.586008-2-michael.hudson@canonical.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210921102524.586008-1-michael.hudson@canonical.com> References: <20210921102524.586008-1-michael.hudson@canonical.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Sep 2021 10:25:40 -0000 From: Adhemerval Zanella It ensures a continuous range of file descriptor and avoid hitting the RLIMIT_NOFILE. Checked on x86_64-linux-gnu. (cherry picked from commit 6b20880b22d1d0fce7e9f506baa6fe2d5c7fcfdc) --- io/tst-closefrom.c | 21 +++++---------- posix/tst-spawn5.c | 13 +--------- sysdeps/unix/sysv/linux/tst-close_range.c | 31 ++++++++--------------- 3 files changed, 17 insertions(+), 48 deletions(-) diff --git a/io/tst-closefrom.c b/io/tst-closefrom.c index d4c187073c..395ec0d894 100644 --- a/io/tst-closefrom.c +++ b/io/tst-closefrom.c @@ -24,31 +24,22 @@ #include #include #include +#include #include #define NFDS 100 -static int -open_multiple_temp_files (void) -{ - /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = xopen ("/dev/null", O_RDONLY, 0600); - for (int i = 1; i <= NFDS; i++) - TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600), lowfd + i); - return lowfd; -} - static int closefrom_test (void) { struct support_descriptors *descrs = support_descriptors_list (); - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); - const int maximum_fd = lowfd + NFDS; + const int maximum_fd = lowfd + NFDS - 1; const int half_fd = lowfd + NFDS / 2; - const int gap = maximum_fd / 4; + const int gap = lowfd + NFDS / 4; /* Close half of the descriptors and check result. */ closefrom (half_fd); @@ -58,7 +49,7 @@ closefrom_test (void) TEST_COMPARE (fcntl (i, F_GETFL), -1); TEST_COMPARE (errno, EBADF); } - for (int i = 0; i < half_fd; i++) + for (int i = lowfd; i < half_fd; i++) TEST_VERIFY (fcntl (i, F_GETFL) > -1); /* Create some gaps, close up to a threshold, and check result. */ @@ -74,7 +65,7 @@ closefrom_test (void) TEST_COMPARE (fcntl (i, F_GETFL), -1); TEST_COMPARE (errno, EBADF); } - for (int i = 0; i < gap; i++) + for (int i = lowfd; i < gap; i++) TEST_VERIFY (fcntl (i, F_GETFL) > -1); /* Close the remmaining but the last one. */ diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c index ac66738004..a95199af6b 100644 --- a/posix/tst-spawn5.c +++ b/posix/tst-spawn5.c @@ -47,17 +47,6 @@ static int initial_argv_count; #define NFDS 100 -static int -open_multiple_temp_files (void) -{ - /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = xopen ("/dev/null", O_RDONLY, 0600); - for (int i = 1; i <= NFDS; i++) - TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600), - lowfd + i); - return lowfd; -} - static int parse_fd (const char *str) { @@ -185,7 +174,7 @@ spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd, static void do_test_closefrom (void) { - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); const int half_fd = lowfd + NFDS / 2; /* Close half of the descriptors and check result. */ diff --git a/sysdeps/unix/sysv/linux/tst-close_range.c b/sysdeps/unix/sysv/linux/tst-close_range.c index dccb6189c5..f5069d1b8a 100644 --- a/sysdeps/unix/sysv/linux/tst-close_range.c +++ b/sysdeps/unix/sysv/linux/tst-close_range.c @@ -36,23 +36,12 @@ #define NFDS 100 -static int -open_multiple_temp_files (void) -{ - /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = xopen ("/dev/null", O_RDONLY, 0600); - for (int i = 1; i <= NFDS; i++) - TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600), - lowfd + i); - return lowfd; -} - static void close_range_test_max_upper_limit (void) { struct support_descriptors *descrs = support_descriptors_list (); - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); { int r = close_range (lowfd, ~0U, 0); @@ -68,7 +57,7 @@ close_range_test_max_upper_limit (void) static void close_range_test_common (int lowfd, unsigned int flags) { - const int maximum_fd = lowfd + NFDS; + const int maximum_fd = lowfd + NFDS - 1; const int half_fd = lowfd + NFDS / 2; const int gap_1 = maximum_fd - 8; @@ -121,7 +110,7 @@ close_range_test (void) struct support_descriptors *descrs = support_descriptors_list (); /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); close_range_test_common (lowfd, 0); @@ -146,7 +135,7 @@ close_range_test_subprocess (void) struct support_descriptors *descrs = support_descriptors_list (); /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); struct support_stack stack = support_stack_alloc (4096); @@ -184,7 +173,7 @@ close_range_unshare_test (void) struct support_descriptors *descrs1 = support_descriptors_list (); /* Check if the temporary file descriptor has no no gaps. */ - int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); struct support_descriptors *descrs2 = support_descriptors_list (); @@ -200,7 +189,7 @@ close_range_unshare_test (void) support_stack_free (&stack); - for (int i = 0; i < NFDS; i++) + for (int i = lowfd; i < lowfd + NFDS; i++) TEST_VERIFY (fcntl (i, F_GETFL) > -1); support_descriptors_check (descrs2); @@ -226,9 +215,9 @@ static void close_range_cloexec_test (void) { /* Check if the temporary file descriptor has no no gaps. */ - const int lowfd = open_multiple_temp_files (); + int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600); - const int maximum_fd = lowfd + NFDS; + const int maximum_fd = lowfd + NFDS - 1; const int half_fd = lowfd + NFDS / 2; const int gap_1 = maximum_fd - 8; @@ -251,13 +240,13 @@ close_range_cloexec_test (void) /* Create some gaps, close up to a threshold, and check result. */ static int gap_close[] = { 57, 78, 81, 82, 84, 90 }; for (int i = 0; i < array_length (gap_close); i++) - xclose (gap_close[i]); + xclose (lowfd + gap_close[i]); TEST_COMPARE (close_range (half_fd + 1, gap_1, CLOSE_RANGE_CLOEXEC), 0); for (int i = half_fd + 1; i < gap_1; i++) { int flags = fcntl (i, F_GETFD); - if (is_in_array (gap_close, array_length (gap_close), i)) + if (is_in_array (gap_close, array_length (gap_close), i - lowfd)) TEST_COMPARE (flags, -1); else { -- 2.30.2