(root)/
glibc-2.38/
support/
tst-support_descriptors.c
       1  /* Tests for monitoring file descriptor usage.
       2     Copyright (C) 2018-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <fcntl.h>
      20  #include <stdbool.h>
      21  #include <stdlib.h>
      22  #include <string.h>
      23  #include <support/capture_subprocess.h>
      24  #include <support/check.h>
      25  #include <support/descriptors.h>
      26  #include <support/support.h>
      27  #include <support/xunistd.h>
      28  
      29  /* This is the next free descriptor that the subprocess will pick.  */
      30  static int free_descriptor;
      31  
      32  static void
      33  subprocess_no_change (void *closure)
      34  {
      35    struct support_descriptors *descrs = support_descriptors_list ();
      36    int fd = xopen ("/dev/null", O_WRONLY, 0);
      37    TEST_COMPARE (fd, free_descriptor);
      38    xclose (fd);
      39    support_descriptors_free (descrs);
      40  }
      41  
      42  static void
      43  subprocess_closed_descriptor (void *closure)
      44  {
      45    int fd = xopen ("/dev/null", O_WRONLY, 0);
      46    TEST_COMPARE (fd, free_descriptor);
      47    struct support_descriptors *descrs = support_descriptors_list ();
      48    xclose (fd);
      49    support_descriptors_check (descrs); /* Will report failure.  */
      50    puts ("EOT");
      51    support_descriptors_free (descrs);
      52  }
      53  
      54  static void
      55  subprocess_opened_descriptor (void *closure)
      56  {
      57    struct support_descriptors *descrs = support_descriptors_list ();
      58    int fd = xopen ("/dev/null", O_WRONLY, 0);
      59    TEST_COMPARE (fd, free_descriptor);
      60    support_descriptors_check (descrs); /* Will report failure.  */
      61    puts ("EOT");
      62    support_descriptors_free (descrs);
      63  }
      64  
      65  static void
      66  subprocess_changed_descriptor (void *closure)
      67  {
      68    int fd = xopen ("/dev/null", O_WRONLY, 0);
      69    TEST_COMPARE (fd, free_descriptor);
      70    struct support_descriptors *descrs = support_descriptors_list ();
      71    xclose (fd);
      72    TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
      73    support_descriptors_check (descrs); /* Will report failure.  */
      74    puts ("EOT");
      75    support_descriptors_free (descrs);
      76  }
      77  
      78  static void
      79  report_subprocess_output (const char *name,
      80                            struct support_capture_subprocess *proc)
      81  {
      82    printf ("info: BEGIN %s output\n"
      83            "%s"
      84            "info: END %s output\n",
      85            name, proc->out.buffer, name);
      86  }
      87  
      88  /* Use an explicit flag to preserve failure status across
      89     support_record_failure_reset calls.  */
      90  static bool good = true;
      91  
      92  static void
      93  test_run (void)
      94  {
      95    struct support_capture_subprocess proc = support_capture_subprocess
      96      (&subprocess_no_change, NULL);
      97    support_capture_subprocess_check (&proc, "subprocess_no_change",
      98                                      0, sc_allow_none);
      99    support_capture_subprocess_free (&proc);
     100  
     101    char *expected = xasprintf ("\nDifferences:\n"
     102                                "error: descriptor %d was closed\n"
     103                                "EOT\n",
     104                                free_descriptor);
     105    good = good && !support_record_failure_is_failed ();
     106    proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
     107    good = good && support_record_failure_is_failed ();
     108    support_record_failure_reset (); /* Discard the reported error.  */
     109    report_subprocess_output ("subprocess_closed_descriptor", &proc);
     110    TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
     111    support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
     112                                      0, sc_allow_stdout);
     113    support_capture_subprocess_free (&proc);
     114    free (expected);
     115  
     116    expected = xasprintf ("\nDifferences:\n"
     117                          "error: descriptor %d was opened (\"/dev/null\")\n"
     118                          "EOT\n",
     119                          free_descriptor);
     120    good = good && !support_record_failure_is_failed ();
     121    proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
     122    good = good && support_record_failure_is_failed ();
     123    support_record_failure_reset (); /* Discard the reported error.  */
     124    report_subprocess_output ("subprocess_opened_descriptor", &proc);
     125    TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
     126    support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
     127                                      0, sc_allow_stdout);
     128    support_capture_subprocess_free (&proc);
     129    free (expected);
     130  
     131    expected = xasprintf ("\nDifferences:\n"
     132                          "error: descriptor %d changed from \"/dev/null\""
     133                          " to \"/dev\"\n"
     134                          "error: descriptor %d changed ino ",
     135                          free_descriptor, free_descriptor);
     136    good = good && !support_record_failure_is_failed ();
     137    proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
     138    good = good && support_record_failure_is_failed ();
     139    support_record_failure_reset (); /* Discard the reported error.  */
     140    report_subprocess_output ("subprocess_changed_descriptor", &proc);
     141    TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
     142    support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
     143                                      0, sc_allow_stdout);
     144    support_capture_subprocess_free (&proc);
     145    free (expected);
     146  }
     147  
     148  static int
     149  do_test (void)
     150  {
     151    puts ("info: initial descriptor set");
     152    {
     153      struct support_descriptors *descrs = support_descriptors_list ();
     154      support_descriptors_dump (descrs, "info:  ", stdout);
     155      support_descriptors_free (descrs);
     156    }
     157  
     158    free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
     159    puts ("info: descriptor set with additional free descriptor");
     160    {
     161      struct support_descriptors *descrs = support_descriptors_list ();
     162      support_descriptors_dump (descrs, "info:  ", stdout);
     163      support_descriptors_free (descrs);
     164    }
     165    TEST_VERIFY (free_descriptor >= 3);
     166    xclose (free_descriptor);
     167  
     168    /* Initial test run without a sentinel descriptor.  The presence of
     169       such a descriptor exercises different conditions in the list
     170       comparison in support_descriptors_check.  */
     171    test_run ();
     172  
     173    /* Allocate a sentinel descriptor at the end of the descriptor list,
     174       after free_descriptor.  */
     175    int sentinel_fd;
     176    {
     177      int fd = xopen ("/dev/full", O_WRONLY, 0);
     178      TEST_COMPARE (fd, free_descriptor);
     179      sentinel_fd = dup (fd);
     180      TEST_VERIFY_EXIT (sentinel_fd > fd);
     181      xclose (fd);
     182    }
     183    puts ("info: descriptor set with sentinel descriptor");
     184    {
     185      struct support_descriptors *descrs = support_descriptors_list ();
     186      support_descriptors_dump (descrs, "info:  ", stdout);
     187      support_descriptors_free (descrs);
     188    }
     189  
     190    /* Second test run with sentinel descriptor.  */
     191    test_run ();
     192  
     193    xclose (sentinel_fd);
     194  
     195    return !good;
     196  }
     197  
     198  #include <support/test-driver.c>