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