2018-12-06 14:39:42 +00:00
|
|
|
/* Tests for monitoring file descriptor usage.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2018-2021 Free Software Foundation, Inc.
|
2018-12-06 14:39:42 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-12-06 14:39:42 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <support/capture_subprocess.h>
|
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/descriptors.h>
|
|
|
|
#include <support/support.h>
|
|
|
|
#include <support/xunistd.h>
|
|
|
|
|
|
|
|
/* This is the next free descriptor that the subprocess will pick. */
|
|
|
|
static int free_descriptor;
|
|
|
|
|
|
|
|
static void
|
|
|
|
subprocess_no_change (void *closure)
|
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
|
|
TEST_COMPARE (fd, free_descriptor);
|
|
|
|
xclose (fd);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
subprocess_closed_descriptor (void *closure)
|
|
|
|
{
|
|
|
|
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
|
|
TEST_COMPARE (fd, free_descriptor);
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
xclose (fd);
|
|
|
|
support_descriptors_check (descrs); /* Will report failure. */
|
|
|
|
puts ("EOT");
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
subprocess_opened_descriptor (void *closure)
|
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
|
|
TEST_COMPARE (fd, free_descriptor);
|
|
|
|
support_descriptors_check (descrs); /* Will report failure. */
|
|
|
|
puts ("EOT");
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
subprocess_changed_descriptor (void *closure)
|
|
|
|
{
|
|
|
|
int fd = xopen ("/dev/null", O_WRONLY, 0);
|
|
|
|
TEST_COMPARE (fd, free_descriptor);
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
xclose (fd);
|
|
|
|
TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
|
|
|
|
support_descriptors_check (descrs); /* Will report failure. */
|
|
|
|
puts ("EOT");
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
report_subprocess_output (const char *name,
|
|
|
|
struct support_capture_subprocess *proc)
|
|
|
|
{
|
|
|
|
printf ("info: BEGIN %s output\n"
|
|
|
|
"%s"
|
|
|
|
"info: END %s output\n",
|
|
|
|
name, proc->out.buffer, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use an explicit flag to preserve failure status across
|
|
|
|
support_record_failure_reset calls. */
|
|
|
|
static bool good = true;
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_run (void)
|
|
|
|
{
|
|
|
|
struct support_capture_subprocess proc = support_capture_subprocess
|
|
|
|
(&subprocess_no_change, NULL);
|
|
|
|
support_capture_subprocess_check (&proc, "subprocess_no_change",
|
|
|
|
0, sc_allow_none);
|
|
|
|
support_capture_subprocess_free (&proc);
|
|
|
|
|
|
|
|
char *expected = xasprintf ("\nDifferences:\n"
|
|
|
|
"error: descriptor %d was closed\n"
|
|
|
|
"EOT\n",
|
|
|
|
free_descriptor);
|
|
|
|
good = good && !support_record_failure_is_failed ();
|
|
|
|
proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
|
|
|
|
good = good && support_record_failure_is_failed ();
|
|
|
|
support_record_failure_reset (); /* Discard the reported error. */
|
|
|
|
report_subprocess_output ("subprocess_closed_descriptor", &proc);
|
|
|
|
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
|
|
support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
|
|
|
|
0, sc_allow_stdout);
|
|
|
|
support_capture_subprocess_free (&proc);
|
|
|
|
free (expected);
|
|
|
|
|
|
|
|
expected = xasprintf ("\nDifferences:\n"
|
|
|
|
"error: descriptor %d was opened (\"/dev/null\")\n"
|
|
|
|
"EOT\n",
|
|
|
|
free_descriptor);
|
|
|
|
good = good && !support_record_failure_is_failed ();
|
|
|
|
proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
|
|
|
|
good = good && support_record_failure_is_failed ();
|
|
|
|
support_record_failure_reset (); /* Discard the reported error. */
|
|
|
|
report_subprocess_output ("subprocess_opened_descriptor", &proc);
|
|
|
|
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
|
|
support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
|
|
|
|
0, sc_allow_stdout);
|
|
|
|
support_capture_subprocess_free (&proc);
|
|
|
|
free (expected);
|
|
|
|
|
|
|
|
expected = xasprintf ("\nDifferences:\n"
|
|
|
|
"error: descriptor %d changed from \"/dev/null\""
|
|
|
|
" to \"/dev\"\n"
|
|
|
|
"error: descriptor %d changed ino ",
|
|
|
|
free_descriptor, free_descriptor);
|
|
|
|
good = good && !support_record_failure_is_failed ();
|
|
|
|
proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
|
|
|
|
good = good && support_record_failure_is_failed ();
|
|
|
|
support_record_failure_reset (); /* Discard the reported error. */
|
|
|
|
report_subprocess_output ("subprocess_changed_descriptor", &proc);
|
|
|
|
TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
|
|
|
|
support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
|
|
|
|
0, sc_allow_stdout);
|
|
|
|
support_capture_subprocess_free (&proc);
|
|
|
|
free (expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
puts ("info: initial descriptor set");
|
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
support_descriptors_dump (descrs, "info: ", stdout);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
|
|
|
|
puts ("info: descriptor set with additional free descriptor");
|
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
support_descriptors_dump (descrs, "info: ", stdout);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
TEST_VERIFY (free_descriptor >= 3);
|
|
|
|
xclose (free_descriptor);
|
|
|
|
|
|
|
|
/* Initial test run without a sentinel descriptor. The presence of
|
|
|
|
such a descriptor exercises different conditions in the list
|
|
|
|
comparison in support_descriptors_check. */
|
|
|
|
test_run ();
|
|
|
|
|
|
|
|
/* Allocate a sentinel descriptor at the end of the descriptor list,
|
|
|
|
after free_descriptor. */
|
|
|
|
int sentinel_fd;
|
|
|
|
{
|
|
|
|
int fd = xopen ("/dev/full", O_WRONLY, 0);
|
|
|
|
TEST_COMPARE (fd, free_descriptor);
|
|
|
|
sentinel_fd = dup (fd);
|
|
|
|
TEST_VERIFY_EXIT (sentinel_fd > fd);
|
|
|
|
xclose (fd);
|
|
|
|
}
|
|
|
|
puts ("info: descriptor set with sentinel descriptor");
|
|
|
|
{
|
|
|
|
struct support_descriptors *descrs = support_descriptors_list ();
|
|
|
|
support_descriptors_dump (descrs, "info: ", stdout);
|
|
|
|
support_descriptors_free (descrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second test run with sentinel descriptor. */
|
|
|
|
test_run ();
|
|
|
|
|
|
|
|
xclose (sentinel_fd);
|
|
|
|
|
|
|
|
return !good;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <support/test-driver.c>
|