From: Sergei Trofimovich <siarh...@google.com>

I noticed test failures when ran gcc test suite from under mc shell.
mc opens fd=9 and exposes it to child processes. As a result a few
tests failes:
    FAIL: b2test_buildid
    FAIL: btest_gnudebuglink
    FAIL: btest
    FAIL: btest_lto
    FAIL: btest_alloc
    FAIL: ctestg
    FAIL: ctesta
    FAIL: ctestg_alloc
    FAIL: ctesta_alloc
    FAIL: dwarf5
    FAIL: dwarf5_alloc

Instead of trying to close file descripts in range test polls for
first available file descriptor by creating it via dup(1).

libbacktrace/

        * btest.c (check_open_files): Use last free file descriptor as a
        signal for flie descriptor leak.
---
 libbacktrace/btest.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index 9f9c03babf3..d5cf321640c 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -458,22 +458,32 @@ test5 (void)
   return failures;
 }
 
+/* Peek at first free flie descriptior.  */
+
+static int probe_first_dree_fd (void) {
+  int fd;
+
+  fd = dup(1);
+  close(fd);
+
+  return fd;
+}
+
 /* Check that are no files left open.  */
 
 static void
-check_open_files (void)
+check_open_files (int last_free_fd)
 {
-  int i;
+  int fd;
+
+  fd = probe_first_dree_fd();
 
-  for (i = 3; i < 10; i++)
+  if (fd != last_free_fd)
     {
-      if (close (i) == 0)
-       {
-         fprintf (stderr,
-                  "ERROR: descriptor %d still open after tests complete\n",
-                  i);
-         ++failures;
-       }
+      fprintf (stderr,
+              "ERROR: descriptor %d still open after tests complete\n",
+              last_free_fd);
+      ++failures;
     }
 }
 
@@ -482,8 +492,11 @@ check_open_files (void)
 int
 main (int argc ATTRIBUTE_UNUSED, char **argv)
 {
+  int first_free_fd;
+
   state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
                                  error_callback_create, NULL);
+  first_free_fd = probe_first_dree_fd ();
 
 #if BACKTRACE_SUPPORTED
   test1 ();
@@ -495,7 +508,7 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
 #endif
 #endif
 
-  check_open_files ();
+  check_open_files (first_free_fd);
 
   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
 }
-- 
2.32.0

Reply via email to