From: Mark Wielaard Date: Wed, 3 Sep 2025 16:29:43 +0000 (+0200) Subject: Remove fdleak.h CLOSE_INHERITED_FDS workaround X-Git-Tag: VALGRIND_3_26_0~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6ab5a762d094cfb61696cdbad5413ef0366c496;p=thirdparty%2Fvalgrind.git 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. --- diff --git a/none/tests/fdleak.h b/none/tests/fdleak.h index 54c4895db..9225dd3da 100644 --- a/none/tests/fdleak.h +++ b/none/tests/fdleak.h @@ -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_ */ diff --git a/none/tests/fdleak_cmsg.c b/none/tests/fdleak_cmsg.c index d4300bcf2..638bca3d2 100644 --- a/none/tests/fdleak_cmsg.c +++ b/none/tests/fdleak_cmsg.c @@ -159,7 +159,7 @@ int main (int argc, char **argv) { int pid, status; - CLOSE_INHERITED_FDS; + pid = getpid(); sprintf(filea, "/tmp/data1.%d", pid); diff --git a/none/tests/fdleak_creat.c b/none/tests/fdleak_creat.c index 3c7c40174..e3fc03397 100644 --- a/none/tests/fdleak_creat.c +++ b/none/tests/fdleak_creat.c @@ -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) ); diff --git a/none/tests/fdleak_dup.c b/none/tests/fdleak_dup.c index f526121e2..c4fedbee3 100644 --- a/none/tests/fdleak_dup.c +++ b/none/tests/fdleak_dup.c @@ -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) ); diff --git a/none/tests/fdleak_dup2.c b/none/tests/fdleak_dup2.c index 96a7271c6..02d641649 100644 --- a/none/tests/fdleak_dup2.c +++ b/none/tests/fdleak_dup2.c @@ -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) ); diff --git a/none/tests/fdleak_fcntl.c b/none/tests/fdleak_fcntl.c index c57a2608b..909c26092 100644 --- a/none/tests/fdleak_fcntl.c +++ b/none/tests/fdleak_fcntl.c @@ -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) ); diff --git a/none/tests/fdleak_ipv4.c b/none/tests/fdleak_ipv4.c index 05f297762..8313ef75d 100644 --- a/none/tests/fdleak_ipv4.c +++ b/none/tests/fdleak_ipv4.c @@ -80,7 +80,7 @@ int main (int argc, char **argv) { int pid, status; - CLOSE_INHERITED_FDS; + if ((pid = fork()) == 0) { server(); diff --git a/none/tests/fdleak_open.c b/none/tests/fdleak_open.c index cf6a35e97..7b0255363 100644 --- a/none/tests/fdleak_open.c +++ b/none/tests/fdleak_open.c @@ -4,7 +4,7 @@ int main (int argc, char **argv) { - CLOSE_INHERITED_FDS; + (void) DO( open("/dev/null", O_RDONLY) ); diff --git a/none/tests/fdleak_pipe.c b/none/tests/fdleak_pipe.c index 82ff24e88..0a2d7102c 100644 --- a/none/tests/fdleak_pipe.c +++ b/none/tests/fdleak_pipe.c @@ -5,7 +5,7 @@ int main (int argc, char **argv) { int fds[2]; - CLOSE_INHERITED_FDS; + (void) DO( pipe(fds) ); diff --git a/none/tests/fdleak_socketpair.c b/none/tests/fdleak_socketpair.c index 620592004..bd3a9b8a3 100644 --- a/none/tests/fdleak_socketpair.c +++ b/none/tests/fdleak_socketpair.c @@ -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) ); diff --git a/none/tests/file_dclose.c b/none/tests/file_dclose.c index b3f864ef9..3b7b127bb 100644 --- a/none/tests/file_dclose.c +++ b/none/tests/file_dclose.c @@ -20,7 +20,7 @@ closefile (const char *f, int fd) int main () { - CLOSE_INHERITED_FDS; + const char *TMPFILE = "file_dclose.tmp"; int fd; diff --git a/none/tests/rlimit64_nofile.c b/none/tests/rlimit64_nofile.c index 54c3edf73..d94f78dc9 100644 --- a/none/tests/rlimit64_nofile.c +++ b/none/tests/rlimit64_nofile.c @@ -15,7 +15,7 @@ int main(int argc, char **argv) struct rlimit64 newrlim; int fd; - CLOSE_INHERITED_FDS; + if (getrlimit64(RLIMIT_NOFILE, &oldrlim) < 0) { diff --git a/none/tests/rlimit_nofile.c b/none/tests/rlimit_nofile.c index 4410c3e48..084f94b3b 100644 --- a/none/tests/rlimit_nofile.c +++ b/none/tests/rlimit_nofile.c @@ -14,7 +14,7 @@ int main(int argc, char **argv) struct rlimit newrlim; int fd; - CLOSE_INHERITED_FDS; + if (getrlimit(RLIMIT_NOFILE, &oldrlim) < 0) { diff --git a/none/tests/socket_close.c b/none/tests/socket_close.c index 59d70c000..7a632940b 100644 --- a/none/tests/socket_close.c +++ b/none/tests/socket_close.c @@ -26,7 +26,7 @@ void open_socket() int main () { - CLOSE_INHERITED_FDS; + open_socket();