|
Packit Service |
864d16 |
/* Tests for monitoring file descriptor usage.
|
|
Packit Service |
864d16 |
Copyright (C) 2018 Free Software Foundation, Inc.
|
|
Packit Service |
864d16 |
This file is part of the GNU C Library.
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
864d16 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
864d16 |
License as published by the Free Software Foundation; either
|
|
Packit Service |
864d16 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
864d16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
864d16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
864d16 |
Lesser General Public License for more details.
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
864d16 |
License along with the GNU C Library; if not, see
|
|
Packit Service |
864d16 |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
#include <fcntl.h>
|
|
Packit Service |
864d16 |
#include <stdbool.h>
|
|
Packit Service |
864d16 |
#include <stdlib.h>
|
|
Packit Service |
864d16 |
#include <string.h>
|
|
Packit Service |
864d16 |
#include <support/capture_subprocess.h>
|
|
Packit Service |
864d16 |
#include <support/check.h>
|
|
Packit Service |
864d16 |
#include <support/descriptors.h>
|
|
Packit Service |
864d16 |
#include <support/support.h>
|
|
Packit Service |
864d16 |
#include <support/xunistd.h>
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
/* This is the next free descriptor that the subprocess will pick. */
|
|
Packit Service |
864d16 |
static int free_descriptor;
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
subprocess_no_change (void *closure)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
TEST_COMPARE (fd, free_descriptor);
|
|
Packit Service |
864d16 |
xclose (fd);
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
subprocess_closed_descriptor (void *closure)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
TEST_COMPARE (fd, free_descriptor);
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
xclose (fd);
|
|
Packit Service |
864d16 |
support_descriptors_check (descrs); /* Will report failure. */
|
|
Packit Service |
864d16 |
puts ("EOT");
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
subprocess_opened_descriptor (void *closure)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
TEST_COMPARE (fd, free_descriptor);
|
|
Packit Service |
864d16 |
support_descriptors_check (descrs); /* Will report failure. */
|
|
Packit Service |
864d16 |
puts ("EOT");
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
subprocess_changed_descriptor (void *closure)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
TEST_COMPARE (fd, free_descriptor);
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
xclose (fd);
|
|
Packit Service |
864d16 |
TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
|
|
Packit Service |
864d16 |
support_descriptors_check (descrs); /* Will report failure. */
|
|
Packit Service |
864d16 |
puts ("EOT");
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
report_subprocess_output (const char *name,
|
|
Packit Service |
864d16 |
struct support_capture_subprocess *proc)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
printf ("info: BEGIN %s output\n"
|
|
Packit Service |
864d16 |
"%s"
|
|
Packit Service |
864d16 |
"info: END %s output\n",
|
|
Packit Service |
864d16 |
name, proc->out.buffer, name);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
/* Use an explicit flag to preserve failure status across
|
|
Packit Service |
864d16 |
support_record_failure_reset calls. */
|
|
Packit Service |
864d16 |
static bool good = true;
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static void
|
|
Packit Service |
864d16 |
test_run (void)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_capture_subprocess proc = support_capture_subprocess
|
|
Packit Service |
864d16 |
(&subprocess_no_change, NULL);
|
|
Packit Service |
864d16 |
support_capture_subprocess_check (&proc, "subprocess_no_change",
|
|
Packit Service |
864d16 |
0, sc_allow_none);
|
|
Packit Service |
864d16 |
support_capture_subprocess_free (&proc;;
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
char *expected = xasprintf ("\nDifferences:\n"
|
|
Packit Service |
864d16 |
"error: descriptor %d was closed\n"
|
|
Packit Service |
864d16 |
"EOT\n",
|
|
Packit Service |
864d16 |
free_descriptor);
|
|
Packit Service |
864d16 |
good = good && !support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
|
|
Packit Service |
864d16 |
good = good && support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
support_record_failure_reset (); /* Discard the reported error. */
|
|
Packit Service |
864d16 |
report_subprocess_output ("subprocess_closed_descriptor", &proc;;
|
|
Packit Service |
864d16 |
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
Packit Service |
864d16 |
support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
|
|
Packit Service |
864d16 |
0, sc_allow_stdout);
|
|
Packit Service |
864d16 |
support_capture_subprocess_free (&proc;;
|
|
Packit Service |
864d16 |
free (expected);
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
expected = xasprintf ("\nDifferences:\n"
|
|
Packit Service |
864d16 |
"error: descriptor %d was opened (\"/dev/null\")\n"
|
|
Packit Service |
864d16 |
"EOT\n",
|
|
Packit Service |
864d16 |
free_descriptor);
|
|
Packit Service |
864d16 |
good = good && !support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
|
|
Packit Service |
864d16 |
good = good && support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
support_record_failure_reset (); /* Discard the reported error. */
|
|
Packit Service |
864d16 |
report_subprocess_output ("subprocess_opened_descriptor", &proc;;
|
|
Packit Service |
864d16 |
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
Packit Service |
864d16 |
support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
|
|
Packit Service |
864d16 |
0, sc_allow_stdout);
|
|
Packit Service |
864d16 |
support_capture_subprocess_free (&proc;;
|
|
Packit Service |
864d16 |
free (expected);
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
expected = xasprintf ("\nDifferences:\n"
|
|
Packit Service |
864d16 |
"error: descriptor %d changed from \"/dev/null\""
|
|
Packit Service |
864d16 |
" to \"/dev\"\n"
|
|
Packit Service |
864d16 |
"error: descriptor %d changed ino ",
|
|
Packit Service |
864d16 |
free_descriptor, free_descriptor);
|
|
Packit Service |
864d16 |
good = good && !support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
|
|
Packit Service |
864d16 |
good = good && support_record_failure_is_failed ();
|
|
Packit Service |
864d16 |
support_record_failure_reset (); /* Discard the reported error. */
|
|
Packit Service |
864d16 |
report_subprocess_output ("subprocess_changed_descriptor", &proc;;
|
|
Packit Service |
864d16 |
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
Packit Service |
864d16 |
support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
|
|
Packit Service |
864d16 |
0, sc_allow_stdout);
|
|
Packit Service |
864d16 |
support_capture_subprocess_free (&proc;;
|
|
Packit Service |
864d16 |
free (expected);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
static int
|
|
Packit Service |
864d16 |
do_test (void)
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
puts ("info: initial descriptor set");
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
support_descriptors_dump (descrs, "info: ", stdout);
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
puts ("info: descriptor set with additional free descriptor");
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
support_descriptors_dump (descrs, "info: ", stdout);
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
TEST_VERIFY (free_descriptor >= 3);
|
|
Packit Service |
864d16 |
xclose (free_descriptor);
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
/* Initial test run without a sentinel descriptor. The presence of
|
|
Packit Service |
864d16 |
such a descriptor exercises different conditions in the list
|
|
Packit Service |
864d16 |
comparison in support_descriptors_check. */
|
|
Packit Service |
864d16 |
test_run ();
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
/* Allocate a sentinel descriptor at the end of the descriptor list,
|
|
Packit Service |
864d16 |
after free_descriptor. */
|
|
Packit Service |
864d16 |
int sentinel_fd;
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
int fd = xopen ("/dev/full", O_WRONLY, 0);
|
|
Packit Service |
864d16 |
TEST_COMPARE (fd, free_descriptor);
|
|
Packit Service |
864d16 |
sentinel_fd = dup (fd);
|
|
Packit Service |
864d16 |
TEST_VERIFY_EXIT (sentinel_fd > fd);
|
|
Packit Service |
864d16 |
xclose (fd);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
puts ("info: descriptor set with sentinel descriptor");
|
|
Packit Service |
864d16 |
{
|
|
Packit Service |
864d16 |
struct support_descriptors *descrs = support_descriptors_list ();
|
|
Packit Service |
864d16 |
support_descriptors_dump (descrs, "info: ", stdout);
|
|
Packit Service |
864d16 |
support_descriptors_free (descrs);
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
/* Second test run with sentinel descriptor. */
|
|
Packit Service |
864d16 |
test_run ();
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
xclose (sentinel_fd);
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
return !good;
|
|
Packit Service |
864d16 |
}
|
|
Packit Service |
864d16 |
|
|
Packit Service |
864d16 |
#include <support/test-driver.c>
|