1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/* Smoke test for the closefrom.
Copyright (C) 2021-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <unistd.h>
#include <support/check.h>
#include <support/descriptors.h>
#include <support/xunistd.h>
#include <support/support.h>
#include <array_length.h>
#define NFDS 100
static int
closefrom_test (void)
{
struct support_descriptors *descrs = support_descriptors_list ();
int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
const int maximum_fd = lowfd + NFDS - 1;
const int half_fd = lowfd + NFDS / 2;
const int gap = lowfd + NFDS / 4;
/* Close half of the descriptors and check result. */
closefrom (half_fd);
for (int i = half_fd; i <= maximum_fd; i++)
{
TEST_COMPARE (fcntl (i, F_GETFL), -1);
TEST_COMPARE (errno, EBADF);
}
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. */
xclose (lowfd + 35);
xclose (lowfd + 38);
xclose (lowfd + 42);
xclose (lowfd + 46);
/* Close half of the descriptors and check result. */
closefrom (gap);
for (int i = gap + 1; i < maximum_fd; i++)
{
TEST_COMPARE (fcntl (i, F_GETFL), -1);
TEST_COMPARE (errno, EBADF);
}
for (int i = lowfd; i < gap; i++)
TEST_VERIFY (fcntl (i, F_GETFL) > -1);
/* Close the remmaining but the last one. */
closefrom (lowfd + 1);
for (int i = lowfd + 1; i <= maximum_fd; i++)
{
TEST_COMPARE (fcntl (i, F_GETFL), -1);
TEST_COMPARE (errno, EBADF);
}
TEST_VERIFY (fcntl (lowfd, F_GETFL) > -1);
/* Close the last one. */
closefrom (lowfd);
TEST_COMPARE (fcntl (lowfd, F_GETFL), -1);
TEST_COMPARE (errno, EBADF);
/* Double check by check the /proc. */
support_descriptors_check (descrs);
support_descriptors_free (descrs);
return 0;
}
/* Check if closefrom works even when no new file descriptors can be
created. */
static int
closefrom_test_file_desc_limit (void)
{
int max_fd = NFDS;
{
struct rlimit rl;
if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd);
rl.rlim_cur = max_fd;
if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
}
/* Exhauste the file descriptor limit. */
int lowfd = xopen ("/dev/null", O_RDONLY, 0600);
for (;;)
{
int fd = open ("/dev/null", O_RDONLY, 0600);
if (fd == -1)
{
if (errno != EMFILE)
FAIL_EXIT1 ("open: %m");
break;
}
TEST_VERIFY_EXIT (fd < max_fd);
}
closefrom (lowfd);
for (int i = lowfd; i < NFDS; i++)
{
TEST_COMPARE (fcntl (i, F_GETFL), -1);
TEST_COMPARE (errno, EBADF);
}
return 0;
}
static int
do_test (void)
{
closefrom_test ();
closefrom_test_file_desc_limit ();
return 0;
}
#include <support/test-driver.c>
|