]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Remove fdleak.h CLOSE_INHERITED_FDS workaround
authorMark Wielaard <mark@klomp.org>
Wed, 3 Sep 2025 16:29:43 +0000 (18:29 +0200)
committerMark Wielaard <mark@klomp.org>
Wed, 3 Sep 2025 17:04:39 +0000 (19:04 +0200)
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.

14 files changed:
none/tests/fdleak.h
none/tests/fdleak_cmsg.c
none/tests/fdleak_creat.c
none/tests/fdleak_dup.c
none/tests/fdleak_dup2.c
none/tests/fdleak_fcntl.c
none/tests/fdleak_ipv4.c
none/tests/fdleak_open.c
none/tests/fdleak_pipe.c
none/tests/fdleak_socketpair.c
none/tests/file_dclose.c
none/tests/rlimit64_nofile.c
none/tests/rlimit_nofile.c
none/tests/socket_close.c

index 54c4895dba55e69da7bf68321a99ff3aeded91e0..9225dd3dabdfaa9df2a5e8be87ad20160807d10c 100644 (file)
       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_ */
index d4300bcf2da1a2e9c11c17322d7c2d064554a998..638bca3d2c3027b9d015d097789dd0bbe2237be8 100644 (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);
index 3c7c40174d88e91b66667ce1af5e4da4d0ab88e2..e3fc0339780db4d3cd3c805044db230759ce764c 100644 (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) );
index f526121e2ed51dbfd9055b6cba3e73e730b86a04..c4fedbee3c33c9c06a023c357caa4b5b1ad9bdda 100644 (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) );
index 96a7271c69cf78d4ebcd700cbba4ed2d1b6fe6c3..02d641649e251c300ba95c56df05569cbe8e1fa3 100644 (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) );
index c57a2608b65a9fc30dea0832967c92386ca33a7f..909c260924415bd6d0cd7de7a04cc92c7c17eae9 100644 (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) );
index 05f2977620851a2a86ae66a7ff6842ccc086f0d1..8313ef75d2f9fc9cbdc2c1b37e797f53ff888294 100644 (file)
@@ -80,7 +80,7 @@ int main (int argc, char **argv)
 {
    int pid, status;
 
-   CLOSE_INHERITED_FDS;
+
 
    if ((pid = fork()) == 0) {
       server();
index cf6a35e97c0335a907968073d3cf474da870d2cf..7b02553633712aeec642d1488246cf290d6ebb76 100644 (file)
@@ -4,7 +4,7 @@
 
 int main (int argc, char **argv)
 {
-   CLOSE_INHERITED_FDS;
+
 
    (void) DO( open("/dev/null", O_RDONLY) );
 
index 82ff24e885603b18743f44ddfc47873707ce94ad..0a2d7102cf82f1d9dd79d4b716fa07661dc0b607 100644 (file)
@@ -5,7 +5,7 @@ int main (int argc, char **argv)
 {
    int fds[2];
 
-   CLOSE_INHERITED_FDS;
+
 
    (void) DO( pipe(fds) );
 
index 62059200411e6b3423a3d8d6f6f981246198c0e4..bd3a9b8a3b83c54ed73187daead321e59bcbc67d 100644 (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) );
 
index b3f864ef99b08654dd6357fbb5a8821f2666fd30..3b7b127bb47e302892875232b80273f6973d25dd 100644 (file)
@@ -20,7 +20,7 @@ closefile (const char *f, int fd)
 
 int main ()
 {
-   CLOSE_INHERITED_FDS;
+
 
    const char *TMPFILE = "file_dclose.tmp";
    int fd;
index 54c3edf73ab781d0c4a33a4e4dcb851985b71dd4..d94f78dc9b32ec6eeb6444cc40bc72aaa68d165c 100644 (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)
    {
index 4410c3e489bca433fc6702bf748e54315d2b09e1..084f94b3b7de811db2b52eab5e6ac33e15403bf6 100644 (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)
    {
index 59d70c0009265ec5b3065a292d1962af6e93d709..7a632940bbaf03100afae935522e3d8780f335c0 100644 (file)
@@ -26,7 +26,7 @@ void open_socket()
 
 int main ()
 {
-  CLOSE_INHERITED_FDS;
+
 
   open_socket();