Remove fdleak.h CLOSE_INHERITED_FDS workaround

This workaround was necessary with very old perl implementations (from
2008) which might execute programs with some non-standard file
descriptors not closed. The macro would close all file descriptors 3
or higher so --track-fds wouldn't report on them.

More recently --track-fds also reports on bad file descriptor
usage. First only double or bad close calls were reported. This would
cause lots of warnings for the close_inherited file descriptor loop
because almost all of those file descriptors were never opened, so
--track-fds would report those. To work around that an fstat call was
added before the close to make sure the file descriptor existed.

This fstat workaround in close_inherited only worked because fstat
didn't have a fd_allowed check. Which is a bug that should be
fixed. On some systems fstat actually calls the fstatat syscall and
that did recently got the fd_allowed check, so on systems that use
fstatat for fstat various fdleak tests started failing.

We could test for and use close_range, which is smart enough to not
warn about never opened file descriptors in the range. But it seems
simpler to just get rid of the CLOSE_INHERITED_FDS macro because the
problematic perl implementation is now so old that nobody uses it
anymore.
This commit is contained in:
Mark Wielaard
2025-09-03 18:29:43 +02:00
parent a7fe47937b
commit b6ab5a762d
14 changed files with 13 additions and 38 deletions

View File

@@ -17,29 +17,4 @@
res; \
})
/*
* The macro below closes file descriptors inherited from the process
* that forked the current process. Close these file descriptors right
* after the start of main() in order to get consistent results across
* different releases. Known behavior:
* - Fedora Core 1's Perl opens /dev/pts/2 as fd 10.
* - For Ubuntu 8.04, see also
* https://bugs.launchpad.net/ubuntu/+source/seahorse/+bug/235184
*/
__attribute__((unused))
static void close_inherited (void) {
struct stat sb;
int i; int max_fds = sysconf (_SC_OPEN_MAX);
if (max_fds < 0)
max_fds = 1024; /* Fallback if sysconf fails, returns -1. */
/* Only leave 0 (stdin), 1 (stdout) and 2 (stderr) open. */
for (i = 3; i < max_fds; i++)
if (fstat (i, &sb) != -1) /* Test if the file descriptor exists first. */
close(i);
}
#define CLOSE_INHERITED_FDS close_inherited ()
/* Note that the following would be nicer, but close_range is fairly new. */
// #define CLOSE_INHERITED_FDS close_range (3, ~0U, 0)
#endif /* _FDLEAK_H_ */

View File

@@ -159,7 +159,7 @@ int main (int argc, char **argv)
{
int pid, status;
CLOSE_INHERITED_FDS;
pid = getpid();
sprintf(filea, "/tmp/data1.%d", pid);

View File

@@ -7,7 +7,7 @@ int main (int argc, char **argv)
{
char filename[24];
CLOSE_INHERITED_FDS;
sprintf(filename, "/tmp/file.%ld", (long) getpid());
(void) DO( creat(filename, 0) );

View File

@@ -6,7 +6,7 @@ int main (int argc, char **argv)
{
int s;
CLOSE_INHERITED_FDS;
s = DO( open("/dev/null", O_RDONLY) );
(void) DO( dup(s) );

View File

@@ -7,7 +7,7 @@ int main (int argc, char **argv)
int s1;
int s2;
CLOSE_INHERITED_FDS;
s1 = DO( open("/dev/null", O_RDONLY) );
s2 = DO( open("/dev/null", O_RDONLY) );

View File

@@ -7,7 +7,7 @@ int main (int argc, char **argv)
{
int s1;
CLOSE_INHERITED_FDS;
s1 = DO( open("/dev/null", O_RDONLY) );
(void) DO( fcntl(s1, F_DUPFD, s1) );

View File

@@ -80,7 +80,7 @@ int main (int argc, char **argv)
{
int pid, status;
CLOSE_INHERITED_FDS;
if ((pid = fork()) == 0) {
server();

View File

@@ -4,7 +4,7 @@
int main (int argc, char **argv)
{
CLOSE_INHERITED_FDS;
(void) DO( open("/dev/null", O_RDONLY) );

View File

@@ -5,7 +5,7 @@ int main (int argc, char **argv)
{
int fds[2];
CLOSE_INHERITED_FDS;
(void) DO( pipe(fds) );

View File

@@ -8,7 +8,7 @@ int main (int argc, char **argv)
{
int fds[2];
CLOSE_INHERITED_FDS;
(void) DO( socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fds) );

View File

@@ -20,7 +20,7 @@ closefile (const char *f, int fd)
int main ()
{
CLOSE_INHERITED_FDS;
const char *TMPFILE = "file_dclose.tmp";
int fd;

View File

@@ -15,7 +15,7 @@ int main(int argc, char **argv)
struct rlimit64 newrlim;
int fd;
CLOSE_INHERITED_FDS;
if (getrlimit64(RLIMIT_NOFILE, &oldrlim) < 0)
{

View File

@@ -14,7 +14,7 @@ int main(int argc, char **argv)
struct rlimit newrlim;
int fd;
CLOSE_INHERITED_FDS;
if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0)
{

View File

@@ -26,7 +26,7 @@ void open_socket()
int main ()
{
CLOSE_INHERITED_FDS;
open_socket();