1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-10-31 22:10:34 +03:00
Files
glibc/stdio-common/tst-freopen4-main.c
H.J. Lu 6463d4a7b2 tst-freopen4-main.c: Call support_capture_subprocess with chroot
Update tst-freopen4-main.c to call support_capture_subprocess with chroot,
which makes temporary files inaccessible, so that temporary files can be
deleted.

This partially fixes BZ #33182.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2025-08-04 14:41:29 -07:00

119 lines
3.7 KiB
C

/* Test freopen in chroot.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
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
<https://www.gnu.org/licenses/>. */
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/file_contents.h>
#include <support/namespace.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
#include <support/xunistd.h>
#include <support/capture_subprocess.h>
static void
do_test_chroot (void *data)
{
char *temp_dir = (char *) data;
FILE *fp;
int ret;
xchroot (temp_dir);
/* Test freopen with NULL, renamed file. This verifies that
reopening succeeds (and resets the file position indicator to
start of file) even when the original path could no longer be
opened, or fails without a memory leak. (It is not possible to
use <support/descriptors.h> to test for file descriptor leaks
here, because that also depends on /proc.) */
verbose_printf ("testing freopen with NULL, renamed file\n");
fp = xfopen ("/file1", "w+");
ret = fputs ("file has been renamed", fp);
TEST_VERIFY (ret >= 0);
ret = rename ("/file1", "/file1a");
TEST_COMPARE (ret, 0);
fp = FREOPEN (NULL, "r+", fp);
if (fp != NULL)
{
puts ("freopen of renamed file succeeded");
TEST_COMPARE_FILE_STRING (fp, "file has been renamed");
xfclose (fp);
}
else
puts ("freopen of renamed file failed (OK)");
ret = rename ("/file1a", "/file1");
TEST_COMPARE (ret, 0);
/* Test freopen with NULL, deleted file. This verifies that
reopening succeeds (and resets the file position indicator to
start of file) even when the original path could no longer be
opened, or fails without a memory leak. */
verbose_printf ("testing freopen with NULL, deleted file\n");
fp = xfopen ("/file1", "r+");
ret = fputs ("file has now been deleted", fp);
TEST_VERIFY (ret >= 0);
ret = remove ("/file1");
TEST_COMPARE (ret, 0);
fp = FREOPEN (NULL, "r+", fp);
if (fp != NULL)
{
puts ("freopen of deleted file succeeded");
TEST_COMPARE_FILE_STRING (fp, "file has now been deleted");
xfclose (fp);
}
else
puts ("freopen of deleted file failed (OK)");
free (temp_dir);
}
int
do_test (void)
{
mtrace ();
char *temp_dir;
/* These chroot tests verify that either reopening a renamed or
deleted file works even in the absence of /proc, or that it fails
(without memory leaks); thus, for example, such reopening does
not crash in the absence of /proc. */
support_become_root ();
if (!support_can_chroot ())
return EXIT_UNSUPPORTED;
temp_dir = support_create_temp_directory ("tst-freopen4");
struct support_capture_subprocess result;
result = support_capture_subprocess (do_test_chroot, temp_dir);
support_capture_subprocess_check (&result, "freopen4", 0,
sc_allow_stdout);
fputs (result.out.buffer, stdout);
support_capture_subprocess_free (&result);
return 0;
}
#include <support/test-driver.c>