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