50f89d
commit f255336a9301619519045548acb2e1027065a837
50f89d
Author: Florian Weimer <fweimer@redhat.com>
50f89d
Date:   Thu Dec 6 15:39:42 2018 +0100
50f89d
50f89d
    support: Implement <support/descriptors.h> to track file descriptors
50f89d
50f89d
diff --git a/support/Makefile b/support/Makefile
50f89d
index a2536980d1d5a89b..93a514301654132e 100644
50f89d
--- a/support/Makefile
50f89d
+++ b/support/Makefile
50f89d
@@ -46,6 +46,7 @@ libsupport-routines = \
50f89d
   support_chroot \
50f89d
   support_copy_file_range \
50f89d
   support_descriptor_supports_holes \
50f89d
+  support_descriptors \
50f89d
   support_enter_mount_namespace \
50f89d
   support_enter_network_namespace \
50f89d
   support_format_address_family \
50f89d
@@ -195,6 +196,7 @@ tests = \
50f89d
   tst-support-namespace \
50f89d
   tst-support_blob_repeat \
50f89d
   tst-support_capture_subprocess \
50f89d
+  tst-support_descriptors \
50f89d
   tst-support_format_dns_packet \
50f89d
   tst-support_quote_blob \
50f89d
   tst-support_quote_string \
50f89d
diff --git a/support/check.h b/support/check.h
50f89d
index e6765289f2492501..7ea9a86a9c7ed055 100644
50f89d
--- a/support/check.h
50f89d
+++ b/support/check.h
50f89d
@@ -183,6 +183,10 @@ int support_report_failure (int status)
50f89d
 /* Internal function used to test the failure recording framework.  */
50f89d
 void support_record_failure_reset (void);
50f89d
 
50f89d
+/* Returns true or false depending on whether there have been test
50f89d
+   failures or not.  */
50f89d
+int support_record_failure_is_failed (void);
50f89d
+
50f89d
 __END_DECLS
50f89d
 
50f89d
 #endif /* SUPPORT_CHECK_H */
50f89d
diff --git a/support/descriptors.h b/support/descriptors.h
50f89d
new file mode 100644
50f89d
index 0000000000000000..8ec4cbbdfb8f1770
50f89d
--- /dev/null
50f89d
+++ b/support/descriptors.h
50f89d
@@ -0,0 +1,47 @@
50f89d
+/* Monitoring file descriptor usage.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#ifndef SUPPORT_DESCRIPTORS_H
50f89d
+#define SUPPORT_DESCRIPTORS_H
50f89d
+
50f89d
+#include <stdio.h>
50f89d
+
50f89d
+/* Opaque pointer, for capturing file descriptor lists.  */
50f89d
+struct support_descriptors;
50f89d
+
50f89d
+/* Record the currently open file descriptors and store them in the
50f89d
+   returned list.  Terminate the process if the listing operation
50f89d
+   fails.  */
50f89d
+struct support_descriptors *support_descriptors_list (void);
50f89d
+
50f89d
+/* Deallocate the list of descriptors.  */
50f89d
+void support_descriptors_free (struct support_descriptors *);
50f89d
+
50f89d
+/* Write the list of descriptors to STREAM, adding PREFIX to each
50f89d
+   line.  */
50f89d
+void support_descriptors_dump (struct support_descriptors *,
50f89d
+                               const char *prefix, FILE *stream);
50f89d
+
50f89d
+/* Check for file descriptor leaks and other file descriptor changes:
50f89d
+   Compare the current list of descriptors with the passed list.
50f89d
+   Record a test failure if there are additional open descriptors,
50f89d
+   descriptors have been closed, or if a change in file descriptor can
50f89d
+   be detected.  */
50f89d
+void support_descriptors_check (struct support_descriptors *);
50f89d
+
50f89d
+#endif /* SUPPORT_DESCRIPTORS_H */
50f89d
diff --git a/support/support_descriptors.c b/support/support_descriptors.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..d66cf550800201c5
50f89d
--- /dev/null
50f89d
+++ b/support/support_descriptors.c
50f89d
@@ -0,0 +1,274 @@
50f89d
+/* Monitoring file descriptor usage.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <dirent.h>
50f89d
+#include <stdio.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <string.h>
50f89d
+#include <support/check.h>
50f89d
+#include <support/support.h>
50f89d
+#include <sys/stat.h>
50f89d
+#include <sys/sysmacros.h>
50f89d
+#include <xunistd.h>
50f89d
+
50f89d
+struct procfs_descriptor
50f89d
+{
50f89d
+  int fd;
50f89d
+  char *link_target;
50f89d
+  dev_t dev;
50f89d
+  ino64_t ino;
50f89d
+};
50f89d
+
50f89d
+/* Used with qsort.  */
50f89d
+static int
50f89d
+descriptor_compare (const void *l, const void *r)
50f89d
+{
50f89d
+  const struct procfs_descriptor *left = l;
50f89d
+  const struct procfs_descriptor *right = r;
50f89d
+  /* Cannot overflow due to limited file descriptor range.  */
50f89d
+  return left->fd - right->fd;
50f89d
+}
50f89d
+
50f89d
+#define DYNARRAY_STRUCT descriptor_list
50f89d
+#define DYNARRAY_ELEMENT struct procfs_descriptor
50f89d
+#define DYNARRAY_PREFIX descriptor_list_
50f89d
+#define DYNARRAY_ELEMENT_FREE(e) free ((e)->link_target)
50f89d
+#define DYNARRAY_INITIAL_SIZE 0
50f89d
+#include <malloc/dynarray-skeleton.c>
50f89d
+
50f89d
+struct support_descriptors
50f89d
+{
50f89d
+  struct descriptor_list list;
50f89d
+};
50f89d
+
50f89d
+struct support_descriptors *
50f89d
+support_descriptors_list (void)
50f89d
+{
50f89d
+  struct support_descriptors *result = xmalloc (sizeof (*result));
50f89d
+  descriptor_list_init (&result->list);
50f89d
+
50f89d
+  DIR *fds = opendir ("/proc/self/fd");
50f89d
+  if (fds == NULL)
50f89d
+    FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
50f89d
+
50f89d
+  while (true)
50f89d
+    {
50f89d
+      errno = 0;
50f89d
+      struct dirent64 *e = readdir64 (fds);
50f89d
+      if (e == NULL)
50f89d
+        {
50f89d
+          if (errno != 0)
50f89d
+            FAIL_EXIT1 ("readdir: %m");
50f89d
+          break;
50f89d
+        }
50f89d
+
50f89d
+      if (e->d_name[0] == '.')
50f89d
+        continue;
50f89d
+
50f89d
+      char *endptr;
50f89d
+      long int fd = strtol (e->d_name, &endptr, 10);
50f89d
+      if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
50f89d
+        FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
50f89d
+                    e->d_name);
50f89d
+
50f89d
+      /* Skip the descriptor which is used to enumerate the
50f89d
+         descriptors.  */
50f89d
+      if (fd == dirfd (fds))
50f89d
+        continue;
50f89d
+
50f89d
+      char *target;
50f89d
+      {
50f89d
+        char *path = xasprintf ("/proc/self/fd/%ld", fd);
50f89d
+        target = xreadlink (path);
50f89d
+        free (path);
50f89d
+      }
50f89d
+      struct stat64 st;
50f89d
+      if (fstat64 (fd, &st) != 0)
50f89d
+        FAIL_EXIT1 ("readdir: fstat64 (%ld) failed: %m", fd);
50f89d
+
50f89d
+      struct procfs_descriptor *item = descriptor_list_emplace (&result->list);
50f89d
+      if (item == NULL)
50f89d
+        FAIL_EXIT1 ("descriptor_list_emplace: %m");
50f89d
+      item->fd = fd;
50f89d
+      item->link_target = target;
50f89d
+      item->dev = st.st_dev;
50f89d
+      item->ino = st.st_ino;
50f89d
+    }
50f89d
+
50f89d
+  closedir (fds);
50f89d
+
50f89d
+  /* Perform a merge join between descrs and current.  This assumes
50f89d
+     that the arrays are sorted by file descriptor.  */
50f89d
+
50f89d
+  qsort (descriptor_list_begin (&result->list),
50f89d
+         descriptor_list_size (&result->list),
50f89d
+         sizeof (struct procfs_descriptor), descriptor_compare);
50f89d
+
50f89d
+  return result;
50f89d
+}
50f89d
+
50f89d
+void
50f89d
+support_descriptors_free (struct support_descriptors *descrs)
50f89d
+{
50f89d
+  descriptor_list_free (&descrs->list);
50f89d
+  free (descrs);
50f89d
+}
50f89d
+
50f89d
+void
50f89d
+support_descriptors_dump (struct support_descriptors *descrs,
50f89d
+                          const char *prefix, FILE *fp)
50f89d
+{
50f89d
+  struct procfs_descriptor *end = descriptor_list_end (&descrs->list);
50f89d
+  for (struct procfs_descriptor *d = descriptor_list_begin (&descrs->list);
50f89d
+       d != end; ++d)
50f89d
+    {
50f89d
+      char *quoted = support_quote_string (d->link_target);
50f89d
+      fprintf (fp, "%s%d: target=\"%s\" major=%lld minor=%lld ino=%lld\n",
50f89d
+               prefix, d->fd, quoted,
50f89d
+               (long long int) major (d->dev),
50f89d
+               (long long int) minor (d->dev),
50f89d
+               (long long int) d->ino);
50f89d
+      free (quoted);
50f89d
+    }
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+dump_mismatch (bool *first,
50f89d
+               struct support_descriptors *descrs,
50f89d
+               struct support_descriptors *current)
50f89d
+{
50f89d
+  if (*first)
50f89d
+    *first = false;
50f89d
+  else
50f89d
+    return;
50f89d
+
50f89d
+  puts ("error: Differences found in descriptor set");
50f89d
+  puts ("Reference descriptor set:");
50f89d
+  support_descriptors_dump (descrs, "  ", stdout);
50f89d
+  puts ("Current descriptor set:");
50f89d
+  support_descriptors_dump (current, "  ", stdout);
50f89d
+  puts ("Differences:");
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+report_closed_descriptor (bool *first,
50f89d
+                          struct support_descriptors *descrs,
50f89d
+                          struct support_descriptors *current,
50f89d
+                          struct procfs_descriptor *left)
50f89d
+{
50f89d
+  support_record_failure ();
50f89d
+  dump_mismatch (first, descrs, current);
50f89d
+  printf ("error: descriptor %d was closed\n", left->fd);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+report_opened_descriptor (bool *first,
50f89d
+                          struct support_descriptors *descrs,
50f89d
+                          struct support_descriptors *current,
50f89d
+                          struct procfs_descriptor *right)
50f89d
+{
50f89d
+  support_record_failure ();
50f89d
+  dump_mismatch (first, descrs, current);
50f89d
+  char *quoted = support_quote_string (right->link_target);
50f89d
+  printf ("error: descriptor %d was opened (\"%s\")\n", right->fd, quoted);
50f89d
+  free (quoted);
50f89d
+}
50f89d
+
50f89d
+void
50f89d
+support_descriptors_check (struct support_descriptors *descrs)
50f89d
+{
50f89d
+  struct support_descriptors *current = support_descriptors_list ();
50f89d
+
50f89d
+  /* Perform a merge join between descrs and current.  This assumes
50f89d
+     that the arrays are sorted by file descriptor.  */
50f89d
+
50f89d
+  struct procfs_descriptor *left = descriptor_list_begin (&descrs->list);
50f89d
+  struct procfs_descriptor *left_end = descriptor_list_end (&descrs->list);
50f89d
+  struct procfs_descriptor *right = descriptor_list_begin (&current->list);
50f89d
+  struct procfs_descriptor *right_end = descriptor_list_end (&current->list);
50f89d
+
50f89d
+  bool first = true;
50f89d
+  while (left != left_end && right != right_end)
50f89d
+    {
50f89d
+      if (left->fd == right->fd)
50f89d
+        {
50f89d
+          if (strcmp (left->link_target, right->link_target) != 0)
50f89d
+            {
50f89d
+              support_record_failure ();
50f89d
+              char *left_quoted = support_quote_string (left->link_target);
50f89d
+              char *right_quoted = support_quote_string (right->link_target);
50f89d
+              dump_mismatch (&first, descrs, current);
50f89d
+              printf ("error: descriptor %d changed from \"%s\" to \"%s\"\n",
50f89d
+                      left->fd, left_quoted, right_quoted);
50f89d
+              free (left_quoted);
50f89d
+              free (right_quoted);
50f89d
+            }
50f89d
+          if (left->dev != right->dev)
50f89d
+            {
50f89d
+              support_record_failure ();
50f89d
+              dump_mismatch (&first, descrs, current);
50f89d
+              printf ("error: descriptor %d changed device"
50f89d
+                      " from %lld:%lld to %lld:%lld\n",
50f89d
+                      left->fd,
50f89d
+                      (long long int) major (left->dev),
50f89d
+                      (long long int) minor (left->dev),
50f89d
+                      (long long int) major (right->dev),
50f89d
+                      (long long int) minor (right->dev));
50f89d
+            }
50f89d
+          if (left->ino != right->ino)
50f89d
+            {
50f89d
+              support_record_failure ();
50f89d
+              dump_mismatch (&first, descrs, current);
50f89d
+              printf ("error: descriptor %d changed ino from %lld to %lld\n",
50f89d
+                      left->fd,
50f89d
+                      (long long int) left->ino, (long long int) right->ino);
50f89d
+            }
50f89d
+          ++left;
50f89d
+          ++right;
50f89d
+        }
50f89d
+      else if (left->fd < right->fd)
50f89d
+        {
50f89d
+          /* Gap on the right.  */
50f89d
+          report_closed_descriptor (&first, descrs, current, left);
50f89d
+          ++left;
50f89d
+        }
50f89d
+      else
50f89d
+        {
50f89d
+          /* Gap on the left.  */
50f89d
+          TEST_VERIFY_EXIT (left->fd > right->fd);
50f89d
+          report_opened_descriptor (&first, descrs, current, right);
50f89d
+          ++right;
50f89d
+        }
50f89d
+    }
50f89d
+
50f89d
+  while (left != left_end)
50f89d
+    {
50f89d
+      /* Closed descriptors (more descriptors on the left).  */
50f89d
+      report_closed_descriptor (&first, descrs, current, left);
50f89d
+      ++left;
50f89d
+    }
50f89d
+
50f89d
+  while (right != right_end)
50f89d
+    {
50f89d
+      /* Opened descriptors (more descriptors on the right).  */
50f89d
+      report_opened_descriptor (&first, descrs, current, right);
50f89d
+      ++right;
50f89d
+    }
50f89d
+
50f89d
+  support_descriptors_free (current);
50f89d
+}
50f89d
diff --git a/support/support_record_failure.c b/support/support_record_failure.c
50f89d
index 356798f55608ca71..17ab1d80ef2bbdea 100644
50f89d
--- a/support/support_record_failure.c
50f89d
+++ b/support/support_record_failure.c
50f89d
@@ -104,3 +104,11 @@ support_record_failure_reset (void)
50f89d
   __atomic_store_n (&state->failed, 0, __ATOMIC_RELAXED);
50f89d
   __atomic_add_fetch (&state->counter, 0, __ATOMIC_RELAXED);
50f89d
 }
50f89d
+
50f89d
+int
50f89d
+support_record_failure_is_failed (void)
50f89d
+{
50f89d
+  /* Relaxed MO is sufficient because we need (blocking) external
50f89d
+     synchronization for reliable test error reporting anyway.  */
50f89d
+  return __atomic_load_n (&state->failed, __ATOMIC_RELAXED);
50f89d
+}
50f89d
diff --git a/support/tst-support_descriptors.c b/support/tst-support_descriptors.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..5e9e824bc3820499
50f89d
--- /dev/null
50f89d
+++ b/support/tst-support_descriptors.c
50f89d
@@ -0,0 +1,198 @@
50f89d
+/* Tests for monitoring file descriptor usage.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <fcntl.h>
50f89d
+#include <stdbool.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <string.h>
50f89d
+#include <support/capture_subprocess.h>
50f89d
+#include <support/check.h>
50f89d
+#include <support/descriptors.h>
50f89d
+#include <support/support.h>
50f89d
+#include <support/xunistd.h>
50f89d
+
50f89d
+/* This is the next free descriptor that the subprocess will pick.  */
50f89d
+static int free_descriptor;
50f89d
+
50f89d
+static void
50f89d
+subprocess_no_change (void *closure)
50f89d
+{
50f89d
+  struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+  int fd = xopen ("/dev/null", O_WRONLY, 0);
50f89d
+  TEST_COMPARE (fd, free_descriptor);
50f89d
+  xclose (fd);
50f89d
+  support_descriptors_free (descrs);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+subprocess_closed_descriptor (void *closure)
50f89d
+{
50f89d
+  int fd = xopen ("/dev/null", O_WRONLY, 0);
50f89d
+  TEST_COMPARE (fd, free_descriptor);
50f89d
+  struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+  xclose (fd);
50f89d
+  support_descriptors_check (descrs); /* Will report failure.  */
50f89d
+  puts ("EOT");
50f89d
+  support_descriptors_free (descrs);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+subprocess_opened_descriptor (void *closure)
50f89d
+{
50f89d
+  struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+  int fd = xopen ("/dev/null", O_WRONLY, 0);
50f89d
+  TEST_COMPARE (fd, free_descriptor);
50f89d
+  support_descriptors_check (descrs); /* Will report failure.  */
50f89d
+  puts ("EOT");
50f89d
+  support_descriptors_free (descrs);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+subprocess_changed_descriptor (void *closure)
50f89d
+{
50f89d
+  int fd = xopen ("/dev/null", O_WRONLY, 0);
50f89d
+  TEST_COMPARE (fd, free_descriptor);
50f89d
+  struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+  xclose (fd);
50f89d
+  TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
50f89d
+  support_descriptors_check (descrs); /* Will report failure.  */
50f89d
+  puts ("EOT");
50f89d
+  support_descriptors_free (descrs);
50f89d
+}
50f89d
+
50f89d
+static void
50f89d
+report_subprocess_output (const char *name,
50f89d
+                          struct support_capture_subprocess *proc)
50f89d
+{
50f89d
+  printf ("info: BEGIN %s output\n"
50f89d
+          "%s"
50f89d
+          "info: END %s output\n",
50f89d
+          name, proc->out.buffer, name);
50f89d
+}
50f89d
+
50f89d
+/* Use an explicit flag to preserve failure status across
50f89d
+   support_record_failure_reset calls.  */
50f89d
+static bool good = true;
50f89d
+
50f89d
+static void
50f89d
+test_run (void)
50f89d
+{
50f89d
+  struct support_capture_subprocess proc = support_capture_subprocess
50f89d
+    (&subprocess_no_change, NULL);
50f89d
+  support_capture_subprocess_check (&proc, "subprocess_no_change",
50f89d
+                                    0, sc_allow_none);
50f89d
+  support_capture_subprocess_free (&proc;;
50f89d
+
50f89d
+  char *expected = xasprintf ("\nDifferences:\n"
50f89d
+                              "error: descriptor %d was closed\n"
50f89d
+                              "EOT\n",
50f89d
+                              free_descriptor);
50f89d
+  good = good && !support_record_failure_is_failed ();
50f89d
+  proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
50f89d
+  good = good && support_record_failure_is_failed ();
50f89d
+  support_record_failure_reset (); /* Discard the reported error.  */
50f89d
+  report_subprocess_output ("subprocess_closed_descriptor", &proc;;
50f89d
+  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
50f89d
+  support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
50f89d
+                                    0, sc_allow_stdout);
50f89d
+  support_capture_subprocess_free (&proc;;
50f89d
+  free (expected);
50f89d
+
50f89d
+  expected = xasprintf ("\nDifferences:\n"
50f89d
+                        "error: descriptor %d was opened (\"/dev/null\")\n"
50f89d
+                        "EOT\n",
50f89d
+                        free_descriptor);
50f89d
+  good = good && !support_record_failure_is_failed ();
50f89d
+  proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
50f89d
+  good = good && support_record_failure_is_failed ();
50f89d
+  support_record_failure_reset (); /* Discard the reported error.  */
50f89d
+  report_subprocess_output ("subprocess_opened_descriptor", &proc;;
50f89d
+  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
50f89d
+  support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
50f89d
+                                    0, sc_allow_stdout);
50f89d
+  support_capture_subprocess_free (&proc;;
50f89d
+  free (expected);
50f89d
+
50f89d
+  expected = xasprintf ("\nDifferences:\n"
50f89d
+                        "error: descriptor %d changed from \"/dev/null\""
50f89d
+                        " to \"/dev\"\n"
50f89d
+                        "error: descriptor %d changed ino ",
50f89d
+                        free_descriptor, free_descriptor);
50f89d
+  good = good && !support_record_failure_is_failed ();
50f89d
+  proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
50f89d
+  good = good && support_record_failure_is_failed ();
50f89d
+  support_record_failure_reset (); /* Discard the reported error.  */
50f89d
+  report_subprocess_output ("subprocess_changed_descriptor", &proc;;
50f89d
+  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
50f89d
+  support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
50f89d
+                                    0, sc_allow_stdout);
50f89d
+  support_capture_subprocess_free (&proc;;
50f89d
+  free (expected);
50f89d
+}
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  puts ("info: initial descriptor set");
50f89d
+  {
50f89d
+    struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+    support_descriptors_dump (descrs, "info:  ", stdout);
50f89d
+    support_descriptors_free (descrs);
50f89d
+  }
50f89d
+
50f89d
+  free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
50f89d
+  puts ("info: descriptor set with additional free descriptor");
50f89d
+  {
50f89d
+    struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+    support_descriptors_dump (descrs, "info:  ", stdout);
50f89d
+    support_descriptors_free (descrs);
50f89d
+  }
50f89d
+  TEST_VERIFY (free_descriptor >= 3);
50f89d
+  xclose (free_descriptor);
50f89d
+
50f89d
+  /* Initial test run without a sentinel descriptor.  The presence of
50f89d
+     such a descriptor exercises different conditions in the list
50f89d
+     comparison in support_descriptors_check.  */
50f89d
+  test_run ();
50f89d
+
50f89d
+  /* Allocate a sentinel descriptor at the end of the descriptor list,
50f89d
+     after free_descriptor.  */
50f89d
+  int sentinel_fd;
50f89d
+  {
50f89d
+    int fd = xopen ("/dev/full", O_WRONLY, 0);
50f89d
+    TEST_COMPARE (fd, free_descriptor);
50f89d
+    sentinel_fd = dup (fd);
50f89d
+    TEST_VERIFY_EXIT (sentinel_fd > fd);
50f89d
+    xclose (fd);
50f89d
+  }
50f89d
+  puts ("info: descriptor set with sentinel descriptor");
50f89d
+  {
50f89d
+    struct support_descriptors *descrs = support_descriptors_list ();
50f89d
+    support_descriptors_dump (descrs, "info:  ", stdout);
50f89d
+    support_descriptors_free (descrs);
50f89d
+  }
50f89d
+
50f89d
+  /* Second test run with sentinel descriptor.  */
50f89d
+  test_run ();
50f89d
+
50f89d
+  xclose (sentinel_fd);
50f89d
+
50f89d
+  return !good;
50f89d
+}
50f89d
+
50f89d
+#include <support/test-driver.c>